Fix getPersistentNetIdentity() do not use daemon anymore.

Fix change PeerInfo to MultiAddress.
This commit is contained in:
cheatfate 2019-11-12 21:56:37 +02:00 committed by zah
parent 2630ffa058
commit 6e8a518077
2 changed files with 47 additions and 40 deletions

View File

@ -134,7 +134,7 @@ proc addBootstrapNode(node: BeaconNode, bootstrapNode: BootstrapAddr) =
proc useBootstrapFile(node: BeaconNode, bootstrapFile: string) = proc useBootstrapFile(node: BeaconNode, bootstrapFile: string) =
for ln in lines(bootstrapFile): for ln in lines(bootstrapFile):
node.addBootstrapNode BootstrapAddr.init(string ln) node.addBootstrapNode BootstrapAddr.initAddress(string ln)
proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async.} = proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async.} =
new result new result
@ -1013,10 +1013,7 @@ when isMainModule:
let bootstrapFile = config.outputBootstrapFile.string let bootstrapFile = config.outputBootstrapFile.string
if bootstrapFile.len > 0: if bootstrapFile.len > 0:
let bootstrapAddrLine = when networkBackend != rlpxBackend: let bootstrapAddrLine = $bootstrapAddress
$bootstrapAddress.addresses[0] & "/p2p/" & bootstrapAddress.peer.pretty
else:
$bootstrapAddress
writeFile(bootstrapFile, bootstrapAddrLine) writeFile(bootstrapFile, bootstrapAddrLine)
echo "Wrote ", bootstrapFile echo "Wrote ", bootstrapFile

View File

