You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 =)
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:
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.The text was updated successfully, but these errors were encountered: