0

I have got a question about concurrently calls to a smart contract. If there are two users call a smart contract to set the same variable's state simultaneously, how does miners process them and what is the final consistency of the smart contract?

For example, since transactions are ordered, if userA set variable1 = 1, and userB set variable1 = 2, when two miners arrange these two transactions not in the same order, the final state of variable1 might be different. That means minerA arranges transactionA before transactionB, and variable1 will eventually be 2, whereas minerB arranges transactionB before transactionA, and variable1 will eventually be 1. The final state of variable1 will be inconsistent.

3 Answers 3

0

All blocks are arranged in sequence, so as the transactions. There is no possible way that two block shares the same parent and has the same block number.

It used to happen before the merge. In POW time, it is very often to see two miners produce a block almost simultaneously. In these cases, one of the blocks has to be orphaned and this is called a chain reorg.

An orphaned block's transactions re-enter the mempool and wait to be mined again in future blocks.

3
  • Considering another situation, minerA has transactionA in its mempool, and the variable1's value in minerA's world state trie has set to 1, but as some network fluctuation occurs, minerA has received a broadcasted block containing transactionB but no transactionA before receiving the broadcasted transactionB. So in this situation, will minerA drop the new received block, or create a secondary world state trie to set variable1 to 2, or just overwrite variable1 to 2 in the main world state trie? Code run by EVM in transaction validation phase or block validation phase?
    – Dzanvu
    Mar 14, 2023 at 2:40
  • Are we talking about a miner node or a normal node here? Mar 14, 2023 at 5:57
  • So if the data is corrupted due to a network issue, the block will be marked as a bad block and the whole syncing process will stop until it is resolved. Mar 14, 2023 at 5:59
0

If two users set the same variable's state simultaneously, only one transaction will be included in the blockchain. This is a "race condition" and can lead to unexpected behavior. Developers can use locks or other techniques to ensure only one transaction can modify the variable at a time, or use conflict resolution mechanisms to handle concurrent access.

contract LockedCounter {
    uint256 private counter;
    bool private locked;

    function increment() public {
        require(!locked, "LockedCounter: contract is locked");
        locked = true;
        counter += 1;
        locked = false;
    }

    function getCounter() public view returns (uint256) {
        return counter;
    }
}
0

A contract's state can only be modified from a transaction. Transactions are executed sequentially in the order given by the miner/block proposer.

The blockchain construction makes it impossible to execute two transaction at the same time to get into an inconsistent state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.