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

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Apr 16, 2024
1 parent e5fc51d commit ba8df43
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 59 deletions.
20 changes: 3 additions & 17 deletions geth-utils/gethutil/mpt/trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,20 +655,6 @@ func isBranch(proofEl []byte) bool {
return c == 17
}

func isTxLeaf(proofEl []byte) bool {
elems, _, _ := rlp.SplitList(proofEl)
c, _ := rlp.CountValues(elems)

// 9: for tx (Nonce, Gas, GasPrice, Value, To, Data, r, s, v)
return (c == 9) && !isTxExt(proofEl)
}

func isTxExt(proofEl []byte) bool {
elems, _, _ := rlp.SplitList(proofEl)
idx := proofEl[0] - 225
return len(proofEl) < 50 && proofEl[0] < 248 && elems[idx] == 160
}

func printProof(ps [][]byte, t, idx []byte) {

enable := byte(150)
Expand Down Expand Up @@ -701,21 +687,21 @@ func printProof(ps [][]byte, t, idx []byte) {
}

func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value []byte) (StackProof, error) {
fmt.Println(" ====", indexBuf, "-->", KeybytesToHex(indexBuf))
// fmt.Println(" ====", indexBuf, "-->", KeybytesToHex(indexBuf))

proofS, nibblesS, typesS, err := st.GetProof(db, indexBuf)
if err != nil {
return StackProof{}, err
}
printProof(proofS, typesS, indexBuf)
// printProof(proofS, typesS, indexBuf)

st.Update(indexBuf, value)

proofC, nibblesC, typesC, err := st.GetProof(db, indexBuf)
if err != nil {
return StackProof{}, err
}
printProof(proofC, typesC, indexBuf)
// printProof(proofC, typesC, indexBuf)

return StackProof{proofS, proofC, nibblesS, nibblesC, typesS, typesC}, nil
}
Expand Down
35 changes: 8 additions & 27 deletions geth-utils/gethutil/mpt/witness/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)

func PrepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []byte, extValues [][]byte, key, driftedInd byte,
isBranchSPlaceholder, isBranchCPlaceholder, isExtension bool) Node {
return prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes, extValues, key, driftedInd,
isBranchSPlaceholder, isBranchCPlaceholder, isExtension)
}

