feat(wakunode2): add nwaku agent-string to switch (#1302)

This commit is contained in:
Alvaro Revuelta 2022-10-28 15:12:06 +02:00 committed by GitHub
parent 979f1d397a
commit d479aacec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 4 deletions

View File

@ -36,6 +36,11 @@ type
desc: "prints the version"
defaultValue: false
name: "version" }: bool
agentString* {.
defaultValue: "nwaku",
desc: "Node agent string which is used as identifier in network"
name: "agent-string" .}: string
nodekey* {.
desc: "P2P node private key as 64 char hex string.",

View File

@ -283,7 +283,8 @@ proc initNode(conf: WakuNodeConf,
dnsResolver,
conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled
dns4DomainName,
discv5UdpPort
discv5UdpPort,
some(conf.agentString)
)
except:
return err("failed to create waku node instance: " & getCurrentExceptionMsg())

View File

@ -209,3 +209,39 @@ procSuite "WakuNode":
check:
node.announcedAddresses.len == 1
node.announcedAddresses.contains(expectedDns4Addr)
asyncTest "Agent string is set and advertised correctly":
let
# custom agent string
expectedAgentString1 = "node1-agent-string"
# bump when updating nim-libp2p
expectedAgentString2 = "nim-libp2p/0.0.1"
let
# node with custom agent string
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
node1 = WakuNode.new(nodeKey1, ValidIpAddress.init("0.0.0.0"), Port(60000),
agentString = some(expectedAgentString1))
# node with default agent string from libp2p
nodeKey2 = crypto.PrivateKey.random(Secp256k1, rng[])[]
node2 = WakuNode.new(nodeKey2, ValidIpAddress.init("0.0.0.0"), Port(60002))
await node1.start()
await node1.mountRelay()
await node2.start()
await node2.mountRelay()
await node1.connectToNodes(@[node2.switch.peerInfo.toRemotePeerInfo()])
await node2.connectToNodes(@[node1.switch.peerInfo.toRemotePeerInfo()])
let node1Agent = node2.switch.peerStore[AgentBook][node1.switch.peerInfo.toRemotePeerInfo().peerId]
let node2Agent = node1.switch.peerStore[AgentBook][node2.switch.peerInfo.toRemotePeerInfo().peerId]
check:
node1Agent == expectedAgentString1
node2Agent == expectedAgentString2
await allFutures(node1.stop(), node2.stop())

View File

@ -129,7 +129,9 @@ proc new*(T: type WakuNode,
nameResolver: NameResolver = nil,
sendSignedPeerRecord = false,
dns4DomainName = none(string),
discv5UdpPort = none(Port)): T {.raises: [Defect, LPError, IOError, TLSStreamProtocolError].} =
discv5UdpPort = none(Port),
agentString = none(string), # defaults to nim-libp2p version
): T {.raises: [Defect, LPError, IOError, TLSStreamProtocolError].} =
## Creates a Waku Node instance.
## Initialize addresses
@ -198,7 +200,8 @@ proc new*(T: type WakuNode,
secureKeyPath = secureKey,
secureCertPath = secureCert,
nameResolver = nameResolver,
sendSignedPeerRecord = sendSignedPeerRecord
sendSignedPeerRecord = sendSignedPeerRecord,
agentString = agentString
)
let wakuNode = WakuNode(

View File

@ -70,7 +70,9 @@ proc newWakuSwitch*(
sendSignedPeerRecord = false,
wssEnabled: bool = false,
secureKeyPath: string = "",
secureCertPath: string = ""): Switch
secureCertPath: string = "",
agentString = none(string), # defaults to nim-libp2p version
): Switch
{.raises: [Defect, IOError, LPError].} =
var b = SwitchBuilder
@ -86,6 +88,8 @@ proc newWakuSwitch*(
.withNameResolver(nameResolver)
.withSignedPeerRecord(sendSignedPeerRecord)
if agentString.isSome():
b = b.withAgentVersion(agentString.get())
if privKey.isSome():
b = b.withPrivateKey(privKey.get())
if wsAddress.isSome():