From b80a5b90dfa70e9ad7a0057b1278298eb3ba69af Mon Sep 17 00:00:00 2001 From: kdeme Date: Tue, 24 Mar 2020 10:54:17 +0100 Subject: [PATCH] Adjustments to store bootnode enrs in discovery object + bump nim-eth --- beacon_chain/beacon_node.nim | 23 ++------------------ beacon_chain/eth2_discovery.nim | 38 +++++++++++++++++++++------------ beacon_chain/eth2_network.nim | 15 ++++++------- vendor/nim-eth | 2 +- 4 files changed, 33 insertions(+), 45 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 440d40932..e8382ccee 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -56,8 +56,6 @@ type forkVersion: array[4, byte] netKeys: KeyPair requestManager: RequestManager - bootstrapNodes: seq[ENode] - bootstrapEnrs: seq[enr.Record] db: BeaconChainDB config: BeaconNodeConf attachedValidators: ValidatorPool @@ -188,16 +186,6 @@ proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async # monitor mainchainMonitor.start() - var bootNodes: seq[ENode] - var bootEnrs: seq[enr.Record] - for node in conf.bootstrapNodes: - addBootstrapNode(node, bootNodes, bootEnrs, ourPubKey) - loadBootstrapFile(string conf.bootstrapNodesFile, bootNodes, bootEnrs, ourPubKey) - - let persistentBootstrapFile = conf.dataDir / "bootstrap_nodes.txt" - if fileExists(persistentBootstrapFile): - loadBootstrapFile(persistentBootstrapFile, bootNodes, bootEnrs, ourPubKey) - let network = await createEth2Node(conf) let rpcServer = if conf.rpcEnabled: @@ -211,8 +199,6 @@ proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async forkVersion: blockPool.headState.data.data.fork.current_version, netKeys: netKeys, requestManager: RequestManager.init(network), - bootstrapNodes: bootNodes, - bootstrapEnrs: bootEnrs, db: db, config: conf, attachedValidators: ValidatorPool.init(), @@ -250,12 +236,7 @@ proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async return res proc connectToNetwork(node: BeaconNode) {.async.} = - if node.bootstrapEnrs.len > 0: - info "Connecting to bootstrap nodes", bootstrapEnrs = node.bootstrapEnrs - else: - info "Waiting for connections" - - await node.network.connectToNetwork(node.bootstrapEnrs) + await node.network.connectToNetwork() let addressFile = node.config.dataDir / "beacon_node.address" writeFile(addressFile, node.network.announcedENR.toURI) @@ -1221,7 +1202,7 @@ when isMainModule: bootstrapEnr = enr.Record.init( 1, # sequence number networkKeys.seckey.asEthKey, - bootstrapAddress) + some(bootstrapAddress)) writeFile(bootstrapFile, bootstrapEnr.toURI) echo "Wrote ", bootstrapFile diff --git a/beacon_chain/eth2_discovery.nim b/beacon_chain/eth2_discovery.nim index b228aaad7..427eb1f3a 100644 --- a/beacon_chain/eth2_discovery.nim +++ b/beacon_chain/eth2_discovery.nim @@ -12,20 +12,7 @@ type PublicKey = keys.PublicKey export - Eth2DiscoveryProtocol, open, close, result - -proc new*(T: type Eth2DiscoveryProtocol, - conf: BeaconNodeConf, - ip: IpAddress, rawPrivKeyBytes: openarray[byte]): T = - # TODO - # Implement more configuration options: - # * for setting up a specific key - # * for using a persistent database - var - pk = initPrivateKey(rawPrivKeyBytes) - db = DiscoveryDB.init(newMemoryDB()) - - newProtocol(pk, db, ip, conf.tcpPort, conf.udpPort) + Eth2DiscoveryProtocol, open, start, close, result proc toENode*(a: MultiAddress): Result[ENode, cstring] = if not IPFS.match(a): @@ -165,3 +152,26 @@ proc loadBootstrapFile*(bootstrapFile: string, error "Unknown bootstrap file format", ext quit 1 +proc new*(T: type Eth2DiscoveryProtocol, + conf: BeaconNodeConf, + ip: IpAddress, rawPrivKeyBytes: openarray[byte]): T = + # TODO + # Implement more configuration options: + # * for setting up a specific key + # * for using a persistent database + var + pk = initPrivateKey(rawPrivKeyBytes) + ourPubKey = pk.getPublicKey() + db = DiscoveryDB.init(newMemoryDB()) + + var bootNodes: seq[ENode] + var bootEnrs: seq[enr.Record] + for node in conf.bootstrapNodes: + addBootstrapNode(node, bootNodes, bootEnrs, ourPubKey) + loadBootstrapFile(string conf.bootstrapNodesFile, bootNodes, bootEnrs, ourPubKey) + + let persistentBootstrapFile = conf.dataDir / "bootstrap_nodes.txt" + if fileExists(persistentBootstrapFile): + loadBootstrapFile(persistentBootstrapFile, bootNodes, bootEnrs, ourPubKey) + + newProtocol(pk, db, ip, conf.tcpPort, conf.udpPort, bootEnrs) diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 9fff9afb0..665a5adb1 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -702,6 +702,7 @@ template addKnownPeer*(node: Eth2Node, peer: ENode|enr.Record) = proc start*(node: Eth2Node) {.async.} = node.discovery.open() + node.discovery.start() node.libp2pTransportLoops = await node.switch.start() traceAsyncErrors node.runDiscoveryLoop() @@ -918,7 +919,7 @@ proc getPersistenBootstrapAddr*(conf: BeaconNodeConf, return enr.Record.init(1'u64, # sequence number pair.seckey.asEthKey, - enodeAddress) + some(enodeAddress)) proc announcedENR*(node: Eth2Node): enr.Record = doAssert node.discovery != nil, "The Eth2Node must be initialized" @@ -933,18 +934,14 @@ proc toPeerInfo(enode: ENode): PeerInfo = addresses = @[MultiAddress.init enode.toMultiAddressStr] return PeerInfo.init(peerId, addresses) -proc connectToNetwork*(node: Eth2Node, - bootstrapEnrs: seq[enr.Record]) {.async.} = - for bootstrapNode in bootstrapEnrs: - debug "Adding known peer", peer = bootstrapNode - node.addKnownPeer bootstrapNode - +proc connectToNetwork*(node: Eth2Node) {.async.} = await node.start() proc checkIfConnectedToBootstrapNode {.async.} = await sleepAsync(30.seconds) - if bootstrapEnrs.len > 0 and libp2p_successful_dials.value == 0: - fatal "Failed to connect to any bootstrap node. Quitting", bootstrapEnrs + if node.discovery.bootstrapRecords.len > 0 and libp2p_successful_dials.value == 0: + fatal "Failed to connect to any bootstrap node. Quitting", + bootstrapEnrs = node.discovery.bootstrapRecords quit 1 # TODO: The initial sync forces this to time out. diff --git a/vendor/nim-eth b/vendor/nim-eth index 9c442bf65..c3f23e591 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit 9c442bf65b52a4c857cc6e51efe901352e8b6ebf +Subproject commit c3f23e5912efff98fc6c8181db579037e5a19a2c