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
This commit is contained in:
ledgerwatch 2020-05-21 06:18:25 +01:00 committed by GitHub
parent 12a6b2f9f6
commit 5904332035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 25 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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 {

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {