From c773e10c1a085a2c60915ee4f8c05ec4489bbbbc Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 10 Jun 2020 22:36:54 +0300 Subject: [PATCH] Attempt to reduce the risk of dropped network connections during the loading of KeyStores --- beacon_chain/beacon_node.nim | 22 +++++++++++++--------- beacon_chain/eth2_network.nim | 10 ++++++---- beacon_chain/validator_duties.nim | 6 ------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 45d095de5..c55a75738 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -254,9 +254,6 @@ proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async proc connectToNetwork(node: BeaconNode) {.async.} = await node.network.connectToNetwork() - let addressFile = node.config.dataDir / "beacon_node.address" - writeFile(addressFile, node.network.announcedENR.toURI) - proc onAttestation(node: BeaconNode, attestation: Attestation) = # We received an attestation from the network but don't know much about it # yet - in particular, we haven't verified that it belongs to particular chain @@ -538,7 +535,7 @@ proc runForwardSyncLoop(node: BeaconNode) {.async.} = result = node.blockPool.head.blck.slot proc getLocalWallSlot(): Slot {.gcsafe.} = - let epoch = node.beaconClock.now().toSlot().slot.compute_epoch_at_slot() + + let epoch = node.beaconClock.now().slotOrZero.compute_epoch_at_slot() + 1'u64 result = epoch.compute_start_slot_at_epoch() @@ -815,8 +812,6 @@ proc start(node: BeaconNode) = # actually need to make this part of normal application flow - # losing all connections might happen at any time and we should be # prepared to handle it. - waitFor node.connectToNetwork() - let head = node.blockPool.head finalizedHead = node.blockPool.finalizedHead @@ -837,12 +832,21 @@ proc start(node: BeaconNode) = cat = "init", pcs = "start_beacon_node" - let - bs = BlockSlot(blck: head.blck, slot: head.blck.slot) + node.network.startListening() + let addressFile = node.config.dataDir / "beacon_node.address" + writeFile(addressFile, node.network.announcedENR.toURI) + + let bs = BlockSlot(blck: head.blck, slot: head.blck.slot) node.blockPool.withState(node.blockPool.tmpState, bs): - node.addLocalValidators(state) + for validatorKey in node.config.validatorKeys: + node.addLocalValidator state, validatorKey + # Allow some network events to be processed: + waitFor sleepAsync(1) + info "Local validators attached ", count = node.attachedValidators.count + + waitFor node.network.connectToNetwork() node.run() func formatGwei(amount: uint64): string = diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index cef29e9ee..3a0dfdf47 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -821,17 +821,19 @@ proc init*(T: type Eth2Node, conf: BeaconNodeConf, enrForkId: ENRForkID, if msg.protocolMounter != nil: msg.protocolMounter result - for i in 0 ..< ConcurrentConnections: - result.connWorkers.add(connectWorker(result)) - template publicKey*(node: Eth2Node): keys.PublicKey = node.discovery.privKey.toPublicKey.tryGet() template addKnownPeer*(node: Eth2Node, peer: enr.Record) = node.discovery.addNode peer -proc start*(node: Eth2Node) {.async.} = +proc startListening*(node: Eth2Node) = node.discovery.open() + +proc start*(node: Eth2Node) {.async.} = + for i in 0 ..< ConcurrentConnections: + node.connWorkers.add connectWorker(node) + node.discovery.start() node.libp2pTransportLoops = await node.switch.start() node.discoveryLoop = node.runDiscoveryLoop() diff --git a/beacon_chain/validator_duties.nim b/beacon_chain/validator_duties.nim index 9adbf2198..f34674010 100644 --- a/beacon_chain/validator_duties.nim +++ b/beacon_chain/validator_duties.nim @@ -53,12 +53,6 @@ proc addLocalValidator*(node: BeaconNode, node.attachedValidators.addLocalValidator(pubKey, privKey) -proc addLocalValidators*(node: BeaconNode, state: BeaconState) = - for validatorKey in node.config.validatorKeys: - node.addLocalValidator state, validatorKey - - info "Local validators attached ", count = node.attachedValidators.count - func getAttachedValidator*(node: BeaconNode, state: BeaconState, idx: ValidatorIndex): AttachedValidator =