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

Commit 08778eb

Browse files
committed
(wip) ignore modext case and working when num of txs is less than 257
1 parent 799a02d commit 08778eb

File tree

6 files changed

+165
-134
lines changed

6 files changed

+165
-134
lines changed

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func printProof(ps [][]byte) {
664664
}
665665

666666
func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value []byte) (StackProof, error) {
667-
fmt.Println("====")
667+
fmt.Println(" ====")
668668
proofS, nibblesS, err := st.GetProof(db, indexBuf)
669669
if err != nil {
670670
return StackProof{}, err
@@ -679,7 +679,6 @@ func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value
679679
return StackProof{}, err
680680
}
681681
len2 := len(proofC)
682-
fmt.Println(" Proof S C ", len1, len2)
683682
printProof(proofC)
684683

685684
// fmt.Println(len1, len2)
@@ -703,6 +702,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
703702
// order is correct.
704703
var indexBuf []byte
705704
for i := 1; i < list.Len() && i <= 0x7f; i++ {
705+
fmt.Print(i)
706706
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i))
707707
value := types.EncodeForDerive(list, i, valueBuf)
708708
proof, err := st.UpdateAndGetProof(db, indexBuf, value)
@@ -715,6 +715,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
715715
// special case when index is 0
716716
// rlp.AppendUint64() encodes index 0 to [128]
717717
if list.Len() > 0 {
718+
fmt.Print("0")
718719
indexBuf = rlp.AppendUint64(indexBuf[:0], 0)
719720
value := types.EncodeForDerive(list, 0, valueBuf)
720721
proof, err := st.UpdateAndGetProof(db, indexBuf, value)
@@ -725,6 +726,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
725726
}
726727

727728
for i := 0x80; i < list.Len(); i++ {
729+
fmt.Print(i)
728730
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i))
729731
value := types.EncodeForDerive(list, i, valueBuf)
730732
proof, err := st.UpdateAndGetProof(db, indexBuf, value)
@@ -740,7 +742,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
740742

741743
func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, [][]byte, error) {
742744
k := KeybytesToHex(key)
743-
fmt.Println("k", k)
745+
// fmt.Println("k", k)
744746
if st.nodeType == emptyNode {
745747
return [][]byte{}, nil, nil
746748
}
@@ -832,8 +834,8 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
832834
// fmt.Println("** ", element)
833835

834836
// FIXME only one nibble case
835-
nibble := element[0]
836-
// fmt.Println(" Ext nibble:", nibble)
837+
nibble := element[0] - 16
838+
// fmt.Println(" Ext nibble:", element)
837839
nibbles = append(nibbles, []byte{nibble})
838840
}
839841
}
@@ -844,9 +846,5 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
844846
slices.Reverse(proof)
845847
}
846848

847-
// given a default value
848-
if len(nibbles) == 0 {
849-
nibbles = append(nibbles, []byte{0})
850-
}
851849
return proof, nibbles, nil
852850
}

geth-utils/gethutil/mpt/witness/branch.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ func isTxLeaf(proofEl []byte) bool {
3131
check(err)
3232
c, err1 := rlp.CountValues(elems)
3333
check(err1)
34-
fmt.Println("ISLEAF:", c)
34+
isHashedNode := false
35+
if proofEl[0] == 226 && proofEl[1] == 32 && proofEl[2] == 160 {
36+
isHashedNode = true
37+
}
3538
// 9: for tx (Nonce, Gas, GasPrice, Value, To, Data, r, s, v)
36-
return c == 9
39+
return c == 9 || isHashedNode
3740
}
3841

3942
// prepareBranchWitness takes the rows that are to be filled with branch data and it takes
@@ -84,8 +87,11 @@ func prepareBranchWitness(rows [][]byte, branch []byte, branchStart int, branchR
8487
}
8588
}
8689

