Skip to content

Commit

Permalink
Add support for standard genesis.json format
Browse files Browse the repository at this point in the history
  • Loading branch information
amsanghi committed Oct 29, 2024
1 parent aa6d69a commit dda8af2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type InitConfig struct {
ImportWasm bool `koanf:"import-wasm"`
AccountsPerSync uint `koanf:"accounts-per-sync"`
ImportFile string `koanf:"import-file"`
GenesisJsonFile string `koanf:"genesis-json-file"`
ThenQuit bool `koanf:"then-quit"`
Prune string `koanf:"prune"`
PruneBloomSize uint64 `koanf:"prune-bloom-size"`
Expand All @@ -51,6 +52,7 @@ var InitConfigDefault = InitConfig{
Empty: false,
ImportWasm: false,
ImportFile: "",
GenesisJsonFile: "",
AccountsPerSync: 100000,
ThenQuit: false,
Prune: "",
Expand Down Expand Up @@ -79,6 +81,7 @@ func InitConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Bool(prefix+".import-wasm", InitConfigDefault.ImportWasm, "if set, import the wasm directory when downloading a database (contains executable code - only use with highly trusted source)")
f.Bool(prefix+".then-quit", InitConfigDefault.ThenQuit, "quit after init is done")
f.String(prefix+".import-file", InitConfigDefault.ImportFile, "path for json data to import")
f.String(prefix+".genesis-json-file", InitConfigDefault.GenesisJsonFile, "path for genesis json file")
f.Uint(prefix+".accounts-per-sync", InitConfigDefault.AccountsPerSync, "during init - sync database every X accounts. Lower value for low-memory systems. 0 disables.")
f.String(prefix+".prune", InitConfigDefault.Prune, "pruning for a given use: \"full\" for full nodes serving RPC requests, or \"validator\" for validators")
f.Uint64(prefix+".prune-bloom-size", InitConfigDefault.PruneBloomSize, "the amount of memory in megabytes to use for the pruning bloom filter (higher values prune better)")
Expand Down
38 changes: 35 additions & 3 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,36 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo

var chainConfig *params.ChainConfig

if config.Init.GenesisJsonFile != "" {
if initDataReader != nil {
return chainDb, nil, errors.New("multiple init methods supplied")
}
genesisJson, err := os.ReadFile(config.Init.GenesisJsonFile)
if err != nil {
return chainDb, nil, err
}
var gen core.Genesis
if err := json.Unmarshal(genesisJson, &gen); err != nil {
return chainDb, nil, err
}
var accounts []statetransfer.AccountInitializationInfo
for address, account := range gen.Alloc {
accounts = append(accounts, statetransfer.AccountInitializationInfo{
Addr: address,
EthBalance: account.Balance,
Nonce: account.Nonce,
ContractInfo: &statetransfer.AccountInitContractInfo{
Code: account.Code,
ContractStorage: account.Storage,
},
})
}
initDataReader = statetransfer.NewMemoryInitDataReader(&statetransfer.ArbosInitializationInfo{
Accounts: accounts,
})
chainConfig = gen.Config
}

var l2BlockChain *core.BlockChain
txIndexWg := sync.WaitGroup{}
if initDataReader == nil {
Expand All @@ -712,9 +742,11 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
if err != nil {
return chainDb, nil, err
}
chainConfig, err = chaininfo.GetChainConfig(new(big.Int).SetUint64(config.Chain.ID), config.Chain.Name, genesisBlockNr, config.Chain.InfoFiles, config.Chain.InfoJson)
if err != nil {
return chainDb, nil, err
if chainConfig == nil {
chainConfig, err = chaininfo.GetChainConfig(new(big.Int).SetUint64(config.Chain.ID), config.Chain.Name, genesisBlockNr, config.Chain.InfoFiles, config.Chain.InfoJson)
if err != nil {
return chainDb, nil, err
}
}
testUpdateTxIndex(chainDb, chainConfig, &txIndexWg)
ancients, err := chainDb.Ancients()
Expand Down

0 comments on commit dda8af2

Please sign in to comment.