@ -123,7 +123,7 @@ when networkBackend == rlpxBackend:
proc saveConnectionAddressFile*(node: Eth2Node, filename: string) = proc saveConnectionAddressFile*(node: Eth2Node, filename: string) =
writeFile(filename, $node.listeningAddress) writeFile(filename, $node.listeningAddress)
proc init*(T: type BootstrapAddr, str: string): T = proc initAddress*(T: type BootstrapAddr, str: string): T =
initENode(str) initENode(str)
func peersCount*(node: Eth2Node): int = func peersCount*(node: Eth2Node): int =
@ -144,7 +144,7 @@ else:
else: else:
import import
libp2p/daemon/daemonapi, libp2p_daemon_backend libp2p/daemon/daemonapi, libp2p/multiaddress, libp2p_daemon_backend
export export
libp2p_daemon_backend libp2p_daemon_backend
@ -154,19 +154,16 @@ else:
networkKeyFilename = "privkey.protobuf" networkKeyFilename = "privkey.protobuf"
type type
BootstrapAddr* = PeerInfo BootstrapAddr* = MultiAddress
Eth2NodeIdentity* = PeerInfo Eth2NodeIdentity* = KeyPair
proc init*(T: type BootstrapAddr, str: string): T = proc initAddress*(T: type BootstrapAddr, str: string): T =
# TODO: The code below is quite awkward. let address = MultiAddress.init(str)
# How do we parse a PeerInfo object out of a bootstrap MultiAddress string such as: if IPFS.match(address) and matchPartial(multiaddress.TCP, address):
# /ip4/10.20.30.40/tcp/9100/p2p/16Uiu2HAmEAmp4FdpPzypKwTMmsbCdnUafDvXZCpFrUDbYJZNk7hX result = address
var parts = str.split("/p2p/")
if parts.len == 2:
result.peer = PeerID.init(parts[1])
result.addresses.add MultiAddress.init(parts[0])
else: else:
raise newException(ValueError, "Invalid bootstrap multi-address") raise newException(MultiAddressError,
"Invalid bootstrap node multi-address")
proc ensureNetworkIdFile(conf: BeaconNodeConf): string = proc ensureNetworkIdFile(conf: BeaconNodeConf): string =
result = conf.dataDir / networkKeyFilename result = conf.dataDir / networkKeyFilename
@ -176,13 +173,17 @@ else:
writeFile(result, pk.getBytes) writeFile(result, pk.getBytes)
proc getPersistentNetIdentity*(conf: BeaconNodeConf): Eth2NodeIdentity = proc getPersistentNetIdentity*(conf: BeaconNodeConf): Eth2NodeIdentity =
# Using waitFor here is reasonable, because this proc is needed only let privateKeyFile = conf.dataDir / networkKeyFilename
# prior to connecting to the network. The RLPx alternative reads from var privKey: PrivateKey
# file and it's much easier to use if it's not async. if not fileExists(privateKeyFile):
# TODO: revisit in the future when we have our own Lib2P2 implementation. createDir conf.dataDir.string
let daemon = waitFor newDaemonApi(id = conf.ensureNetworkIdFile) privKey = PrivateKey.random(Secp256k1)
result = waitFor daemon.identity() writeFile(privateKeyFile, privKey.getBytes())
waitFor daemon.close() else:
let strdata = readFile(privateKeyFile)
privKey = PrivateKey.init(cast[seq[byte]](strdata))
result = KeyPair(seckey: privKey, pubkey: privKey.getKey())
template tcpEndPoint(address, port): auto = template tcpEndPoint(address, port): auto =
MultiAddress.init(address, Protocol.IPPROTO_TCP, port) MultiAddress.init(address, Protocol.IPPROTO_TCP, port)
@ -191,8 +192,7 @@ else:
proc allMultiAddresses(nodes: seq[BootstrapAddr]): seq[string] = proc allMultiAddresses(nodes: seq[BootstrapAddr]): seq[string] =
for node in nodes: for node in nodes:
for a in node.addresses: result.add $node
result.add $a & "/ipfs/" & node.peer.pretty()
proc createEth2Node*(conf: BeaconNodeConf, proc createEth2Node*(conf: BeaconNodeConf,
bootstrapNodes: seq[BootstrapAddr]): Future[Eth2Node] {.async.} = bootstrapNodes: seq[BootstrapAddr]): Future[Eth2Node] {.async.} =
@ -219,9 +219,11 @@ else:
bootstrapNodes = allMultiAddresses(bootstrapNodes), bootstrapNodes = allMultiAddresses(bootstrapNodes),
peersRequired = 1) peersRequired = 1)
info "Daemon started"
mainDaemon = await daemonFut mainDaemon = await daemonFut
var identity = await mainDaemon.identity()
info "LibP2P daemon started", peer = identity.peer.pretty(),
addresses = identity.addresses
proc closeDaemon() {.noconv.} = proc closeDaemon() {.noconv.} =
info "Shutting down the LibP2P daemon" info "Shutting down the LibP2P daemon"
@ -232,26 +234,34 @@ else:
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf, proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
ip: IpAddress, port: Port): BootstrapAddr = ip: IpAddress, port: Port): BootstrapAddr =
result = getPersistentNetIdentity(conf) let pair = getPersistentNetIdentity(conf)
result.addresses = @[tcpEndPoint(ip, port)] let pidma = MultiAddress.init(multiCodec("p2p"), PeerID.init(pair.pubkey))
result = tcpEndPoint(ip, port) & pidma
proc isSameNode*(bootstrapNode: BootstrapAddr, id: Eth2NodeIdentity): bool = proc isSameNode*(bootstrapNode: BootstrapAddr, id: Eth2NodeIdentity): bool =
bootstrapNode.peer == id.peer if IPFS.match(bootstrapNode):
let pid1 = PeerID.init(bootstrapNode[2].protoAddress())
let pid2 = PeerID.init(id.pubkey)
result = (pid1 == pid2)
proc shortForm*(id: Eth2NodeIdentity): string = proc shortForm*(id: Eth2NodeIdentity): string =
# TODO: Make this shorter $PeerID.init(id.pubkey)
$id
proc connectToNetwork*(node: Eth2Node, bootstrapNodes: seq[PeerInfo]) {.async.} = proc connectToNetwork*(node: Eth2Node,
bootstrapNodes: seq[MultiAddress]) {.async.} =
# TODO: perhaps we should do these in parallel # TODO: perhaps we should do these in parallel
var connected = false var connected = false
for bootstrapNode in bootstrapNodes: for bootstrapNode in bootstrapNodes:
try: try:
await node.daemon.connect(bootstrapNode.peer, bootstrapNode.addresses) if IPFS.match(bootstrapNode):
var peer = node.getPeer(bootstrapNode.peer) let pid = PeerID.init(bootstrapNode[2].protoAddress())
peer.wasDialed = true await node.daemon.connect(pid, @[bootstrapNode[0] & bootstrapNode[1]])
await initializeConnection(peer) var peer = node.getPeer(pid)
connected = true peer.wasDialed = true
await initializeConnection(peer)
connected = true
else:
raise newException(CatchableError, "Incorrect bootstrap address")
except CatchableError as err: except CatchableError as err:
error "Failed to connect to bootstrap node", error "Failed to connect to bootstrap node",
node = bootstrapNode, err = err.msg node = bootstrapNode, err = err.msg