Imagine I have deposit function which can receive funds from user and the amount sent will be updated in state based on balanceBefore
and balanceAfter
of token in the contract:
1: function deposit(IERC20 token, uint256 amount) public{
2: uint256 balanceBefore = token.balanceOf(address(this));
3:
4: token.transferFrom(msg.sender,address(this),amount);
5:
6: uint256 balanceAfter = token.balanceOf(address(this));
7:
8: balance[msg.sender] = balanceAfter - balanceBefore;
9: }
now lets say user A calls deposit and in same time user B calls deposit but user's B transaction will be mined when user's A transaction is in middle of execution(in proccesing after line 3) so user B has sent token to contract and contract's balance is increased. In such a scenario user's A balance will be updated more than expected.
Is it possible this scenario happen? if yes is it based on luck?