Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation about awaiting async events #599

Closed
jochem-brouwer opened this issue Sep 19, 2019 · 2 comments · Fixed by #601
Closed

Documentation about awaiting async events #599

jochem-brouwer opened this issue Sep 19, 2019 · 2 comments · Fixed by #601

Comments

@jochem-brouwer
Copy link
Member

It might be my lack of Javascript knowledge but it took me a while to figure this out. The idea is to use the step event of the VM and make execute some async calls which we want to await before we continue executing the next step in the VM. An use case for this is to run a "handmade" mainnet fork to test some code on: copy the chain code and once it tries to read storage for anything (e.g. balance/SLOAD/EXTCODECOPY) we first read this from the blockchain itself, dump it in our VM chain and then continue executing.

From the docs I assumed that I could create an async event where I await these web3 calls - but this does NOT yield the VM.

The solution is to:

      function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
      }

      chain.on('step', async function (data, nextEventFunction) {
        await sleep(1000)
        nextEventFunction()
      })

The nextEventFunction MUST get called if it is provided as an argument in the function (if the argument length > 1 this MUST get called).

This also has the weird side effect that if, for some reason, you create a function (does not even have to be async) which you attach to an event which has more than 1 argument in it, the VM will stop immediately after firing the event if you do NOT call the second argument.

@alcuadrado
Copy link
Member

Hey @jochem-brouwer, sorry for the confusion!

I was wrong about how this behaved, and I wrote that documentation. Most of my vm-related projects were running just by chance 🤦🏻‍♂️

I'll create a PR fixing the docs.

@jochem-brouwer
Copy link
Member Author

jochem-brouwer commented Sep 20, 2019

To be honest I would have just assumed that if you would "normally" await things in the event handler it would also yield the calling (emitting) event - but apparently it does not =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants