2019-03-05 22:54:08 +00:00
|
|
|
import
|
2019-03-19 03:57:19 +00:00
|
|
|
options, chronos, json_serialization, strutils,
|
2019-04-18 00:02:14 +00:00
|
|
|
chronicles,
|
2019-03-18 03:54:08 +00:00
|
|
|
spec/digest, version, conf
|
2019-03-05 22:54:08 +00:00
|
|
|
|
|
|
|
const
|
2019-03-18 03:54:08 +00:00
|
|
|
clientId = "Nimbus beacon node v" & fullVersionStr()
|
2019-03-05 22:54:08 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
when useRLPx:
|
2019-03-05 22:54:08 +00:00
|
|
|
import
|
2019-03-18 03:54:08 +00:00
|
|
|
os,
|
2019-04-18 00:02:14 +00:00
|
|
|
eth/[rlp, p2p, keys, net/nat], gossipsub_protocol,
|
2019-03-22 11:33:10 +00:00
|
|
|
eth/p2p/peer_pool # for log on connected peers
|
2019-03-05 22:54:08 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
p2p, rlp, gossipsub_protocol
|
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
const
|
|
|
|
netBackendName* = "rlpx"
|
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
type
|
|
|
|
Eth2Node* = EthereumNode
|
|
|
|
BootstrapAddr* = ENode
|
|
|
|
|
|
|
|
template libp2pProtocol*(name, version: string) {.pragma.}
|
|
|
|
|
2019-04-18 00:02:14 +00:00
|
|
|
proc setupNat(conf: BeaconNodeConf): tuple[ip: IpAddress, tcpPort: Port, udpPort: Port] =
|
|
|
|
# defaults
|
|
|
|
result.ip = parseIpAddress("127.0.0.1")
|
|
|
|
result.tcpPort = Port(conf.tcpPort)
|
|
|
|
result.udpPort = Port(conf.udpPort)
|
|
|
|
|
|
|
|
var nat: NatStrategy
|
|
|
|
case conf.nat.toLowerAscii:
|
|
|
|
of "any":
|
|
|
|
nat = NatAny
|
|
|
|
of "none":
|
|
|
|
nat = NatNone
|
|
|
|
of "upnp":
|
|
|
|
nat = NatUpnp
|
|
|
|
of "pmp":
|
|
|
|
nat = NatPmp
|
|
|
|
else:
|
|
|
|
if conf.nat.startsWith("extip:") and isIpAddress(conf.nat[6..^1]):
|
|
|
|
# any required port redirection is assumed to be done by hand
|
|
|
|
result.ip = parseIpAddress(conf.nat[6..^1])
|
|
|
|
nat = NatNone
|
|
|
|
else:
|
|
|
|
error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
if nat != NatNone:
|
|
|
|
let extIP = getExternalIP(nat)
|
|
|
|
if extIP.isSome:
|
|
|
|
result.ip = extIP.get()
|
|
|
|
let extPorts = redirectPorts(tcpPort = result.tcpPort,
|
|
|
|
udpPort = result.udpPort,
|
|
|
|
description = clientId)
|
|
|
|
if extPorts.isSome:
|
|
|
|
(result.tcpPort, result.udpPort) = extPorts.get()
|
2019-03-19 03:57:19 +00:00
|
|
|
|
2019-03-20 01:18:49 +00:00
|
|
|
proc ensureNetworkKeys*(conf: BeaconNodeConf): KeyPair =
|
2019-03-18 03:54:08 +00:00
|
|
|
let privateKeyFile = conf.dataDir / "network.privkey"
|
|
|
|
var privKey: PrivateKey
|
|
|
|
if not fileExists(privateKeyFile):
|
|
|
|
privKey = newPrivateKey()
|
2019-03-19 17:22:17 +00:00
|
|
|
createDir conf.dataDir.string
|
2019-03-18 03:54:08 +00:00
|
|
|
writeFile(privateKeyFile, $privKey)
|
|
|
|
else:
|
|
|
|
privKey = initPrivateKey(readFile(privateKeyFile).string)
|
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
KeyPair(seckey: privKey, pubkey: privKey.getPublicKey())
|
|
|
|
|
|
|
|
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
|
|
|
|
ip: IpAddress, port: Port): BootstrapAddr =
|
2019-03-05 22:54:08 +00:00
|
|
|
let
|
2019-03-19 17:22:17 +00:00
|
|
|
keys = ensureNetworkKeys(conf)
|
|
|
|
address = Address(ip: ip, tcpPort: port, udpPort: port)
|
|
|
|
|
|
|
|
initENode(keys.pubKey, address)
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: BootstrapAddr) {.inline.} =
|
|
|
|
writer.writeValue $value
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var BootstrapAddr) {.inline.} =
|
|
|
|
value = initENode reader.readValue(string)
|
|
|
|
|
|
|
|
proc createEth2Node*(conf: BeaconNodeConf): Future[EthereumNode] {.async.} =
|
|
|
|
let
|
|
|
|
keys = ensureNetworkKeys(conf)
|
2019-04-18 00:02:14 +00:00
|
|
|
(ip, tcpPort, udpPort) = setupNat(conf)
|
|
|
|
address = Address(ip: ip,
|
|
|
|
tcpPort: tcpPort,
|
|
|
|
udpPort: udpPort)
|
2019-03-05 22:54:08 +00:00
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
# TODO there are more networking options to add here: local bind ip, ipv6
|
|
|
|
# etc.
|
2019-03-12 13:14:30 +00:00
|
|
|
return newEthereumNode(keys, address, 0,
|
2019-03-28 11:56:44 +00:00
|
|
|
nil, clientId)
|
2019-03-05 22:54:08 +00:00
|
|
|
|
|
|
|
proc saveConnectionAddressFile*(node: Eth2Node, filename: string) =
|
|
|
|
writeFile(filename, $node.listeningAddress)
|
|
|
|
|
|
|
|
proc init*(T: type BootstrapAddr, str: string): T =
|
|
|
|
initENode(str)
|
|
|
|
|
2019-03-28 11:01:28 +00:00
|
|
|
func connectedPeers*(enode: EthereumNode): int =
|
2019-03-22 11:33:10 +00:00
|
|
|
enode.peerPool.len
|
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
else:
|
|
|
|
import
|
2019-04-18 00:02:14 +00:00
|
|
|
libp2p/daemon/daemonapi,
|
2019-03-05 22:54:08 +00:00
|
|
|
libp2p_backend
|
|
|
|
|
|
|
|
export
|
|
|
|
libp2p_backend
|
|
|
|
|
|
|
|
type
|
|
|
|
BootstrapAddr* = PeerInfo
|
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
const
|
|
|
|
netBackendName* = "libp2p"
|
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} =
|
|
|
|
writer.writeValue value.pretty
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var PeerID) {.inline.} =
|
|
|
|
value = PeerID.init reader.readValue(string)
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter, value: MultiAddress) {.inline.} =
|
|
|
|
writer.writeValue $value
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader, value: var MultiAddress) {.inline.} =
|
|
|
|
value = MultiAddress.init reader.readValue(string)
|
|
|
|
|
|
|
|
proc init*(T: type BootstrapAddr, str: string): T =
|
|
|
|
Json.decode(str, PeerInfo)
|
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async.} =
|
2019-03-05 22:54:08 +00:00
|
|
|
var node = new Eth2Node
|
|
|
|
await node.init()
|
|
|
|
return node
|
|
|
|
|
|
|
|
proc connectToNetwork*(node: Eth2Node, bootstrapNodes: seq[PeerInfo]) {.async.} =
|
|
|
|
# TODO: perhaps we should do these in parallel
|
|
|
|
for bootstrapNode in bootstrapNodes:
|
|
|
|
try:
|
|
|
|
await node.daemon.connect(bootstrapNode.peer, bootstrapNode.addresses)
|
2019-03-18 03:54:08 +00:00
|
|
|
let peer = node.getPeer(bootstrapNode.peer)
|
|
|
|
await peer.performProtocolHandshakes()
|
2019-03-05 22:54:08 +00:00
|
|
|
except PeerDisconnected:
|
|
|
|
error "Failed to connect to bootstrap node", node = bootstrapNode
|
|
|
|
|
|
|
|
proc saveConnectionAddressFile*(node: Eth2Node, filename: string) =
|
|
|
|
let id = waitFor node.daemon.identity()
|
|
|
|
Json.saveFile(filename, id, pretty = false)
|
|
|
|
|
|
|
|
proc loadConnectionAddressFile*(filename: string): PeerInfo =
|
|
|
|
Json.loadFile(filename, PeerInfo)
|
|
|
|
|