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

Commit 056fa7f

Browse files
committed
test: added proof equality check
1 parent 05f358f commit 056fa7f

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

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

+17-9
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,9 @@ func (st *StackTrie) getNodeFromBranchRLP(branch []byte, idx int) []byte {
567567
// `81` is the length of the payload and payload starts from `128`
568568
start := int(branch[0]) - RLP_LONG_LIST_FLAG + 2
569569

570-
// If 1st node is not 128(empty node) or 160, it should be a leaf
570+
// If 1st node is neither 128(empty node) nor 160, it should be a leaf
571571
b := int(branch[start])
572-
if b != RLP_SHORT_STR_FLAG || b != (RLP_SHORT_STR_FLAG+LEN_OF_HASH) {
572+
if b != RLP_SHORT_STR_FLAG && b != (RLP_SHORT_STR_FLAG+LEN_OF_HASH) {
573573
return []byte{0}
574574
}
575575

@@ -601,6 +601,14 @@ type StackProof struct {
601601
proofC [][]byte
602602
}
603603

604+
func (sp *StackProof) GetProofS() [][]byte {
605+
return sp.proofS
606+
}
607+
608+
func (sp *StackProof) GetProofC() [][]byte {
609+
return sp.proofC
610+
}
611+
604612
func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value []byte) (StackProof, error) {
605613
proofS, err := st.GetProof(db, indexBuf)
606614
if err != nil {
@@ -627,7 +635,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
627635
// order that `list` provides hashes in. This insertion sequence ensures that the
628636
// order is correct.
629637
var indexBuf []byte
630-
for i := 1; i < list.Len() && i <= 0x7f; i++ {
638+
for i := 0; i < list.Len() && i <= 0x7f; i++ {
631639
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i))
632640
value := types.EncodeForDerive(list, i, valueBuf)
633641

@@ -638,12 +646,12 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri
638646

639647
proofs = append(proofs, proof)
640648
}
641-
if list.Len() > 0 {
642-
indexBuf = rlp.AppendUint64(indexBuf[:0], 0)
643-
value := types.EncodeForDerive(list, 0, valueBuf)
644-
// TODO: get proof
645-
st.Update(indexBuf, value)
646-
}
649+
// if list.Len() > 0 {
650+
// indexBuf = rlp.AppendUint64(indexBuf[:0], 0)
651+
// value := types.EncodeForDerive(list, 0, valueBuf)
652+
// // TODO: get proof
653+
// st.Update(indexBuf, value)
654+
// }
647655
for i := 0x80; i < list.Len(); i++ {
648656
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i))
649657
value := types.EncodeForDerive(list, i, valueBuf)

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

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package witness
22

33
import (
4+
"bytes"
45
"fmt"
56
"main/gethutil/mpt/trie"
67
"main/gethutil/mpt/types"
@@ -87,59 +88,67 @@ The second element is the 16-th transaction. For example, the third byte (16) re
8788
the transaction index.
8889
*/
8990

90-
func transactionsStackTrieInsertionTemplate(n int) {
91+
func transactionsStackTrieInsertionTemplate(t *testing.T, n int) {
9192
txs := makeTransactions(n)
9293
db := rawdb.NewMemoryDatabase()
9394
stackTrie := trie.NewStackTrie(db)
9495

95-
stackTrie.UpdateAndGetProofs(db, types.Transactions(txs))
96+
proofs, _ := stackTrie.UpdateAndGetProofs(db, types.Transactions(txs))
9697

97-
fmt.Println("===")
98+
rlp_last_tx, _ := txs[n-1].MarshalBinary()
99+
last_proofC := proofs[len(proofs)-1].GetProofC()
100+
last_leaf_proof := last_proofC[len(last_proofC)-1]
101+
102+
if !bytes.Equal(last_leaf_proof, rlp_last_tx) {
103+
fmt.Println("- last_tx ", rlp_last_tx)
104+
fmt.Println("- last_proof ", last_leaf_proof)
105+
t.Fail()
106+
}
98107
}
99108

100109
func TestStackTrieInsertion_1Tx(t *testing.T) {
101110
// Only one leaf
102-
transactionsStackTrieInsertionTemplate(1)
111+
transactionsStackTrieInsertionTemplate(t, 1)
103112
}
104113

105114
func TestStackTrieInsertion_2Txs(t *testing.T) {
106115
// One ext. node and one leaf
107-
transactionsStackTrieInsertionTemplate(2)
116+
transactionsStackTrieInsertionTemplate(t, 2)
108117
}
109118

110119
func TestStackTrieInsertion_3Txs(t *testing.T) {
111120
// One ext. node, one branch and one leaf
112-
transactionsStackTrieInsertionTemplate(3)
121+
transactionsStackTrieInsertionTemplate(t, 3)
113122
}
114123

115124
func TestStackTrieInsertion_4Txs(t *testing.T) {
116125
// One ext. node, one branch and two leaves
117-
transactionsStackTrieInsertionTemplate(4)
126+
transactionsStackTrieInsertionTemplate(t, 4)
118127
}
119128

120129
func TestStackTrieInsertion_16Txs(t *testing.T) {
121130
// One ext. node and one branch with full leaves (16 leaves)
122-
transactionsStackTrieInsertionTemplate(16)
131+
transactionsStackTrieInsertionTemplate(t, 16)
123132
}
124133

125134
func TestStackTrieInsertion_17Txs(t *testing.T) {
126135
// One ext. node, 3 branches and 17 leaves.
127136
// The original ext. node turns into a branch (B1) which has children at position 0 and 1.
128137
// At position 0 of B1, it has a branch with full leaves
129138
// At position 1 of B1, it has a newly leaf
130-
transactionsStackTrieInsertionTemplate(17)
139+
transactionsStackTrieInsertionTemplate(t, 17)
131140
}
132141

133142
func TestStackTrieInsertion_33Txs(t *testing.T) {
134143
// Follow above test and have one more branch generated
135-
transactionsStackTrieInsertionTemplate(33)
144+
transactionsStackTrieInsertionTemplate(t, 33)
136145
}
137146

138147
func TestStackTrieInsertion_ManyTxs(t *testing.T) {
139148
// Just randomly picking a large number.
140149
// The cap of block gas limit is 30M, the minimum gas cost of a tx is 21k
141150
// 30M / 21k ~= 1429
142-
transactionsStackTrieInsertionTemplate(2000)
151+
transactionsStackTrieInsertionTemplate(t, 2000)
143152
}
144153

145154
/*

0 commit comments

Comments
 (0)