|
21 | 21 | chronicles, |
22 | 22 | confutils, |
23 | 23 | confutils/defs, |
24 | | - confutils/std/net |
| 24 | + confutils/std/net, |
| 25 | + results |
25 | 26 | ], |
26 | 27 | eth/[common, net/nat], |
27 | 28 | ./networking/[bootnodes, eth1_enr as enr], |
@@ -163,13 +164,16 @@ type |
163 | 164 | "- sepolia/11155111: Test network (proof-of-work)\n" & |
164 | 165 | "- holesky/17000 : The holesovice post-merge testnet\n" & |
165 | 166 | "- hoodi/560048 : The second long-standing, merged-from-genesis, public Ethereum testnet\n" & |
166 | | - "- other : Custom" |
167 | | - defaultValue: "" # the default value is set in makeConfig |
| 167 | + "- path : /path/to/genesis-or-network-configuration.json\n" & |
| 168 | + "Both --network: name/path --network:id can be set at the same time to override network id number" |
| 169 | + defaultValue: @[] # the default value is set in makeConfig |
168 | 170 | defaultValueDesc: "mainnet(1)" |
169 | 171 | abbr: "i" |
170 | | - name: "network" }: string |
| 172 | + name: "network" }: seq[string] |
171 | 173 |
|
| 174 | + # TODO: disable --custom-network if both hive and kurtosis not using this anymore. |
172 | 175 | customNetwork {. |
| 176 | + hidden |
173 | 177 | desc: "Use custom genesis block for private Ethereum Network (as /path/to/genesis.json)" |
174 | 178 | defaultValueDesc: "" |
175 | 179 | abbr: "c" |
@@ -669,22 +673,86 @@ proc loadBootstrapFile(fileName: string, output: var seq[ENode]) = |
669 | 673 | proc loadStaticPeersFile(fileName: string, output: var seq[ENode]) = |
670 | 674 | fileName.loadEnodeFile(output, "static peers") |
671 | 675 |
|
672 | | -proc getNetworkId(conf: NimbusConf): Option[NetworkId] = |
673 | | - if conf.network.len == 0: |
674 | | - return none NetworkId |
| 676 | +func decOrHex(s: string): bool = |
| 677 | + const allowedDigits = Digits + HexDigits + {'x', 'X'} |
| 678 | + for c in s: |
| 679 | + if c notin allowedDigits: |
| 680 | + return false |
| 681 | + true |
675 | 682 |
|
676 | | - let network = toLowerAscii(conf.network) |
677 | | - case network |
678 | | - of "mainnet": return some MainNet |
679 | | - of "sepolia": return some SepoliaNet |
680 | | - of "holesky": return some HoleskyNet |
681 | | - of "hoodi": return some HoodiNet |
| 683 | +proc parseNetworkId(network: string): NetworkId = |
| 684 | + try: |
| 685 | + return parseHexOrDec256(network) |
| 686 | + except CatchableError: |
| 687 | + error "Failed to parse network id", id=network |
| 688 | + quit QuitFailure |
| 689 | + |
| 690 | +proc parseNetworkParams(network: string): (NetworkParams, bool) = |
| 691 | + case toLowerAscii(network) |
| 692 | + of "mainnet": (networkParams(MainNet), false) |
| 693 | + of "sepolia": (networkParams(SepoliaNet), false) |
| 694 | + of "holesky": (networkParams(HoleskyNet), false) |
| 695 | + of "hoodi" : (networkParams(HoodiNet), false) |
682 | 696 | else: |
683 | | - try: |
684 | | - some parseHexOrDec256(network) |
685 | | - except CatchableError: |
686 | | - error "Failed to parse network name or id", network |
| 697 | + var params: NetworkParams |
| 698 | + if not loadNetworkParams(network, params): |
| 699 | + # `loadNetworkParams` have it's own error log |
687 | 700 | quit QuitFailure |
| 701 | + (params, true) |
| 702 | + |
| 703 | +proc processNetworkParamsAndNetworkId(conf: var NimbusConf) = |
| 704 | + if conf.network.len == 0 and conf.customNetwork.isNone: |
| 705 | + # Default value if none is set |
| 706 | + conf.networkId = MainNet |
| 707 | + conf.networkParams = networkParams(MainNet) |
| 708 | + return |
| 709 | + |
| 710 | + var |
| 711 | + params: Opt[NetworkParams] |
| 712 | + id: Opt[NetworkId] |
| 713 | + simulatedCustomNetwork = false |
| 714 | + |
| 715 | + for network in conf.network: |
| 716 | + if decOrHex(network): |
| 717 | + if id.isSome: |
| 718 | + warn "Network ID already set, ignore new value", id=network |
| 719 | + continue |
| 720 | + id = Opt.some parseNetworkId(network) |
| 721 | + else: |
| 722 | + if params.isSome: |
| 723 | + warn "Network configuration already set, ignore new value", network |
| 724 | + continue |
| 725 | + let (parsedParams, custom) = parseNetworkParams(network) |
| 726 | + params = Opt.some parsedParams |
| 727 | + # Simulate --custom-network while it is still not disabled. |
| 728 | + if custom: |
| 729 | + conf.customNetwork = some parsedParams |
| 730 | + simulatedCustomNetwork = true |
| 731 | + |
| 732 | + if conf.customNetwork.isSome: |
| 733 | + if params.isNone: |
| 734 | + warn "`--custom-network` is deprecated, please use `--network`" |
| 735 | + elif not simulatedCustomNetwork: |
| 736 | + warn "Network configuration already set by `--network`, `--custom-network` override it" |
| 737 | + params = if conf.customNetwork.isSome: Opt.some conf.customNetwork.get |
| 738 | + else: Opt.none(NetworkParams) |
| 739 | + if id.isNone: |
| 740 | + # WARNING: networkId and chainId are two distinct things |
| 741 | + # their usage should not be mixed in other places. |
| 742 | + # We only set networkId to chainId if networkId not set in cli and |
| 743 | + # --custom-network is set. |
| 744 | + # If chainId is not defined in config file, it's ok because |
| 745 | + # zero means CustomNet |
| 746 | + id = Opt.some NetworkId(params.value.config.chainId) |
| 747 | + |
| 748 | + if id.isNone and params.isSome: |
| 749 | + id = Opt.some NetworkId(params.value.config.chainId) |
| 750 | + |
| 751 | + if conf.customNetwork.isNone and params.isNone: |
| 752 | + params = Opt.some networkParams(id.value) |
| 753 | + |
| 754 | + conf.networkParams = params.expect("Network params exists") |
| 755 | + conf.networkId = id.expect("Network ID exists") |
688 | 756 |
|
689 | 757 | proc getRpcFlags(api: openArray[string]): set[RpcFlag] = |
690 | 758 | if api.len == 0: |
@@ -830,27 +898,7 @@ proc makeConfig*(cmdLine = commandLineParams()): NimbusConf |
830 | 898 |
|
831 | 899 | setupLogging(result.logLevel, result.logStdout, none(OutFile)) |
832 | 900 |
|
833 | | - var networkId = result.getNetworkId() |
834 | | - |
835 | | - if result.customNetwork.isSome: |
836 | | - result.networkParams = result.customNetwork.get() |
837 | | - if networkId.isNone: |
838 | | - # WARNING: networkId and chainId are two distinct things |
839 | | - # they usage should not be mixed in other places. |
840 | | - # We only set networkId to chainId if networkId not set in cli and |
841 | | - # --custom-network is set. |
842 | | - # If chainId is not defined in config file, it's ok because |
843 | | - # zero means CustomNet |
844 | | - networkId = some(NetworkId(result.networkParams.config.chainId)) |
845 | | - |
846 | | - if networkId.isNone: |
847 | | - # bootnodes is set via getBootNodes |
848 | | - networkId = some MainNet |
849 | | - |
850 | | - result.networkId = networkId.get() |
851 | | - |
852 | | - if result.customNetwork.isNone: |
853 | | - result.networkParams = networkParams(result.networkId) |
| 901 | + processNetworkParamsAndNetworkId(result) |
854 | 902 |
|
855 | 903 | if result.cmd == noCommand: |
856 | 904 | if result.udpPort == Port(0): |
|
0 commit comments