Adjustments to store bootnode enrs in discovery object + bump nim-eth

This commit is contained in:
kdeme 2020-03-24 10:54:17 +01:00 committed by zah
parent f2434139e9
commit b80a5b90df
4 changed files with 33 additions and 45 deletions

View File

@ -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

View File

@ -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)

View File

@ -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.

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 9c442bf65b52a4c857cc6e51efe901352e8b6ebf
Subproject commit c3f23e5912efff98fc6c8181db579037e5a19a2c