Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit db52db3

Browse files
committed
test: complete tests and refine GenerateWitness
1 parent ea2f647 commit db52db3

File tree

3 files changed

+247
-189
lines changed

3 files changed

+247
-189
lines changed

geth-utils/gethutil/mpt/trie/stacktrie.go

+50-50
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type StackTrie struct {
6969
// NewStackTrie allocates and initializes an empty trie.
7070
func NewStackTrie(db ethdb.KeyValueWriter) *StackTrie {
7171
return &StackTrie{
72-
nodeType: emptyNode,
72+
nodeType: EmptyNode,
7373
db: db,
7474
}
7575
}
@@ -166,7 +166,7 @@ func (st *StackTrie) setDb(db ethdb.KeyValueWriter) {
166166

167167
func newLeaf(ko int, key, val []byte, db ethdb.KeyValueWriter) *StackTrie {
168168
st := stackTrieFromPool(db)
169-
st.nodeType = leafNode
169+
st.nodeType = LeafNode
170170
st.keyOffset = ko
171171
st.key = append(st.key, key[ko:]...)
172172
st.val = val
@@ -175,7 +175,7 @@ func newLeaf(ko int, key, val []byte, db ethdb.KeyValueWriter) *StackTrie {
175175

176176
func newExt(ko int, key []byte, child *StackTrie, db ethdb.KeyValueWriter) *StackTrie {
177177
st := stackTrieFromPool(db)
178-
st.nodeType = extNode
178+
st.nodeType = ExtNode
179179
st.keyOffset = ko
180180
st.key = append(st.key, key[ko:]...)
181181
st.children[0] = child
@@ -184,11 +184,11 @@ func newExt(ko int, key []byte, child *StackTrie, db ethdb.KeyValueWriter) *Stac
184184

185185
// List all values that StackTrie#nodeType can hold
186186
const (
187-
emptyNode = iota
188-
branchNode
189-
extNode
190-
leafNode
191-
hashedNode
187+
EmptyNode = iota
188+
BranchNode
189+
ExtNode
190+
LeafNode
191+
HashedNode
192192
)
193193

194194
// TryUpdate inserts a (key, value) pair into the stack trie
@@ -214,7 +214,7 @@ func (st *StackTrie) Reset() {
214214
for i := range st.children {
215215
st.children[i] = nil
216216
}
217-
st.nodeType = emptyNode
217+
st.nodeType = EmptyNode
218218
st.keyOffset = 0
219219
}
220220

@@ -233,12 +233,12 @@ func (st *StackTrie) getDiffIndex(key []byte) int {
233233
// https://github.dev/ethereum/go-ethereum/blob/00905f7dc406cfb67f64cd74113777044fb886d8/core/types/hashing.go#L105-L134
234234
func (st *StackTrie) insert(key, value []byte) {
235235
switch st.nodeType {
236-
case branchNode: /* Branch */
236+
case BranchNode: /* Branch */
237237
idx := int(key[st.keyOffset])
238238
// Unresolve elder siblings
239239
for i := idx - 1; i >= 0; i-- {
240240
if st.children[i] != nil {
241-
if st.children[i].nodeType != hashedNode {
241+
if st.children[i].nodeType != HashedNode {
242242
st.children[i].hash(true)
243243
}
244244
break
@@ -252,7 +252,7 @@ func (st *StackTrie) insert(key, value []byte) {
252252
st.children[idx].insert(key, value)
253253
}
254254

255-
case extNode: /* Ext */
255+
case ExtNode: /* Ext */
256256
// Compare both key chunks and see where they differ
257257
diffidx := st.getDiffIndex(key)
258258
// Check if chunks are identical. If so, recurse into
@@ -287,13 +287,13 @@ func (st *StackTrie) insert(key, value []byte) {
287287
// a branch node.
288288
st.children[0] = nil
289289
p = st
290-
st.nodeType = branchNode
290+
st.nodeType = BranchNode
291291
} else {
292292
// the common prefix is at least one byte
293293
// long, insert a new intermediate branch
294294
// node.
295295
st.children[0] = stackTrieFromPool(st.db)
296-
st.children[0].nodeType = branchNode
296+
st.children[0].nodeType = BranchNode
297297
st.children[0].keyOffset = st.keyOffset + diffidx
298298
p = st.children[0]
299299
}
@@ -307,7 +307,7 @@ func (st *StackTrie) insert(key, value []byte) {
307307
p.children[newIdx] = o
308308
st.key = st.key[:diffidx]
309309

310-
case leafNode: /* Leaf */
310+
case LeafNode: /* Leaf */
311311
// Compare both key chunks and see where they differ
312312
diffidx := st.getDiffIndex(key)
313313

@@ -327,15 +327,15 @@ func (st *StackTrie) insert(key, value []byte) {
327327
var p *StackTrie
328328
if diffidx == 0 {
329329
// Convert current leaf into a branch
330-
st.nodeType = branchNode
330+
st.nodeType = BranchNode
331331
p = st
332332
st.children[0] = nil
333333
} else {
334334
// Convert current node into an ext,
335335
// and insert a child branch node.
336-
st.nodeType = extNode
336+
st.nodeType = ExtNode
337337
st.children[0] = NewStackTrie(st.db)
338-
st.children[0].nodeType = branchNode
338+
st.children[0].nodeType = BranchNode
339339
st.children[0].keyOffset = st.keyOffset + diffidx
340340
p = st.children[0]
341341
}
@@ -355,19 +355,19 @@ func (st *StackTrie) insert(key, value []byte) {
355355
// over to the children.
356356
st.key = st.key[:diffidx]
357357
st.val = nil
358-
case emptyNode: /* Empty */
359-
st.nodeType = leafNode
358+
case EmptyNode: /* Empty */
359+
st.nodeType = LeafNode
360360
st.key = key[st.keyOffset:]
361361
st.val = value
362-
case hashedNode:
362+
case HashedNode:
363363
panic("trying to insert into hash")
364364
default:
365365
panic("invalid type")
366366
}
367367
}
368368

369369
func (st *StackTrie) branchToHasher(doUpdate bool) *hasher {
370-
if st.nodeType != branchNode {
370+
if st.nodeType != BranchNode {
371371
panic("Converting branch to RLP: wrong node")
372372
}
373373
var nodes [17]Node
@@ -401,7 +401,7 @@ func (st *StackTrie) branchToHasher(doUpdate bool) *hasher {
401401
}
402402

403403
func (st *StackTrie) extNodeToHasher(doUpdate bool) *hasher {
404-
if st.nodeType != extNode {
404+
if st.nodeType != ExtNode {
405405
panic("Converting extension node to RLP: wrong node")
406406
}
407407
st.children[0].hash(doUpdate)
@@ -433,22 +433,22 @@ func (st *StackTrie) extNodeToHasher(doUpdate bool) *hasher {
433433
return h
434434
}
435435

436-
// hash() hashes the node 'st' and converts it into 'hashedNode', if possible.
436+
// hash() hashes the node 'st' and converts it into 'HashedNode', if possible.
437437
// Possible outcomes:
438438
// 1. The rlp-encoded value was >= 32 bytes:
439439
// - Then the 32-byte `hash` will be accessible in `st.val`.
440-
// - And the 'st.type' will be 'hashedNode'
440+
// - And the 'st.type' will be 'HashedNode'
441441
//
442442
// 2. The rlp-encoded value was < 32 bytes
443443
// - Then the <32 byte rlp-encoded value will be accessible in 'st.val'.
444-
// - And the 'st.type' will be 'hashedNode' AGAIN
444+
// - And the 'st.type' will be 'HashedNode' AGAIN
445445
//
446446
// This method will also:
447-
// set 'st.type' to hashedNode
447+
// set 'st.type' to HashedNode
448448
// clear 'st.key'
449449
func (st *StackTrie) hash(doUpdate bool) {
450450
/* Shortcut if node is already hashed */
451-
if st.nodeType == hashedNode {
451+
if st.nodeType == HashedNode {
452452
return
453453
}
454454
// The 'hasher' is taken from a pool, but we don't actually
@@ -457,11 +457,11 @@ func (st *StackTrie) hash(doUpdate bool) {
457457
var h *hasher
458458

459459
switch st.nodeType {
460-
case branchNode:
460+
case BranchNode:
461461
h = st.branchToHasher(doUpdate)
462-
case extNode:
462+
case ExtNode:
463463
h = st.extNodeToHasher(doUpdate)
464-
case leafNode:
464+
case LeafNode:
465465
h = NewHasher(false)
466466
defer returnHasherToPool(h)
467467
h.tmp.Reset()
@@ -478,17 +478,17 @@ func (st *StackTrie) hash(doUpdate bool) {
478478
if err := rlp.Encode(&h.tmp, n); err != nil {
479479
panic(err)
480480
}
481-
case emptyNode:
481+
case EmptyNode:
482482
st.val = emptyRoot.Bytes()
483483
st.key = st.key[:0]
484-
st.nodeType = hashedNode
484+
st.nodeType = HashedNode
485485
return
486486
default:
487487
panic("Invalid node type")
488488
}
489489
if doUpdate {
490490
st.key = st.key[:0]
491-
st.nodeType = hashedNode
491+
st.nodeType = HashedNode
492492
}
493493
if len(h.tmp) < 32 {
494494
st.val = common.CopyBytes(h.tmp)
@@ -674,20 +674,20 @@ func printProof(ps [][]byte, t, idx []byte) {
674674
enable := byte(150)
675675
fmt.Print(" [")
676676
for i, p := range ps {
677-
if t[i] == extNode {
677+
if t[i] == ExtNode {
678678
fmt.Print("EXT - ")
679679
if idx[0] >= enable {
680680
fmt.Print(" (", p, ") - ")
681681
}
682-
} else if t[i] == branchNode {
682+
} else if t[i] == BranchNode {
683683
fmt.Print("BRANCH - ")
684684
// fmt.Print(" (", p, ") - ")
685-
} else if t[i] == leafNode {
685+
} else if t[i] == LeafNode {
686686
fmt.Print("LEAF - ")
687687
if idx[0] >= enable {
688688
fmt.Print(" (", p, ") - ")
689689
}
690-
} else if t[i] == hashedNode {
690+
} else if t[i] == HashedNode {
691691
fmt.Print("HASHED - ")
692692
// elems, _, _ := rlp.SplitList(p)
693693
// c, _ := rlp.CountValues(elems)
@@ -774,8 +774,8 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
774774
func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, [][]byte, []uint8, error) {
775775
k := KeybytesToHex(key)
776776
// fmt.Println(" k", k)
777-
if st.nodeType == emptyNode {
778-
return [][]byte{}, nil, []uint8{emptyNode}, nil
777+
if st.nodeType == EmptyNode {
778+
return [][]byte{}, nil, []uint8{EmptyNode}, nil
779779
}
780780

781781
// Note that when root is a leaf, this leaf should be returned even if you ask for a different key (than the key of
@@ -786,8 +786,8 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
786786
// (the one not just added) is the same as in the S proof. This wouldn't work if we would have a placeholder leaf
787787
// in the S proof (another reason is that the S proof with a placeholder leaf would be an empty trie and thus with
788788
// a root of an empty trie - which is not the case in S proof).
789-
if st.nodeType == leafNode {
790-
return [][]byte{st.val}, nil, []uint8{leafNode}, nil
789+
if st.nodeType == LeafNode {
790+
return [][]byte{st.val}, nil, []uint8{LeafNode}, nil
791791
}
792792

793793
var nibbles [][]byte
@@ -799,21 +799,21 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
799799
for i := 0; i < len(k); i++ {
800800
// fmt.Print(" ", k[i], "/", c.nodeType, " | ")
801801
proofType = append(proofType, c.nodeType)
802-
if c.nodeType == extNode {
802+
if c.nodeType == ExtNode {
803803
// fmt.Print(c.key, " ")
804804
i += len(c.key) - 1
805805
nodes = append(nodes, c)
806806
c = c.children[0]
807-
} else if c.nodeType == branchNode {
807+
} else if c.nodeType == BranchNode {
808808
nodes = append(nodes, c)
809809
c = c.children[k[i]]
810810
if c == nil {
811811
break
812812
}
813-
} else if c.nodeType == leafNode {
813+
} else if c.nodeType == LeafNode {
814814
nodes = append(nodes, c)
815815
break
816-
} else if c.nodeType == hashedNode {
816+
} else if c.nodeType == HashedNode {
817817
c_rlp, error := db.Get(c.val)
818818
if error != nil {
819819
panic(error)
@@ -840,7 +840,7 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
840840
}
841841
c.val = branchChild
842842
// if there are children, the node type should be branch
843-
proofType[len(proofType)-1] = branchNode
843+
proofType[len(proofType)-1] = BranchNode
844844
}
845845
}
846846

@@ -852,23 +852,23 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
852852
lNodes := len(nodes)
853853
for i := lNodes - 1; i >= 0; i-- {
854854
node := nodes[i]
855-
if node.nodeType == leafNode {
855+
if node.nodeType == LeafNode {
856856
nibbles = append(nibbles, []byte{})
857857
rlp, error := db.Get(node.val)
858858
if error != nil { // TODO: avoid error when RLP
859859
proof = append(proof, node.val) // already have RLP
860860
} else {
861861
proof = append(proof, rlp)
862862
}
863-
} else if node.nodeType == branchNode || node.nodeType == extNode {
863+
} else if node.nodeType == BranchNode || node.nodeType == ExtNode {
864864
node.hash(false)
865865

866866
raw_rlp, error := db.Get(node.val)
867867
if error != nil {
868868
return nil, nil, nil, error
869869
}
870870
proof = append(proof, raw_rlp)
871-
if node.nodeType == extNode {
871+
if node.nodeType == ExtNode {
872872

873873
rlp_flag := uint(raw_rlp[0])
874874
if rlp_flag < 192 || rlp_flag >= 248 {

0 commit comments

Comments
 (0)