@@ -292,7 +292,7 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st
292
292
// prepareStackTrieWitness obtains the GetProof proof before and after the modification for each
293
293
// of the modification. It then converts the two proofs into an MPT circuit witness for each of
294
294
// the modifications and stores it into a file.
295
- func prepareStackTrieWitness (testName string , list types.DerivableList ) {
295
+ func prepareStackTrieWitness (testName string , list types.DerivableList , loop bool ) {
296
296
db := rawdb .NewMemoryDatabase ()
297
297
stackTrie := trie .NewStackTrie (db )
298
298
proofs , _ := stackTrie .UpdateAndGetProofs (db , list )
@@ -303,14 +303,21 @@ func prepareStackTrieWitness(testName string, list types.DerivableList) {
303
303
for i , proof := range proofs {
304
304
idx := i + 1
305
305
306
- // debug section
307
- // i := len(proofs) - 1
308
- // if len(proofs) > 128 {
309
- // i = len(proofs) - 1
310
- // }
311
- // proof := proofs[i]
312
- // idx := i
306
+ // ==== debug section
307
+ if ! loop {
308
+ i := len (proofs ) - 2
309
+ if len (proofs ) > 128 {
310
+ i = len (proofs ) - 1
311
+ }
312
+ proof = proofs [i ]
313
+ idx = i
314
+
315
+ // for _, p := range proof.GetProofC() {
316
+ // fmt.Println("C: ", p)
317
+ // }
318
+ }
313
319
// =====
320
+
314
321
var subNodes []Node
315
322
subNodes = append (subNodes , GetStartNode (testName , common.Hash {}, root , 0 ))
316
323
var node []Node
@@ -326,6 +333,10 @@ func prepareStackTrieWitness(testName string, list types.DerivableList) {
326
333
verifyNodeNumber (subNodes , proof )
327
334
328
335
nodes = append (nodes , subNodes ... )
336
+
337
+ if ! loop {
338
+ break
339
+ }
329
340
}
330
341
StoreNodes (testName , nodes )
331
342
@@ -411,12 +422,19 @@ func prepareWitnessSpecial(testName string, trieModifications []TrieModification
411
422
// -[o] [BRANCH] -> [BRANCH - LEAF] --> 0
412
423
// -[o] [BRANCH - BRANCH - LEAF] -> [BRANCH - BRANCH - EXT - BRANCH - LEAF] --> 129
413
424
// -[o] [BRANCH - BRANCH - EXT - BRANCH] -> [BRANCH - BRANCH - EXT - BRANCH - LEAF] --> 130
414
- // -[M] [BRANCH - BRANCH - EXT - BRANCH - HASHED] -> [BRANCH - BRANCH - BRANCH - LEAF] --> 144(hashed node)
415
- // -[M] [BRANCH - BRANCH - EXT - BRANCH - BRANCH - HASHED] -> [BRANCH - BRANCH - EXT - BRANCH - LEAF] --> 512 (hashed node)
425
+ // -[M] [BRANCH - BRANCH - EXT - BRANCH - HASHED] -> [BRANCH - BRANCH - BRANCH - LEAF] --> 144
426
+ // -[M] [BRANCH - BRANCH - EXT - BRANCH - BRANCH - HASHED] -> [BRANCH - BRANCH - EXT - BRANCH - LEAF] --> 512
416
427
// -[o] [BRANCH - BRANCH - (...BRANCH)] -> [BRANCH - BRANCH - (...BRANCH) - LEAF] --> 146 ~ 176
417
428
// -[o] [BRANCH - BRANCH - EXT - (BRANCH..)] -> [BRANCH - BRANCH - EXT - (BRANCH..) - LEAF] --> 258~
418
429
// -[o] [BRANCH - BRANCH - EXT - BRANCH - LEAF] -> [BRANCH - BRANCH - EXT - BRANCH - EXT - BRANCH - LEAF] --> 513
419
430
// -[o] [BRANCH - BRANCH - EXT - BRANCH - EXT - BRANCH] -> [BRANCH - BRANCH - EXT - BRANCH - EXT - BRANCH - LEAF] --> 514~
431
+ //
432
+ // TODO modified extension node
433
+ // Take tx144 as example, the proof is
434
+ // [BRANCH_S1 - BRANCH_S2 - EXT_S - BRANCH_S3 - HASHED] -> [BRANCH_C1 - BRANCH_C2 - BRANCH_C3 - LEAF]
435
+ // We need to generate a json with nodes
436
+ // [{BRANCH_S1-BRANCH_C1}, {BRANCH_S2-BRANCH_C2}, {EXT_S, BRANCH_S3-placeholder}, {placeholder-BRANCH_C3}, {placeholder-LEAF}]
437
+ // We didn't have the 4th node, {placeholder-BRANCH_C3} now.
420
438
func GenerateWitness (txIdx uint , key , value []byte , proof * trie.StackProof ) []Node {
421
439
k := trie .KeybytesToHex (key )
422
440
k = k [:len (k )- 1 ]
@@ -436,7 +454,8 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
436
454
len1 := len (proofS )
437
455
len2 := len (proofC )
438
456
var nodes []Node
439
- // Empty stack trie
457
+
458
+ // Special case for the 1st tx, an empty stack trie
440
459
if len1 == 0 {
441
460
leafNode := prepareTxLeafAndPlaceholderNode (1 , proofC [0 ], k , false )
442
461
return append (nodes , leafNode )
@@ -450,26 +469,24 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
450
469
// FIXME: using enum(branch, leaf...) to replace magic numbers
451
470
upTo := minLen
452
471
additionalBranch := true
453
- if len1 > 0 {
454
- // If both of proofs end with a leaf or a hashed node, e.g. [BRANCH - LEAF] --> [BRANCH - BRANCH - LEAF]
455
- // upTo minus 1 means we handle branch placeholder and leaf in `additionalBranch`
456
- // if len1 != len2 && (lastProofTypeS == lastProofTypeC) && (lastProofTypeC == 3) {
457
- if len1 != len2 && (lastProofTypeS == 3 || lastProofTypeS == 4 ) && (lastProofTypeC == 3 || lastProofTypeC == 4 ) {
458
- upTo --
459
- }
460
472
461
- // The length of proofS and proofC is equal and
462
- // the last element of proofS is a hashed node or a leaf
463
- if len1 == len2 && (lastProofTypeS == 3 || lastProofTypeS == 4 ) {
464
- additionalBranch = false
465
- }
473
+ // If both of proofs end with either a leaf or a hashed node, e.g. [BRANCH - LEAF] --> [BRANCH - BRANCH - LEAF]
474
+ // The 2nd BRANCH in above proofC should have a branch placeholder for it.
475
+ // We handle branch placeholder in `additionalBranch` and that's why we need to minus `upTo` by 1 here.
476
+ if len1 != len2 && (lastProofTypeS == 3 || lastProofTypeS == 4 ) && (lastProofTypeC == 3 || lastProofTypeC == 4 ) {
477
+ upTo --
478
+ }
466
479
467
- // Special case for the 2nd tx.
468
- // In this case, proofS only contains a leaf node and proofC are [EXT - BRANCH - LEAF].
469
- // `additionalBranch` can handle the mismatched the order of the type.
470
- if len1 == 1 && lastProofTypeS == 3 {
471
- upTo = 0
472
- }
480
+ // The length of proofS and proofC is equal and the last element of proofS is a hashed node or a leaf
481
+ if len1 == len2 && (lastProofTypeS == 3 || lastProofTypeS == 4 ) {
482
+ additionalBranch = false
483
+ }
484
+
485
+ // Special case for the 2nd tx.
486
+ // In this case, proofS only contains a leaf node and proofC is [EXT - BRANCH - LEAF].
487
+ // `additionalBranch` can handle the mismatched the order of the type.
488
+ if len1 == 1 && lastProofTypeS == 3 {
489
+ upTo = 0
473
490
}
474
491
475
492
var extListRlpBytes []byte
@@ -478,14 +495,16 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
478
495
extValues = append (extValues , make ([]byte , 34 ))
479
496
}
480
497
498
+ var numberOfNibbles byte
481
499
isExtension := false
482
500
mismatchedIdx := - 1
483
501
fmt .Println ("upto" , upTo , additionalBranch , proofTypeS )
484
502
for i := 0 ; i < upTo ; i ++ {
485
503
if proofTypeS [i ] != 1 {
486
504
// fmt.Println("extNibbleS/C", extNibblesS, "|", extNibblesC)
487
505
488
- // This is designed for ext-mod case due to the order of the types will be mismatched. See this example,
506
+ // This is for the case of extension modified node due to the order of the types mismatched.
507
+ // See this example,
489
508
// [BRANCH - BRANCH - EXT - BRANCH - HASHED] -> [BRANCH - BRANCH - BRANCH - LEAF]
490
509
// In this case, `mismatchedIdx`` is 2 and stops at `EXT` node
491
510
if proofTypeS [i ] != proofTypeC [i ] {
@@ -495,12 +514,9 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
495
514
496
515
areThereNibbles := len (extNibblesS [i ]) != 0 || len (extNibblesC [i ]) != 0
497
516
if areThereNibbles { // extension node
498
- var numberOfNibbles byte
499
517
isExtension = true
500
-
501
518
numberOfNibbles , extListRlpBytes , extValues = prepareExtensions (extNibblesS [i ], proofS [i ], proofC [i ])
502
519
keyIndex += int (numberOfNibbles )
503
- // fmt.Println("Increase keyIdx", keyIndex)
504
520
continue
505
521
}
506
522
@@ -514,7 +530,6 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
514
530
extNode2 = proofC [i - 1 ]
515
531
}
516
532
517
- // fmt.Print(i, "/", proofTypeS[i], " - ")
518
533
bNode := prepareBranchNode (
519
534
proofS [i ], proofC [i ], extNode1 , extNode2 , extListRlpBytes ,
520
535
extValues , k [keyIndex ], k [keyIndex ],
@@ -528,66 +543,59 @@ func GenerateWitness(txIdx uint, key, value []byte, proof *trie.StackProof) []No
528
543
529
544
// To address the length of proofS and proofC is not equal or the order of the type is matched.
530
545
if additionalBranch {
531
- // fmt.Println("additionalBranch: extNibbleS/C", extNibblesS, "|", extNibblesC)
546
+ lastProofType := lastProofTypeS
532
547
leafRow0 := proofS [len1 - 1 ] // To compute the drifted position.
533
548
if len1 > len2 {
534
549
leafRow0 = proofC [len2 - 1 ]
550
+ lastProofType = lastProofTypeC
535
551
}
536
552
537
553
// In most of cases, proofs are like this [BRANCH - (BRANCH, EXT)] -> [BRANCH - (BRANCH, EXT) - LEAF]
538
- // That means proofC only appends a leaf node (or a hashed node in some cases) to proofS.
539
- // In these cases, we don't need to add a placeholder branch
540
- skip_branch_placeholder := len1 == len2 - 1 && (lastProofTypeS != lastProofTypeC ) && (lastProofTypeC == 3 )
541
-
542
- isModifiedExtNode := false
543
- if ! skip_branch_placeholder {
544
- var curProofS []byte
554
+ // That means proofC only appends a leaf node to proofS.
555
+ // In such cases, we don't need to add a placeholder branch
556
+ need_branch_placeholder := ! (len1 == len2 - 1 && (lastProofTypeS != lastProofTypeC ) && (lastProofTypeC == 3 ))
557
+ if need_branch_placeholder {
558
+ var extProofS []byte
545
559
if mismatchedIdx != - 1 {
546
- curProofS = proofS [mismatchedIdx ]
560
+ extProofS = proofS [mismatchedIdx ]
547
561
}
548
562
549
563
// This is a special case when the number of txs is 2.
550
- // In this case, proofS is a leaf and len1 is 1 but there is no nibbles
564
+ // In this case, proofS is a leaf and len1 is 1, but there is no nibbles
551
565
var lastExtNibbleS []byte
552
566
if len (extNibblesS ) != 0 {
553
567
lastExtNibbleS = extNibblesS [len1 - 1 ]
554
568
}
555
569
556
570
var branchNode Node
557
- isModifiedExtNode , _ , _ , branchNode =
558
- addBranchAndPlaceholder (
559
- proofS , proofC , lastExtNibbleS , extNibblesC [len2 - 1 ], curProofS ,
560
- leafRow0 , k , keyIndex , lastProofTypeS == 3 )
571
+ _ , _ , _ , branchNode = addBranchAndPlaceholder (
572
+ proofS , proofC , lastExtNibbleS , extNibblesC [len2 - 1 ], extProofS ,
573
+ leafRow0 , k , keyIndex , lastProofType == 3 )
561
574
nodes = append (nodes , branchNode )
562
575
}
563
576
564
577
var leafNode Node
565
- // Add a tx leaf after branch placeholder
566
- if ! isModifiedExtNode {
567
- if skip_branch_placeholder && (lastProofTypeS == 3 || lastProofTypeS == 4 ) {
578
+ // In stack trie proofs, the order of the type is the same except the case of modification extension node
579
+ // So, we use `mismatchedIdx` to represent the case.
580
+ if mismatchedIdx == - 1 {
581
+ // Add a tx leaf after branch placeholder
582
+ if lastProofTypeS == 3 {
568
583
leafNode = prepareTxLeafNode (txIdx , proofS [len1 - 1 ], proofC [len2 - 1 ], k , nil , isBranch (proofS [len1 - 1 ]), false , false )
569
584
} else {
570
585
leafNode = prepareTxLeafAndPlaceholderNode (txIdx , proofC [len2 - 1 ], k , false )
571
586
}
587
+
572
588
} else {
573
- fmt .Println ("MODIFIED EXT CASE, IGNORE NOW!!" )
574
- // TODO might not fit our case bcs we have [EXT - BRANCH] --> [BRANCH - LEAF]
575
- // isSModExtension := false
576
- // if len2 > len1 {
577
- // isSModExtension = true
578
- // }
579
- // leafNode = prepareTxLeafAndPlaceholderNode(txIdx, proofC[len2-1], k, isSModExtension)
589
+ fmt .Println ("MODIFIED EXT CASE!!" )
590
+ leafNode = prepareTxLeafAndPlaceholderNode (txIdx , proofC [len2 - 1 ], k , true )
580
591
581
592
// When a proof element is a modified extension node (new extension node appears at the position
582
593
// of the existing extension node), additional rows are added (extension node before and after
583
594
// modification).
584
- // leafNode = equipLeafWithModExtensionNode(nil, leafNode, common.Address{0}, proofS, proofC, proofC,
585
- // extNibblesS, extNibblesC, k, nil,
586
- // keyIndex, numberOfNibbles, false, &toBeHashed)
595
+ leafNode = equipLeafWithModExtensionNode (nil , leafNode , common.Address {byte (txIdx )}, proofS , proofC ,
596
+ extNibblesS , extNibblesC , proofS [mismatchedIdx ], k , keyIndex , int (numberOfNibbles ), false )
587
597
}
588
-
589
598
nodes = append (nodes , leafNode )
590
-
591
599
}
592
600
593
601
return nodes
@@ -759,7 +767,7 @@ func convertProofToWitness(statedb *state.StateDB, addr common.Address, addrh []
759
767
// of the existing extension node), additional rows are added (extension node before and after
760
768
// modification).
761
769
if isModifiedExtNode {
762
- leafNode = equipLeafWithModExtensionNode (statedb , leafNode , addr , proof1 , proof2 , nil , extNibblesS , extNibblesC ,
770
+ leafNode = equipLeafWithModExtensionNode (statedb , leafNode , addr , proof1 , proof2 , extNibblesS , extNibblesC , nil ,
763
771
key , keyIndex , numberOfNibbles , isAccountProof )
764
772
}
765
773
nodes = append (nodes , leafNode )
0 commit comments