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

Commit edb7a56

Browse files
committed
extNibbles returned by getProof has the same number as the proof now
1 parent 13a7759 commit edb7a56

File tree

6 files changed

+63
-38
lines changed

6 files changed

+63
-38
lines changed

geth-utils/gethutil/mpt/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# MPT witness generator
2+
3+
## Generate witnesses
4+
5+
To generate witnesses for the MPT circuit, execute
6+
7+
```
8+
go test -v ./...
9+
```
10+
11+
To generate the tests that use a local blockchain you need a local `geth`. You would
12+
need to run something like:
13+
```
14+
geth --dev --http --ipcpath ~/Library/Ethereum/geth.ipc
15+
16+
```
17+
The local `geth` is used to generate some tests that have a small number of accounts so that
18+
these accounts appear in the first or second level of the trie. You might need to remove the
19+
database if you already have some accounts:
20+
21+
```
22+
geth removedb
23+
```
24+
25+
The witness files will appear in generated_witnesses folder.
26+
27+
## Format the code
28+
29+
To format the code use:
30+
31+
```
32+
gofmt -w ./*
33+
```

geth-utils/gethutil/mpt/trie/proof.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,15 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) (
107107
// copy n.Key before it gets changed in ProofHash
108108
var nCopy []byte
109109
if short, ok := n.(*ShortNode); ok {
110-
if !hasTerm(short.Key) { // only for extension keys
110+
if !hasTerm(short.Key) { // extension keys
111111
nCopy = make([]byte, len(short.Key))
112112
copy(nCopy, short.Key)
113113
extNibbles = append(extNibbles, nCopy)
114+
} else {
115+
extNibbles = append(extNibbles, []byte{})
114116
}
117+
} else {
118+
extNibbles = append(extNibbles, []byte{})
115119
}
116120

117121
n, _ = hasher.ProofHash(n)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ func getDriftedPosition(leafKeyRow []byte, numberOfNibbles int) byte {
204204

205205
// addBranchAndPlaceholder adds to the rows a branch and its placeholder counterpart
206206
// (used when one of the proofs have one branch more than the other).
207-
func addBranchAndPlaceholder(proof1, proof2,
208-
extNibblesS, extNibblesC [][]byte,
207+
func addBranchAndPlaceholder(proof1, proof2 [][]byte,
208+
extNibblesS, extNibblesC []byte,
209209
leafRow0, key, neighbourNode []byte,
210-
keyIndex, extensionNodeInd int,
210+
keyIndex int,
211211
additionalBranch, isAccountProof, nonExistingAccountProof,
212212
isShorterProofLastLeaf bool, toBeHashed *[][]byte) (bool, bool, int, Node) {
213213
len1 := len(proof1)
@@ -226,9 +226,9 @@ func addBranchAndPlaceholder(proof1, proof2,
226226
if isExtension {
227227
var numNibbles byte
228228
if len1 > len2 {
229-
numNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesS, extensionNodeInd, proof1[len1-3], proof1[len1-3])
229+
numNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesS, proof1[len1-3], proof1[len1-3])
230230
} else {
231-
numNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesC, extensionNodeInd, proof2[len2-3], proof2[len2-3])
231+
numNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesC, proof2[len2-3], proof2[len2-3])
232232
}
233233
numberOfNibbles = int(numNibbles)
234234
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package witness
22

3-
func prepareExtensions(extNibbles [][]byte, extensionNodeInd int, proofEl1, proofEl2 []byte) (byte, []byte, [][]byte) {
3+
func prepareExtensions(extNibbles []byte, proofEl1, proofEl2 []byte) (byte, []byte, [][]byte) {
44
var values [][]byte
55
v1 := make([]byte, valueLen)
66
v2 := make([]byte, valueLen)
@@ -30,9 +30,9 @@ func prepareExtensions(extNibbles [][]byte, extensionNodeInd int, proofEl1, proo
3030
}
3131
}
3232
ind := 0
33-
for j := startNibblePos; j < len(extNibbles[extensionNodeInd]); j += 2 {
33+
for j := startNibblePos; j < len(extNibbles); j += 2 {
3434
v3[2+ind] = // TODO: check 2 + ind
35-
extNibbles[extensionNodeInd][j]
35+
extNibbles[j]
3636
ind++
3737
}
3838
values = append(values, v1)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr common.Address, proof1, proof2,
1515
extNibblesS, extNibblesC [][]byte,
1616
key, neighbourNode []byte,
17-
keyIndex, extensionNodeInd, numberOfNibbles int,
17+
keyIndex, numberOfNibbles int,
1818
additionalBranch, isAccountProof, nonExistingAccountProof,
1919
isShorterProofLastLeaf bool, toBeHashed *[][]byte) Node {
2020
len1 := len(proof1)
@@ -34,7 +34,7 @@ func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr c
3434
extNibbles = extNibblesS
3535
}
3636

37-
_, extListRlpBytesS, extValuesS := prepareExtensions(extNibbles, extensionNodeInd, longExtNode, longExtNode)
37+
_, extListRlpBytesS, extValuesS := prepareExtensions(extNibbles[len(extNibbles)-1], longExtNode, longExtNode)
3838

3939
// Get nibbles of the extension node that gets shortened because of the newly insertd
4040
// extension node:
@@ -107,7 +107,7 @@ func equipLeafWithModExtensionNode(statedb *state.StateDB, leafNode Node, addr c
107107
// Enable `prepareExtensionRows` call:
108108
extNibbles = append(extNibbles, nibbles)
109109

110-
_, extListRlpBytesC, extValuesC = prepareExtensions(extNibbles, extensionNodeInd+1, shortExtNode, shortExtNode)
110+
_, extListRlpBytesC, extValuesC = prepareExtensions(extNibbles[len(extNibbles)-1], shortExtNode, shortExtNode)
111111
} else {
112112
// When the short node is a branch (and not an extension node), we have nothing to be put in
113113
// the C extension node witness (as a short node). We copy the long node (S extension node) to let

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,6 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st
267267
// Needs to be after `specialTest == 1` preparation:
268268
nodes = append(nodes, GetStartNode(proofType, sRoot, cRoot, specialTest))
269269

270-
/*
271-
if tMod.Type == StorageDoesNotExist {
272-
fmt.Println("===================");
273-
fmt.Println(addr);
274-
fmt.Println(tMod.Key);
275-
fmt.Println("");
276-
277-
for i := 0; i < len(storageProof); i++ {
278-
fmt.Println(storageProof[i])
279-
fmt.Println("")
280-
}
281-
fmt.Println("========");
282-
fmt.Println("");
283-
}
284-
*/
285-
286270
// In convertProofToWitness, we can't use account address in its original form (non-hashed), because
287271
// of the "special" test for which we manually manipulate the "hashed" address and we don't have a preimage.
288272
// TODO: addr is used for calling GetProof for modified extension node only, might be done in a different way
@@ -380,8 +364,6 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
380364
}
381365

382366
var isExtension bool
383-
extensionNodeInd := 0
384-
385367
var extListRlpBytes []byte
386368
var extValues [][]byte
387369
for i := 0; i < 4; i++ {
@@ -393,7 +375,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
393375
for i := 0; i < upTo; i++ {
394376
if !isBranch(proof1[i]) {
395377
isNonExistingProof := (isAccountProof && nonExistingAccountProof) || (!isAccountProof && nonExistingStorageProof)
396-
areThereNibbles := len(extNibblesS) != 0 || len(extNibblesC) != 0
378+
areThereNibbles := len(extNibblesS[i]) != 0 || len(extNibblesC[i]) != 0
397379
// If i < upTo-1, it means it's not a leaf, so it's an extension node.
398380
// There is no any special relation between isNonExistingProof and isExtension,
399381
// except that in the non-existing proof the extension node can appear in `i == upTo-1`.
@@ -404,10 +386,9 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
404386
if (i != upTo-1) || (areThereNibbles && isNonExistingProof) { // extension node
405387
var numberOfNibbles byte
406388
isExtension = true
407-
numberOfNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesS, extensionNodeInd, proof1[i], proof2[i])
389+
numberOfNibbles, extListRlpBytes, extValues = prepareExtensions(extNibblesS[i], proof1[i], proof2[i])
408390

409391
keyIndex += int(numberOfNibbles)
410-
extensionNodeInd++
411392
continue
412393
}
413394

@@ -445,9 +426,10 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
445426
leafRow0 = proof2[len2-1]
446427
}
447428

448-
isModifiedExtNode, _, numberOfNibbles, bNode := addBranchAndPlaceholder(proof1, proof2, extNibblesS, extNibblesC,
429+
isModifiedExtNode, _, numberOfNibbles, bNode := addBranchAndPlaceholder(proof1, proof2,
430+
extNibblesS[len1-1], extNibblesC[len2-1],
449431
leafRow0, key, neighbourNode,
450-
keyIndex, extensionNodeInd, additionalBranch,
432+
keyIndex, additionalBranch,
451433
isAccountProof, nonExistingAccountProof, isShorterProofLastLeaf, &toBeHashed)
452434

453435
nodes = append(nodes, bNode)
@@ -488,7 +470,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
488470
// modification).
489471
if isModifiedExtNode {
490472
leafNode = equipLeafWithModExtensionNode(statedb, leafNode, addr, proof1, proof2, extNibblesS, extNibblesC, key, neighbourNode,
491-
keyIndex, extensionNodeInd, numberOfNibbles, additionalBranch,
473+
keyIndex, numberOfNibbles, additionalBranch,
492474
isAccountProof, nonExistingAccountProof, isShorterProofLastLeaf, &toBeHashed)
493475
}
494476
nodes = append(nodes, leafNode)
@@ -513,7 +495,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
513495
node := prepareStorageLeafPlaceholderNode(storage_key, key, keyIndex)
514496
nodes = append(nodes, node)
515497
}
516-
} else if len(extNibblesC) > len(proof2)-1 {
498+
} else {
517499
isLastExtNode := len(extNibblesC[len(proof2)-1]) != 0
518500
if isLastExtNode {
519501
// We need to add a placeholder branch and a placeholder leaf.
@@ -564,7 +546,11 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
564546
newKey[keyIndex] = byte(i)
565547
k := trie.HexToKeybytes(newKey)
566548
ky := common.BytesToHash(k)
567-
proof, _, _, _, _, err = statedb.GetStorageProof(addr, ky)
549+
if isAccountProof {
550+
proof, _, _, _, _, err = statedb.GetProof(addr)
551+
} else {
552+
proof, _, _, _, _, err = statedb.GetStorageProof(addr, ky)
553+
}
568554
check(err)
569555
if !isBranch(proof[len(proof)-1]) {
570556
break
@@ -586,6 +572,8 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
586572
copy(path, key[:l])
587573
// The remaining `key` nibbles are to be stored in the constructed leaf - in our example [1 2 4 ...]
588574

575+
// TODO: construct for account proof
576+
589577
compact := trie.HexToCompact(key[l:])
590578
// Add RLP:
591579
compactLen := byte(len(compact))

0 commit comments

Comments
 (0)