test: broken example when using database with lifetime#2201
test: broken example when using database with lifetime#2201Wodann wants to merge 2 commits intobluealloy:mainfrom
Conversation
| cfg: &CfgEnv, | ||
| db: &mut dyn DBSuperTrait, | ||
| tx: TxEnv, | ||
| inspector: &mut InspectorT, |
There was a problem hiding this comment.
Yeah, the Inspector takes the lifetime of the Context and it is impossible to use in the way we expect and it sucks. This was a reason why inspector field is part Evm struct, if you send full Evm here it will work, or even impl InspectEvm
There was a problem hiding this comment.
For some of our use cases that's possible, like building a block.
In other cases we construct the EVM ad-hoc. The flow is similar to this, e.g. for eth_call:
- Retrieve
stateat the requested block number fromblockchain - Construct a
BlockEnvfor the requested block number - Construct a transaction for the
eth_callrequest - Construct EVM with DatabaseComponents (for
blockchainandstate), the requestedeth_calltransaction,BlockEnv, andCfgEnv - Run
replay_transact
I might just have to inline the construction of an EVM in that case using an efficient builder pattern API.
There was a problem hiding this comment.
Inspector is the one that makes a problem, as it takes a &mut Context and rust ties those together, I don't have a good solution how to go around that.
This example serves to illustrate a limitation when using the current API with (mutable) references.
The first commit shows the resulting compiler error - when using the current API.
The second commit tries to resolve this by introducing the
Contextas an associated type ontrait Inspectorsuch that we can introduce a'contextlifetime for the context. This lifetime represents the duration during which theContextwill be available to the inspector.This results in the following compiler error:
I've tried a lot of things to circumvent this but have been unable to. This might be a language limitation rather than an API limitation. See this article.
I still wanted to bring this to your attention, in case you had any ideas on how to solve it.