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

Commit

Permalink
(wip) verifying all the cases, not working on mod-ext and hashed node
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Apr 10, 2024
1 parent 147d8dd commit e9d8587
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 74 deletions.
31 changes: 16 additions & 15 deletions geth-utils/gethutil/mpt/trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,13 @@ func (sp *StackProof) GetNibblesC() [][]byte {
return sp.nibblesC
}

func (sp *StackProof) GetTypeS() []byte {
return sp.proofSType
}
func (sp *StackProof) GetTypeC() []byte {
return sp.proofCType
}

func isBranch(proofEl []byte) bool {
elems, _, _ := rlp.SplitList(proofEl)
c, _ := rlp.CountValues(elems)
Expand Down Expand Up @@ -712,7 +719,7 @@ func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value
printProof(proofC, typesC, indexBuf)

// fmt.Println(len1, len2)
if len1 >= len2 {
if len1 > len2 {
fmt.Println(KeybytesToHex(indexBuf))
}

Expand Down Expand Up @@ -772,7 +779,7 @@ func (st *StackTrie) UpdateAndGetProofs(db ethdb.KeyValueReader, list types.Deri

func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, [][]byte, []uint8, error) {
k := KeybytesToHex(key)
// fmt.Println("k", k)
fmt.Println(" k", k)
if st.nodeType == emptyNode {
return [][]byte{}, nil, []uint8{emptyNode}, nil
}
Expand All @@ -794,15 +801,13 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
var proof [][]byte
var nodes []*StackTrie
c := st
// isHashed := false

for i := 0; i < len(k); i++ {
// fmt.Print(k[i], "/", c.nodeType, " | ")
// fmt.Print(" ", k[i], "/", c.nodeType, " | ")
proofType = append(proofType, c.nodeType)
if c.nodeType == extNode {
nodes = append(nodes, c)
c = c.children[0]

} else if c.nodeType == branchNode {
nodes = append(nodes, c)
c = c.children[k[i]]
Expand All @@ -813,7 +818,6 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
nodes = append(nodes, c)
break
} else if c.nodeType == hashedNode {
// isHashed = true
c_rlp, error := db.Get(c.val)
if error != nil {
panic(error)
Expand All @@ -836,10 +840,11 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
// (`branchChild = [128]` in this case), but it is also of length 1 when `c_rlp` is a leaf.
if len(branchChild) == 1 && (branchChild[0] == 128 || branchChild[0] == 0) {
// no child at this position (128 is RLP encoding for nil object)
fmt.Println("NOT CHILD")
break
}
c.val = branchChild
// if there are children, the node type should be branch
proofType[i] = branchNode
}
}

Expand All @@ -848,18 +853,17 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
// to them - which is needed in MPT proof, because we need a proof for each modification (after
// the first modification, some nodes are hashed and we cannot add children to the hashed node).

// if !isHashed {
lNodes := len(nodes)
for i := lNodes - 1; i >= 0; i-- {
node := nodes[i]
if node.nodeType == leafNode {
nibbles = append(nibbles, []byte{})
rlp, error := db.Get(node.val)
if error != nil { // TODO: avoid error when RLP
proof = append(proof, node.val) // already have RLP
} else {
proof = append(proof, rlp)
}
nibbles = append(nibbles, nil)
} else if node.nodeType == branchNode || node.nodeType == extNode {
node.hash(false)

Expand All @@ -884,21 +888,18 @@ func (st *StackTrie) GetProof(db ethdb.KeyValueReader, key []byte) ([][]byte, []
for i := 0; i < int(numNibbles); i++ {
nibble[i] = raw_rlp[i+1] - 16
}
// fmt.Println(" Ext nibble:", numNibbles, nibble, raw_rlp)
// fmt.Println(" Ext nibble:", numNibbles, nibble)
nibbles = append(nibbles, nibble)
} else {
nibbles = append(nibbles, nil)
nibbles = append(nibbles, []byte{})
}
}

}
// if isHashed {
// proof = append(proof, hashedNodeProof)

// }
// The proof is now reversed (only for non-hashed),
// let's reverse it back to have the leaf at the bottom:
slices.Reverse(proof)
slices.Reverse(nibbles)
// }

return proof, nibbles, proofType, nil
Expand Down
10 changes: 3 additions & 7 deletions geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,9 @@ func transactionsStackTrieInsertionTemplate(t *testing.T, n int) {
}
}

func TestTransaction144(t *testing.T) {
txs := makeTransactions(146)
prepareStackTrieWitness("TransactionInsertion", types.Transactions(txs))
}

func TestTransactionInsertion(t *testing.T) {
txs := makeTransactions(145)
// TODO: check drifted_index at 128
txs := makeTransactions(130)
prepareStackTrieWitness("TransactionInsertion", types.Transactions(txs))
}

Expand Down Expand Up @@ -198,7 +194,7 @@ func batchedTransactionsStackTrieProofTemplate(n int) {
var indexBuf []byte
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(1))

proofS, _, err := stackTrie.GetProof(db, indexBuf)
proofS, _, _, err := stackTrie.GetProof(db, indexBuf)
if err != nil {
fmt.Println(err)
return
Expand Down
Loading

0 comments on commit e9d8587

Please sign in to comment.