-
Notifications
You must be signed in to change notification settings - Fork 680
Add ability to listen to the vm step #464
Comments
I think I prefer making the provider an Eventually we'll be making the provider an event emitter for all RPC calls, as well as some internal operations, as this will allow projects to access raw data directly without having to decode it from the JSON-RPC response. |
Yes, emittery is nice. Also agree an emitter interface is a good approach. It seems more complex to implement to me than the option, that's the only thing... In this context if the logger was written even slightly differently, it would be possible to acquire the step info for both calls and sends with very few code changes. |
I just saw this issue and the link to your previous one. This is really a response to the original issue, but I thought I'd post this here since the other is closed. This jumps heavily into ganache internals, but there's a currently working easy solution to what you want which is to use: provider.engine.manager.waitForInitialization(function(err, state) {
// state.blockchain.vm is defined here
} |
@hkalodner Thank you for looking into this. In my case I need to hear the vm step when it runs for To me the code looks like creates a new, ephemeral instance of the VM for each I was using the vm attached to the StateManager before but it wasn't reporting all the data I'd like to see. |
Happy to help. I use ganache in order to instrument the constructors of ethereum contracts https://github.com/OffchainLabs/arbitrum/blob/master/packages/arb-provider-truffle/lib/index.js which is how I came up with that solution. You're totally right about call. For my purposes hooking into the general runtime was enough, but it's definitely incomplete. This was a really interesting question so I decided to get hacky with it 😁, but I think I have a solution that could potentially work for you (obviously 100% unsupported and dangerous). const BlockchainDouble = require("ganache-core/lib/blockchain_double");
const createVMFromStateTrie = BlockchainDouble.prototype.createVMFromStateTrie;
BlockchainDouble.prototype.createVMFromStateTrie = function(state, activatePrecompiles) {
const vm = createVMFromStateTrie.apply(this, arguments);
// vm is defined here
return vm;
} |
@hkalodner Oh that's really smart! Thanks, will try it out. |
Wondering if you'd be open to accepting a PR for the smaller change I've proposed and tabling this more extensive work for later. This thread and others suggest there's an immediate community need to consume the step data and it can be provided in a stable, simple way by passing an option down to that layer in the code. Adding a |
@cgewecke, finally getting around to implementing this and will release it this week in a new ganache alpha (we've re-written ganache: https://github.com/trufflesuite/ganache/releases/tag/ganache%407.0.0-alpha.0). My current plan is to emit something like:
for I plan on having type VmTransactionContext = {
transactionHash: string,
rpc: string, // eth_call, eth_sendTransaction, etc
context: {} // unique to each transaction lifecycle
} If this works for you, then the best way to listen to step events would be something like: const transactionContexts = new Map();
provider.on("vm:tx:before", (event) => {
transactionContexts.add(event.context, []);
});
provider.on("vm:tx:step", (event) => {
const context = transactionContexts.get(event.context);
context.push(event.data);
});
provider.on("vm:tx:after", (event) => {
transactionContexts.remove(event.context);
}); You can listen to only |
Aside: I wonder if there is a way we can offset the gas costs of the coverage instrumentation effects internally? Perhaps we can make it so that |
@davidmurdoch Hi, continuing our discussion in #455 here...
You asked
After looking into this a bit I agree something like the latter would be really helpful. One low-cost approach would be to add a provider option which is a function ganache passes the step info to at createVMFromStateTrie. That method gets run for both transactions and calls and seems like a good place to manage this from.
If you are open to this solution I can submit a PR w/ tests next week. Or if you see a better way happy to help w/ that as well.
The text was updated successfully, but these errors were encountered: