Skip to content

Commit

Permalink
feat(cmd): add flag for spinning up local network (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeren authored Aug 21, 2024
1 parent 0714a0b commit 91ea1ec
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,11 @@ var (
Usage: "iliad test network: pre-configured proof-of-stake test network",
Category: flags.MiscCategory,
}
LocalFlag = &cli.BoolFlag{
Name: "local",
Usage: "local test network: pre-configured local proof-of-stake test network",
Category: flags.MiscCategory,
}

// RPC settings
IPCDisabledFlag = &cli.BoolFlag{
Expand Down Expand Up @@ -973,6 +978,7 @@ var (
SepoliaFlag,
HoleskyFlag,
IliadFlag,
LocalFlag,
}
// NetworkFlags is the flag group of all built-in supported networks.
NetworkFlags = append([]cli.Flag{MainnetFlag}, TestnetFlags...)
Expand Down Expand Up @@ -1005,6 +1011,9 @@ func MakeDataDir(ctx *cli.Context) string {
if ctx.Bool(IliadFlag.Name) {
return filepath.Join(path, "iliad")
}
if ctx.Bool(LocalFlag.Name) {
return filepath.Join(path, "local")
}
return path
}
Fatalf("Cannot determine default data directory, please set manually (--datadir)")
Expand Down Expand Up @@ -1069,6 +1078,8 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls = params.GoerliBootnodes
case ctx.Bool(IliadFlag.Name):
urls = params.IliadBootnodes
case ctx.Bool(LocalFlag.Name):
urls = params.LocalBootnodes
}
}
cfg.BootstrapNodes = mustParseBootnodes(urls)
Expand Down Expand Up @@ -1150,7 +1161,7 @@ func SplitAndTrim(input string) (ret []string) {
// setHTTP creates the HTTP RPC listener interface string from the set
// command line flags, returning empty if the HTTP endpoint is disabled.
func setHTTP(ctx *cli.Context, cfg *node.Config) {
if ctx.Bool(HTTPEnabledFlag.Name) {
if ctx.Bool(HTTPEnabledFlag.Name) || ctx.Bool(LocalFlag.Name) {
if cfg.HTTPHost == "" {
cfg.HTTPHost = "127.0.0.1"
}
Expand Down Expand Up @@ -1502,6 +1513,8 @@ func SetDataDir(ctx *cli.Context, cfg *node.Config) {
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "holesky")
case ctx.Bool(IliadFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "iliad")
case ctx.Bool(LocalFlag.Name) && cfg.DataDir == node.DefaultDataDir():
cfg.DataDir = filepath.Join(node.DefaultDataDir(), "local")
}
}

Expand Down Expand Up @@ -1657,7 +1670,7 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
// SetEthConfig applies eth-related command line flags to the config.
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
// Avoid conflicting network flags
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, GoerliFlag, SepoliaFlag, HoleskyFlag, IliadFlag)
CheckExclusive(ctx, MainnetFlag, DeveloperFlag, GoerliFlag, SepoliaFlag, HoleskyFlag, IliadFlag, LocalFlag)
CheckExclusive(ctx, DeveloperFlag, ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer

// Set configurations from CLI flags
Expand Down Expand Up @@ -1838,6 +1851,11 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
cfg.Genesis = core.DefaultIliadGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.IliadGenesisHash)
case ctx.Bool(LocalFlag.Name):
if !ctx.IsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1511
}
cfg.Genesis = core.DefaultLocalGenesisBlock()
case ctx.Bool(DeveloperFlag.Name):
if !ctx.IsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1337
Expand Down Expand Up @@ -2163,6 +2181,8 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
genesis = core.DefaultGoerliGenesisBlock()
case ctx.Bool(IliadFlag.Name):
genesis = core.DefaultIliadGenesisBlock()
case ctx.Bool(LocalFlag.Name):
genesis = core.DefaultLocalGenesisBlock()
case ctx.Bool(DeveloperFlag.Name):
Fatalf("Developer chains are ephemeral")
}
Expand Down
15 changes: 15 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return params.GoerliChainConfig
case ghash == params.IliadGenesisHash:
return params.IliadChainConfig
case ghash == params.LocalGenesisHash:
return params.LocalChainConfig
default:
return params.AllEthashProtocolChanges
}
Expand Down Expand Up @@ -588,6 +590,19 @@ func DefaultIliadGenesisBlock() *Genesis {
}
}

// DefaultLocalGenesisBlock returns the network genesis block for local testing.
func DefaultLocalGenesisBlock() *Genesis {
return &Genesis{
Config: params.LocalChainConfig,
Difficulty: big.NewInt(0x20000),
GasLimit: 0x7A1200,
Nonce: 0x42,
Timestamp: 0,
Alloc: decodePrealloc(localAllocData),
}
}


// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
// Override the default period to the user requested one
Expand Down
1 change: 1 addition & 0 deletions core/genesis_alloc.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ func testSetupGenesis(t *testing.T, scheme string) {
wantHash: params.IliadGenesisHash,
wantConfig: params.IliadChainConfig,
},
{
name: "custom block in DB, genesis == local",
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
tdb := triedb.NewDatabase(db, newDbConfig(scheme))
customg.Commit(db, tdb)
return SetupGenesisBlock(db, tdb, DefaultLocalGenesisBlock())
},
wantErr: &GenesisMismatchError{Stored: customghash, New: params.LocalGenesisHash},
wantHash: params.LocalGenesisHash,
wantConfig: params.LocalChainConfig,
},
{
name: "compatible config in DB",
fn: func(db ethdb.Database) (*params.ChainConfig, common.Hash, error) {
Expand Down Expand Up @@ -198,6 +209,7 @@ func TestGenesisHashes(t *testing.T) {
{DefaultGoerliGenesisBlock(), params.GoerliGenesisHash},
{DefaultSepoliaGenesisBlock(), params.SepoliaGenesisHash},
{DefaultIliadGenesisBlock(), params.IliadGenesisHash},
{DefaultLocalGenesisBlock(), params.LocalGenesisHash},
} {
// Test via MustCommit
db := rawdb.NewMemoryDatabase()
Expand Down
3 changes: 3 additions & 0 deletions params/bootnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var SepoliaBootnodes = []string{
"enode://9e9492e2e8836114cc75f5b929784f4f46c324ad01daf87d956f98b3b6c5fcba95524d6e5cf9861dc96a2c8a171ea7105bb554a197455058de185fa870970c7c@138.68.123.152:30303", // sepolia-bootnode-1-ams3
}

// LocalBootnodes are the enode URLs of the P2P bootstrap nodes running on the local network.
var LocalBootnodes = []string{}

// IliadBootnodes are the enode URLs of the P2P bootstrap nodes running on the
// Iliad test network.
var IliadBootnodes = []string{
Expand Down
22 changes: 22 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
SepoliaGenesisHash = common.HexToHash("0x25a5cc106eea7138acab33231d7160d69cb777ee0c2c553fcddf5138993e6dd9")
GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a")
IliadGenesisHash = common.HexToHash("0x0be40479b95ce64a5d7662b6ac3f4fc5de2479d68095b7cd57e752309e2f060d")
LocalGenesisHash = common.HexToHash("0x0be40479b95ce64a5d7662b6ac3f4fc5de2479d68095b7cd57e752309e2f060d")
)

func newUint64(val uint64) *uint64 { return &val }
Expand Down Expand Up @@ -159,6 +160,26 @@ var (
CancunTime: newUint64(0),
}

LocalChainConfig = &ChainConfig{
ChainID: big.NewInt(1511),
HomesteadBlock: big.NewInt(0),
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: big.NewInt(0),
ArrowGlacierBlock: big.NewInt(0),
GrayGlacierBlock: big.NewInt(0),
TerminalTotalDifficulty: big.NewInt(0),
TerminalTotalDifficultyPassed: true,
ShanghaiTime: newUint64(0),
CancunTime: newUint64(0),
}

// AllEthashProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Ethash consensus.
AllEthashProtocolChanges = &ChainConfig{
Expand Down Expand Up @@ -339,6 +360,7 @@ var NetworkNames = map[string]string{
SepoliaChainConfig.ChainID.String(): "sepolia",
HoleskyChainConfig.ChainID.String(): "holesky",
IliadChainConfig.ChainID.String(): "iliad",
LocalChainConfig.ChainID.String(): "local",
}

// ChainConfig is the core config which determines the blockchain settings.
Expand Down

0 comments on commit 91ea1ec

Please sign in to comment.