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

Commit a90ae32

Browse files
committed
Construct placeholder leaf for wrong extension node case
1 parent bd82e59 commit a90ae32

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

geth-utils/gethutil/mpt/witness/prepare_witness.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,42 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
554554
branchRlp := proof[len(proof)-2] // the last element has to be a leaf
555555
isExtension := true
556556

557-
extNode := proof2[len(proof2)-1]
557+
extNode := proof2[len(proof2)-1] // Let's name it E1
558558
bNode := prepareBranchNode(branchRlp, branchRlp, extNode, extNode, extListRlpBytes, extValues,
559559
key[keyIndex], key[keyIndex], false, false, isExtension)
560560
nodes = append(nodes, bNode)
561561

562-
node := prepareStorageLeafNode(proof[len(proof)-1], proof[len(proof)-1], nil, storage_key, key, nonExistingStorageProof, false, false, false, false)
562+
/*
563+
Let's say we have an extension node E1 at the following path [3, 5, 8].
564+
Let's say E1 has nibbles [1, 2, 3]. Let's say we want to prove there does not exist
565+
a leaf at [3, 5, 8, 1, 2] (because there is E1 there).
566+
567+
We need to construct a leaf L1 that will have the key equal to the queried key.
568+
This means the nibbles are the same as in the path to E1 (without extension nibbles).
569+
570+
In the circuit, the leaf L1 will have the same key as the queried key once
571+
the KeyData will be queried with offset 1 (to get the accumulated key RLC up until E1).
572+
The nibbles stored in L1 will be added to the RLC and compared with the queried
573+
key (has to be the same).
574+
*/
575+
576+
l := keyIndex - len(nibbles)
577+
path := make([]byte, l) // Up to the E1 nibbles (without them)
578+
copy(path, key[:l])
579+
// The remaining `key` nibbles are to be stored in the constructed leaf.
580+
581+
compact := trie.HexToCompact(key[l:])
582+
// Add RLP:
583+
compactLen := byte(len(compact))
584+
rlp2 := 128 + compactLen
585+
rlp1 := 192 + compactLen + 1
586+
// Constructed leaf L1:
587+
constructedLeaf := append([]byte{rlp1, rlp2}, compact...)
588+
589+
// Add dummy value:
590+
constructedLeaf = append(constructedLeaf, 1)
591+
592+
node := prepareStorageLeafNode(proof[len(proof)-1], constructedLeaf, nil, storage_key, key, nonExistingStorageProof, false, false, false, false)
563593
nodes = append(nodes, node)
564594
}
565595
}

0 commit comments

Comments
 (0)