mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 21:17:16 +00:00
137daa6c67
* Debugging changesets * Kinda works * Fix compile error * Fix formatting * Fix lint * Duplicate entries kludge * Fix compile error * Cleanup
186 lines
5.2 KiB
Go
186 lines
5.2 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/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(block.NumberU64() - 1)
|
|
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\nIn the database: ======================\n", blockNum)
|
|
if err = changeset.AccountChangeSetPlainBytes(dbAccountChanges).Walk(func(k, v []byte) error {
|
|
fmt.Printf("0x%x: %x\n", k, v)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Expected: ==========================\n")
|
|
if err = changeset.AccountChangeSetPlainBytes(expectedAccountChanges).Walk(func(k, v []byte) error {
|
|
fmt.Printf("0x%x %x\n", k, v)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
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\nIn the database: ======================\n", blockNum)
|
|
if err = changeset.StorageChangeSetPlainBytes(dbStorageChanges).Walk(func(k, v []byte) error {
|
|
fmt.Printf("0x%x: [%x]\n", k, v)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("Expected: ==========================\n")
|
|
if err = changeset.StorageChangeSetPlainBytes(expectedtorageSerialized).Walk(func(k, v []byte) error {
|
|
fmt.Printf("0x%x: [%x]\n", k, v)
|
|
return nil
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
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
|
|
}
|