Skip to content

Commit

Permalink
Genesis.toBlock remove db from arguments (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Apr 6, 2021
1 parent 86021e3 commit dc49abc
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 94 deletions.
6 changes: 4 additions & 2 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ func loadChain(chainfile string, genesis string) (*Chain, error) {
if err = json.Unmarshal(chainConfig, &gen); err != nil {
return nil, err
}
gblock, _, _ := gen.ToBlock(nil, false)

gblock, _, err := gen.ToBlock(false)
if err != nil {
return nil, err
}
// Load chain.rlp.
fh, err := os.Open(chainfile)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"time"

"github.com/holiman/uint256"
cli "github.com/urfave/cli"
"github.com/urfave/cli"

"github.com/ledgerwatch/turbo-geth/cmd/evm/internal/compiler"
"github.com/ledgerwatch/turbo-geth/cmd/utils"
Expand Down Expand Up @@ -138,14 +138,16 @@ func runCmd(ctx *cli.Context) error {
db := ethdb.NewMemDatabase()
if ctx.GlobalString(GenesisFlag.Name) != "" {
gen := readGenesis(ctx.GlobalString(GenesisFlag.Name))
_, _, err := gen.Commit(db, false)
if err != nil {
return err
}
genesisConfig = gen
_, statedb, _ = gen.ToBlock(db, false /* history */)
chainConfig = gen.Config
} else {
tds := state.NewTrieDbState(common.Hash{}, db, 0)
statedb = state.New(tds)
genesisConfig = new(core.Genesis)
}
statedb = state.New(state.NewPlainStateReader(db))
if ctx.GlobalString(SenderFlag.Name) != "" {
sender = common.HexToAddress(ctx.GlobalString(SenderFlag.Name))
}
Expand Down
24 changes: 11 additions & 13 deletions cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/ledgerwatch/turbo-geth/core/state"
"github.com/ledgerwatch/turbo-geth/core/vm"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/tests"

Expand Down Expand Up @@ -100,22 +99,22 @@ func stateTestCmd(ctx *cli.Context) error {
for _, st := range test.Subtests() {
// Run the test and aggregate the result
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true, Error: new(string)}
statedb, tds, err := test.Run(context.Background(), st, cfg)
statedb, err := test.Run(context.Background(), st, cfg)
// print state root for evmlab tracing
if ctx.GlobalBool(MachineFlag.Name) && statedb != nil {
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", tds.Trie().Root())
}
//if ctx.GlobalBool(MachineFlag.Name) && statedb != nil {
// fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%x\"}\n", tds.Trie().Root())
//}
if err != nil {
// Test failed, mark as so and dump any state to aid debugging
result.Pass, *result.Error = false, err.Error()
if ctx.GlobalBool(DumpFlag.Name) && statedb != nil {
tx, err1 := tds.Database().(ethdb.HasKV).KV().Begin(context.Background())
if err1 != nil {
return fmt.Errorf("transition cannot open tx: %v", err1)
}
dump := state.NewDumper(tx, tds.GetBlockNr()).DefaultRawDump()
tx.Rollback()
result.State = &dump
//tx, err1 := tds.Database().(ethdb.HasKV).KV().Begin(context.Background())
//if err1 != nil {
// return fmt.Errorf("transition cannot open tx: %v", err1)
//}
//dump := state.NewDumper(tx, tds.GetBlockNr()).DefaultRawDump()
//tx.Rollback()
//result.State = &dump
}
}

Expand All @@ -128,7 +127,6 @@ func stateTestCmd(ctx *cli.Context) error {
vm.WriteTrace(os.Stderr, debugger.StructLogs())
}
}
tds.Database().Close()
}
}
out, _ := json.MarshalIndent(results, "", " ")
Expand Down
3 changes: 1 addition & 2 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ func printFullNodeRLPs() {
}

func testDifficulty() {
db := ethdb.NewMemDatabase()
genesisBlock, _, err := core.DefaultGenesisBlock().ToBlock(db, false /* history */)
genesisBlock, _, err := core.DefaultGenesisBlock().ToBlock(false)
tool.Check(err)
genesisHeader := genesisBlock.Header()
d1 := ethash.CalcDifficulty(params.MainnetChainConfig, 100000, genesisHeader.Time, genesisHeader.Difficulty, genesisHeader.Number, genesisHeader.UncleHash)
Expand Down
2 changes: 1 addition & 1 deletion cmd/headers/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func NewControlServer(db ethdb.Database, sentryClient proto_sentry.SentryClient,
default:
return nil, fmt.Errorf("chain %s is not known", chain)
}
if chainConfig, _, _, err = core.SetupGenesisBlock(db, genesis, false /* history */, false /* overwrite */); err != nil {
if chainConfig, _, err = core.SetupGenesisBlock(db, genesis, false /* history */, false /* overwrite */); err != nil {
return nil, fmt.Errorf("setup genesis block: %w", err)
}
engine := ethconfig.CreateConsensusEngine(chainConfig, ethashConfig, nil, false, db)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func initialState1() error {
signer = types.HomesteadSigner{}
)
// Create intermediate hash bucket since it is mandatory now
_, genesisHash, _, err := core.SetupGenesisBlock(db, gspec, true, false)
_, genesisHash, err := core.SetupGenesisBlock(db, gspec, true, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -462,7 +462,7 @@ func initialState1() error {
kv = db.KV()
snapshotDB := db.MemCopy()

_, _, _, err = core.SetupGenesisBlock(db, gspec, true, false)
_, _, err = core.SetupGenesisBlock(db, gspec, true, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/get_chain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestGetChainConfig(t *testing.T) {
db := ethdb.NewMemDatabase()
defer db.Close()
config, _, _, err := core.SetupGenesisBlock(db, core.DefaultGenesisBlock(), false /* history */, false /* overwrite */)
config, _, err := core.SetupGenesisBlock(db, core.DefaultGenesisBlock(), false /* history */, false /* overwrite */)
if err != nil {
t.Fatalf("setting up genensis block: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func createTestDb() (ethdb.Database, error) {
signer = types.HomesteadSigner{}
)
// Create intermediate hash bucket since it is mandatory now
_, genesisHash, _, err := core.SetupGenesisBlock(db, gspec, true, false)
_, genesisHash, err := core.SetupGenesisBlock(db, gspec, true, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
func MakeChain(ctx *cli.Context, stack *node.Node, readOnly bool) (chainConfig *params.ChainConfig, chain *core.BlockChain, chainDb *ethdb.ObjectDatabase) {
var err error
chainDb = MakeChainDatabase(ctx, stack)
config, _, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx), false /* history */, false /* overwrite */)
config, _, err := core.SetupGenesisBlock(chainDb, MakeGenesis(ctx), false /* history */, false /* overwrite */)
if err != nil {
Fatalf("%v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestClique(t *testing.T) {
continue
}

genesisBlock, _, _ := genesis.ToBlock(db, false /* history */)
genesisBlock, _, _ := genesis.ToBlock(false)
blocks, _, err := core.GenerateChain(&config, genesisBlock, engine, db, len(tt.votes), func(j int, gen *core.BlockGen) {
// Cast the vote contained in this block
gen.SetCoinbase(accounts.address(tt.votes[j].voted))
Expand Down
64 changes: 32 additions & 32 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,18 @@ func (e *GenesisMismatchError) Error() string {
// error is a *params.ConfigCompatError and the new, unwritten config is returned.
//
// The returned chain configuration is never nil.
func SetupGenesisBlock(db ethdb.Database, genesis *Genesis, history bool, overwrite bool) (*params.ChainConfig, common.Hash, *state.IntraBlockState, error) {
func SetupGenesisBlock(db ethdb.Database, genesis *Genesis, history bool, overwrite bool) (*params.ChainConfig, common.Hash, error) {
return SetupGenesisBlockWithOverride(db, genesis, nil, history, overwrite)
}

func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, overrideBerlin *big.Int, history bool, overwrite bool) (*params.ChainConfig, common.Hash, *state.IntraBlockState, error) {
var stateDB *state.IntraBlockState
func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, overrideBerlin *big.Int, history bool, overwrite bool) (*params.ChainConfig, common.Hash, error) {
if genesis != nil && genesis.Config == nil {
return params.AllEthashProtocolChanges, common.Hash{}, stateDB, ErrGenesisNoConfig
return params.AllEthashProtocolChanges, common.Hash{}, ErrGenesisNoConfig
}
// Just commit the new block if there is no stored genesis block.
stored, err := rawdb.ReadCanonicalHash(db, 0)
if err != nil {
return nil, common.Hash{}, nil, err
return nil, common.Hash{}, err
}
if overwrite || (stored == common.Hash{}) {
if genesis == nil {
Expand All @@ -176,23 +175,21 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
} else {
log.Info("Writing custom genesis block")
}
block, stateDB1, err := genesis.Commit(db, history)
if err != nil {
return genesis.Config, common.Hash{}, nil, err
block, _, err1 := genesis.Commit(db, history)
if err1 != nil {
return genesis.Config, common.Hash{}, err1
}
return genesis.Config, block.Hash(), stateDB1, nil
return genesis.Config, block.Hash(), nil
}
// Check whether the genesis block is already written.
if genesis != nil {
db := ethdb.NewMemDatabase()
defer db.Close()
block, stateDB1, err1 := genesis.ToBlock(db, history)
block, _, err1 := genesis.ToBlock(history)
if err1 != nil {
return genesis.Config, common.Hash{}, nil, err1
return genesis.Config, common.Hash{}, err1
}
hash := block.Hash()
if hash != stored {
return genesis.Config, block.Hash(), stateDB1, &GenesisMismatchError{stored, hash}
return genesis.Config, block.Hash(), &GenesisMismatchError{stored, hash}
}
}
// Get the existing chain configuration.
Expand All @@ -201,25 +198,25 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
newcfg.BerlinBlock = overrideBerlin
}
if err := newcfg.CheckConfigForkOrder(); err != nil {
return newcfg, common.Hash{}, nil, err
return newcfg, common.Hash{}, err
}
storedcfg, err := rawdb.ReadChainConfig(db, stored)
if err != nil {
return newcfg, common.Hash{}, nil, err
return newcfg, common.Hash{}, err
}
if overwrite || storedcfg == nil {
log.Warn("Found genesis block without chain config")
err1 := rawdb.WriteChainConfig(db, stored, newcfg)
if err1 != nil {
return newcfg, common.Hash{}, nil, err1
return newcfg, common.Hash{}, err1
}
return newcfg, stored, stateDB, nil
return newcfg, stored, nil
}
// Special case: don't change the existing config of a non-mainnet chain if no new
// config is supplied. These chains would get AllProtocolChanges (and a compat error)
// if we just continued here.
if genesis == nil && stored != params.MainnetGenesisHash {
return storedcfg, stored, stateDB, nil
return storedcfg, stored, nil
}
// Check config compatibility and write the config. Compatibility errors
// are returned to the caller unless we're already at block zero.
Expand All @@ -229,13 +226,13 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
} else {
compatErr := storedcfg.CheckCompatible(newcfg, *height)
if compatErr != nil && *height != 0 && compatErr.RewindTo != 0 {
return newcfg, stored, stateDB, compatErr
return newcfg, stored, compatErr
}
}
if err := rawdb.WriteChainConfig(db, stored, newcfg); err != nil {
return newcfg, common.Hash{}, nil, err
return newcfg, common.Hash{}, err
}
return newcfg, stored, stateDB, nil
return newcfg, stored, nil
}

func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
Expand All @@ -259,7 +256,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {

// ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil).
func (g *Genesis) ToBlock(db ethdb.Database, history bool) (*types.Block, *state.IntraBlockState, error) {
func (g *Genesis) ToBlock(history bool) (*types.Block, *state.IntraBlockState, error) {
tmpDB := ethdb.NewMemDatabase()
defer tmpDB.Close()
r, w := state.NewDbStateReader(tmpDB), state.NewDbStateWriter(tmpDB, 0)
Expand All @@ -278,14 +275,6 @@ func (g *Genesis) ToBlock(db ethdb.Database, history bool) (*types.Block, *state
if len(account.Code) > 0 || len(account.Storage) > 0 {
statedb.SetIncarnation(addr, 1)
}
if len(account.Code) == 0 && len(account.Storage) > 0 {
// Special case for weird tests - inaccessible storage
var b [8]byte
binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation)
if err := db.Put(dbutils.IncarnationMapBucket, addr[:], b[:]); err != nil {
return nil, nil, err
}
}
}
err := statedb.FinalizeTx(context.Background(), w)
if err != nil {
Expand Down Expand Up @@ -319,10 +308,21 @@ func (g *Genesis) ToBlock(db ethdb.Database, history bool) (*types.Block, *state
}

func (g *Genesis) WriteGenesisState(tx ethdb.Database, history bool) (*types.Block, *state.IntraBlockState, error) {
block, statedb, err := g.ToBlock(tx, history)
block, statedb, err := g.ToBlock(history)
if err != nil {
return nil, nil, err
}
for addr, account := range g.Alloc {
if len(account.Code) == 0 && len(account.Storage) > 0 {
// Special case for weird tests - inaccessible storage
var b [8]byte
binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation)
if err := tx.Put(dbutils.IncarnationMapBucket, addr[:], b[:]); err != nil {
return nil, nil, err
}
}
}

if block.Number().Sign() != 0 {
return nil, statedb, fmt.Errorf("can't commit genesis block with number > 0")
}
Expand Down
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
}
}

chainConfig, genesisHash, _, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideBerlin, config.StorageMode.History, false /* overwrite */)
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideBerlin, config.StorageMode.History, false /* overwrite */)

if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
return nil, genesisErr
Expand Down
4 changes: 2 additions & 2 deletions eth/stagedsync/headers_verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestVerifyHeadersEthash(t *testing.T) {
db := ethdb.NewMemDatabase()
defer db.Close()

config, _, _, err := core.SetupGenesisBlock(db, core.DefaultGenesisBlock(), false /* history */, false /* overwrite */)
config, _, err := core.SetupGenesisBlock(db, core.DefaultGenesisBlock(), false /* history */, false /* overwrite */)
if err != nil {
t.Fatalf("setting up genensis block: %v", err)
}
Expand All @@ -85,7 +85,7 @@ func TestVerifyHeadersClique(t *testing.T) {

engine := clique.New(params.RinkebyChainConfig.Clique, db)

config, _, _, err := core.SetupGenesisBlock(db, core.DefaultRinkebyGenesisBlock(), false /* history */, false /* overwrite */)
config, _, err := core.SetupGenesisBlock(db, core.DefaultRinkebyGenesisBlock(), false /* history */, false /* overwrite */)
if err != nil {
t.Fatalf("setting up genensis block: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion ethdb/kv_lmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (opts LmdbOpts) Open() (kv KV, err error) {

var flags = opts.flags
if opts.inMem {
flags |= lmdb.NoMetaSync
flags |= lmdb.NoMetaSync | lmdb.NoSync
}

var exclusiveLock fileutil.Releaser
Expand Down
12 changes: 9 additions & 3 deletions ethdb/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (opts MdbxOpts) Open() (KV, error) {
var flags = opts.flags
if opts.inMem {
flags ^= mdbx.Durable
flags |= mdbx.NoMetaSync | mdbx.UtterlyNoSync | mdbx.WriteMap // it's ok for tests
flags |= mdbx.NoMetaSync | mdbx.UtterlyNoSync // it's ok for tests
opts.dirtyListMaxPages = 8 * 1024
}

Expand All @@ -128,8 +128,14 @@ func (opts MdbxOpts) Open() (KV, error) {
}

if opts.flags&mdbx.Accede == 0 {
if err = env.SetGeometry(-1, -1, int(opts.mapSize), int(2*datasize.GB), -1, 4*1024); err != nil {
return nil, err
if opts.inMem {
if err = env.SetGeometry(int(1*datasize.MB), int(1*datasize.MB), int(64*datasize.MB), int(1*datasize.MB), 0, 4*1024); err != nil {
return nil, err
}
} else {
if err = env.SetGeometry(-1, -1, int(opts.mapSize), int(2*datasize.GB), -1, 4*1024); err != nil {
return nil, err
}
}
if err = env.SetOption(mdbx.OptRpAugmentLimit, 32*1024*1024); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func createMiner(t *testing.T) (*Miner, *event.TypeMux) {
// Create chainConfig
chainDB := ethdb.NewMemDatabase()
genesis := core.DeveloperGenesisBlock(15, common.HexToAddress("12345"))
chainConfig, _, _, err := core.SetupGenesisBlock(chainDB, genesis, false, false)
chainConfig, _, err := core.SetupGenesisBlock(chainDB, genesis, false, false)
if err != nil {
t.Fatalf("can't create new chain config: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func TestState(t *testing.T) {
return UnsupportedForkError{subtest.Fork}
}
ctx := config.WithEIPsFlags(context.Background(), big.NewInt(1))
_, tds, err := test.Run(ctx, subtest, vmconfig)
defer tds.Database().Close()
_, err := test.Run(ctx, subtest, vmconfig)
return st.checkFailure(t, err)
})
})
Expand Down
Loading

0 comments on commit dc49abc

Please sign in to comment.