87-
func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []byte, extValues [][]byte, key, driftedInd byte,
90+
func prepareBranchNode(
91+
branch1, branch2, extNode1, extNode2, extListRlpBytes []byte,
92+
extValues [][]byte, key, driftedInd byte,
8893
isBranchSPlaceholder, isBranchCPlaceholder, isExtension bool) Node {
94+
8995
extensionNode := ExtensionNode{
9096
ListRlpBytes: extListRlpBytes,
9197
}
@@ -244,7 +250,10 @@ func addBranchAndPlaceholder(proof1, proof2, extNibblesS, extNibblesC [][]byte,
244250
// For stack trie
245251
// if 1 st node of proof2 is a branch node and 1st node of Proof1 is an ext node
246252
need_placeholder_ext := isBranch(proof2[0]) && (!isTxLeaf(proof1[0]) && !isBranch(proof1[0]))
247-
253+
if need_placeholder_ext {
254+
fmt.Println("need_placeholder_ext", isTxLeaf(proof1[0]), isBranch(proof1[0]), proof1[0])
255+
fmt.Println("need_placeholder_ext", isBranch(proof2[0]), proof2[0])
256+
}
248257
isExtension := (len1 == len2+2) || (len2 == len1+2)
249258
if isExtension || need_placeholder_ext {
250259
var numNibbles byte
@@ -301,6 +310,8 @@ func addBranchAndPlaceholder(proof1, proof2, extNibblesS, extNibblesC [][]byte,
301310
var extNode []byte
302311
if need_placeholder_ext {
303312
extNode = proof1[0]
313+
// FIXME should move to above and need to avoid above [len-3] operation
314+
isExtension = need_placeholder_ext
304315
} else {
305316
if isExtension {
306317
if len1 > len2 {
@@ -311,9 +322,6 @@ func addBranchAndPlaceholder(proof1, proof2, extNibblesS, extNibblesC [][]byte,
311322
}
312323
}
313324

314-
// FIXME should move to above and need to avoid above [len-3] operation
315-
isExtension = need_placeholder_ext
316-
317325
// Note that isModifiedExtNode happens also when we have a branch instead of shortExtNode
318326
isModifiedExtNode := (!isBranch(longExtNode) && !isShorterProofLastLeaf) || need_placeholder_ext
319327

@@ -324,7 +332,6 @@ func addBranchAndPlaceholder(proof1, proof2, extNibblesS, extNibblesC [][]byte,
324332
if len1 > len2 {
325333
node = prepareBranchNode(proof1[len1-2], proof1[len1-2], extNode, extNode, extListRlpBytes, extValues,
326334
key[keyIndex+numberOfNibbles], driftedInd, false, true, isExtension)
327-
328335
} else {
329336
node = prepareBranchNode(proof2[len2-2], proof2[len2-2], extNode, extNode, extListRlpBytes, extValues,
330337
key[keyIndex+numberOfNibbles], driftedInd, true, false, isExtension)

geth-utils/gethutil/mpt/witness/gen_witness_from_local_blockchain_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,20 @@ func TestExtNodeDeletedExtShortIsBranchFirstLevel(t *testing.T) {
922922

923923
ExtNodeDeleted(key1, key2, key3, "ExtNodeDeletedExtShortIsBranchFirstLevel")
924924
}
925+
926+
func TestSimulateStackTrieUnder128Txs(t *testing.T) {
927+
SkipIfNoGeth(t)
928+
// The trie is empty before we add key1, key2, key3.
929+
key1 := common.HexToHash("0x2345610000000000000000000000000000000000000000000000000000000000")
930+
// After inserting key1, there is only one leaf in the trie.
931+
932+
key2 := common.HexToHash("0x2345630000000000000000000000000000000000000000000000000000000000")
933+
// After inserting key2, we have an extension node E in the trie with the following nibbles: 2 3 4 5 6.
934+
// The branch of the extension node has two leaves - key1 at position 1 and key2 at position 3.
935+
936+
key3 := common.HexToHash("0x2345800000000000000000000000000000000000000000000000000000000000")
937+
// After inserting key3, we have an extension node E1 with nibbles: 2 3 4 5.
938+
// The branch of E1 has two nodes: the branch at position 6 and the leaf at position 8.
939+
940+
ExtNodeInserted(key1, key2, key3, "ExtNodeInsertedExtShortIsBranchSecondLevel")
941+
}

geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func transactionsStackTrieInsertionTemplate(t *testing.T, n int) {
121121
}
122122

123123
func TestTransactionInsertion(t *testing.T) {
124-
txs := makeTransactions(4)
124+
txs := makeTransactions(256)
125125
prepareStackTrieWitness("TransactionInsertion", types.Transactions(txs))
126126
}
127127

geth-utils/gethutil/mpt/witness/leaf.go

+59-53
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func prepareAccountLeafNode(addr common.Address, addrh []byte, leafS, leafC, nei
163163
driftedRlpBytes := []byte{0}
164164
keyDrifted := make([]byte, valueLen)
165165
if neighbourNode != nil {
166-
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighbourNode, false, false)
166+
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighbourNode, false, false, false)
167167
}
168168

169169
wrongValue := make([]byte, valueLen)
@@ -349,12 +349,12 @@ func prepareLeafAndPlaceholderNode(addr common.Address, addrh []byte, proof1, pr
349349
func prepareTxLeafNode(idx uint, leafS, leafC, key, neighborNode []byte, isSPlaceholder, isSModExtension, isCModExtension bool) Node {
350350
var rows [][]byte
351351

352-
keyS, valueS, listRlpBytes1, valueRlpBytes1 := prepareStorageLeafInfo(leafS, false, isSPlaceholder)
352+
keyS, valueS, listRlpBytes1, valueRlpBytes1 := prepareStorageLeafInfo(leafS, false, isSPlaceholder, true)
353353

354354
rows = append(rows, keyS)
355355
rows = append(rows, valueS)
356356

357-
keyC, valueC, listRlpBytes2, valueRlpBytes2 := prepareStorageLeafInfo(leafC, false, false)
357+
keyC, valueC, listRlpBytes2, valueRlpBytes2 := prepareStorageLeafInfo(leafC, false, false, true)
358358

359359
rows = append(rows, keyC)
360360
rows = append(rows, valueC)
@@ -370,15 +370,10 @@ func prepareTxLeafNode(idx uint, leafS, leafC, key, neighborNode []byte, isSPlac
370370
driftedRlpBytes := []byte{0}
371371
keyDrifted := make([]byte, valueLen)
372372
if neighborNode != nil {
373-
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighborNode, false, false)
373+
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighborNode, false, false, true)
374374
}
375375
rows = append(rows, keyDrifted)
376376

377-
// var nonExistingStorageRow []byte
378-
// var wrongRlpBytes []byte
379-
// nonExistingStorageRow = prepareEmptyNonExistingStorageRow()
380-
// rows = append(rows, nonExistingStorageRow)
381-
382377
// These rows are only used in the case of a modified extension node.
383378
// These rows are actually set in equipLeafWithModExtensionNode function.
384379
for i := 0; i < modifiedExtensionNodeRowLen; i++ {
@@ -489,7 +484,7 @@ func prepareStorageLeafPlaceholderNode(storage_key common.Hash, key []byte, keyI
489484
return prepareStorageLeafNode(leaf, leaf, nil, storage_key, key, false, true, true, false, false)
490485
}
491486

492-
func prepareStorageLeafInfo(row []byte, valueIsZero, isPlaceholder bool) ([]byte, []byte, []byte, []byte) {
487+
func prepareStorageLeafInfo(row []byte, valueIsZero, isPlaceholder, isTxLeaf bool) ([]byte, []byte, []byte, []byte) {
493488
var keyRlp []byte
494489
var valueRlp []byte
495490
var keyRlpLen byte
@@ -520,51 +515,62 @@ func prepareStorageLeafInfo(row []byte, valueIsZero, isPlaceholder bool) ([]byte
520515

521516
keyLen := byte(0)
522517
offset := byte(1)
523-
if len(row) < 32 { // the node doesn't get hashed in this case
518+
if isTxLeaf {
524519
keyRlpLen = 1
525520
keyRlp = make([]uint8, keyRlpLen)
526521
copy(keyRlp, row[:keyRlpLen])
527-
528-
// 192 + 32 = 224
529-
if row[1] < 128 {
530-
// last level: [194,32,1]
531-
// or
532-
// only one nibble in a leaf (as soon as the leaf has two nibbles, row[1] will have 128 + length)
533-
// [194,48,1] - this one contains nibble 0 = 48 - 48
534-
keyLen = byte(1)
535-
copy(key, row[keyRlpLen:keyLen+1])
536-
offset = byte(1)
537-
} else {
538-
// [196,130,32,0,1]
539-
keyLen = row[1] - 128
540-
copy(key, row[keyRlpLen:keyLen+2])
541-
offset = byte(2)
542-
}
543-
} else if row[0] == 248 {
544-
// [248,67,160,59,138,106,70,105,186,37,13,38,205,122,69,158,202,157,33,95,131,7,227,58,235,229,3,121,188,90,54,23,236,52,68,161,160,...
545-
keyRlpLen = 2
546-
keyLen = row[2] - 128
547-
keyRlp = row[:keyRlpLen]
548-
copy(key, row[keyRlpLen:keyLen+3])
549-
offset = byte(3)
522+
// [248 200 129 128 131 4 147 224 98 148 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 130 5 9 184 100 0 0 0 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 ...]
523+
key[0] = row[0]
524+
key[1] = row[1]
525+
keyLen = byte(2)
526+
offset = byte(0)
550527
} else {
551-
keyRlpLen = 1
552-
keyRlp = make([]uint8, keyRlpLen)
553-
copy(keyRlp, row[:keyRlpLen])
554-
if row[1] < 128 {
555-
// last level:
556-
// [227,32,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
557-
// one nibble:
558-
// [227,48,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
559-
key[0] = row[0]
560-
key[1] = row[1]
561-
keyLen = byte(2)
562-
offset = byte(0)
528+
if len(row) < 32 { // the node doesn't get hashed in this case
529+
keyRlpLen = 1
530+
keyRlp = make([]uint8, keyRlpLen)
531+
copy(keyRlp, row[:keyRlpLen])
532+
533+
// 192 + 32 = 224
534+
if row[1] < 128 {
535+
// last level: [194,32,1]
536+
// or
537+
// only one nibble in a leaf (as soon as the leaf has two nibbles, row[1] will have 128 + length)
538+
// [194,48,1] - this one contains nibble 0 = 48 - 48
539+
keyLen = byte(1)
540+
copy(key, row[keyRlpLen:keyLen+1])
541+
offset = byte(1)
542+
} else {
543+
// [196,130,32,0,1]
544+
keyLen = row[1] - 128
545+
copy(key, row[keyRlpLen:keyLen+2])
546+
offset = byte(2)
547+
}
548+
} else if row[0] == 248 {
549+
// [248,67,160,59,138,106,70,105,186,37,13,38,205,122,69,158,202,157,33,95,131,7,227,58,235,229,3,121,188,90,54,23,236,52,68,161,160,...
550+
keyRlpLen = 2
551+
keyLen = row[2] - 128
552+
keyRlp = row[:keyRlpLen]
553+
copy(key, row[keyRlpLen:keyLen+3])
554+
offset = byte(3)
563555
} else {
564-
// [226,160,59,138,106,70,105,186,37,13,38[227,32,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
565-
keyLen = row[1] - 128
566-
copy(key, row[keyRlpLen:keyLen+2])
567-
offset = byte(2)
556+
keyRlpLen = 1
557+
keyRlp = make([]uint8, keyRlpLen)
558+
copy(keyRlp, row[:keyRlpLen])
559+
if row[1] < 128 {
560+
// last level:
561+
// [227,32,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
562+
// one nibble:
563+
// [227,48,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
564+
key[0] = row[0]
565+
key[1] = row[1]
566+
keyLen = byte(2)
567+
offset = byte(0)
568+
} else {
569+
// [226,160,59,138,106,70,105,186,37,13,38[227,32,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
570+
keyLen = row[1] - 128
571+
copy(key, row[keyRlpLen:keyLen+2])
572+
offset = byte(2)
573+
}
568574
}
569575
}
570576
setKeyValue(keyLen, offset)
@@ -575,12 +581,12 @@ func prepareStorageLeafInfo(row []byte, valueIsZero, isPlaceholder bool) ([]byte
575581
func prepareStorageLeafNode(leafS, leafC, neighbourNode []byte, storage_key common.Hash, key []byte, nonExistingStorageProof, isSPlaceholder, isCPlaceholder, isSModExtension, isCModExtension bool) Node {
576582
var rows [][]byte
577583

578-
keyS, valueS, listRlpBytes1, valueRlpBytes1 := prepareStorageLeafInfo(leafS, false, isSPlaceholder)
584+
keyS, valueS, listRlpBytes1, valueRlpBytes1 := prepareStorageLeafInfo(leafS, false, isSPlaceholder, false)
579585

580586
rows = append(rows, keyS)
581587
rows = append(rows, valueS)
582588

583-
keyC, valueC, listRlpBytes2, valueRlpBytes2 := prepareStorageLeafInfo(leafC, false, isCPlaceholder)
589+
keyC, valueC, listRlpBytes2, valueRlpBytes2 := prepareStorageLeafInfo(leafC, false, isCPlaceholder, false)
584590

585591
fmt.Println("-", key)
586592
fmt.Println("-", keyS, leafS)
@@ -600,7 +606,7 @@ func prepareStorageLeafNode(leafS, leafC, neighbourNode []byte, storage_key comm
600606
driftedRlpBytes := []byte{0}
601607
keyDrifted := make([]byte, valueLen)
602608
if neighbourNode != nil {
603-
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighbourNode, false, false)
609+
keyDrifted, _, driftedRlpBytes, _ = prepareStorageLeafInfo(neighbourNode, false, false, false)
604610
}
605611
rows = append(rows, keyDrifted)
606612

0 commit comments

Comments
 (0)