2021-05-20 06:01:57 +00:00
|
|
|
import
|
|
|
|
std/[os, parseopt, strutils],
|
2022-10-20 08:34:59 +00:00
|
|
|
eth/common, stint, ../nimbus/config
|
2021-05-20 06:01:57 +00:00
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
from ../nimbus/common/chain_config import
|
2021-05-20 06:01:57 +00:00
|
|
|
MainNet,
|
2023-10-25 06:27:55 +00:00
|
|
|
GoerliNet,
|
|
|
|
SepoliaNet,
|
|
|
|
HoleskyNet
|
2019-01-09 06:37:40 +00:00
|
|
|
|
|
|
|
type
|
2021-09-11 14:58:01 +00:00
|
|
|
ConfigStatus* = enum
|
|
|
|
## Configuration status flags
|
|
|
|
Success, ## Success
|
|
|
|
EmptyOption, ## No options in category
|
|
|
|
ErrorUnknownOption, ## Unknown option in command line found
|
|
|
|
ErrorParseOption, ## Error in parsing command line option
|
|
|
|
ErrorIncorrectOption, ## Option has incorrect value
|
|
|
|
Error ## Unspecified error
|
|
|
|
|
2019-01-09 06:37:40 +00:00
|
|
|
PremixConfiguration* = ref object
|
|
|
|
dataDir*: string
|
2022-04-08 04:54:11 +00:00
|
|
|
head*: UInt256
|
2019-01-09 06:37:40 +00:00
|
|
|
maxBlocks*: int
|
|
|
|
numCommits*: int
|
2021-05-20 06:01:57 +00:00
|
|
|
netId*: NetworkId
|
2019-01-09 06:37:40 +00:00
|
|
|
|
|
|
|
var premixConfig {.threadvar.}: PremixConfiguration
|
|
|
|
|
|
|
|
proc getConfiguration*(): PremixConfiguration {.gcsafe.}
|
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
proc processInteger(v: string, o: var int): ConfigStatus =
|
|
|
|
## Convert string to integer.
|
|
|
|
try:
|
|
|
|
o = parseInt(v)
|
|
|
|
result = Success
|
|
|
|
except ValueError:
|
|
|
|
result = ErrorParseOption
|
|
|
|
|
2019-01-09 06:37:40 +00:00
|
|
|
proc initConfiguration(): PremixConfiguration =
|
|
|
|
result = new PremixConfiguration
|
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
const dataDir = defaultDataDir()
|
2019-01-09 06:37:40 +00:00
|
|
|
|
2021-09-11 14:58:01 +00:00
|
|
|
result.dataDir = dataDir
|
2019-01-09 06:37:40 +00:00
|
|
|
result.head = 0.u256
|
|
|
|
result.maxBlocks = 0
|
|
|
|
result.numCommits = 128
|
2020-06-19 10:52:19 +00:00
|
|
|
result.netId = MainNet
|
2019-01-09 06:37:40 +00:00
|
|
|
|
|
|
|
proc getConfiguration*(): PremixConfiguration =
|
|
|
|
if isNil(premixConfig):
|
|
|
|
premixConfig = initConfiguration()
|
|
|
|
result = premixConfig
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc processU256(val: string, o: var UInt256): ConfigStatus =
|
2019-01-09 06:37:40 +00:00
|
|
|
if val.len > 2 and val[0] == '0' and val[1] == 'x':
|
2022-04-08 04:54:11 +00:00
|
|
|
o = UInt256.fromHex(val)
|
2019-01-09 06:37:40 +00:00
|
|
|
else:
|
2022-04-08 04:54:11 +00:00
|
|
|
o = parse(val, UInt256)
|
2019-01-09 06:37:40 +00:00
|
|
|
result = Success
|
|
|
|
|
2021-05-20 06:01:57 +00:00
|
|
|
proc processNetId(val: string, o: var NetworkId): ConfigStatus =
|
2020-06-19 10:52:19 +00:00
|
|
|
case val.toLowerAscii()
|
|
|
|
of "main": o = MainNet
|
|
|
|
of "goerli": o = GoerliNet
|
2023-10-25 06:27:55 +00:00
|
|
|
of "sepolia": o = SepoliaNet
|
|
|
|
of "holesky": o = HoleskyNet
|
2020-06-19 10:52:19 +00:00
|
|
|
|
2021-05-20 06:01:57 +00:00
|
|
|
template checkArgument(fun, o, value: untyped) =
|
2019-01-09 06:37:40 +00:00
|
|
|
## Checks if arguments got processed successfully
|
2021-05-20 06:01:57 +00:00
|
|
|
let res = fun(value, o)
|
2019-01-09 06:37:40 +00:00
|
|
|
if res == Success:
|
|
|
|
continue
|
|
|
|
elif res == ErrorParseOption:
|
|
|
|
msg = "Error processing option [" & key & "] with value [" & value & "]"
|
|
|
|
result = res
|
|
|
|
break
|
|
|
|
elif res == ErrorIncorrectOption:
|
|
|
|
msg = "Incorrect value for option [" & key & "] value [" & value & "]"
|
|
|
|
result = res
|
|
|
|
break
|
|
|
|
|
|
|
|
proc processArguments*(msg: var string): ConfigStatus =
|
|
|
|
var
|
|
|
|
opt = initOptParser()
|
|
|
|
length = 0
|
|
|
|
config = getConfiguration()
|
|
|
|
|
|
|
|
result = Success
|
|
|
|
for kind, key, value in opt.getopt():
|
|
|
|
case kind
|
|
|
|
of cmdArgument:
|
|
|
|
discard
|
|
|
|
of cmdLongOption, cmdShortOption:
|
|
|
|
inc(length)
|
|
|
|
case key.toLowerAscii()
|
|
|
|
of "datadir": config.dataDir = value
|
|
|
|
of "maxblocks":
|
2021-05-20 06:01:57 +00:00
|
|
|
checkArgument(processInteger, config.maxBlocks, value)
|
2019-01-09 06:37:40 +00:00
|
|
|
of "head":
|
2021-05-20 06:01:57 +00:00
|
|
|
checkArgument(processU256, config.head, value)
|
2019-01-09 06:37:40 +00:00
|
|
|
of "numcommits":
|
2021-05-20 06:01:57 +00:00
|
|
|
checkArgument(processInteger, config.numCommits, value)
|
2019-01-09 06:37:40 +00:00
|
|
|
config.numCommits = max(config.numCommits, 512)
|
2020-06-19 10:52:19 +00:00
|
|
|
of "netid":
|
2021-05-20 06:01:57 +00:00
|
|
|
checkArgument(processNetId, config.netId, value)
|
2019-01-09 06:37:40 +00:00
|
|
|
else:
|
|
|
|
msg = "Unknown option " & key
|
|
|
|
if value.len > 0: msg = msg & " : " & value
|
|
|
|
result = ErrorUnknownOption
|
|
|
|
break
|
|
|
|
of cmdEnd:
|
|
|
|
msg = "Error processing option [" & key & "]"
|
|
|
|
result = ErrorParseOption
|
|
|
|
break
|