From 5904332035ab91917e7eb93362f07713a4f3dac7 Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Thu, 21 May 2020 06:18:25 +0100 Subject: [PATCH] hack --action resetState to create both hashed and plain genesis, clean all buckets (#563) * Reset plain genesis * Change to optimised freelist implementation * Fix linter * Revert "Change to optimised freelist implementation" This reverts commit 65e28f1737801178e95585381a789785504ec318. * Noerrcheck --- cmd/hack/hack.go | 19 ++++++++--- cmd/utils/flags.go | 4 +-- core/genesis.go | 38 +++++++++++++++------- core/state/database.go | 5 +++ eth/downloader/stagedsync_downloader.go | 8 ++--- eth/downloader/stagedsync_stage_execute.go | 2 +- 6 files changed, 51 insertions(+), 25 deletions(-) diff --git a/cmd/hack/hack.go b/cmd/hack/hack.go index e68330883..8baa96d2e 100644 --- a/cmd/hack/hack.go +++ b/cmd/hack/hack.go @@ -1999,12 +1999,21 @@ func resetState(chaindata string) { db, err := ethdb.NewBoltDatabase(chaindata) check(err) defer db.Close() - err = db.DeleteBucket(dbutils.CurrentStateBucket) - check(err) - err = db.DeleteBucket(dbutils.AccountChangeSetBucket) - check(err) - err = db.DeleteBucket(dbutils.StorageChangeSetBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.CurrentStateBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.AccountChangeSetBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.StorageChangeSetBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.PlainStateBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.PlainAccountChangeSetBucket) + //nolint:errcheck + db.DeleteBucket(dbutils.PlainStorageChangeSetBucket) + _, _, err = core.DefaultGenesisBlock().CommitGenesisState(db, false) check(err) + core.UsePlainStateExecution = true _, _, err = core.DefaultGenesisBlock().CommitGenesisState(db, false) check(err) err = downloader.SaveStageProgress(db, downloader.Execution, 0) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 38fca6c2b..dc7be2321 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1521,8 +1521,8 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { setWhitelist(ctx, cfg) setLes(ctx, cfg) - downloader.UsePlainStateExecution = ctx.Bool(StagedSyncPlainExecFlag.Name) - log.Info("setting up plain text execution", "plain", downloader.UsePlainStateExecution) + core.UsePlainStateExecution = ctx.Bool(StagedSyncPlainExecFlag.Name) + log.Info("setting up plain text execution", "plain", core.UsePlainStateExecution) if ctx.GlobalIsSet(SyncModeFlag.Name) { cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode) diff --git a/core/genesis.go b/core/genesis.go index 355b7e974..7d98bd2bf 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -40,6 +40,9 @@ import ( "github.com/ledgerwatch/turbo-geth/rlp" ) +var UsePlainStateExecution = false // FIXME: when we can move the hashed state forward. +// ^--- will be overriden e when parsing flags anyway + //go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go //go:generate gencodec -type GenesisAccount -field-override genesisAccountMarshaling -out gen_genesis_account.go @@ -299,18 +302,29 @@ func (g *Genesis) CommitGenesisState(db ethdb.Database, history bool) (*types.Bl return nil, statedb, fmt.Errorf("can't commit genesis block with number > 0") } tds.SetBlockNr(0) - blockWriter := tds.DbStateWriter() - if err := statedb.CommitBlock(context.Background(), blockWriter); err != nil { - return nil, statedb, fmt.Errorf("cannot write state: %v", err) - } - // Always write changesets - if err := blockWriter.WriteChangeSets(); err != nil { - return nil, statedb, fmt.Errorf("cannot write change sets: %v", err) - } - // Optionally write history - if history { - if err := blockWriter.WriteHistory(); err != nil { - return nil, statedb, fmt.Errorf("cannot write history: %v", err) + if UsePlainStateExecution { + blockWriter := tds.PlainStateWriter() + if err := statedb.CommitBlock(context.Background(), blockWriter); err != nil { + return nil, statedb, fmt.Errorf("cannot write state: %v", err) + } + // Always write changesets + if err := blockWriter.WriteChangeSets(); err != nil { + return nil, statedb, fmt.Errorf("cannot write change sets: %v", err) + } + } else { + blockWriter := tds.DbStateWriter() + if err := statedb.CommitBlock(context.Background(), blockWriter); err != nil { + return nil, statedb, fmt.Errorf("cannot write state: %v", err) + } + // Always write changesets + if err := blockWriter.WriteChangeSets(); err != nil { + return nil, statedb, fmt.Errorf("cannot write change sets: %v", err) + } + // Optionally write history + if history { + if err := blockWriter.WriteHistory(); err != nil { + return nil, statedb, fmt.Errorf("cannot write history: %v", err) + } } } if _, err := batch.Commit(); err != nil { diff --git a/core/state/database.go b/core/state/database.go index e7836b3b3..5dab808ad 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -1306,6 +1306,11 @@ func (tds *TrieDbState) DbStateWriter() *DbStateWriter { return &DbStateWriter{blockNr: tds.blockNr, db: tds.db, pw: tds.pw, csw: NewChangeSetWriter(), incarnationMap: tds.incarnationMap} } +// DbStateWriter creates a writer that is designed to write changes into the database batch +func (tds *TrieDbState) PlainStateWriter() *PlainStateWriter { + return NewPlainStateWriter(tds.db, tds.blockNr, tds.incarnationMap) +} + func (tsw *TrieStateWriter) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error { addrHash, err := tsw.tds.pw.HashAddress(address, false /*save*/) if err != nil { diff --git a/eth/downloader/stagedsync_downloader.go b/eth/downloader/stagedsync_downloader.go index bad81a5b3..b4e3442b5 100644 --- a/eth/downloader/stagedsync_downloader.go +++ b/eth/downloader/stagedsync_downloader.go @@ -3,12 +3,10 @@ package downloader import ( "fmt" + "github.com/ledgerwatch/turbo-geth/core" "github.com/ledgerwatch/turbo-geth/log" ) -var UsePlainStateExecution = false // FIXME: when we can move the hashed state forward. -// ^--- will be overriden e when parsing flags anyway - func (d *Downloader) doStagedSyncWithFetchers(p *peerConnection, headersFetchers []func() error) error { log.Info("Sync stage 1/5. Downloading headers...") @@ -40,7 +38,7 @@ func (d *Downloader) doStagedSyncWithFetchers(p *peerConnection, headersFetchers case Execution: err = unwindExecutionStage(unwindPoint, d.stateDB) case HashCheck: - if !UsePlainStateExecution { + if !core.UsePlainStateExecution { err = d.unwindHashCheckStage(unwindPoint) } default: @@ -93,7 +91,7 @@ func (d *Downloader) doStagedSyncWithFetchers(p *peerConnection, headersFetchers // Further stages go there log.Info("Sync stage 5/5. Validating final hash") - if !UsePlainStateExecution { + if !core.UsePlainStateExecution { if err = d.spawnCheckFinalHashStage(syncHeadNumber); err != nil { return err } diff --git a/eth/downloader/stagedsync_stage_execute.go b/eth/downloader/stagedsync_stage_execute.go index a06b4a235..8a3312f29 100644 --- a/eth/downloader/stagedsync_stage_execute.go +++ b/eth/downloader/stagedsync_stage_execute.go @@ -110,7 +110,7 @@ func spawnExecuteBlocksStage(stateDB ethdb.Database, blockchain BlockChain) (uin var stateReader state.StateReader var stateWriter state.WriterWithChangeSets - if UsePlainStateExecution { + if core.UsePlainStateExecution { stateReader = state.NewPlainStateReaderWithFallback(mutation, uncommitedIncarnations, hashStateReader) stateWriter = state.NewPlainStateWriter(mutation, blockNum, uncommitedIncarnations) } else {