mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-10 14:26:26 +00:00
[LibP2P] Persistent network key for the bootstrap node
This commit is contained in:
parent
12e9367f78
commit
15fdf78d6a
@ -118,8 +118,8 @@ when networkBackend == rlpxBackend:
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
import
|
import
|
||||||
random,
|
os, random, std_shims/io,
|
||||||
libp2p/daemon/daemonapi, eth/async_utils,
|
libp2p/crypto/crypto, libp2p/daemon/daemonapi, eth/async_utils,
|
||||||
ssz
|
ssz
|
||||||
|
|
||||||
when networkBackend == libp2pSpecBackend:
|
when networkBackend == libp2pSpecBackend:
|
||||||
@ -136,6 +136,9 @@ else:
|
|||||||
BootstrapAddr* = PeerInfo
|
BootstrapAddr* = PeerInfo
|
||||||
Eth2NodeIdentity* = PeerInfo
|
Eth2NodeIdentity* = PeerInfo
|
||||||
|
|
||||||
|
const
|
||||||
|
networkKeyFilename = "privkey.protobuf"
|
||||||
|
|
||||||
proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} =
|
proc writeValue*(writer: var JsonWriter, value: PeerID) {.inline.} =
|
||||||
writer.writeValue value.pretty
|
writer.writeValue value.pretty
|
||||||
|
|
||||||
@ -151,20 +154,26 @@ else:
|
|||||||
proc init*(T: type BootstrapAddr, str: string): T =
|
proc init*(T: type BootstrapAddr, str: string): T =
|
||||||
Json.decode(str, PeerInfo)
|
Json.decode(str, PeerInfo)
|
||||||
|
|
||||||
proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async.} =
|
proc ensureNetworkIdFile(conf: BeaconNodeConf): string =
|
||||||
var node = new Eth2Node
|
result = conf.dataDir / networkKeyFilename
|
||||||
await node.init()
|
if not fileExists(result):
|
||||||
return node
|
createDir conf.dataDir.string
|
||||||
|
let pk = PrivateKey.random(Ed25519)
|
||||||
|
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
|
# Using waitFor here is reasonable, because this proc is needed only
|
||||||
# prior to connecting to the network. The RLPx alternative reads from
|
# prior to connecting to the network. The RLPx alternative reads from
|
||||||
# file and it's much easier to use if it's not async.
|
# file and it's much easier to use if it's not async.
|
||||||
# TODO: revisit in the future when we have our own Lib2P2 implementation.
|
# TODO: revisit in the future when we have our own Lib2P2 implementation.
|
||||||
let daemon = waitFor newDaemonApi()
|
let daemon = waitFor newDaemonApi(id = conf.ensureNetworkIdFile)
|
||||||
result = waitFor daemon.identity()
|
result = waitFor daemon.identity()
|
||||||
waitFor daemon.close()
|
waitFor daemon.close()
|
||||||
|
|
||||||
|
proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async.} =
|
||||||
|
var daemon = await newDaemonApi({PSGossipSub}, id = conf.ensureNetworkIdFile)
|
||||||
|
return await Eth2Node.init(daemon)
|
||||||
|
|
||||||
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
|
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
|
||||||
ip: IpAddress, port: Port): BootstrapAddr =
|
ip: IpAddress, port: Port): BootstrapAddr =
|
||||||
# TODO what about the ports?
|
# TODO what about the ports?
|
||||||
|
@ -91,19 +91,20 @@ include libp2p_backends_common
|
|||||||
include eth/p2p/p2p_backends_helpers
|
include eth/p2p/p2p_backends_helpers
|
||||||
include eth/p2p/p2p_tracing
|
include eth/p2p/p2p_tracing
|
||||||
|
|
||||||
proc init*(node: Eth2Node) {.async.} =
|
proc init*(T: type Eth2Node, daemon: DaemonAPI): Future[T] {.async.} =
|
||||||
node.daemon = await newDaemonApi({PSGossipSub})
|
new result
|
||||||
node.daemon.userData = node
|
result.daemon = daemon
|
||||||
init node.peers
|
result.daemon.userData = result
|
||||||
|
init result.peers
|
||||||
|
|
||||||
newSeq node.protocolStates, allProtocols.len
|
newSeq result.protocolStates, allProtocols.len
|
||||||
for proto in allProtocols:
|
for proto in allProtocols:
|
||||||
if proto.networkStateInitializer != nil:
|
if proto.networkStateInitializer != nil:
|
||||||
node.protocolStates[proto.index] = proto.networkStateInitializer(node)
|
result.protocolStates[proto.index] = proto.networkStateInitializer(result)
|
||||||
|
|
||||||
for msg in proto.messages:
|
for msg in proto.messages:
|
||||||
if msg.libp2pProtocol.len > 0:
|
if msg.libp2pProtocol.len > 0:
|
||||||
await node.daemon.addHandler(@[msg.libp2pProtocol], msg.thunk)
|
await daemon.addHandler(@[msg.libp2pProtocol], msg.thunk)
|
||||||
|
|
||||||
proc readMsg(stream: P2PStream, MsgType: type,
|
proc readMsg(stream: P2PStream, MsgType: type,
|
||||||
deadline: Future[void]): Future[Option[MsgType]] {.async.} =
|
deadline: Future[void]): Future[Option[MsgType]] {.async.} =
|
||||||
|
@ -160,17 +160,18 @@ include eth/p2p/p2p_tracing
|
|||||||
|
|
||||||
proc handleConnectingBeaconChainPeer(daemon: DaemonAPI, stream: P2PStream) {.async, gcsafe.}
|
proc handleConnectingBeaconChainPeer(daemon: DaemonAPI, stream: P2PStream) {.async, gcsafe.}
|
||||||
|
|
||||||
proc init*(node: Eth2Node) {.async.} =
|
proc init*(T: type Eth2Node, daemon: DaemonAPI): Future[Eth2Node] {.async.} =
|
||||||
node.daemon = await newDaemonApi({PSGossipSub})
|
new result
|
||||||
node.daemon.userData = node
|
result.daemon = daemon
|
||||||
init node.peers
|
result.daemon.userData = result
|
||||||
|
result.peers = initTable[PeerID, Peer]()
|
||||||
|
|
||||||
newSeq node.protocolStates, allProtocols.len
|
newSeq result.protocolStates, allProtocols.len
|
||||||
for proto in allProtocols:
|
for proto in allProtocols:
|
||||||
if proto.networkStateInitializer != nil:
|
if proto.networkStateInitializer != nil:
|
||||||
node.protocolStates[proto.index] = proto.networkStateInitializer(node)
|
result.protocolStates[proto.index] = proto.networkStateInitializer(result)
|
||||||
|
|
||||||
await node.daemon.addHandler(@[beaconChainProtocol], handleConnectingBeaconChainPeer)
|
await daemon.addHandler(@[beaconChainProtocol], handleConnectingBeaconChainPeer)
|
||||||
|
|
||||||
proc init*(T: type Peer, network: Eth2Node, id: PeerID): Peer =
|
proc init*(T: type Peer, network: Eth2Node, id: PeerID): Peer =
|
||||||
new result
|
new result
|
||||||
|
@ -13,12 +13,24 @@ asyncTest "connect two nodes":
|
|||||||
|
|
||||||
var c1 = BeaconNodeConf.defaults
|
var c1 = BeaconNodeConf.defaults
|
||||||
c1.dataDir = OutDir(tempDir / "node-1")
|
c1.dataDir = OutDir(tempDir / "node-1")
|
||||||
|
|
||||||
|
var n1PersistentAddress = c1.getPersistenBootstrapAddr(
|
||||||
|
parseIpAddress("127.0.0.1"), Port 50000)
|
||||||
|
|
||||||
var n1 = await createEth2Node(c1)
|
var n1 = await createEth2Node(c1)
|
||||||
var n1Address = getPersistenBootstrapAddr(c1, parseIpAddress("127.0.0.1"), Port 50000)
|
var n1ActualAddress = await n1.daemon.identity()
|
||||||
|
|
||||||
|
echo "persistent: ", n1PersistentAddress
|
||||||
|
echo "actual:", n1ActualAddress
|
||||||
|
doAssert n1PersistentAddress == n1ActualAddress
|
||||||
|
|
||||||
|
echo "Node 1 address: ", n1PersistentAddress
|
||||||
|
echo "Press any key to continue"
|
||||||
|
discard stdin.readLine()
|
||||||
|
|
||||||
var c2 = BeaconNodeConf.defaults
|
var c2 = BeaconNodeConf.defaults
|
||||||
c2.dataDir = OutDir(tempDir / "node-2")
|
c2.dataDir = OutDir(tempDir / "node-2")
|
||||||
var n2 = await createEth2Node(c2)
|
var n2 = await createEth2Node(c2)
|
||||||
|
|
||||||
await n2.connectToNetwork(bootstrapNodes = @[n1Address])
|
await n2.connectToNetwork(bootstrapNodes = @[n1ActualAddress])
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user