diff --git a/mpt-witness-generator/witness/leaf.go b/mpt-witness-generator/witness/leaf.go index 39f36be95e..bec36a10b1 100644 --- a/mpt-witness-generator/witness/leaf.go +++ b/mpt-witness-generator/witness/leaf.go @@ -275,6 +275,11 @@ func prepareAccountLeafNode(addr common.Address, addrh []byte, leafS, leafC, nei values[AccountDrifted] = keyDrifted values[AccountWrong] = wrongValue + for i := 0; i < 6; i++ { + row := make([]byte, rowLen) + values = append(values, row) + } + leaf := AccountNode{ Address: addr, Key: addrh, @@ -538,6 +543,11 @@ func prepareStorageLeafNode(leafS, leafC, neighbourNode []byte, storage_key comm } rows = append(rows, nonExistingStorageRow) + for i := 0; i < 6; i++ { + row := make([]byte, rowLen) + rows = append(rows, row) + } + leaf := StorageNode{ Address: storage_key, Key: trie.HexToKeybytes(key), diff --git a/mpt-witness-generator/witness/modified_extension_node.go b/mpt-witness-generator/witness/modified_extension_node.go index ad4d0073de..1359d2134a 100644 --- a/mpt-witness-generator/witness/modified_extension_node.go +++ b/mpt-witness-generator/witness/modified_extension_node.go @@ -6,11 +6,11 @@ import ( "github.com/privacy-scaling-explorations/mpt-witness-generator/trie" ) -// prepareModExtensionNode adds rows for a modified extension node before and after modification. +// equipLeafWithModExtensionNode adds rows for a modified extension node before and after modification. // 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 prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows *[][]byte, proof1, proof2, +func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr common.Address, rows *[][]byte, proof1, proof2, extNibblesS, extNibblesC [][]byte, key, neighbourNode []byte, keyIndex, extensionNodeInd, numberOfNibbles int, @@ -175,9 +175,6 @@ func prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows * */ listRlpBytes := [2][]byte{extListRlpBytesS, extListRlpBytesC} - modExtensionNode := ModExtensionNode{ - ListRlpBytes: listRlpBytes, - } var values [][]byte extValuesS = append(extValuesS[:1], extValuesS[2:]...) @@ -189,9 +186,18 @@ func prepareModExtensionNode(statedb *state.StateDB, addr common.Address, rows * keccakData = append(keccakData, longExtNode) keccakData = append(keccakData, shortExtNode) - return Node{ - ModExtension: &modExtensionNode, - Values: values, - KeccakData: keccakData, + if leafNode.Account == nil { + leafNode.Storage.ModListRlpBytes = listRlpBytes + } else { + leafNode.Account.ModListRlpBytes = listRlpBytes + } + + l := len(leafNode.Values) + for i := 0; i < 6; i++ { + leafNode.Values[l-6+i] = values[i] } + + leafNode.KeccakData = append(leafNode.KeccakData, keccakData...) + + return leafNode } diff --git a/mpt-witness-generator/witness/nodes.go b/mpt-witness-generator/witness/nodes.go index 9377f99649..70f7a07656 100644 --- a/mpt-witness-generator/witness/nodes.go +++ b/mpt-witness-generator/witness/nodes.go @@ -69,17 +69,6 @@ type ExtensionBranchNode struct { Branch BranchNode `json:"branch"` } -type ModExtensionNode struct { - ListRlpBytes [2][]byte -} - -func (n *ModExtensionNode) MarshalJSON() ([]byte, error) { - listRlpBytes1 := base64ToString(n.ListRlpBytes[0]) - listRlpBytes2 := base64ToString(n.ListRlpBytes[1]) - jsonResult := fmt.Sprintf(`{"list_rlp_bytes":[%s,%s]}`, listRlpBytes1, listRlpBytes2) - return []byte(jsonResult), nil -} - type AccountNode struct { Address common.Address Key []byte @@ -89,6 +78,7 @@ type AccountNode struct { DriftedRlpBytes []byte WrongRlpBytes []byte IsModExtension [2]bool `json:"is_mod_extension"` + ModListRlpBytes [2][]byte } func (n *AccountNode) MarshalJSON() ([]byte, error) { @@ -102,9 +92,11 @@ func (n *AccountNode) MarshalJSON() ([]byte, error) { valueListRlpBytes2 := base64ToString(n.ValueListRlpBytes[1]) driftedRlpBytes := base64ToString(n.DriftedRlpBytes) wrongRlpBytes := base64ToString(n.WrongRlpBytes) - jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "value_list_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t]}`, + modListRlpBytes1 := base64ToString(n.ModListRlpBytes[0]) + modListRlpBytes2 := base64ToString(n.ModListRlpBytes[1]) + jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "value_list_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t], "mod_list_rlp_bytes":[%s,%s]}`, address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, valueListRlpBytes1, valueListRlpBytes2, - driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1]) + driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1], modListRlpBytes1, modListRlpBytes2) return []byte(jsonResult), nil } @@ -116,6 +108,7 @@ type StorageNode struct { DriftedRlpBytes []byte `json:"drifted_rlp_bytes"` WrongRlpBytes []byte `json:"wrong_rlp_bytes"` IsModExtension [2]bool `json:"is_mod_extension"` + ModListRlpBytes [2][]byte } func (n *StorageNode) MarshalJSON() ([]byte, error) { @@ -127,8 +120,11 @@ func (n *StorageNode) MarshalJSON() ([]byte, error) { valueRlpBytes2 := base64ToString(n.ValueRlpBytes[1]) driftedRlpBytes := base64ToString(n.DriftedRlpBytes) wrongRlpBytes := base64ToString(n.WrongRlpBytes) - jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t]}`, - address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1]) + modListRlpBytes1 := base64ToString(n.ModListRlpBytes[0]) + modListRlpBytes2 := base64ToString(n.ModListRlpBytes[1]) + jsonResult := fmt.Sprintf(`{"address":%s, "key":%s, "list_rlp_bytes":[%s,%s], "value_rlp_bytes":[%s,%s], "drifted_rlp_bytes":%s, "wrong_rlp_bytes":%s, "is_mod_extension": [%t, %t], "mod_list_rlp_bytes":[%s,%s]}`, + address, key, listRlpBytes1, listRlpBytes2, valueRlpBytes1, valueRlpBytes2, driftedRlpBytes, wrongRlpBytes, n.IsModExtension[0], n.IsModExtension[1], + modListRlpBytes1, modListRlpBytes2) return []byte(jsonResult), nil } @@ -153,7 +149,6 @@ type Node struct { ExtensionBranch *ExtensionBranchNode `json:"extension_branch"` Account *AccountNode `json:"account"` Storage *StorageNode `json:"storage"` - ModExtension *ModExtensionNode `json:"mod_extension"` Values JSONableValues `json:"values"` KeccakData JSONableValues `json:"keccak_data"` } diff --git a/mpt-witness-generator/witness/prepare_witness.go b/mpt-witness-generator/witness/prepare_witness.go index f51c566412..ae320de593 100644 --- a/mpt-witness-generator/witness/prepare_witness.go +++ b/mpt-witness-generator/witness/prepare_witness.go @@ -1,7 +1,6 @@ package witness import ( - "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -445,11 +444,11 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh [] nodes = append(nodes, bNode) + var leafNode Node if isAccountProof { // Add account leaf after branch placeholder: - var node Node if !isModifiedExtNode { - node = prepareAccountLeafNode(addr, addrh, proof1[len1-1], proof2[len2-1], neighbourNode, key, false, false, false) + leafNode = prepareAccountLeafNode(addr, addrh, proof1[len1-1], proof2[len2-1], neighbourNode, key, false, false, false) } else { isSModExtension := false isCModExtension := false @@ -458,14 +457,12 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh [] } else { isCModExtension = true } - node = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension) + leafNode = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension) } - nodes = append(nodes, node) } else { // Add storage leaf after branch placeholder - var node Node if !isModifiedExtNode { - node = prepareStorageLeafNode(proof1[len1-1], proof2[len2-1], neighbourNode, storage_key, key, nonExistingStorageProof, false, false, false, false) + leafNode = prepareStorageLeafNode(proof1[len1-1], proof2[len2-1], neighbourNode, storage_key, key, nonExistingStorageProof, false, false, false, false) } else { isSModExtension := false isCModExtension := false @@ -474,22 +471,19 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh [] } else { isCModExtension = true } - node = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension) + leafNode = prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, isSModExtension, isCModExtension) } - nodes = append(nodes, node) } // When a proof element is a modified extension node (new extension node appears at the position // of the existing extension node), additional rows are added (extension node before and after // modification). if isModifiedExtNode { - // TODO - modExtensionNode := prepareModExtensionNode(statedb, addr, &rows, proof1, proof2, extNibblesS, extNibblesC, key, neighbourNode, + leafNode = equipLeafWithModExtensionNode(statedb, leafNode, addr, &rows, proof1, proof2, extNibblesS, extNibblesC, key, neighbourNode, keyIndex, extensionNodeInd, numberOfNibbles, additionalBranch, isAccountProof, nonExistingAccountProof, isShorterProofLastLeaf, branchC16, branchC1, &toBeHashed) - // node = append(nodes, modExtensionNode) - fmt.Println(modExtensionNode) } + nodes = append(nodes, leafNode) } else { node := prepareLeafAndPlaceholderNode(addr, addrh, proof1, proof2, storage_key, key, nonExistingAccountProof, isAccountProof, false, false) nodes = append(nodes, node)