Skip to content

Hook into the EVM execution loop #18

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

Closed

Conversation

nanocryk
Copy link
Contributor

Adds a Hook trait defining functions to inspect the state of the EVM :

  • before and after each step (opcode execution)
  • before and after each context (entering and leaving a contract scope)

It is set using the set_hook(&mut self, mut hook: Option<H>) -> Option<H> on a StackExecutor, which will return the previous optional hook. Having ownership of the hook while executing the bytecode avoids mutability issues, and it can be retrieved after for inspection.

This can be used for multiple purposes :

  • Implement RPC calls such as debug_traceTransaction
  • Implement a more fine-tuned gas to weight mapping in Frontier as in this experiment.

Hook is implemented for () which is the default type used by StackExecutor<'config, S, H = ()> to avoid breaking changes.
No hook is attached by default, adding only a self.hook.is_some() check overhead once per context.

@sorpaas
Copy link
Member

sorpaas commented Mar 9, 2021

@nanocryk Can you try if you can use the Handler trait for those hooks? A problem with the current hook design is that set_hook can only set things of the same type, which takes the majority of the flexibility away. In this case, directly adding those hooks to Handler may work better and is equivalent.

@nanocryk
Copy link
Contributor Author

nanocryk commented Mar 10, 2021

Handler is implemented by StackExecutor, so I suppose you're thinking of StackState or Backend. Backend is not specific to the StackExecutor, but since all interesting data since to be contained in the implementor of StackState, I suppose it could be integrated in there.

However, it means that changing the hooked behavior require to re-implement the entire StackState/Backend implementor. In the example linked in the first comment using the Hook trait, you can see that only the frontier Runner is modified. A user of frontier wanting to have its own hooking behavior would just have to implement it's own Runner while still using SubstrateStackState. The Hook trait strategy thus seems simpler to me for both this crate and downstream crates, with the additional benefit of not breaking the public API.

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

Successfully merging this pull request may close these issues.

2 participants