From 47ff4450e7f5cecc420ebb9430ba626b92d0f793 Mon Sep 17 00:00:00 2001 From: KimiWu Date: Tue, 16 Apr 2024 16:12:57 +0800 Subject: [PATCH] cleanup --- geth-utils/gethutil/mpt/trie/stacktrie.go | 20 ++--------- geth-utils/gethutil/mpt/witness/branch.go | 35 +++++-------------- .../gen_witness_from_local_blockchain_test.go | 1 + .../witness/gen_witness_transactions_test.go | 2 +- geth-utils/gethutil/mpt/witness/leaf.go | 5 --- .../mpt/witness/modified_extension_node.go | 4 +-- .../gethutil/mpt/witness/prepare_witness.go | 11 +++--- 7 files changed, 20 insertions(+), 58 deletions(-) diff --git a/geth-utils/gethutil/mpt/trie/stacktrie.go b/geth-utils/gethutil/mpt/trie/stacktrie.go index ee1c7f648de..88a570a62e4 100644 --- a/geth-utils/gethutil/mpt/trie/stacktrie.go +++ b/geth-utils/gethutil/mpt/trie/stacktrie.go @@ -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) @@ -701,13 +687,13 @@ 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) @@ -715,7 +701,7 @@ func (st *StackTrie) UpdateAndGetProof(db ethdb.KeyValueReader, indexBuf, value if err != nil { return StackProof{}, err } - printProof(proofC, typesC, indexBuf) + // printProof(proofC, typesC, indexBuf) return StackProof{proofS, proofC, nibblesS, nibblesC, typesS, typesC}, nil } diff --git a/geth-utils/gethutil/mpt/witness/branch.go b/geth-utils/gethutil/mpt/witness/branch.go index 987993a85f8..ccee58d889a 100644 --- a/geth-utils/gethutil/mpt/witness/branch.go +++ b/geth-utils/gethutil/mpt/witness/branch.go @@ -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) @@ -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 @@ -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 { @@ -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) @@ -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 { @@ -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 diff --git a/geth-utils/gethutil/mpt/witness/gen_witness_from_local_blockchain_test.go b/geth-utils/gethutil/mpt/witness/gen_witness_from_local_blockchain_test.go index e769031e829..fccf83c9656 100644 --- a/geth-utils/gethutil/mpt/witness/gen_witness_from_local_blockchain_test.go +++ b/geth-utils/gethutil/mpt/witness/gen_witness_from_local_blockchain_test.go @@ -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") diff --git a/geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go b/geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go index 313b5760b40..73388578265 100644 --- a/geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go +++ b/geth-utils/gethutil/mpt/witness/gen_witness_transactions_test.go @@ -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) } diff --git a/geth-utils/gethutil/mpt/witness/leaf.go b/geth-utils/gethutil/mpt/witness/leaf.go index 333c91dc982..67dccb577bb 100644 --- a/geth-utils/gethutil/mpt/witness/leaf.go +++ b/geth-utils/gethutil/mpt/witness/leaf.go @@ -1,7 +1,6 @@ package witness import ( - "fmt" "math" "main/gethutil/mpt/trie" @@ -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) diff --git a/geth-utils/gethutil/mpt/witness/modified_extension_node.go b/geth-utils/gethutil/mpt/witness/modified_extension_node.go index 13e78a5f4c7..726beea3e4c 100644 --- a/geth-utils/gethutil/mpt/witness/modified_extension_node.go +++ b/geth-utils/gethutil/mpt/witness/modified_extension_node.go @@ -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 { @@ -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 { diff --git a/geth-utils/gethutil/mpt/witness/prepare_witness.go b/geth-utils/gethutil/mpt/witness/prepare_witness.go index be498ca8105..beab5ff1f40 100644 --- a/geth-utils/gethutil/mpt/witness/prepare_witness.go +++ b/geth-utils/gethutil/mpt/witness/prepare_witness.go @@ -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() @@ -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 @@ -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] @@ -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 {