This repository was archived by the owner on Jun 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
all: rework genesis api #567
Merged
huyngopt1994
merged 2 commits into
axieinfinity:path-base-implementing
from
Francesco4203:rework-genesis-api
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,96 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { | |
| return nil | ||
| } | ||
|
|
||
| // deriveHash computes the state root according to the genesis specification. | ||
| func (ga *GenesisAlloc) deriveHash() (common.Hash, error) { | ||
| // Create an ephemeral in-memory database for computing hash, | ||
| // all the derived states will be discarded to not pollute disk. | ||
| db := state.NewDatabase(rawdb.NewMemoryDatabase()) | ||
| statedb, err := state.New(common.Hash{}, 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) | ||
| } | ||
| } | ||
| return statedb.Commit(false) | ||
| } | ||
|
|
||
| // flush is very similar with deriveHash, but the main difference is | ||
| // all the generated states will be persisted into the given database. | ||
| // Also, the genesis state specification will be flushed as well. | ||
| func (ga *GenesisAlloc) flush(db ethdb.Database) error { | ||
| statedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil) | ||
| if err != nil { | ||
| return 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 err | ||
| } | ||
| err = statedb.Database().TrieDB().Commit(root, true, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Marshal the genesis state specification and persist. | ||
| blob, err := json.Marshal(ga) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| rawdb.WriteGenesisStateSpec(db, root, 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.ReadGenesisStateSpec(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") | ||
| } | ||
| } | ||
| return alloc.flush(db) | ||
| } | ||
|
|
||
| // GenesisAccount is an account in the state of the genesis block. | ||
| type GenesisAccount struct { | ||
| Code []byte `json:"code,omitempty"` | ||
|
|
@@ -185,7 +275,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override | |
| genesis = DefaultGenesisBlock() | ||
| } | ||
| // Ensure the stored genesis matches with the given one. | ||
| hash := genesis.ToBlock(nil).Hash() | ||
| hash := genesis.ToBlock().Hash() | ||
| if hash != stored { | ||
| return genesis.Config, hash, &GenesisMismatchError{stored, hash} | ||
| } | ||
|
|
@@ -197,7 +287,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override | |
| } | ||
| // Check whether the genesis block is already written. | ||
| if genesis != nil { | ||
| hash := genesis.ToBlock(nil).Hash() | ||
| hash := genesis.ToBlock().Hash() | ||
| if hash != stored { | ||
| return genesis.Config, hash, &GenesisMismatchError{stored, hash} | ||
| } | ||
|
|
@@ -262,25 +352,12 @@ 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) *types.Block { | ||
| if db == nil { | ||
| db = rawdb.NewMemoryDatabase() | ||
| } | ||
| statedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil) | ||
| // ToBlock returns the genesis block according to genesis specification. | ||
| func (g *Genesis) ToBlock() *types.Block { | ||
| root, err := g.Alloc.deriveHash() | ||
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it safe to remove the recalculated root in this line?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root is calculated by flush logic which is committed, so we don't need to callI IntermediateRoot here 🤔 |
||
| head := &types.Header{ | ||
| Number: new(big.Int).SetUint64(g.Number), | ||
| Nonce: types.EncodeNonce(g.Nonce), | ||
|
|
@@ -308,16 +385,13 @@ 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)) | ||
| } | ||
|
|
||
| // Commit writes the block and state of a genesis specification to the database. | ||
| // The block is committed as the canonical head block. | ||
| func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) { | ||
| block := g.ToBlock(db) | ||
| block := g.ToBlock() | ||
| if block.Number().Sign() != 0 { | ||
| return nil, errors.New("can't commit genesis block with number > 0") | ||
| } | ||
|
|
@@ -331,6 +405,12 @@ 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") | ||
| } | ||
| // All the checks has passed, flush the states derived from the genesis | ||
| // specification as well as the specification itself into the provided | ||
| // database. | ||
| if err := g.Alloc.flush(db); 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) | ||
|
|
@@ -352,15 +432,6 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block { | |
| return block | ||
| } | ||
|
|
||
| // GenesisBlockForTesting creates and writes a block in which addr has the given wei balance. | ||
| func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block { | ||
| g := Genesis{ | ||
| Alloc: GenesisAlloc{addr: {Balance: balance}}, | ||
| BaseFee: big.NewInt(params.InitialBaseFee), | ||
| } | ||
| return g.MustCommit(db) | ||
| } | ||
|
|
||
| // DefaultGenesisBlock returns the Ethereum main net genesis block. | ||
| func DefaultGenesisBlock() *Genesis { | ||
| return &Genesis{ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could u recheck all params when recommit genesis to disk?