-
Notifications
You must be signed in to change notification settings - Fork 189
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
KIP-111 Use ExtHash in trie database #1859
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
blukat29
force-pushed
the
trie-use-exthash
branch
3 times, most recently
from
June 14, 2023 10:40
f9068b9
to
e30137f
Compare
9 tasks
blukat29
force-pushed
the
trie-use-exthash
branch
3 times, most recently
from
June 20, 2023 10:09
786488d
to
cb437cb
Compare
blukat29
force-pushed
the
trie-use-exthash
branch
from
June 26, 2023 06:16
3e408ff
to
860507d
Compare
blukat29
requested review from
yoomee1313,
aidan-kwon,
ethan-kr,
jeongkyun-oh,
hyunsooda,
2dvorak,
kjeom and
ian0371
as code owners
June 26, 2023 06:34
blukat29
changed the title
Use ExtHash in trie database
KIP-111 Use ExtHash in trie database
Jun 26, 2023
hyunsooda
reviewed
Jun 29, 2023
9 tasks
ian0371
reviewed
Jul 5, 2023
ian0371
reviewed
Jul 5, 2023
yoomee1313
reviewed
Jul 7, 2023
blukat29
force-pushed
the
trie-use-exthash
branch
from
July 11, 2023 01:02
ca9f49d
to
4060deb
Compare
hyunsooda
approved these changes
Jul 11, 2023
ian0371
approved these changes
Jul 11, 2023
yoomee1313
approved these changes
Jul 12, 2023
aidan-kwon
approved these changes
Jul 17, 2023
9 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed changes
Use ExtHash type throught the trie-handling code. This is a step towards implmementing the KIP-111 State Pruning. This PR must not change any behavior of the Klaytn node.
Methodology
Identify trie node hashes in the codebase. Convert the variable type, function argument type, and return types (collectively "hash types") to ExtHash if needed. Trie node hashes can be categorized into three.
block.Root()
.account.StorageRoot()
.If a value always conveys State roots, then keep the hash type
common.Hash
.If a value can potentially carry Storage roots or Child nodes, then make the hash type to
common.ExtHash
.For example
StateDB.IntermediateRoot() common.Hash
kept its type because it is the State root.In contrast,
DBManager.ReadTrieNode(common.ExtHash)
changed its type because it receives all kinds of node hashes.CodeHash (contract bytecode) is unrelated to trie nodes, so it stays
common.Hash
.Use
hash.ExtendLegacy()
andexthash.Unextend()
to convert between the two types.After this PR, any ExtHash value flowing in the codebase must be extended with Legacy Nonce.
Notable changes
DiskDB - DBManager: All TrieNode accessors uses ExtHash. Code accessors are Hash.
Trie node keyed by ExtHashLegacy (i.e.
hash.ExtendLegacy()
) are stored with 32-byte key (i.e.db.Put(exthash.Unextend(), node)
)stateTrieMigrationDB
in favor ofDBManager
.Node encoding: Trie nodes may contain ExtHash references to other trie nodes. Encoding and Decoding mechanisms are updated.
decodeNode()
can decode both Hash and ExtHash references.hasher.hashRoot()
injects either Hash or ExtHash when encoding the nodes.TrieDB - Database: Most internal fields and methods carry ExtHash. Below are the exceptional cases.
Reference(H,H)
is diversified intoReference(EH,EH)
for linking state trie leaf (EH) and storage trie root (EH)ReferenceRoot(H)
for referencing the State root (H)Dereference(H)
is left untouched because it's exclusively used with State root (H)Commit(H)
is left untouched because it's exclusively used with State root (H)TrieDB - Trie: trie node references are internally stored in
hashNode == []byte
, so not much type changes.However, some functions are diversified into two variants.
NewTrie(H)
andNewSecureTrie(H)
open State tries. They are left untouched because it's only for State root.NewStorageTrie(EH)
andNewSecureStorageTrie(EH)
open account storage tries. They accept ExtHash as root hash.Hash()
andCommit()
returns theContract Account: A contract account (CA), contains a reference to its Storage root. Therefore its encoding format must change.
SmartContractAccount.StorageRoot
is now ExtHash.GetStorageRoot()
andSetStorageRoot
handles ExtHash.Full list of changes are available in the table in Further Comments section.
Types of changes
Checklist
$ make test
)Related issues
Further comments
Comprehensive list of hash type changes.
=
means interface unchanged,~
means changed,+
means newly added.ReadTrieNode (H)
HasTrieNode (H)
WriteTrieNode (H)
PutTrieNodeToBatch (H)
DeleteTrieNode (H)
interface ProofDBReader {H}
interface StateTrieReadDB {H}
~ ReadTrieNode (EH)
~ HasTrieNode (EH)
~ WriteTrieNode (EH)
~ PutTrieNodeToBatch (EH)
~ DeleteTrieNode (EH)
~ interface ProofDBReader {EH}
~ interface StateTrieReadDB {EH}
encoding
hasher.hash ()->H
~ hasher.hash ()->EH
61b175b
Node (H)
NodeFromOld (H)
DoesExistCachedNode (H)
DoesExistNodeInPersistent (H)
Nodes ()->[]H
NodeChildren (H)->[]H
CollectChildrenStats (H)
insert (H)
Reference (H,H)
-
Dereference (H)
Commit (H)
~ Node (EH)
~ NodeFromOld (EH)
~ DoesExistCachedNode (EH)
~ DoesExistNodeInPersistent (EH)
~ Nodes ()->[]EH
~ NodeChildren (EH)->[]EH
~ CollectChildrenStats (EH)
~ insert (EH)
~ Reference (EH,EH)
+ ReferenceRoot (H)
= Dereference (H)
= Commit(H)
29d069f
2968339
-
NewSecureTrie (H)
-
OpenTrie (H)
OpenStorageTrie (H)
Hash ()->H
-
Commit ()->H
-
LeafCallback (H)
+ NewStorageTrie (EH)
= NewSecureTrie (H)
+ NewSecureStorageTrie (EH)
= OpenTrie (H)
+ OpenStorageTrie (EH)
= Hash ()->H
+ HashExt ()->EH
= Commit ()->H
+ CommitExt ()->EH
~ LeafCallback (EH)
VerifyRangeProof (H)
= VerifyRangeProof (H)
SyncResult {H}
AddSubTrie (H)
AddCodeEntry (parent H)
Missing ()->(nodes []H)
= SyncResult {H}
= AddSubTrie (H)
= AddCodeEntry (parent H)
= Missing ()->(nodes []H)
statedb.nodeIterator.Parent ()->H
state.NodeIterator.Hash: H
state.NodeIterator.Parent: H
= statedb.nodeIterator.Parent ()->H
= state.NodeIterator.Hash: H
= state.NodeIterator.Parent: H
Commit ()->H
Reset (H)
GetContractStorageRoot ()->H
openStorageTrie (H)
= Commit ()->H
= Reset (H)
~ GetContractStorageRoot ()->EH
~ openStorageTrie (EH)
3e408ff
Account
GetStorageRoot ()->H
SetStorageRoot (H)
~ GetStorageRoot ()->EH
~ SetStorageRoot (EH)
860507d