2020-06-10 20:07:14 +00:00
|
|
|
package verify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-06-16 13:36:16 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateTxLookups(chaindata string) error {
|
2020-06-16 13:36:16 +00:00
|
|
|
db := ethdb.MustOpen(chaindata)
|
2020-06-10 20:07:14 +00:00
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
quitCh := make(chan struct{})
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
close(quitCh)
|
|
|
|
}()
|
|
|
|
t := time.Now()
|
|
|
|
defer func() {
|
|
|
|
log.Info("Validation ended", "it took", time.Since(t))
|
|
|
|
}()
|
|
|
|
var blockNum uint64
|
|
|
|
iterations := 0
|
|
|
|
var interrupt bool
|
|
|
|
// Validation Process
|
|
|
|
blockBytes := big.NewInt(0)
|
|
|
|
for !interrupt {
|
|
|
|
if err := common.Stopped(quitCh); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-10 06:05:56 +00:00
|
|
|
blockHash, err := rawdb.ReadCanonicalHash(db, blockNum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-10 20:07:14 +00:00
|
|
|
body := rawdb.ReadBody(db, blockHash, blockNum)
|
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
log.Error("Empty body", "blocknum", blockNum)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
blockBytes.SetUint64(blockNum)
|
|
|
|
bn := blockBytes.Bytes()
|
|
|
|
|
|
|
|
for _, tx := range body.Transactions {
|
|
|
|
val, err := db.Get(dbutils.TxLookupPrefix, tx.Hash().Bytes())
|
|
|
|
iterations++
|
|
|
|
if iterations%100000 == 0 {
|
|
|
|
log.Info("Validated", "entries", iterations, "number", blockNum)
|
|
|
|
|
|
|
|
}
|
|
|
|
if !bytes.Equal(val, bn) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("Validation process failed(%d). Expected %b, got %b", iterations, bn, val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blockNum++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|