Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
We recently updated our SputnikVM version to include a recent change that makes precompiles able to make direct EVM calls using a
handle
object. This object effectively hooks directly into the EVM runtime, which got me thinking that it would be easy to give thehandle
access to EVM state as well. Then a precompile would be able to do anything a normal contract is able to. Therefore, the precompiles mechanism could be used to make "native short-cuts" we have discussed previously (the idea the we could have performance improvements by skipping over the EVM interpreter through executing equivalent native code directly -- previous results have shown native code can vastly outperform EVM interpretation).Description
In a separate commit I added state access to the
PrecompileHandle
trait in SputnikVM. We could make this into a proper PR if we think this approach is worth pursuing.In this PR I use this new ability to make a native implementation of the ERC-20 standard which is binary-compatible with the Solidity version (i.e. reads and writes the EVM state in exactly the same way). Additionally, I add some functionality to the engine to dynamically assign addresses to precompiles. This allows deployed ERC-20 contracts to be replaced with a "native short-cut". Then I test out this new feature by short-cutting all the ERC-20 tokens used in our Uniswap tests.
Performance / NEAR gas cost considerations
The entire purpose of this change is to give us another tool we can use for performance optimizations. In the case of
test_uniswap_input_multihop
(our most expensive uniswap test), we see about a 15% reduction in the amount of NEAR gas used. Honestly I was hoping for a bigger impact given how much effort it is to translate even a simple Solidity contract into a native counterpart. I suspect much of the gas cost still comes from state access and the overhead involved with making calls in the EVM (each call creates a new stack frame inside SputnikVM, which is needed to respectrevert
semantics properly, and this is a fairly computationally expensive operation).Some tests actually now use a little more NEAR gas with this change due to the additional state read which is required to look for the new dynamic precompiles ("native contracts").
How should this be reviewed
This PR is a PoC, not intended for production in its current form. As discussed below, there are a number of considerations when it comes to deciding if we want to adopt a feature like this, and if we do adopt it, what contracts we want to give native short-cuts to.
In particular, the reviewer should not focus on the details of the native ERC-20 implementation (for example, I did not implement the events the Solidity contract emits, and I think there are probably some edge case incompatibilities which would need to be ironed out if we wanted this in production). Instead, please focus on the design of the feature as a whole, and the high level ideas of translating a contract from Solidity into this Rust dialect.
Additional information
The purpose of this PoC is research. The main deliverable from this PR is us deciding if we want to spend more time pursing this kind of feature. What follows are some pros and cons I can think of, but please give your own opinions in your reviews!
Pros
Cons