ethereumjs-monorepo icon indicating copy to clipboard operation
ethereumjs-monorepo copied to clipboard

Support using hashed account and storage tries

Open am1r021 opened this issue 3 years ago • 8 comments

Currently, in order to put an account into the state manager, it has to be done so using the putAccount function, which requires an account address. During snap sync, account addresses are not known, only their hash. In order to store state from snap sync in state manager, it is necessary to add this functionality.

Also see #2345.

am1r021 avatar Oct 10 '22 01:10 am1r021

The same would also apply for storages. Can we not directly dump these into an unhashed trie into snap sync, or do we really need to use StateManager?

jochem-brouwer avatar Oct 10 '22 08:10 jochem-brouwer

The same would also apply for storages. Can we not directly dump these into an unhashed trie into snap sync, or do we really need to use StateManager?

It is possible to put the data directly into tries, but it seems like we would be recreating a significant amount of StateManager functionality. It may be advantageous for code reuse to use a modified StateManager.

am1r021 avatar Oct 10 '22 14:10 am1r021

@jochem-brouwer @scorbajio is it eventually possible to combine both and initialize a StateManager "as is" with an unhashed trie? 🤔 Just to throw another option in the ring.

holgerd77 avatar Oct 13 '22 10:10 holgerd77

Hmm that would most likely fix the problem in an elegant way. Thoughts @scorbajio ?

jochem-brouwer avatar Oct 13 '22 15:10 jochem-brouwer

@jochem-brouwer @scorbajio is it eventually possible to combine both and initialize a StateManager "as is" with an unhashed trie? thinking Just to throw another option in the ring.

@holgerd77 What do you mean by "combine both"? Do you mean support both hashed and unhashed trie puts for StateManager?

am1r021 avatar Oct 15 '22 02:10 am1r021

I meant: combine both ideas 🙂, so using the Trie library in an unhashed fashion (idea 1) and then put this in the StateManager (idea 2).

That was not a super-thought-through works-for-sure suggestion though, so I am neither sure if this has caveats or works at all. I just threw this in the ring as an additional eventual option, feel free to discard immediately if not fitting. 😋

holgerd77 avatar Oct 17 '22 09:10 holgerd77

@holgerd77 If I understand correctly, you are saying initialize the StateManager with useKeyHashing set to false, which means that if an address is passed in, it will have to be hashed before putting it, and if an already hashed key is passed in, it can just be put immediately, since it's not necessary to rehash it. Please correct me if I have misunderstood you.

The above seems like the best way to do it. One possible alternative is to just parameterize useKeyHashing for StateManager, though this would be less flexible since it won't give the hybrid option of putting by both address as well as hash.

am1r021 avatar Oct 19 '22 00:10 am1r021

Looking at the code, it seems like we can wrap all inputs for address with a function that returns the hash if it is an address or just the hash itself if it is already a hash. @jochem-brouwer do you see any potential blockers or shortcomings with this implementation approach? I'd be happy to work on this ticket if there are no immediate takers.

am1r021 avatar Oct 20 '22 00:10 am1r021