From 7ae6c4a79006ce27b19f144be09af8211c7055e5 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 22 Mar 2022 17:53:22 +0800 Subject: [PATCH] core: store genesis allocation and recommit them if necessary (#24460) * core: store genesis allocation and recommit them if necessary * core: recover predefined genesis allocation if possible --- core/blockchain.go | 13 +++++ core/genesis.go | 92 +++++++++++++++++++++++++++----- core/genesis_test.go | 30 +++++++++++ core/rawdb/accessors_metadata.go | 13 +++++ core/rawdb/database.go | 2 + core/rawdb/schema.go | 10 +++- 6 files changed, 145 insertions(+), 15 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index bc6422a701..f272522624 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -542,6 +542,19 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo } } if beyondRoot || newHeadBlock.NumberU64() == 0 { + if newHeadBlock.NumberU64() == 0 { + // Recommit the genesis state into disk in case the rewinding destination + // is genesis block and the relevant state is gone. In the future this + // rewinding destination can be the earliest block stored in the chain + // if the historical chain pruning is enabled. In that case the logic + // needs to be improved here. + if !bc.HasState(bc.genesisBlock.Root()) { + if err := CommitGenesisState(bc.db, bc.genesisBlock.Hash()); err != nil { + log.Crit("Failed to commit genesis state", "err", err) + } + log.Debug("Recommitted genesis state to disk") + } + } log.Debug("Rewound to block with state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash()) break } diff --git a/core/genesis.go b/core/genesis.go index b773b903cc..b1a4c03636 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -80,6 +80,81 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { return nil } +// flush adds allocated genesis accounts into a fresh new statedb and +// commit the state changes into the given database handler. +func (ga *GenesisAlloc) flush(db ethdb.Database) (common.Hash, error) { + statedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil) + if err != nil { + return common.Hash{}, err + } + for addr, account := range *ga { + statedb.AddBalance(addr, account.Balance) + statedb.SetCode(addr, account.Code) + statedb.SetNonce(addr, account.Nonce) + for key, value := range account.Storage { + statedb.SetState(addr, key, value) + } + } + root, err := statedb.Commit(false) + if err != nil { + return common.Hash{}, err + } + err = statedb.Database().TrieDB().Commit(root, true, nil) + if err != nil { + return common.Hash{}, err + } + return root, nil +} + +// write writes the json marshaled genesis state into database +// with the given block hash as the unique identifier. +func (ga *GenesisAlloc) write(db ethdb.KeyValueWriter, hash common.Hash) error { + blob, err := json.Marshal(ga) + if err != nil { + return err + } + rawdb.WriteGenesisState(db, hash, blob) + return nil +} + +// CommitGenesisState loads the stored genesis state with the given block +// hash and commits them into the given database handler. +func CommitGenesisState(db ethdb.Database, hash common.Hash) error { + var alloc GenesisAlloc + blob := rawdb.ReadGenesisState(db, hash) + if len(blob) != 0 { + if err := alloc.UnmarshalJSON(blob); err != nil { + return err + } + } else { + // Genesis allocation is missing and there are several possibilities: + // the node is legacy which doesn't persist the genesis allocation or + // the persisted allocation is just lost. + // - supported networks(mainnet, testnets), recover with defined allocations + // - private network, can't recover + var genesis *Genesis + switch hash { + case params.MainnetGenesisHash: + genesis = DefaultGenesisBlock() + case params.RopstenGenesisHash: + genesis = DefaultRopstenGenesisBlock() + case params.RinkebyGenesisHash: + genesis = DefaultRinkebyGenesisBlock() + case params.GoerliGenesisHash: + genesis = DefaultGoerliGenesisBlock() + case params.SepoliaGenesisHash: + genesis = DefaultSepoliaGenesisBlock() + } + if genesis != nil { + alloc = genesis.Alloc + } else { + return errors.New("not found") + } + } + _, err := alloc.flush(db) + return err +} + // GenesisAccount is an account in the state of the genesis block. type GenesisAccount struct { Code []byte `json:"code,omitempty"` @@ -267,19 +342,10 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { if db == nil { db = rawdb.NewMemoryDatabase() } - statedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil) + root, err := g.Alloc.flush(db) if err != nil { panic(err) } - for addr, account := range g.Alloc { - statedb.AddBalance(addr, account.Balance) - statedb.SetCode(addr, account.Code) - statedb.SetNonce(addr, account.Nonce) - for key, value := range account.Storage { - statedb.SetState(addr, key, value) - } - } - root := statedb.IntermediateRoot(false) head := &types.Header{ Number: new(big.Int).SetUint64(g.Number), Nonce: types.EncodeNonce(g.Nonce), @@ -307,9 +373,6 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { head.BaseFee = new(big.Int).SetUint64(params.InitialBaseFee) } } - statedb.Commit(false) - statedb.Database().TrieDB().Commit(root, true, nil) - return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)) } @@ -330,6 +393,9 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) { if config.Clique != nil && len(block.Extra()) == 0 { return nil, errors.New("can't start clique chain without signers") } + if err := g.Alloc.write(db, block.Hash()); err != nil { + return nil, err + } rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty()) rawdb.WriteBlock(db, block) rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil) diff --git a/core/genesis_test.go b/core/genesis_test.go index f3d6b23e5f..e8010e3d4e 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -213,3 +213,33 @@ func TestGenesis_Commit(t *testing.T) { t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty()) } } + +func TestReadWriteGenesisAlloc(t *testing.T) { + var ( + db = rawdb.NewMemoryDatabase() + alloc = &GenesisAlloc{ + {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, + {2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}}, + } + hash = common.HexToHash("0xdeadbeef") + ) + alloc.write(db, hash) + + var reload GenesisAlloc + err := reload.UnmarshalJSON(rawdb.ReadGenesisState(db, hash)) + if err != nil { + t.Fatalf("Failed to load genesis state %v", err) + } + if len(reload) != len(*alloc) { + t.Fatal("Unexpected genesis allocation") + } + for addr, account := range reload { + want, ok := (*alloc)[addr] + if !ok { + t.Fatal("Account is not found") + } + if !reflect.DeepEqual(want, account) { + t.Fatal("Unexpected account") + } + } +} diff --git a/core/rawdb/accessors_metadata.go b/core/rawdb/accessors_metadata.go index 3b0fcf0f2d..f5a161adb6 100644 --- a/core/rawdb/accessors_metadata.go +++ b/core/rawdb/accessors_metadata.go @@ -81,6 +81,19 @@ func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.Cha } } +// ReadGenesisState retrieves the genesis state based on the given genesis hash. +func ReadGenesisState(db ethdb.KeyValueReader, hash common.Hash) []byte { + data, _ := db.Get(genesisKey(hash)) + return data +} + +// WriteGenesisState writes the genesis state into the disk. +func WriteGenesisState(db ethdb.KeyValueWriter, hash common.Hash, data []byte) { + if err := db.Put(genesisKey(hash), data); err != nil { + log.Crit("Failed to store genesis state", "err", err) + } +} + // crashList is a list of unclean-shutdown-markers, for rlp-encoding to the // database type crashList struct { diff --git a/core/rawdb/database.go b/core/rawdb/database.go index be66828eea..e18a200b4b 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -386,6 +386,8 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error { preimages.Add(size) case bytes.HasPrefix(key, configPrefix) && len(key) == (len(configPrefix)+common.HashLength): metadata.Add(size) + case bytes.HasPrefix(key, genesisPrefix) && len(key) == (len(genesisPrefix)+common.HashLength): + metadata.Add(size) case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength): bloomBits.Add(size) case bytes.HasPrefix(key, BloomBitsIndexPrefix): diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index b43db7ab95..08f3734880 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -97,8 +97,9 @@ var ( CodePrefix = []byte("c") // CodePrefix + code hash -> account code skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header - PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage - configPrefix = []byte("ethereum-config-") // config prefix for the db + PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage + configPrefix = []byte("ethereum-config-") // config prefix for the db + genesisPrefix = []byte("ethereum-genesis-") // genesis state prefix for the db // Chain index prefixes (use `i` + single byte to avoid mixing data types). BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress @@ -242,3 +243,8 @@ func IsCodeKey(key []byte) (bool, []byte) { func configKey(hash common.Hash) []byte { return append(configPrefix, hash.Bytes()...) } + +// genesisKey = genesisPrefix + hash +func genesisKey(hash common.Hash) []byte { + return append(genesisPrefix, hash.Bytes()...) +}