Let's say I have a contract like this:

contract Test{
  uint8 test = 0;
  // getter and setter

and I want to issue it using some python code like:

initial = contract.getTest()
contract.setTest(1, {"from": 0x123123123...})
final = contract.getTest()

since getTest is a call, and setTest is a transaction, this should not be safe code right? Or call are dispatched synchronously with transaction and there is no race condition in this code?

Will getTest always return 1 if I run the above in order?

  • Can you rephrase the question to clarify what you mean by "safe"? Sep 14 at 19:42
  • @PatrickCollins with safe, i mean that there is no race condition in this code... for the example provided, that final will always be 1 Sep 15 at 10:47

To 100% guarantee that this will execute in order, you can wait for a transaction to be mined.

initial = contract.getTest()
set_transaction = contract.setTest(1, {"from": 0x123123123...})
final = contract.getTest()

wait(1) means we are going to wait 1 block for the transaction to finish.

  • since Vitalik said that to be sure that the consensus is reached we need to wait 7/8 blocks, do I need to use tx.wait(7)? Sep 15 at 12:48
  • Yes! You got it! Sep 15 at 22:19
  • 1
    You know, I'm pretty new to the programming world, but since I started, I loved to help people online (at least for what I can do) and so I'm pretty familiar with StackOverflow... now, in this blog I spent at most two hours, and in those 2 hours, I saw a tons of your answer... at the same time, I'm watching a video on youtube about blockchain, and by pure chance, I saw in the video the (i suppose it's called) gitlens ext on vscode opening the profile picture of the creator of the video, and he's you, I literally recognized you by pure chance... I mean, thank you so much for all of this! Sep 16 at 10:02

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.