startup cleanup
* fix several memory leaks due to temporaries not being reset during init * avoid massive main() function with lots of stuff in it * disable nim-prompt (unused) * reuse validator pool instance in eth2_processor * style cleanup
This commit is contained in:
parent
3f6834cce7
commit
0dbc7162ac
|
@ -37,7 +37,7 @@ type
|
||||||
netKeys*: KeyPair
|
netKeys*: KeyPair
|
||||||
db*: BeaconChainDB
|
db*: BeaconChainDB
|
||||||
config*: BeaconNodeConf
|
config*: BeaconNodeConf
|
||||||
attachedValidators*: ValidatorPool
|
attachedValidators*: ref ValidatorPool
|
||||||
chainDag*: ChainDAGRef
|
chainDag*: ChainDAGRef
|
||||||
quarantine*: QuarantineRef
|
quarantine*: QuarantineRef
|
||||||
attestationPool*: ref AttestationPool
|
attestationPool*: ref AttestationPool
|
||||||
|
|
|
@ -511,7 +511,7 @@ type
|
||||||
desc: "Delay in seconds between retries after unsuccessful attempts to connect to a beacon node"
|
desc: "Delay in seconds between retries after unsuccessful attempts to connect to a beacon node"
|
||||||
name: "retry-delay" }: int
|
name: "retry-delay" }: int
|
||||||
|
|
||||||
proc defaultDataDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
proc defaultDataDir*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
let dataDir = when defined(windows):
|
let dataDir = when defined(windows):
|
||||||
"AppData" / "Roaming" / "Nimbus"
|
"AppData" / "Roaming" / "Nimbus"
|
||||||
elif defined(macosx):
|
elif defined(macosx):
|
||||||
|
@ -521,29 +521,29 @@ proc defaultDataDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
|
|
||||||
getHomeDir() / dataDir / "BeaconNode"
|
getHomeDir() / dataDir / "BeaconNode"
|
||||||
|
|
||||||
func dumpDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func dumpDir*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
conf.dataDir / "dump"
|
config.dataDir / "dump"
|
||||||
|
|
||||||
func dumpDirInvalid*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func dumpDirInvalid*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
conf.dumpDir / "invalid" # things that failed validation
|
config.dumpDir / "invalid" # things that failed validation
|
||||||
|
|
||||||
func dumpDirIncoming*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func dumpDirIncoming*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
conf.dumpDir / "incoming" # things that couldn't be validated (missingparent etc)
|
config.dumpDir / "incoming" # things that couldn't be validated (missingparent etc)
|
||||||
|
|
||||||
func dumpDirOutgoing*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func dumpDirOutgoing*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
conf.dumpDir / "outgoing" # things we produced
|
config.dumpDir / "outgoing" # things we produced
|
||||||
|
|
||||||
proc createDumpDirs*(conf: BeaconNodeConf) =
|
proc createDumpDirs*(config: BeaconNodeConf) =
|
||||||
if conf.dumpEnabled:
|
if config.dumpEnabled:
|
||||||
let resInv = secureCreatePath(conf.dumpDirInvalid)
|
let resInv = secureCreatePath(config.dumpDirInvalid)
|
||||||
if resInv.isErr():
|
if resInv.isErr():
|
||||||
warn "Could not create dump directory", path = conf.dumpDirInvalid
|
warn "Could not create dump directory", path = config.dumpDirInvalid
|
||||||
let resInc = secureCreatePath(conf.dumpDirIncoming)
|
let resInc = secureCreatePath(config.dumpDirIncoming)
|
||||||
if resInc.isErr():
|
if resInc.isErr():
|
||||||
warn "Could not create dump directory", path = conf.dumpDirIncoming
|
warn "Could not create dump directory", path = config.dumpDirIncoming
|
||||||
let resOut = secureCreatePath(conf.dumpDirOutgoing)
|
let resOut = secureCreatePath(config.dumpDirOutgoing)
|
||||||
if resOut.isErr():
|
if resOut.isErr():
|
||||||
warn "Could not create dump directory", path = conf.dumpDirOutgoing
|
warn "Could not create dump directory", path = config.dumpDirOutgoing
|
||||||
|
|
||||||
func parseCmdArg*(T: type GraffitiBytes, input: TaintedString): T
|
func parseCmdArg*(T: type GraffitiBytes, input: TaintedString): T
|
||||||
{.raises: [ValueError, Defect].} =
|
{.raises: [ValueError, Defect].} =
|
||||||
|
@ -611,65 +611,65 @@ proc parseCmdArg*(T: type enr.Record, p: TaintedString): T
|
||||||
proc completeCmdArg*(T: type enr.Record, val: TaintedString): seq[string] =
|
proc completeCmdArg*(T: type enr.Record, val: TaintedString): seq[string] =
|
||||||
return @[]
|
return @[]
|
||||||
|
|
||||||
func validatorsDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func validatorsDir*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
string conf.validatorsDirFlag.get(InputDir(conf.dataDir / "validators"))
|
string config.validatorsDirFlag.get(InputDir(config.dataDir / "validators"))
|
||||||
|
|
||||||
func secretsDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func secretsDir*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
string conf.secretsDirFlag.get(InputDir(conf.dataDir / "secrets"))
|
string config.secretsDirFlag.get(InputDir(config.dataDir / "secrets"))
|
||||||
|
|
||||||
func walletsDir*(conf: BeaconNodeConf): string =
|
func walletsDir*(config: BeaconNodeConf): string =
|
||||||
if conf.walletsDirFlag.isSome:
|
if config.walletsDirFlag.isSome:
|
||||||
conf.walletsDirFlag.get.string
|
config.walletsDirFlag.get.string
|
||||||
else:
|
else:
|
||||||
conf.dataDir / "wallets"
|
config.dataDir / "wallets"
|
||||||
|
|
||||||
func outWalletName*(conf: BeaconNodeConf): Option[WalletName] =
|
func outWalletName*(config: BeaconNodeConf): Option[WalletName] =
|
||||||
proc fail {.noReturn.} =
|
proc fail {.noReturn.} =
|
||||||
raiseAssert "outWalletName should be used only in the right context"
|
raiseAssert "outWalletName should be used only in the right context"
|
||||||
|
|
||||||
case conf.cmd
|
case config.cmd
|
||||||
of wallets:
|
of wallets:
|
||||||
case conf.walletsCmd
|
case config.walletsCmd
|
||||||
of WalletsCmd.create: conf.createdWalletNameFlag
|
of WalletsCmd.create: config.createdWalletNameFlag
|
||||||
of WalletsCmd.restore: conf.restoredWalletNameFlag
|
of WalletsCmd.restore: config.restoredWalletNameFlag
|
||||||
of WalletsCmd.list: fail()
|
of WalletsCmd.list: fail()
|
||||||
of deposits:
|
of deposits:
|
||||||
# TODO: Uncomment when the deposits create command is restored
|
# TODO: Uncomment when the deposits create command is restored
|
||||||
#case conf.depositsCmd
|
#case config.depositsCmd
|
||||||
#of DepositsCmd.create: conf.newWalletNameFlag
|
#of DepositsCmd.create: config.newWalletNameFlag
|
||||||
#else: fail()
|
#else: fail()
|
||||||
fail()
|
fail()
|
||||||
else:
|
else:
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
func outWalletFile*(conf: BeaconNodeConf): Option[OutFile] =
|
func outWalletFile*(config: BeaconNodeConf): Option[OutFile] =
|
||||||
proc fail {.noReturn.} =
|
proc fail {.noReturn.} =
|
||||||
raiseAssert "outWalletName should be used only in the right context"
|
raiseAssert "outWalletName should be used only in the right context"
|
||||||
|
|
||||||
case conf.cmd
|
case config.cmd
|
||||||
of wallets:
|
of wallets:
|
||||||
case conf.walletsCmd
|
case config.walletsCmd
|
||||||
of WalletsCmd.create: conf.createdWalletFileFlag
|
of WalletsCmd.create: config.createdWalletFileFlag
|
||||||
of WalletsCmd.restore: conf.restoredWalletFileFlag
|
of WalletsCmd.restore: config.restoredWalletFileFlag
|
||||||
of WalletsCmd.list: fail()
|
of WalletsCmd.list: fail()
|
||||||
of deposits:
|
of deposits:
|
||||||
# TODO: Uncomment when the deposits create command is restored
|
# TODO: Uncomment when the deposits create command is restored
|
||||||
#case conf.depositsCmd
|
#case config.depositsCmd
|
||||||
#of DepositsCmd.create: conf.newWalletFileFlag
|
#of DepositsCmd.create: config.newWalletFileFlag
|
||||||
#else: fail()
|
#else: fail()
|
||||||
fail()
|
fail()
|
||||||
else:
|
else:
|
||||||
fail()
|
fail()
|
||||||
|
|
||||||
func databaseDir*(conf: BeaconNodeConf|ValidatorClientConf): string =
|
func databaseDir*(config: BeaconNodeConf|ValidatorClientConf): string =
|
||||||
conf.dataDir / "db"
|
config.dataDir / "db"
|
||||||
|
|
||||||
func defaultListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): ValidIpAddress =
|
func defaultListenAddress*(config: BeaconNodeConf|ValidatorClientConf): ValidIpAddress =
|
||||||
# TODO: How should we select between IPv4 and IPv6
|
# TODO: How should we select between IPv4 and IPv6
|
||||||
# Maybe there should be a config option for this.
|
# Maybe there should be a config option for this.
|
||||||
(static ValidIpAddress.init("0.0.0.0"))
|
(static ValidIpAddress.init("0.0.0.0"))
|
||||||
|
|
||||||
func defaultAdminListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): ValidIpAddress =
|
func defaultAdminListenAddress*(config: BeaconNodeConf|ValidatorClientConf): ValidIpAddress =
|
||||||
(static ValidIpAddress.init("127.0.0.1"))
|
(static ValidIpAddress.init("127.0.0.1"))
|
||||||
|
|
||||||
template writeValue*(writer: var JsonWriter,
|
template writeValue*(writer: var JsonWriter,
|
||||||
|
|
|
@ -1114,8 +1114,7 @@ proc getEth1BlockHash*(url: string, blockId: RtBlockIdentifier): Future[BlockHas
|
||||||
await web3.close()
|
await web3.close()
|
||||||
|
|
||||||
proc testWeb3Provider*(web3Url: Uri,
|
proc testWeb3Provider*(web3Url: Uri,
|
||||||
depositContractAddress: Option[Eth1Address],
|
depositContractAddress: Eth1Address) {.async.} =
|
||||||
depositContractDeployedAt: Option[BlockHashOrNumber]) {.async.} =
|
|
||||||
template mustSucceed(action: static string, expr: untyped): untyped =
|
template mustSucceed(action: static string, expr: untyped): untyped =
|
||||||
try: expr
|
try: expr
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
|
@ -1133,14 +1132,13 @@ proc testWeb3Provider*(web3Url: Uri,
|
||||||
echo "Network: ", network
|
echo "Network: ", network
|
||||||
echo "Latest block: ", latestBlock.number.uint64
|
echo "Latest block: ", latestBlock.number.uint64
|
||||||
|
|
||||||
if depositContractAddress.isSome:
|
let ns = web3.contractSender(DepositContract, depositContractAddress)
|
||||||
let ns = web3.contractSender(DepositContract, depositContractAddress.get)
|
try:
|
||||||
try:
|
let depositRoot = awaitWithRetries(
|
||||||
let depositRoot = awaitWithRetries(
|
ns.get_deposit_root.call(blockNumber = latestBlock.number.uint64))
|
||||||
ns.get_deposit_root.call(blockNumber = latestBlock.number.uint64))
|
echo "Deposit root: ", depositRoot
|
||||||
echo "Deposit root: ", depositRoot
|
except CatchableError as err:
|
||||||
except CatchableError as err:
|
echo "Web3 provider is not archive mode: ", err.msg
|
||||||
echo "Web3 provider is not archive mode"
|
|
||||||
|
|
||||||
when hasGenesisDetection:
|
when hasGenesisDetection:
|
||||||
proc init*(T: type Eth1Monitor,
|
proc init*(T: type Eth1Monitor,
|
||||||
|
@ -1261,4 +1259,3 @@ when hasGenesisDetection:
|
||||||
else:
|
else:
|
||||||
doAssert bnStatus == BeaconNodeStatus.Stopping
|
doAssert bnStatus == BeaconNodeStatus.Stopping
|
||||||
return new BeaconStateRef # cannot return nil...
|
return new BeaconStateRef # cannot return nil...
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import
|
||||||
std/[os, strutils],
|
std/[os, strutils],
|
||||||
chronicles, stew/shims/net, stew/results, bearssl,
|
chronicles, stew/shims/net, stew/results, bearssl,
|
||||||
eth/keys, eth/p2p/discoveryv5/[enr, protocol, node],
|
eth/keys, eth/p2p/discoveryv5/[enr, protocol, node],
|
||||||
conf
|
./conf
|
||||||
|
|
||||||
export protocol, keys
|
export protocol, keys
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ proc loadBootstrapFile*(bootstrapFile: string,
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
proc new*(T: type Eth2DiscoveryProtocol,
|
proc new*(T: type Eth2DiscoveryProtocol,
|
||||||
conf: BeaconNodeConf,
|
config: BeaconNodeConf,
|
||||||
ip: Option[ValidIpAddress], tcpPort, udpPort: Port,
|
ip: Option[ValidIpAddress], tcpPort, udpPort: Port,
|
||||||
pk: PrivateKey,
|
pk: PrivateKey,
|
||||||
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
|
enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext):
|
||||||
|
@ -84,14 +84,14 @@ proc new*(T: type Eth2DiscoveryProtocol,
|
||||||
# * for setting up a specific key
|
# * for setting up a specific key
|
||||||
# * for using a persistent database
|
# * for using a persistent database
|
||||||
var bootstrapEnrs: seq[enr.Record]
|
var bootstrapEnrs: seq[enr.Record]
|
||||||
for node in conf.bootstrapNodes:
|
for node in config.bootstrapNodes:
|
||||||
addBootstrapNode(node, bootstrapEnrs)
|
addBootstrapNode(node, bootstrapEnrs)
|
||||||
loadBootstrapFile(string conf.bootstrapNodesFile, bootstrapEnrs)
|
loadBootstrapFile(string config.bootstrapNodesFile, bootstrapEnrs)
|
||||||
|
|
||||||
let persistentBootstrapFile = conf.dataDir / "bootstrap_nodes.txt"
|
let persistentBootstrapFile = config.dataDir / "bootstrap_nodes.txt"
|
||||||
if fileExists(persistentBootstrapFile):
|
if fileExists(persistentBootstrapFile):
|
||||||
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs)
|
||||||
|
|
||||||
newProtocol(pk, ip, tcpPort, udpPort, enrFields, bootstrapEnrs,
|
newProtocol(pk, ip, tcpPort, udpPort, enrFields, bootstrapEnrs,
|
||||||
bindIp = conf.listenAddress, enrAutoUpdate = conf.enrAutoUpdate,
|
bindIp = config.listenAddress, enrAutoUpdate = config.enrAutoUpdate,
|
||||||
rng = rng)
|
rng = rng)
|
||||||
|
|
|
@ -21,11 +21,10 @@ import
|
||||||
libp2p/stream/connection,
|
libp2p/stream/connection,
|
||||||
eth/[keys, async_utils], eth/p2p/p2p_protocol_dsl,
|
eth/[keys, async_utils], eth/p2p/p2p_protocol_dsl,
|
||||||
eth/net/nat, eth/p2p/discoveryv5/[enr, node],
|
eth/net/nat, eth/p2p/discoveryv5/[enr, node],
|
||||||
# Beacon node modules
|
"."/[
|
||||||
version, conf, eth2_discovery, libp2p_json_serialization, conf,
|
version, conf, eth2_discovery, libp2p_json_serialization,
|
||||||
ssz/ssz_serialization,
|
ssz/ssz_serialization, peer_pool, time, keystore_management],
|
||||||
peer_pool, spec/[datatypes, digest, helpers, network], ./time,
|
./spec/[datatypes, digest, helpers, network]
|
||||||
keystore_management
|
|
||||||
|
|
||||||
import libp2p/protocols/pubsub/gossipsub
|
import libp2p/protocols/pubsub/gossipsub
|
||||||
|
|
||||||
|
@ -600,10 +599,11 @@ proc performProtocolHandshakes*(peer: Peer, incoming: bool) {.async.} =
|
||||||
proc initProtocol(name: string,
|
proc initProtocol(name: string,
|
||||||
peerInit: PeerStateInitializer,
|
peerInit: PeerStateInitializer,
|
||||||
networkInit: NetworkStateInitializer): ProtocolInfoObj =
|
networkInit: NetworkStateInitializer): ProtocolInfoObj =
|
||||||
result.name = name
|
ProtocolInfoObj(
|
||||||
result.messages = @[]
|
name: name,
|
||||||
result.peerStateInitializer = peerInit
|
messages: @[],
|
||||||
result.networkStateInitializer = networkInit
|
peerStateInitializer: peerInit,
|
||||||
|
networkStateInitializer: networkInit)
|
||||||
|
|
||||||
proc registerProtocol(protocol: ProtocolInfo) =
|
proc registerProtocol(protocol: ProtocolInfo) =
|
||||||
# TODO: This can be done at compile-time in the future
|
# TODO: This can be done at compile-time in the future
|
||||||
|
@ -913,8 +913,8 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
|
||||||
# when no peers are in the routing table. Don't run it in continuous loop.
|
# when no peers are in the routing table. Don't run it in continuous loop.
|
||||||
await sleepAsync(1.seconds)
|
await sleepAsync(1.seconds)
|
||||||
|
|
||||||
proc getPersistentNetMetadata*(conf: BeaconNodeConf): Eth2Metadata =
|
proc getPersistentNetMetadata*(config: BeaconNodeConf): Eth2Metadata =
|
||||||
let metadataPath = conf.dataDir / nodeMetadataFilename
|
let metadataPath = config.dataDir / nodeMetadataFilename
|
||||||
if not fileExists(metadataPath):
|
if not fileExists(metadataPath):
|
||||||
result = Eth2Metadata()
|
result = Eth2Metadata()
|
||||||
for i in 0 ..< ATTESTATION_SUBNET_COUNT:
|
for i in 0 ..< ATTESTATION_SUBNET_COUNT:
|
||||||
|
@ -1058,53 +1058,60 @@ proc onConnEvent(node: Eth2Node, peerId: PeerID, event: ConnEvent) {.async.} =
|
||||||
peer = peerId, peer_state = peer.connectionState
|
peer = peerId, peer_state = peer.connectionState
|
||||||
peer.connectionState = Disconnected
|
peer.connectionState = Disconnected
|
||||||
|
|
||||||
proc init*(T: type Eth2Node, conf: BeaconNodeConf, enrForkId: ENRForkID,
|
proc new*(T: type Eth2Node, config: BeaconNodeConf, enrForkId: ENRForkID,
|
||||||
switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
|
switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress],
|
||||||
tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool,
|
tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool,
|
||||||
rng: ref BrHmacDrbgContext): T =
|
rng: ref BrHmacDrbgContext): T =
|
||||||
new result
|
let
|
||||||
result.switch = switch
|
metadata = getPersistentNetMetadata(config)
|
||||||
result.pubsub = pubsub
|
|
||||||
result.wantedPeers = conf.maxPeers
|
|
||||||
result.peerPool = newPeerPool[Peer, PeerID](maxPeers = conf.maxPeers)
|
|
||||||
when not defined(local_testnet):
|
when not defined(local_testnet):
|
||||||
result.connectTimeout = 1.minutes
|
let
|
||||||
result.seenThreshold = 5.minutes
|
connectTimeout = 1.minutes
|
||||||
|
seenThreshold = 5.minutes
|
||||||
else:
|
else:
|
||||||
result.connectTimeout = 10.seconds
|
let
|
||||||
result.seenThreshold = 10.seconds
|
connectTimeout = 10.seconds
|
||||||
result.seenTable = initTable[PeerID, SeenItem]()
|
seenThreshold = 10.seconds
|
||||||
result.connTable = initHashSet[PeerID]()
|
|
||||||
# Its important here to create AsyncQueue with limited size, otherwise
|
|
||||||
# it could produce HIGH cpu usage.
|
|
||||||
result.connQueue = newAsyncQueue[PeerAddr](ConcurrentConnections)
|
|
||||||
# TODO: The persistent net metadata should only be used in the case of reusing
|
|
||||||
# the previous netkey.
|
|
||||||
result.metadata = getPersistentNetMetadata(conf)
|
|
||||||
result.forkId = enrForkId
|
|
||||||
result.discovery = Eth2DiscoveryProtocol.new(
|
|
||||||
conf, ip, tcpPort, udpPort, privKey,
|
|
||||||
{"eth2": SSZ.encode(result.forkId), "attnets": SSZ.encode(result.metadata.attnets)},
|
|
||||||
rng)
|
|
||||||
result.discoveryEnabled = discovery
|
|
||||||
result.rng = rng
|
|
||||||
|
|
||||||
newSeq result.protocolStates, allProtocols.len
|
let node = T(
|
||||||
|
switch: switch,
|
||||||
|
pubsub: pubsub,
|
||||||
|
wantedPeers: config.maxPeers,
|
||||||
|
peerPool: newPeerPool[Peer, PeerID](maxPeers = config.maxPeers),
|
||||||
|
# Its important here to create AsyncQueue with limited size, otherwise
|
||||||
|
# it could produce HIGH cpu usage.
|
||||||
|
connQueue: newAsyncQueue[PeerAddr](ConcurrentConnections),
|
||||||
|
# TODO: The persistent net metadata should only be used in the case of reusing
|
||||||
|
# the previous netkey.
|
||||||
|
metadata: metadata,
|
||||||
|
forkId: enrForkId,
|
||||||
|
discovery: Eth2DiscoveryProtocol.new(
|
||||||
|
config, ip, tcpPort, udpPort, privKey,
|
||||||
|
{"eth2": SSZ.encode(enrForkId), "attnets": SSZ.encode(metadata.attnets)},
|
||||||
|
rng),
|
||||||
|
discoveryEnabled: discovery,
|
||||||
|
rng: rng,
|
||||||
|
connectTimeout: connectTimeout,
|
||||||
|
seenThreshold: seenThreshold,
|
||||||
|
)
|
||||||
|
|
||||||
|
newSeq node.protocolStates, allProtocols.len
|
||||||
for proto in allProtocols:
|
for proto in allProtocols:
|
||||||
if proto.networkStateInitializer != nil:
|
if proto.networkStateInitializer != nil:
|
||||||
result.protocolStates[proto.index] = proto.networkStateInitializer(result)
|
node.protocolStates[proto.index] = proto.networkStateInitializer(node)
|
||||||
|
|
||||||
for msg in proto.messages:
|
for msg in proto.messages:
|
||||||
if msg.protocolMounter != nil:
|
if msg.protocolMounter != nil:
|
||||||
msg.protocolMounter result
|
msg.protocolMounter node
|
||||||
|
|
||||||
let node = result
|
|
||||||
proc peerHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
|
proc peerHook(peerId: PeerID, event: ConnEvent): Future[void] {.gcsafe.} =
|
||||||
onConnEvent(node, peerId, event)
|
onConnEvent(node, peerId, event)
|
||||||
|
|
||||||
switch.addConnEventHandler(peerHook, ConnEventKind.Connected)
|
switch.addConnEventHandler(peerHook, ConnEventKind.Connected)
|
||||||
switch.addConnEventHandler(peerHook, ConnEventKind.Disconnected)
|
switch.addConnEventHandler(peerHook, ConnEventKind.Disconnected)
|
||||||
|
|
||||||
|
node
|
||||||
|
|
||||||
template publicKey*(node: Eth2Node): keys.PublicKey =
|
template publicKey*(node: Eth2Node): keys.PublicKey =
|
||||||
node.discovery.privKey.toPublicKey
|
node.discovery.privKey.toPublicKey
|
||||||
|
|
||||||
|
@ -1306,15 +1313,15 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend =
|
||||||
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
|
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
|
||||||
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
|
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
|
||||||
|
|
||||||
proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[ValidIpAddress],
|
proc setupNat(config: BeaconNodeConf): tuple[ip: Option[ValidIpAddress],
|
||||||
tcpPort: Port,
|
tcpPort: Port,
|
||||||
udpPort: Port] =
|
udpPort: Port] =
|
||||||
# defaults
|
# defaults
|
||||||
result.tcpPort = conf.tcpPort
|
result.tcpPort = config.tcpPort
|
||||||
result.udpPort = conf.udpPort
|
result.udpPort = config.udpPort
|
||||||
|
|
||||||
var nat: NatStrategy
|
var nat: NatStrategy
|
||||||
case conf.nat.toLowerAscii:
|
case config.nat.toLowerAscii:
|
||||||
of "any":
|
of "any":
|
||||||
nat = NatAny
|
nat = NatAny
|
||||||
of "none":
|
of "none":
|
||||||
|
@ -1324,16 +1331,16 @@ proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[ValidIpAddress],
|
||||||
of "pmp":
|
of "pmp":
|
||||||
nat = NatPmp
|
nat = NatPmp
|
||||||
else:
|
else:
|
||||||
if conf.nat.startsWith("extip:"):
|
if config.nat.startsWith("extip:"):
|
||||||
try:
|
try:
|
||||||
# any required port redirection is assumed to be done by hand
|
# any required port redirection is assumed to be done by hand
|
||||||
result.ip = some(ValidIpAddress.init(conf.nat[6..^1]))
|
result.ip = some(ValidIpAddress.init(config.nat[6..^1]))
|
||||||
nat = NatNone
|
nat = NatNone
|
||||||
except ValueError:
|
except ValueError:
|
||||||
error "nor a valid IP address", address = conf.nat[6..^1]
|
error "nor a valid IP address", address = config.nat[6..^1]
|
||||||
quit QuitFailure
|
quit QuitFailure
|
||||||
else:
|
else:
|
||||||
error "not a valid NAT mechanism", value = conf.nat
|
error "not a valid NAT mechanism", value = config.nat
|
||||||
quit QuitFailure
|
quit QuitFailure
|
||||||
|
|
||||||
if nat != NatNone:
|
if nat != NatNone:
|
||||||
|
@ -1367,10 +1374,10 @@ template tcpEndPoint(address, port): auto =
|
||||||
MultiAddress.init(address, tcpProtocol, port)
|
MultiAddress.init(address, tcpProtocol, port)
|
||||||
|
|
||||||
proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
||||||
conf: BeaconNodeConf): KeyPair =
|
config: BeaconNodeConf): KeyPair =
|
||||||
case conf.cmd
|
case config.cmd
|
||||||
of noCommand, record:
|
of noCommand, record:
|
||||||
if conf.netKeyFile == "random":
|
if config.netKeyFile == "random":
|
||||||
let res = PrivateKey.random(Secp256k1, rng)
|
let res = PrivateKey.random(Secp256k1, rng)
|
||||||
if res.isErr():
|
if res.isErr():
|
||||||
fatal "Could not generate random network key file"
|
fatal "Could not generate random network key file"
|
||||||
|
@ -1386,17 +1393,17 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
||||||
return KeyPair(seckey: privKey, pubkey: privKey.getKey().tryGet())
|
return KeyPair(seckey: privKey, pubkey: privKey.getKey().tryGet())
|
||||||
else:
|
else:
|
||||||
let keyPath =
|
let keyPath =
|
||||||
if isAbsolute(conf.netKeyFile):
|
if isAbsolute(config.netKeyFile):
|
||||||
conf.netKeyFile
|
config.netKeyFile
|
||||||
else:
|
else:
|
||||||
conf.dataDir / conf.netKeyFile
|
config.dataDir / config.netKeyFile
|
||||||
|
|
||||||
if fileAccessible(keyPath, {AccessFlags.Find}):
|
if fileAccessible(keyPath, {AccessFlags.Find}):
|
||||||
info "Network key storage is present, unlocking", key_path = keyPath
|
info "Network key storage is present, unlocking", key_path = keyPath
|
||||||
|
|
||||||
# Insecure password used only for automated testing.
|
# Insecure password used only for automated testing.
|
||||||
let insecurePassword =
|
let insecurePassword =
|
||||||
if conf.netKeyInsecurePassword:
|
if config.netKeyInsecurePassword:
|
||||||
some(NetworkInsecureKeyPassword)
|
some(NetworkInsecureKeyPassword)
|
||||||
else:
|
else:
|
||||||
none[string]()
|
none[string]()
|
||||||
|
@ -1423,7 +1430,7 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
||||||
|
|
||||||
# Insecure password used only for automated testing.
|
# Insecure password used only for automated testing.
|
||||||
let insecurePassword =
|
let insecurePassword =
|
||||||
if conf.netKeyInsecurePassword:
|
if config.netKeyInsecurePassword:
|
||||||
some(NetworkInsecureKeyPassword)
|
some(NetworkInsecureKeyPassword)
|
||||||
else:
|
else:
|
||||||
none[string]()
|
none[string]()
|
||||||
|
@ -1438,15 +1445,15 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
||||||
return KeyPair(seckey: privKey, pubkey: pubKey)
|
return KeyPair(seckey: privKey, pubkey: pubKey)
|
||||||
|
|
||||||
of createTestnet:
|
of createTestnet:
|
||||||
if conf.netKeyFile == "random":
|
if config.netKeyFile == "random":
|
||||||
fatal "Could not create testnet using `random` network key"
|
fatal "Could not create testnet using `random` network key"
|
||||||
quit QuitFailure
|
quit QuitFailure
|
||||||
|
|
||||||
let keyPath =
|
let keyPath =
|
||||||
if isAbsolute(conf.netKeyFile):
|
if isAbsolute(config.netKeyFile):
|
||||||
conf.netKeyFile
|
config.netKeyFile
|
||||||
else:
|
else:
|
||||||
conf.dataDir / conf.netKeyFile
|
config.dataDir / config.netKeyFile
|
||||||
|
|
||||||
let rres = PrivateKey.random(Secp256k1, rng)
|
let rres = PrivateKey.random(Secp256k1, rng)
|
||||||
if rres.isErr():
|
if rres.isErr():
|
||||||
|
@ -1458,7 +1465,7 @@ proc getPersistentNetKeys*(rng: var BrHmacDrbgContext,
|
||||||
|
|
||||||
# Insecure password used only for automated testing.
|
# Insecure password used only for automated testing.
|
||||||
let insecurePassword =
|
let insecurePassword =
|
||||||
if conf.netKeyInsecurePassword:
|
if config.netKeyInsecurePassword:
|
||||||
some(NetworkInsecureKeyPassword)
|
some(NetworkInsecureKeyPassword)
|
||||||
else:
|
else:
|
||||||
none[string]()
|
none[string]()
|
||||||
|
@ -1501,7 +1508,7 @@ func msgIdProvider(m: messages.Message): seq[byte] =
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
gossipId(m.data, false)
|
gossipId(m.data, false)
|
||||||
|
|
||||||
proc newBeaconSwitch*(conf: BeaconNodeConf, seckey: PrivateKey,
|
proc newBeaconSwitch*(config: BeaconNodeConf, seckey: PrivateKey,
|
||||||
address: MultiAddress,
|
address: MultiAddress,
|
||||||
rng: ref BrHmacDrbgContext): Switch =
|
rng: ref BrHmacDrbgContext): Switch =
|
||||||
proc createMplex(conn: Connection): Muxer =
|
proc createMplex(conn: Connection): Muxer =
|
||||||
|
@ -1514,7 +1521,7 @@ proc newBeaconSwitch*(conf: BeaconNodeConf, seckey: PrivateKey,
|
||||||
muxers = {MplexCodec: mplexProvider}.toTable
|
muxers = {MplexCodec: mplexProvider}.toTable
|
||||||
secureManagers = [Secure(newNoise(rng, seckey))]
|
secureManagers = [Secure(newNoise(rng, seckey))]
|
||||||
|
|
||||||
peerInfo.agentVersion = conf.agentString
|
peerInfo.agentVersion = config.agentString
|
||||||
|
|
||||||
let identify = newIdentify(peerInfo)
|
let identify = newIdentify(peerInfo)
|
||||||
|
|
||||||
|
@ -1524,15 +1531,15 @@ proc newBeaconSwitch*(conf: BeaconNodeConf, seckey: PrivateKey,
|
||||||
identify,
|
identify,
|
||||||
muxers,
|
muxers,
|
||||||
secureManagers,
|
secureManagers,
|
||||||
maxConnections = conf.maxPeers)
|
maxConnections = config.maxPeers)
|
||||||
|
|
||||||
proc createEth2Node*(rng: ref BrHmacDrbgContext,
|
proc createEth2Node*(rng: ref BrHmacDrbgContext,
|
||||||
conf: BeaconNodeConf,
|
config: BeaconNodeConf,
|
||||||
netKeys: KeyPair,
|
netKeys: KeyPair,
|
||||||
enrForkId: ENRForkID): Eth2Node =
|
enrForkId: ENRForkID): Eth2Node =
|
||||||
var
|
var
|
||||||
(extIp, extTcpPort, extUdpPort) = setupNat(conf)
|
(extIp, extTcpPort, extUdpPort) = setupNat(config)
|
||||||
hostAddress = tcpEndPoint(conf.listenAddress, conf.tcpPort)
|
hostAddress = tcpEndPoint(config.listenAddress, config.tcpPort)
|
||||||
announcedAddresses = if extIp.isNone(): @[]
|
announcedAddresses = if extIp.isNone(): @[]
|
||||||
else: @[tcpEndPoint(extIp.get(), extTcpPort)]
|
else: @[tcpEndPoint(extIp.get(), extTcpPort)]
|
||||||
|
|
||||||
|
@ -1543,7 +1550,7 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
|
||||||
# TODO nim-libp2p still doesn't have support for announcing addresses
|
# TODO nim-libp2p still doesn't have support for announcing addresses
|
||||||
# that are different from the host address (this is relevant when we
|
# that are different from the host address (this is relevant when we
|
||||||
# are running behind a NAT).
|
# are running behind a NAT).
|
||||||
var switch = newBeaconSwitch(conf, netKeys.seckey, hostAddress, rng)
|
var switch = newBeaconSwitch(config, netKeys.seckey, hostAddress, rng)
|
||||||
|
|
||||||
let
|
let
|
||||||
params = GossipSubParams(
|
params = GossipSubParams(
|
||||||
|
@ -1587,11 +1594,11 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
|
||||||
|
|
||||||
switch.mount(pubsub)
|
switch.mount(pubsub)
|
||||||
|
|
||||||
result = Eth2Node.init(conf, enrForkId, switch, pubsub,
|
Eth2Node.new(config, enrForkId, switch, pubsub,
|
||||||
extIp, extTcpPort, extUdpPort,
|
extIp, extTcpPort, extUdpPort,
|
||||||
netKeys.seckey.asEthKey,
|
netKeys.seckey.asEthKey,
|
||||||
discovery = conf.discv5Enabled,
|
discovery = config.discv5Enabled,
|
||||||
rng = rng)
|
rng = rng)
|
||||||
|
|
||||||
proc announcedENR*(node: Eth2Node): enr.Record =
|
proc announcedENR*(node: Eth2Node): enr.Record =
|
||||||
doAssert node.discovery != nil, "The Eth2Node must be initialized"
|
doAssert node.discovery != nil, "The Eth2Node must be initialized"
|
||||||
|
|
|
@ -5,7 +5,7 @@ import
|
||||||
spec/[datatypes, digest, crypto, keystore],
|
spec/[datatypes, digest, crypto, keystore],
|
||||||
stew/io2, libp2p/crypto/crypto as lcrypto,
|
stew/io2, libp2p/crypto/crypto as lcrypto,
|
||||||
nimcrypto/utils as ncrutils,
|
nimcrypto/utils as ncrutils,
|
||||||
conf, ssz/merkleization, network_metadata, filepath
|
"."/[conf, ssz/merkleization, network_metadata, filepath]
|
||||||
|
|
||||||
export
|
export
|
||||||
keystore
|
keystore
|
||||||
|
@ -280,13 +280,13 @@ iterator validatorKeysFromDirs*(validatorsDir, secretsDir: string): ValidatorPri
|
||||||
except OSError:
|
except OSError:
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
iterator validatorKeys*(conf: BeaconNodeConf|ValidatorClientConf): ValidatorPrivKey =
|
iterator validatorKeys*(config: BeaconNodeConf|ValidatorClientConf): ValidatorPrivKey =
|
||||||
let validatorsDir = conf.validatorsDir
|
let validatorsDir = config.validatorsDir
|
||||||
try:
|
try:
|
||||||
for kind, file in walkDir(validatorsDir):
|
for kind, file in walkDir(validatorsDir):
|
||||||
if kind == pcDir:
|
if kind == pcDir:
|
||||||
let keyName = splitFile(file).name
|
let keyName = splitFile(file).name
|
||||||
let key = loadKeystore(validatorsDir, conf.secretsDir, keyName, conf.nonInteractive)
|
let key = loadKeystore(validatorsDir, config.secretsDir, keyName, config.nonInteractive)
|
||||||
if key.isSome:
|
if key.isSome:
|
||||||
yield key.get
|
yield key.get
|
||||||
else:
|
else:
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -64,7 +64,7 @@ proc addLocalValidator*(node: BeaconNode,
|
||||||
state: BeaconState,
|
state: BeaconState,
|
||||||
privKey: ValidatorPrivKey) =
|
privKey: ValidatorPrivKey) =
|
||||||
let pubKey = privKey.toPubKey()
|
let pubKey = privKey.toPubKey()
|
||||||
node.attachedValidators.addLocalValidator(
|
node.attachedValidators[].addLocalValidator(
|
||||||
pubKey, privKey, findValidator(state, pubKey))
|
pubKey, privKey, findValidator(state, pubKey))
|
||||||
|
|
||||||
proc addLocalValidators*(node: BeaconNode) =
|
proc addLocalValidators*(node: BeaconNode) =
|
||||||
|
@ -87,11 +87,11 @@ proc addRemoteValidators*(node: BeaconNode) =
|
||||||
inStream: node.vcProcess.inputStream,
|
inStream: node.vcProcess.inputStream,
|
||||||
outStream: node.vcProcess.outputStream,
|
outStream: node.vcProcess.outputStream,
|
||||||
pubKeyStr: $key))
|
pubKeyStr: $key))
|
||||||
node.attachedValidators.addRemoteValidator(key, v)
|
node.attachedValidators[].addRemoteValidator(key, v)
|
||||||
|
|
||||||
proc getAttachedValidator*(node: BeaconNode,
|
proc getAttachedValidator*(node: BeaconNode,
|
||||||
pubkey: ValidatorPubKey): AttachedValidator =
|
pubkey: ValidatorPubKey): AttachedValidator =
|
||||||
node.attachedValidators.getValidator(pubkey)
|
node.attachedValidators[].getValidator(pubkey)
|
||||||
|
|
||||||
proc getAttachedValidator*(node: BeaconNode,
|
proc getAttachedValidator*(node: BeaconNode,
|
||||||
state: BeaconState,
|
state: BeaconState,
|
||||||
|
@ -464,7 +464,7 @@ proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
|
||||||
return head
|
return head
|
||||||
|
|
||||||
let validator =
|
let validator =
|
||||||
node.attachedValidators.getValidator(proposer.get()[1])
|
node.attachedValidators[].getValidator(proposer.get()[1])
|
||||||
|
|
||||||
if validator != nil:
|
if validator != nil:
|
||||||
return await proposeBlock(node, validator, proposer.get()[0], head, slot)
|
return await proposeBlock(node, validator, proposer.get()[0], head, slot)
|
||||||
|
@ -569,7 +569,7 @@ proc updateValidatorMetrics*(node: BeaconNode) =
|
||||||
|
|
||||||
var total: Gwei
|
var total: Gwei
|
||||||
var i = 0
|
var i = 0
|
||||||
for _, v in node.attachedValidators.validators:
|
for _, v in node.attachedValidators[].validators:
|
||||||
let balance =
|
let balance =
|
||||||
if v.index.isNone():
|
if v.index.isNone():
|
||||||
0.Gwei
|
0.Gwei
|
||||||
|
@ -597,7 +597,7 @@ proc updateValidatorMetrics*(node: BeaconNode) =
|
||||||
|
|
||||||
proc handleValidatorDuties*(node: BeaconNode, lastSlot, slot: Slot) {.async.} =
|
proc handleValidatorDuties*(node: BeaconNode, lastSlot, slot: Slot) {.async.} =
|
||||||
## Perform validator duties - create blocks, vote and aggregate existing votes
|
## Perform validator duties - create blocks, vote and aggregate existing votes
|
||||||
if node.attachedValidators.count == 0:
|
if node.attachedValidators[].count == 0:
|
||||||
# Nothing to do because we have no validator attached
|
# Nothing to do because we have no validator attached
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue