diff --git a/geth-utils/gethutil/mpt/trie/stacktrie.go b/geth-utils/gethutil/mpt/trie/stacktrie.go index ee1c7f648d..88a570a62e 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 987993a85f..b982bf111b 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 { @@ -203,15 +180,12 @@ func getNibbles(leafKeyRow []byte) []byte { nibbles = append(nibbles, leafKeyRow[1]-16) } } else { - // [248 202 48 184 199 248 197 128 131 4 147 224 98 148 0 ...] - // `202` (leafKeyRow[1]) is the length of the payload - // `48` (leafKeyRow[2]) is the first byte of the payload - keyLen := int(leafKeyRow[1] - 128) - if (leafKeyRow[2] != 32) && (leafKeyRow[2] != 0) { // second term is for extension node - if leafKeyRow[2] < 32 { // extension node - nibbles = append(nibbles, leafKeyRow[2]-16) + keyLen := int(leafKeyRow[2] - 128) + if (leafKeyRow[3] != 32) && (leafKeyRow[3] != 0) { // second term is for extension node + if leafKeyRow[3] < 32 { // extension node + nibbles = append(nibbles, leafKeyRow[3]-16) } else { // leaf - nibbles = append(nibbles, leafKeyRow[2]-48) + nibbles = append(nibbles, leafKeyRow[3]-48) } } for i := 0; i < keyLen-1; i++ { // -1 because the first byte doesn't have any nibbles @@ -238,8 +212,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 +238,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 +303,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 e769031e82..fccf83c965 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 313b5760b4..7338857826 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 333c91dc98..67dccb577b 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 13e78a5f4c..726beea3e4 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 be498ca810..620f3eacce 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 { @@ -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)