squashmelater: add snaphash verifier function

This commit is contained in:
Martin Holst Swende 2020-03-20 10:57:12 +01:00
parent 976c209c8b
commit 99a495501c
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 74 additions and 7 deletions

View file

@ -18,6 +18,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/state/snapshot"
"os"
@ -616,14 +617,18 @@ func snapToHash(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("Could not create iterator for root %x: %v", root, err)
}
generatedRoot := snapshot.GenerateTrieRoot(it)
if err := it.Error(); err != nil {
fmt.Printf("Iterator error: %v\n", it.Error())
ollKorrekt := snapshot.CrosscheckTriehasher(it, 0, 10000)
//generatedRoot := snapshot.GenerateTrieRoot(it)
//if err := it.Error(); err != nil {
// fmt.Printf("Iterator error: %v\n", it.Error())
//}
//if root != generatedRoot {
// return fmt.Errorf("Wrong hash generated, expected %x, got %x", root, generatedRoot[:])
//}
if !ollKorrekt {
return errors.New("Computer says No, @gballet\n...come on man, fix me already!")
}
if root != generatedRoot {
return fmt.Errorf("Wrong hash generated, expected %x, got %x", root, generatedRoot[:])
}
log.Info("Generation done", "root", generatedRoot)
//log.Info("Generation done", "root", generatedRoot)
return nil
}

View file

@ -39,6 +39,10 @@ func GenerateTrieRoot(it AccountIterator) common.Hash {
return generateTrieRoot(it, StdGenerate)
}
func CrosscheckTriehasher(it AccountIterator, begin,end int) bool {
return verifyHasher(it, StackGenerate, begin, end)
}
func generateTrieRoot(it AccountIterator, generatorFn trieGeneratorFn) common.Hash {
var (
in = make(chan leaf) // chan to pass leaves
@ -73,6 +77,64 @@ func generateTrieRoot(it AccountIterator, generatorFn trieGeneratorFn) common.Ha
return result
}
func verifyHasher(it AccountIterator, generatorFn trieGeneratorFn, begin, end int) bool {
var (
referenceFn = StdGenerate
inA = make(chan leaf) // chan to pass leaves
outA = make(chan common.Hash) // chan to collect result
inB = make(chan leaf) // chan to pass leaves
outB = make(chan common.Hash) // chan to collect result
wg sync.WaitGroup
)
wg.Add(2)
go func() {
referenceFn(inA, outA)
wg.Done()
}()
go func() {
generatorFn(inB, outB)
wg.Done()
}()
// Feed leaves
start := time.Now()
logged := time.Now()
accounts := 0
for it.Next() {
if accounts < begin {
accounts++
continue
}
if end > 0 && accounts > end {
break
}
slimData := it.Account()
fullData := SlimToFull(slimData)
l := leaf{it.Hash(), fullData}
inA <- l
inB <- l
accounts++
if time.Since(logged) > 8*time.Second {
log.Info("Generating trie hash from snapshot",
"at", l.key, "accounts", accounts, "elapsed", time.Since(start))
logged = time.Now()
}
}
close(inA)
close(inB)
resultA := <-outA
resultB := <-outB
log.Info("Generated trie hash from snapshot", "accounts", accounts,
"elapsed", time.Since(start),
"start", begin,
"end", end,
"exp", resultA,
"got", resultB)
wg.Wait()
return resultA == resultB
}
// StackGenerate is a hexary trie builder which is built from the bottom-up as
// keys are added.
func StackGenerate(in chan (leaf), out chan (common.Hash)) {