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

Commit

Permalink
Preparing witness for wrong extension node
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Feb 22, 2024
1 parent 4414c85 commit bd82e59
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
12 changes: 9 additions & 3 deletions geth-utils/gethutil/mpt/witness/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func prepareBranchNode(branch1, branch2, extNode1, extNode2, extListRlpBytes []b
return node
}

// getDriftedPosition returns the position in branch to which the leaf drifted because another
// leaf has been added to the same slot. This information is stored into a branch init row.
func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
// getNibbles returns the nibbles of the leaf or extension node.
func getNibbles(leafKeyRow []byte) []byte {
var nibbles []byte
if leafKeyRow[0] != 248 {
var keyLen int
Expand Down Expand Up @@ -193,6 +192,13 @@ func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
}
}

return nibbles
}

// getDriftedPosition returns the position in branch to which the leaf drifted because another
// leaf has been added to the same slot. This information is stored into a branch init row.
func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
nibbles := getNibbles(leafKeyRow)
return nibbles[numberOfNibbles]
}

Expand Down
37 changes: 35 additions & 2 deletions geth-utils/gethutil/mpt/witness/prepare_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,46 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
// to obtain the underlying branch to be able to finally add (besides this branch)
// the placeholder leaf. So we need to query getProof again with one of the leaves that is
// actually in this extension node.
// TODO:
if isAccountProof {
// TODO
node := prepareAccountLeafPlaceholderNode(addr, addrh, key, keyIndex)
nodes = append(nodes, node)
} else {
node := prepareStorageLeafPlaceholderNode(storage_key, key, keyIndex)
nibbles := getNibbles(proof2[len(proof2)-1])
newKey := make([]byte, len(key))
copy(newKey, key)

for i := 0; i < len(nibbles); i++ {
n := nibbles[i]
if key[i] != n {
newKey[i] = n
}
}

// The last nibble should be the one that gets one of the leaves
// in the branch (not nil):
var proof [][]byte
var err error
for i := 0; i < 16; i++ {
newKey[keyIndex] = byte(i)
k := trie.HexToKeybytes(newKey)
ky := common.BytesToHash(k)
proof, _, _, _, _, err = statedb.GetStorageProof(addr, ky)
check(err)
if !isBranch(proof[len(proof)-1]) {
break
}
}

branchRlp := proof[len(proof)-2] // the last element has to be a leaf
isExtension := true

extNode := proof2[len(proof2)-1]
bNode := prepareBranchNode(branchRlp, branchRlp, extNode, extNode, extListRlpBytes, extValues,
key[keyIndex], key[keyIndex], false, false, isExtension)
nodes = append(nodes, bNode)

node := prepareStorageLeafNode(proof[len(proof)-1], proof[len(proof)-1], nil, storage_key, key, nonExistingStorageProof, false, false, false, false)
nodes = append(nodes, node)
}
}
Expand Down

0 comments on commit bd82e59

Please sign in to comment.