mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 13:07:17 +00:00
ed866e6934
* Fixing history index * Remove chunk generation, fix formatting * Fix compile error, clean up hack.go * Fix output tests * Fix index generator test * Fixed checkChangeSets * Fix linter
162 lines
4.5 KiB
Go
162 lines
4.5 KiB
Go
package stateless
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
"github.com/ledgerwatch/turbo-geth/consensus/ethash"
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
)
|
|
|
|
// CheckChangeSets re-executes historical transactions in read-only mode
|
|
// and checks that their outputs match the database ChangeSets.
|
|
func CheckChangeSets(genesis *core.Genesis, blockNum uint64, chaindata string, historyfile string, nocheck bool, writeReceipts bool) error {
|
|
if len(historyfile) == 0 {
|
|
historyfile = chaindata
|
|
}
|
|
|
|
startTime := time.Now()
|
|
sigs := make(chan os.Signal, 1)
|
|
interruptCh := make(chan bool, 1)
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-sigs
|
|
interruptCh <- true
|
|
}()
|
|
|
|
chainDb := ethdb.MustOpen(chaindata)
|
|
defer chainDb.Close()
|
|
historyDb := chainDb
|
|
if chaindata != historyfile {
|
|
historyDb = ethdb.MustOpen(historyfile)
|
|
}
|
|
|
|
chainConfig := genesis.Config
|
|
engine := ethash.NewFaker()
|
|
vmConfig := vm.Config{}
|
|
bc, err := core.NewBlockChain(chainDb, nil, chainConfig, engine, vmConfig, nil, nil, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
noOpWriter := state.NewNoopWriter()
|
|
|
|
interrupt := false
|
|
batch := chainDb.NewBatch()
|
|
for !interrupt {
|
|
block := bc.GetBlockByNumber(blockNum)
|
|
if block == nil {
|
|
break
|
|
}
|
|
|
|
dbstate := state.NewPlainDBState(historyDb.KV(), block.NumberU64()-1)
|
|
intraBlockState := state.New(dbstate)
|
|
csw := state.NewChangeSetWriterPlain()
|
|
var blockWriter state.StateWriter
|
|
if nocheck {
|
|
blockWriter = noOpWriter
|
|
} else {
|
|
blockWriter = csw
|
|
}
|
|
|
|
receipts, err1 := runBlock(intraBlockState, noOpWriter, blockWriter, chainConfig, bc, block)
|
|
if err1 != nil {
|
|
return err1
|
|
}
|
|
if chainConfig.IsByzantium(block.Number()) {
|
|
receiptSha := types.DeriveSha(receipts)
|
|
if receiptSha != block.Header().ReceiptHash {
|
|
return fmt.Errorf("mismatched receipt headers for block %d", block.NumberU64())
|
|
}
|
|
}
|
|
if writeReceipts {
|
|
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts)
|
|
if batch.BatchSize() >= chainDb.IdealBatchSize() {
|
|
log.Info("Committing receipts", "up to block", block.NumberU64(), "batch size", common.StorageSize(batch.BatchSize()))
|
|
if _, err := batch.Commit(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
if !nocheck {
|
|
accountChanges, err := csw.GetAccountChanges()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var expectedAccountChanges []byte
|
|
expectedAccountChanges, err = changeset.EncodeAccountsPlain(accountChanges)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dbAccountChanges, err := historyDb.GetChangeSetByBlock(false /* storage */, blockNum)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !bytes.Equal(dbAccountChanges, expectedAccountChanges) {
|
|
fmt.Printf("Unexpected account changes in block %d\n%s\nvs\n%s\n", blockNum, hexutil.Encode(dbAccountChanges), hexutil.Encode(expectedAccountChanges))
|
|
csw.PrintChangedAccounts()
|
|
return nil
|
|
}
|
|
|
|
expectedStorageChanges, err := csw.GetStorageChanges()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
expectedtorageSerialized := make([]byte, 0)
|
|
if expectedStorageChanges.Len() > 0 {
|
|
expectedtorageSerialized, err = changeset.EncodeStoragePlain(expectedStorageChanges)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
dbStorageChanges, err := historyDb.GetChangeSetByBlock(true /* storage */, blockNum)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !bytes.Equal(dbStorageChanges, expectedtorageSerialized) {
|
|
fmt.Printf("Unexpected storage changes in block %d\n%s\nvs\n%s\n", blockNum, hexutil.Encode(dbStorageChanges), hexutil.Encode(expectedtorageSerialized))
|
|
return nil
|
|
}
|
|
}
|
|
|
|
blockNum++
|
|
if blockNum%1000 == 0 {
|
|
log.Info("Checked", "blocks", blockNum)
|
|
}
|
|
|
|
// Check for interrupts
|
|
select {
|
|
case interrupt = <-interruptCh:
|
|
fmt.Println("interrupted, please wait for cleanup...")
|
|
default:
|
|
}
|
|
}
|
|
if writeReceipts {
|
|
log.Info("Committing final receipts", "batch size", common.StorageSize(batch.BatchSize()))
|
|
if _, err := batch.Commit(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Info("Checked", "blocks", blockNum, "next time specify --block", blockNum, "duration", time.Since(startTime))
|
|
return nil
|
|
}
|