// isBranch takes GetProof element and returns whether the element is a branch.
func isBranch(proofEl []byte) bool {
elems, _, err := rlp.SplitList(proofEl)
Expand All @@ -25,23 +19,6 @@ func isBranch(proofEl []byte) bool {
return c == 17
}

func isTxLeaf(proofEl []byte) bool {
elems, _, err := rlp.SplitList(proofEl)
check(err)
c, err1 := rlp.CountValues(elems)
check(err1)
// 2: hashed node
// 9: for tx (Nonce, Gas, GasPrice, Value, To, Data, r, s, v)
// ext node is also 2
return (c == 9 || c == 2) && !isTxExt(proofEl)
}

func isTxExt(proofEl []byte) bool {
elems, _, _ := rlp.SplitList(proofEl)
idx := proofEl[0] - 225
return len(proofEl) < 50 && proofEl[0] < 248 && elems[idx] == 160
}

// prepareBranchWitness takes the rows that are to be filled with branch data and it takes
// a branch as returned by GetProof. There are 19 rows for a branch and prepareBranchWitness
// fills the rows from index 1 to index 16 (index 0 is init, index 17 and 18 are for extension
Expand All @@ -53,7 +30,7 @@ func prepareBranchWitness(rows [][]byte, branch []byte, branchStart int, branchR
rowInd := 1
colInd := branchNodeRLPLen - 1

// TODO: if input branch is a leaf node, it'll work abnormally
// TODO: if the input is a leaf, it would throw exception
i := 0
insideInd := -1
for {
Expand Down Expand Up @@ -238,8 +215,11 @@ func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {

// addBranchAndPlaceholder adds to the rows a branch and its placeholder counterpart
// (used when one of the proofs have one branch more than the other).
func addBranchAndPlaceholder(proof1, proof2 [][]byte, extNibblesS, extNibblesC []byte,
extProofTx, leafRow0, key []byte, keyIndex int, isShorterProofLastLeaf bool,
func addBranchAndPlaceholder(
proof1, proof2 [][]byte,
extNibblesS, extNibblesC []byte,
extProofTx, leafRow0, key []byte,
keyIndex int, isShorterProofLastLeaf bool,
) (bool, bool, int, Node) {

len1 := len(proof1)
Expand All @@ -261,6 +241,7 @@ func addBranchAndPlaceholder(proof1, proof2 [][]byte, extNibblesS, extNibblesC [
var proof []byte
var extNibbles []byte
if isTxProof {
// At 16th tx, the length of proofS/C is 2 only
extNibbles = extNibblesS
proof = extProofTx
} else {
Expand Down Expand Up @@ -325,7 +306,7 @@ func addBranchAndPlaceholder(proof1, proof2 [][]byte, extNibblesS, extNibblesC [
}

// Note that isModifiedExtNode happens also when we have a branch instead of shortExtNode
isModifiedExtNode := (!isBranch(longExtNode) && !isShorterProofLastLeaf)
isModifiedExtNode := !isBranch(longExtNode) && !isShorterProofLastLeaf

// We now get the first nibble of the leaf that was turned into branch.
// This first nibble presents the position of the leaf once it moved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ func TestExtNodeDeletedExtShortIsBranchFirstLevel(t *testing.T) {
}

func TestSimulateStackTrieUnder128Txs(t *testing.T) {
t.Skip("For stack trie witness check only")
SkipIfNoGeth(t)
// The trie is empty before we add key1, key2, key3.
key1 := common.HexToHash("0x2345610000000000000000000000000000000000000000000000000000000000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestStackTrieInsertion_33Txs(t *testing.T) {
}

func TestStackTrieInsertion_129Txs(t *testing.T) {
// The first tx (index 0) is inserted into position 8 of the top branch
// The first tx (index 0) is inserted into position 8 of the branch of the first layer
// Th 129th tx is the neighbor of the first tx
transactionsStackTrieInsertionTemplate(t, 129)
}
Expand Down
5 changes: 0 additions & 5 deletions geth-utils/gethutil/mpt/witness/leaf.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package witness

import (
"fmt"
"math"

"main/gethutil/mpt/trie"
Expand Down Expand Up @@ -607,10 +606,6 @@ func prepareStorageLeafNode(leafS, leafC, neighbourNode []byte, storage_key comm

keyC, valueC, listRlpBytes2, valueRlpBytes2 := prepareStorageLeafInfo(leafC, false, isCPlaceholder)

fmt.Println("-", key)
fmt.Println("-", keyS, leafS)
fmt.Println("-", storage_key)

rows = append(rows, keyC)
rows = append(rows, valueC)

Expand Down
4 changes: 2 additions & 2 deletions geth-utils/gethutil/mpt/witness/modified_extension_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
// These rows are added only when an existing extension node gets shortened or elongated (in terms
// of the extension node nibbles) because of another extension node being added or deleted.
// The rows added are somewhat exceptional as otherwise they do not appear.
func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr common.Address,
func equipLeafWithModExtensionNode(
statedb *state.StateDB, leafNode Node, addr common.Address,
proof1, proof2, extNibblesS, extNibblesC [][]byte,
proofTx, key []byte,
keyIndex, numberOfNibbles int, isAccountProof bool) Node {
Expand All @@ -21,7 +22,6 @@ func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr c
isTxProof := len(proofTx) != 0

var longExtNode []byte
// FIXME this is a workaround to get ext node
if isTxProof {
longExtNode = proofTx
} else {
Expand Down
21 changes: 14 additions & 7 deletions geth-utils/gethutil/mpt/witness/prepare_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st

storageProof, neighbourNode1, extNibbles1, isLastLeaf1, isNeighbourNodeHashed1, err := statedb.GetStorageProof(addr, tMod.Key)
check(err)
fmt.Println("Storage ProofS:", len(storageProof), storageProof)

sRoot := statedb.GetTrie().Hash()

Expand All @@ -221,7 +220,6 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st

storageProof1, neighbourNode2, extNibbles2, isLastLeaf2, isNeighbourNodeHashed2, err := statedb.GetStorageProof(addr, tMod.Key)
check(err)
fmt.Println("Storage ProofC:", len(storageProof1), storageProof1)

aNode := aNeighbourNode2
aIsLastLeaf := aIsLastLeaf1
Expand Down Expand Up @@ -318,9 +316,9 @@ func prepareWitnessSpecial(testName string, trieModifications []TrieModification
// E. [(EXT, BRANCH) - LEAF] -> [(EXT, BRANCH) - EXT - BRANCH - LEAF] --> 130, 514
//
// --- TODO: modified extension nodes, not working now
// M. [EXT - BRANCH] -> [BRANCH - LEAF] --> 0 under 16 txs or 16 (modified ext.)
// M. [(BRANCH) - EXT - BRANCH - HASHED] -> [(BRANCH) - BRANCH - LEAF] --> 144
// M. [(BRANCH, EXT) - BRANCH - HASHED] -> [(BRANCH, EXT) - LEAF] --> 512
// M1. [EXT - BRANCH] -> [BRANCH - LEAF] --> 0 under 16 txs or 16 (modified ext.)
// M2. [(BRANCH) - EXT - BRANCH - HASHED] -> [(BRANCH) - BRANCH - LEAF] --> 144
// M3. [(BRANCH, EXT) - BRANCH - HASHED] -> [(BRANCH, EXT) - LEAF] --> 512
// Issue:
// Take tx144 as example, the proof is
// [BRANCH_S1 - BRANCH_S2 - EXT_S - BRANCH_S3 - HASHED] -> [BRANCH_C1 - BRANCH_C2 - BRANCH_C3 - LEAF]
Expand Down Expand Up @@ -518,7 +516,8 @@ func updateStateAndPrepareWitness(testName string, keys, values []common.Hash, a
// convertProofToWitness takes two GetProof proofs (before and after a single modification) and prepares
// a witness for the MPT circuit. Alongside, it prepares the byte streams that need to be hashed
// and inserted into the Keccak lookup table.
func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []byte,
func convertProofToWitness(
statedb *state.StateDB, addr common.Address, addrh []byte,
proof1, proof2, extNibblesS, extNibblesC [][]byte,
storage_key common.Hash, key []byte, neighbourNode []byte,
isAccountProof, nonExistingAccountProof, nonExistingStorageProof, isShorterProofLastLeaf bool) []Node {
Expand Down Expand Up @@ -619,8 +618,16 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
leafRow0 = proof2[len2-1]
}

var lastExtNibbleS, lastExtNibbleC []byte
if len(extNibblesS) != 0 {
lastExtNibbleS = extNibblesS[len1-1]
}
if len(extNibblesC) != 0 {
lastExtNibbleC = extNibblesC[len2-1]
}

isModifiedExtNode, _, numberOfNibbles, bNode :=
addBranchAndPlaceholder(proof1, proof2, extNibblesS[len1-1], extNibblesC[len2-1], nil, leafRow0, key, keyIndex, isShorterProofLastLeaf)
addBranchAndPlaceholder(proof1, proof2, lastExtNibbleS, lastExtNibbleC, nil, leafRow0, key, keyIndex, isShorterProofLastLeaf)

nodes = append(nodes, bNode)

Expand Down

0 comments on commit ba8df43

Please sign in to comment.