3

Suppose a contract has the following:

uint[] arr;

function func(int a, int b) {
    arr.push(a);
    arr.push(b);
}

If two clients call func at essentially the same time with (1, 2) and (3, 4), are the contents of arr guaranteed to be in the order of the calls?

You would expect the order of modifications to be something like:

// contract.func(1, 2) call #1
arr.push(1); // #1
arr.push(2); // #1
// contract.func(3, 4) call #2
arr.push(3); // #2
arr.push(4); // #2
// arr = [1, 2, 3, 4]

However, is it possible that the execution of events are interleaved as such:

// contract.func(1, 2) call #1
// contract.func(3, 4) call #2
arr.push(1); // #1
arr.push(2); // #2
arr.push(4); // #2
arr.push(3); // #1
// arr = [1, 2, 4, 3]

Thank you.

2

Yes, contract calls are atomic and transactions run strictly in series, so you don't need to worry about different transactions treading on each other.

However, you don't know what order transactions will be processed in, so if #1 and #2 are different transactions (not just different calls within the same transaction) you could end up with transaction #2 being mined before transaction #1, resulting in:

// arr = [3, 4, 1, 2]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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