From fee33f3ca6cc56099648b6280fd313babac5e079 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 11 Mar 2022 01:21:42 -0500 Subject: [PATCH 01/13] feat: decode inline leaf nodes --- internal/trie/node/decode.go | 11 ++++++++ internal/trie/node/decode_test.go | 36 ++++++++++++++++++++++++ internal/trie/node/encode_decode_test.go | 4 ++- lib/trie/database.go | 4 +-- lib/trie/proof.go | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index c67e84e49a..a05eb84747 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -105,6 +105,17 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) { ErrDecodeChildHash, i, err) } + // Handle inlined leaf nodes. + if Type(hash[0]>>6) == LeafType && len(hash) != 32 { + leaf, err := decodeLeaf(bytes.NewReader(hash[1:]), hash[0]) + if err != nil { + return nil, fmt.Errorf("%w: at index %d: %s", + ErrDecodeValue, i, err) + } + branch.Children[i] = leaf + continue + } + branch.Children[i] = &Leaf{ HashDigest: hash, } diff --git a/internal/trie/node/decode_test.go b/internal/trie/node/decode_test.go index c6f683aece..9b90726f5f 100644 --- a/internal/trie/node/decode_test.go +++ b/internal/trie/node/decode_test.go @@ -95,6 +95,42 @@ func Test_Decode(t *testing.T) { Dirty: true, }, }, + "branch with two inlined children": { + reader: bytes.NewReader( + []byte{ + 158, // node type 2 (branch without value) and Key length 30 + // Key data start + 195, 101, 195, 207, 89, 214, + 113, 235, 114, 218, 14, 122, + 65, 19, 196, 16, 2, 80, 95, + 14, 123, 144, 18, 9, 107, + 65, 196, 235, 58, 175, + // Key data end + 148, 127, 110, 164, 41, 8, 0, 0, 104, 95, 15, 31, 5, + 21, 244, 98, 205, 207, 132, 224, 241, 214, 4, 93, 252, + 187, 32, 134, 92, 74, 43, 127, 1, 0, 0, + }, + ), + n: &Branch{ + Key: []byte{12, 3, 6, 5, 12, 3, 12, 15, 5, 9, 13, 6, 7, 1, 14, 11, 7, 2, 13, 10, 0, 14, 7, 10, 4, 1, 1, 3, 12, 4}, + Children: [16]Node{ + nil, nil, nil, nil, + &Leaf{ + Key: []byte{14, 7, 11, 9, 0, 1, 2, 0, 9, 6, 11, 4, 1, 12, 4, 14, 11, 3, 10, 10, 15, 9, 4, 7, 15, 6, 14, 10, 4, 2, 9}, + Value: []byte{0, 0}, + Dirty: true, + }, + nil, nil, nil, nil, + &Leaf{ + Key: []byte{15, 1, 15, 0, 5, 1, 5, 15, 4, 6, 2, 12, 13, 12, 15, 8, 4, 14, 0, 15, 1, 13, 6, 0, 4, 5, 13, 15, 12, 11, 11}, + Value: []byte{134, 92, 74, 43, 127, 1, 0, 0}, + Dirty: true, + }, + nil, nil, nil, nil, nil, nil, + }, + Dirty: true, + }, + }, } for name, testCase := range testCases { diff --git a/internal/trie/node/encode_decode_test.go b/internal/trie/node/encode_decode_test.go index f4bb168697..0361c384d0 100644 --- a/internal/trie/node/encode_decode_test.go +++ b/internal/trie/node/encode_decode_test.go @@ -57,7 +57,9 @@ func Test_Branch_Encode_Decode(t *testing.T) { Key: []byte{5}, Children: [16]Node{ &Leaf{ - HashDigest: []byte{0x41, 0x9, 0x4, 0xa}, + Key: []byte{9}, + Value: []byte{10}, + Dirty: true, }, }, Dirty: true, diff --git a/lib/trie/database.go b/lib/trie/database.go index b9822dc47a..5387bd63a6 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -78,8 +78,8 @@ func (t *Trie) store(db chaindb.Batch, n Node) error { return nil } -// loadFromProof create a partial trie based on the proof slice, as it only contains nodes that are in the proof afaik. -func (t *Trie) loadFromProof(rawProof [][]byte, rootHash []byte) error { +// LoadFromProof create a partial trie based on the proof slice, as it only contains nodes that are in the proof afaik. +func (t *Trie) LoadFromProof(rawProof [][]byte, rootHash []byte) error { if len(rawProof) == 0 { return ErrEmptyProof } diff --git a/lib/trie/proof.go b/lib/trie/proof.go index 4bd2b0c066..2d8444d2db 100644 --- a/lib/trie/proof.go +++ b/lib/trie/proof.go @@ -84,7 +84,7 @@ func VerifyProof(proof [][]byte, root []byte, items []Pair) (bool, error) { } proofTrie := NewEmptyTrie() - if err := proofTrie.loadFromProof(proof, root); err != nil { + if err := proofTrie.LoadFromProof(proof, root); err != nil { return false, fmt.Errorf("%w: %s", ErrLoadFromProof, err) } From 3dc3fd5236e513077719b5f217ea4fc108ae308b Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 11 Mar 2022 01:24:36 -0500 Subject: [PATCH 02/13] chore: use const --- internal/trie/node/decode.go | 2 +- internal/trie/node/types.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index a05eb84747..58b989c798 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -106,7 +106,7 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) { } // Handle inlined leaf nodes. - if Type(hash[0]>>6) == LeafType && len(hash) != 32 { + if Type(hash[0]>>6) == LeafType && len(hash) != HashLength { leaf, err := decodeLeaf(bytes.NewReader(hash[1:]), hash[0]) if err != nil { return nil, fmt.Errorf("%w: at index %d: %s", diff --git a/internal/trie/node/types.go b/internal/trie/node/types.go index 33e31a651d..d62e9f273d 100644 --- a/internal/trie/node/types.go +++ b/internal/trie/node/types.go @@ -17,3 +17,6 @@ const ( // InvalidType is used in tests only InvalidType ) + +// HashLength is 256 bits +const HashLength = 32 From cb2104281409357841b3532ff9d9a8b78f1c3357 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 11 Mar 2022 01:33:18 -0500 Subject: [PATCH 03/13] chore: revert iface changes --- lib/trie/database.go | 4 ++-- lib/trie/proof.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/trie/database.go b/lib/trie/database.go index 5387bd63a6..b9822dc47a 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -78,8 +78,8 @@ func (t *Trie) store(db chaindb.Batch, n Node) error { return nil } -// LoadFromProof create a partial trie based on the proof slice, as it only contains nodes that are in the proof afaik. -func (t *Trie) LoadFromProof(rawProof [][]byte, rootHash []byte) error { +// loadFromProof create a partial trie based on the proof slice, as it only contains nodes that are in the proof afaik. +func (t *Trie) loadFromProof(rawProof [][]byte, rootHash []byte) error { if len(rawProof) == 0 { return ErrEmptyProof } diff --git a/lib/trie/proof.go b/lib/trie/proof.go index 2d8444d2db..4bd2b0c066 100644 --- a/lib/trie/proof.go +++ b/lib/trie/proof.go @@ -84,7 +84,7 @@ func VerifyProof(proof [][]byte, root []byte, items []Pair) (bool, error) { } proofTrie := NewEmptyTrie() - if err := proofTrie.LoadFromProof(proof, root); err != nil { + if err := proofTrie.loadFromProof(proof, root); err != nil { return false, fmt.Errorf("%w: %s", ErrLoadFromProof, err) } From ff5ba1e76d46020ada30bbe89c1433ef09164f4c Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 11 Mar 2022 11:13:57 -0500 Subject: [PATCH 04/13] chore: address PR feedback --- internal/trie/node/.decode.go.swp | Bin 0 -> 16384 bytes internal/trie/node/decode.go | 3 +- internal/trie/node/decode_test.go | 33 ++++++++++++++--- internal/trie/node/encode_decode_test.go | 45 ++++++++++++++++++++++- internal/trie/node/types.go | 3 -- 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 internal/trie/node/.decode.go.swp diff --git a/internal/trie/node/.decode.go.swp b/internal/trie/node/.decode.go.swp new file mode 100644 index 0000000000000000000000000000000000000000..dbe9b702d4388755a17b2bf3ccce2cacad34d3e2 GIT binary patch literal 16384 zcmeI2Uu+yl9ml6_fF7ZB36%g*(Bvvxd&zmv=ZFwY?TXk*?DiT5(gcLDqP^SMyOr;D zkDa+*ttfz6s`5|bp;Wvec%W(pw1NjJig-eO0R(-B5WFBEl{X$LfdC1l0^i@v?AdGk z?1T!0(46$~u6K8S^PBI?@3*@%+nl+)@(@4e&ocb(Vyr$m-hAAlUb|Qj#X#S^jm4Kw%7^ciL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SfTx>CHpd(1E;Oc3 z_%qXK(%qyh7t{V`7PmW!&(6%&`RMa{esL4`%AVxj>iG(z_y22n=YBWd#p(T@n>Ro9 z54^*_4xR!JfaBmU@C&@l{|x*T{0MvpJO`cyPk>K=6>tPhfIGo!u>D)`GWZ&}0zL`W zz*$fSK9~mY0p!cOfcrTGoB~b(r+`zyDc}@v3OEIv0#1SdiUOE%vlC)8-;l3;(L5ae z@0<7Q(V7FMuxU9*Py1K+0-qVJQH+~a(=*KD*CC2?;#e&@YTgq473pCQ%Q>lkX7w!1 z=9ch3UUIjs#TYl3UW?KDqm_s^w8T)Hy}-PCVP381G2GHRVamQZ6^H|)xivF&kGT)C zv)>#(39nZi=k$Uj7VXG9nmt-Yq$~Cnd(Y|ohUwg`$$T_}CMald){pT literal 0 HcmV?d00001 diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index 58b989c798..60cf26c7c4 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -106,7 +106,8 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) { } // Handle inlined leaf nodes. - if Type(hash[0]>>6) == LeafType && len(hash) != HashLength { + const hashLength = 32 + if Type(hash[0]>>6) == LeafType && len(hash) != hashLength { leaf, err := decodeLeaf(bytes.NewReader(hash[1:]), hash[0]) if err != nil { return nil, fmt.Errorf("%w: at index %d: %s", diff --git a/internal/trie/node/decode_test.go b/internal/trie/node/decode_test.go index 9b90726f5f..755acbaf44 100644 --- a/internal/trie/node/decode_test.go +++ b/internal/trie/node/decode_test.go @@ -98,7 +98,7 @@ func Test_Decode(t *testing.T) { "branch with two inlined children": { reader: bytes.NewReader( []byte{ - 158, // node type 2 (branch without value) and Key length 30 + 158, // node type 2 (branch w/o value) and key length 30 // Key data start 195, 101, 195, 207, 89, 214, 113, 235, 114, 218, 14, 122, @@ -112,18 +112,41 @@ func Test_Decode(t *testing.T) { }, ), n: &Branch{ - Key: []byte{12, 3, 6, 5, 12, 3, 12, 15, 5, 9, 13, 6, 7, 1, 14, 11, 7, 2, 13, 10, 0, 14, 7, 10, 4, 1, 1, 3, 12, 4}, + Key: []byte{ + 12, 3, 6, 5, 12, 3, + 12, 15, 5, 9, 13, 6, + 7, 1, 14, 11, 7, 2, + 13, 10, 0, 14, 7, 10, + 4, 1, 1, 3, 12, 4, + }, Children: [16]Node{ nil, nil, nil, nil, &Leaf{ - Key: []byte{14, 7, 11, 9, 0, 1, 2, 0, 9, 6, 11, 4, 1, 12, 4, 14, 11, 3, 10, 10, 15, 9, 4, 7, 15, 6, 14, 10, 4, 2, 9}, + Key: []byte{ + 14, 7, 11, 9, 0, 1, + 2, 0, 9, 6, 11, 4, + 1, 12, 4, 14, 11, + 3, 10, 10, 15, 9, + 4, 7, 15, 6, 14, + 10, 4, 2, 9, + }, Value: []byte{0, 0}, Dirty: true, }, nil, nil, nil, nil, &Leaf{ - Key: []byte{15, 1, 15, 0, 5, 1, 5, 15, 4, 6, 2, 12, 13, 12, 15, 8, 4, 14, 0, 15, 1, 13, 6, 0, 4, 5, 13, 15, 12, 11, 11}, - Value: []byte{134, 92, 74, 43, 127, 1, 0, 0}, + Key: []byte{ + 15, 1, 15, 0, 5, 1, + 5, 15, 4, 6, 2, 12, + 13, 12, 15, 8, 4, + 14, 0, 15, 1, 13, + 6, 0, 4, 5, 13, + 15, 12, 11, 11, + }, + Value: []byte{ + 134, 92, 74, 43, + 127, 1, 0, 0, + }, Dirty: true, }, nil, nil, nil, nil, nil, nil, diff --git a/internal/trie/node/encode_decode_test.go b/internal/trie/node/encode_decode_test.go index 0361c384d0..59d01a05f7 100644 --- a/internal/trie/node/encode_decode_test.go +++ b/internal/trie/node/encode_decode_test.go @@ -5,6 +5,7 @@ package node import ( "bytes" + "log" "testing" "github.com/stretchr/testify/assert" @@ -43,7 +44,7 @@ func Test_Branch_Encode_Decode(t *testing.T) { Dirty: true, }, }, - "branch with child": { + "branch with child leaf inline": { branchToEncode: &Branch{ Key: []byte{5}, Children: [16]Node{ @@ -65,6 +66,47 @@ func Test_Branch_Encode_Decode(t *testing.T) { Dirty: true, }, }, + "branch with child leaf hash": { + branchToEncode: &Branch{ + Key: []byte{5}, + Children: [16]Node{ + &Leaf{ + Key: []byte{ + 10, 11, 12, 13, + 14, 15, 16, 17, + 18, 19, 20, 21, + 14, 15, 16, 17, + 10, 11, 12, 13, + 14, 15, 16, 17, + }, + Value: []byte{ + 10, 11, 12, 13, + 14, 15, 16, 17, + 10, 11, 12, 13, + 14, 15, 16, 17, + 10, 11, 12, 13, + }, + }, + }, + }, + branchDecoded: &Branch{ + Key: []byte{5}, + Children: [16]Node{ + &Leaf{ + HashDigest: []byte{ + 2, 18, 48, 30, 98, + 133, 244, 78, 70, + 161, 196, 105, 228, + 190, 159, 228, 199, 29, + 254, 212, 160, 55, 199, + 21, 186, 226, 204, 145, + 132, 5, 39, 204, + }, + }, + }, + Dirty: true, + }, + }, } for name, testCase := range testCases { @@ -85,6 +127,7 @@ func Test_Branch_Encode_Decode(t *testing.T) { resultBranch, err := decodeBranch(buffer, header) require.NoError(t, err) + log.Println(resultBranch) assert.Equal(t, testCase.branchDecoded, resultBranch) }) } diff --git a/internal/trie/node/types.go b/internal/trie/node/types.go index d62e9f273d..33e31a651d 100644 --- a/internal/trie/node/types.go +++ b/internal/trie/node/types.go @@ -17,6 +17,3 @@ const ( // InvalidType is used in tests only InvalidType ) - -// HashLength is 256 bits -const HashLength = 32 From 130487cf2c229d43fc73830bbb5b85ba7b2266a7 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 11 Mar 2022 11:14:25 -0500 Subject: [PATCH 05/13] chore: remove vim swap file --- internal/trie/node/.decode.go.swp | Bin 16384 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 internal/trie/node/.decode.go.swp diff --git a/internal/trie/node/.decode.go.swp b/internal/trie/node/.decode.go.swp deleted file mode 100644 index dbe9b702d4388755a17b2bf3ccce2cacad34d3e2..0000000000000000000000000000000000000000 GIT binary patch literal 0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)#1051EY6-P0gh!ft8 zo7=u|G~Qha(-yvSL{>)vr5w6%%9eP>-50gKJeT zg^6i=L)=5klF-%$A$68cTvW=2x(?3+%9+usOBxSHK^^FPRE%u|D0DcJKTRyslMOv_ zas8V&?&}9euUrfzli?`il#PYm_ifnN(*oVCx6*4V7QbNEAyUQ@NjXlbEM84$ zqwmX-_H>MtS={TVnIdQU`1n3(3*Asgcb$tDmOeh+h+86&VtOUSfTx>CHpd(1E;Oc3 z_%qXK(%qyh7t{V`7PmW!&(6%&`RMa{esL4`%AVxj>iG(z_y22n=YBWd#p(T@n>Ro9 z54^*_4xR!JfaBmU@C&@l{|x*T{0MvpJO`cyPk>K=6>tPhfIGo!u>D)`GWZ&}0zL`W zz*$fSK9~mY0p!cOfcrTGoB~b(r+`zyDc}@v3OEIv0#1SdiUOE%vlC)8-;l3;(L5ae z@0<7Q(V7FMuxU9*Py1K+0-qVJQH+~a(=*KD*CC2?;#e&@YTgq473pCQ%Q>lkX7w!1 z=9ch3UUIjs#TYl3UW?KDqm_s^w8T)Hy}-PCVP381G2GHRVamQZ6^H|)xivF&kGT)C zv)>#(39nZi=k$Uj7VXG9nmt-Yq$~Cnd(Y|ohUwg`$$T_}CMald){pT From 54d9b8ade9c8c7511a0ee264224b769a5bcb9356 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Mon, 14 Mar 2022 11:36:37 -0400 Subject: [PATCH 06/13] fix: add @noot fix for loading inline leaves --- lib/trie/database.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/trie/database.go b/lib/trie/database.go index b9822dc47a..5d0eef7e00 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -186,6 +186,11 @@ func (t *Trie) load(db chaindb.Database, n Node) error { hash := child.GetHash() + _, isLeaf := child.(*node.Leaf) + if len(hash) == 0 && isLeaf { + // node has already been loaded inline + continue + } encodedNode, err := db.Get(hash) if err != nil { return fmt.Errorf("cannot find child node key 0x%x in database: %w", hash, err) From db645979fd42e5a57c6ebaaa81988379e741cc8e Mon Sep 17 00:00:00 2001 From: Bo Du Date: Mon, 14 Mar 2022 14:00:43 -0400 Subject: [PATCH 07/13] chore: add space --- lib/trie/database.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/trie/database.go b/lib/trie/database.go index 5d0eef7e00..fc8b070821 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -191,6 +191,7 @@ func (t *Trie) load(db chaindb.Database, n Node) error { // node has already been loaded inline continue } + encodedNode, err := db.Get(hash) if err != nil { return fmt.Errorf("cannot find child node key 0x%x in database: %w", hash, err) From 3612b6c5755511a4f8b5d3ce58ae72e62ae80985 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Mon, 14 Mar 2022 23:13:34 -0400 Subject: [PATCH 08/13] fix: handle inline child in getFromDB --- lib/trie/database.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/trie/database.go b/lib/trie/database.go index fc8b070821..04ce768416 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -336,12 +336,18 @@ func getFromDB(db chaindb.Database, n Node, key []byte) ( // childIndex is the nibble after the common prefix length in the key being searched. childIndex := key[commonPrefixLength] - childWithHashOnly := branch.Children[childIndex] - if childWithHashOnly == nil { + child := branch.Children[childIndex] + if child == nil { return nil, nil } - childHash := childWithHashOnly.GetHash() + // Child can be either inlined or a hash pointer. + childHash := child.GetHash() + _, isLeaf := child.(*node.Leaf) + if len(childHash) == 0 && isLeaf { + return getFromDB(db, child, key[commonPrefixLength+1:]) + } + encodedChild, err := db.Get(childHash) if err != nil { return nil, fmt.Errorf( From 02a3e6754fea9e198a68f984ebb5b483a61f8849 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 18 Mar 2022 15:53:25 -0400 Subject: [PATCH 09/13] chore: remove debug log --- internal/trie/node/encode_decode_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/trie/node/encode_decode_test.go b/internal/trie/node/encode_decode_test.go index 59d01a05f7..bd59778388 100644 --- a/internal/trie/node/encode_decode_test.go +++ b/internal/trie/node/encode_decode_test.go @@ -5,7 +5,6 @@ package node import ( "bytes" - "log" "testing" "github.com/stretchr/testify/assert" @@ -127,7 +126,6 @@ func Test_Branch_Encode_Decode(t *testing.T) { resultBranch, err := decodeBranch(buffer, header) require.NoError(t, err) - log.Println(resultBranch) assert.Equal(t, testCase.branchDecoded, resultBranch) }) } From cbe4b73eb4d5e5de34d0ba02e97a4667ebc14978 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Sun, 20 Mar 2022 12:45:49 -0400 Subject: [PATCH 10/13] chore: fix naming lint RVV-B0001 --- lib/trie/database.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/trie/database.go b/lib/trie/database.go index 04ce768416..920ae88738 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -436,14 +436,14 @@ func (t *Trie) writeDirty(db chaindb.Batch, n Node) error { // We need to compute the hash values of each newly inserted node. func (t *Trie) GetInsertedNodeHashes() (hashesSet map[common.Hash]struct{}, err error) { hashesSet = make(map[common.Hash]struct{}) - err = t.getInsertedNodeHashes(t.root, hashesSet) + err = t._getInsertedNodeHashes(t.root, hashesSet) if err != nil { return nil, err } return hashesSet, nil } -func (t *Trie) getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (err error) { +func (t *Trie) _getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (err error) { // TODO pass map of hashes or slice as argument to avoid copying // and using more memory. if n == nil || !n.IsDirty() { @@ -472,7 +472,7 @@ func (t *Trie) getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (e continue } - err := t.getInsertedNodeHashes(child, hashes) + err := t._getInsertedNodeHashes(child, hashes) if err != nil { // Note: do not wrap error since this is called recursively. return err From 657b624d91a9cec32905f06cbef9c6ce8a32843c Mon Sep 17 00:00:00 2001 From: Bo Du Date: Tue, 22 Mar 2022 12:17:34 -0400 Subject: [PATCH 11/13] chore: revert lint fix RVV-B0001 --- lib/trie/database.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/trie/database.go b/lib/trie/database.go index 920ae88738..04ce768416 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -436,14 +436,14 @@ func (t *Trie) writeDirty(db chaindb.Batch, n Node) error { // We need to compute the hash values of each newly inserted node. func (t *Trie) GetInsertedNodeHashes() (hashesSet map[common.Hash]struct{}, err error) { hashesSet = make(map[common.Hash]struct{}) - err = t._getInsertedNodeHashes(t.root, hashesSet) + err = t.getInsertedNodeHashes(t.root, hashesSet) if err != nil { return nil, err } return hashesSet, nil } -func (t *Trie) _getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (err error) { +func (t *Trie) getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (err error) { // TODO pass map of hashes or slice as argument to avoid copying // and using more memory. if n == nil || !n.IsDirty() { @@ -472,7 +472,7 @@ func (t *Trie) _getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) ( continue } - err := t._getInsertedNodeHashes(child, hashes) + err := t.getInsertedNodeHashes(child, hashes) if err != nil { // Note: do not wrap error since this is called recursively. return err From c94e83cea2325b285caf47f07504d996add31195 Mon Sep 17 00:00:00 2001 From: Bo Du Date: Fri, 25 Mar 2022 19:30:55 -0400 Subject: [PATCH 12/13] fix: set encoding and hash digest on inline loaded node --- lib/trie/database.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/trie/database.go b/lib/trie/database.go index 04ce768416..273ce9f8d1 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -189,6 +189,12 @@ func (t *Trie) load(db chaindb.Database, n Node) error { _, isLeaf := child.(*node.Leaf) if len(hash) == 0 && isLeaf { // node has already been loaded inline + // just set encoding + hash digest + _, _, err := child.EncodeAndHash(false) + if err != nil { + return err + } + child.SetDirty(false) continue } From 5154e167946cdcc3da0a525069a179a16b66ff4f Mon Sep 17 00:00:00 2001 From: Bo Du Date: Mon, 28 Mar 2022 23:03:53 -0400 Subject: [PATCH 13/13] fix: inline nodes are < 32 bytes --- internal/trie/node/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/trie/node/decode.go b/internal/trie/node/decode.go index 60cf26c7c4..4da4a5bbc2 100644 --- a/internal/trie/node/decode.go +++ b/internal/trie/node/decode.go @@ -107,7 +107,7 @@ func decodeBranch(reader io.Reader, header byte) (branch *Branch, err error) { // Handle inlined leaf nodes. const hashLength = 32 - if Type(hash[0]>>6) == LeafType && len(hash) != hashLength { + if Type(hash[0]>>6) == LeafType && len(hash) < hashLength { leaf, err := decodeLeaf(bytes.NewReader(hash[1:]), hash[0]) if err != nil { return nil, fmt.Errorf("%w: at index %d: %s",