mirror of
https://github.com/codex-storage/nim-libp2p.git
synced 2025-01-12 20:14:09 +00:00
Update SPR when the PeerInfo changes (#711)
This commit is contained in:
parent
c97befb387
commit
9ba5c069c8
@ -28,7 +28,7 @@ type
|
|||||||
agentVersion*: string
|
agentVersion*: string
|
||||||
privateKey*: PrivateKey
|
privateKey*: PrivateKey
|
||||||
publicKey*: PublicKey
|
publicKey*: PublicKey
|
||||||
signedPeerRecord*: Option[Envelope]
|
signedPeerRecord*: SignedPeerRecord
|
||||||
|
|
||||||
func shortLog*(p: PeerInfo): auto =
|
func shortLog*(p: PeerInfo): auto =
|
||||||
(
|
(
|
||||||
@ -40,6 +40,17 @@ func shortLog*(p: PeerInfo): auto =
|
|||||||
)
|
)
|
||||||
chronicles.formatIt(PeerInfo): shortLog(it)
|
chronicles.formatIt(PeerInfo): shortLog(it)
|
||||||
|
|
||||||
|
proc update*(p: PeerInfo) =
|
||||||
|
let sprRes = SignedPeerRecord.init(
|
||||||
|
p.privateKey,
|
||||||
|
PeerRecord.init(p.peerId, p.addrs)
|
||||||
|
)
|
||||||
|
if sprRes.isOk:
|
||||||
|
p.signedPeerRecord = sprRes.get()
|
||||||
|
else:
|
||||||
|
discard
|
||||||
|
#info "Can't update the signed peer record"
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
p: typedesc[PeerInfo],
|
p: typedesc[PeerInfo],
|
||||||
key: PrivateKey,
|
key: PrivateKey,
|
||||||
@ -56,15 +67,6 @@ proc new*(
|
|||||||
|
|
||||||
let peerId = PeerID.init(key).tryGet()
|
let peerId = PeerID.init(key).tryGet()
|
||||||
|
|
||||||
let sprRes = SignedPeerRecord.init(
|
|
||||||
key,
|
|
||||||
PeerRecord.init(peerId, @addrs)
|
|
||||||
)
|
|
||||||
let spr = if sprRes.isOk:
|
|
||||||
some(sprRes.get().envelope)
|
|
||||||
else:
|
|
||||||
none(Envelope)
|
|
||||||
|
|
||||||
let peerInfo = PeerInfo(
|
let peerInfo = PeerInfo(
|
||||||
peerId: peerId,
|
peerId: peerId,
|
||||||
publicKey: pubkey,
|
publicKey: pubkey,
|
||||||
@ -73,6 +75,8 @@ proc new*(
|
|||||||
agentVersion: agentVersion,
|
agentVersion: agentVersion,
|
||||||
addrs: @addrs,
|
addrs: @addrs,
|
||||||
protocols: @protocols,
|
protocols: @protocols,
|
||||||
signedPeerRecord: spr)
|
)
|
||||||
|
|
||||||
|
peerInfo.update()
|
||||||
|
|
||||||
return peerInfo
|
return peerInfo
|
||||||
|
@ -96,8 +96,8 @@ proc encodeMsg(peerInfo: PeerInfo, observedAddr: MultiAddress, sendSpr: bool): P
|
|||||||
|
|
||||||
## Optionally populate signedPeerRecord field.
|
## Optionally populate signedPeerRecord field.
|
||||||
## See https://github.com/libp2p/go-libp2p/blob/ddf96ce1cfa9e19564feb9bd3e8269958bbc0aba/p2p/protocol/identify/pb/identify.proto for reference.
|
## See https://github.com/libp2p/go-libp2p/blob/ddf96ce1cfa9e19564feb9bd3e8269958bbc0aba/p2p/protocol/identify/pb/identify.proto for reference.
|
||||||
if peerInfo.signedPeerRecord.isSome() and sendSpr:
|
if sendSpr:
|
||||||
let sprBuff = peerInfo.signedPeerRecord.get().encode()
|
let sprBuff = peerInfo.signedPeerRecord.envelope.encode()
|
||||||
if sprBuff.isOk():
|
if sprBuff.isOk():
|
||||||
result.write(8, sprBuff.get())
|
result.write(8, sprBuff.get())
|
||||||
|
|
||||||
|
@ -265,6 +265,8 @@ proc start*(s: Switch) {.async, gcsafe.} =
|
|||||||
s.acceptFuts.add(s.accept(t))
|
s.acceptFuts.add(s.accept(t))
|
||||||
s.peerInfo.addrs &= t.addrs
|
s.peerInfo.addrs &= t.addrs
|
||||||
|
|
||||||
|
s.peerInfo.update()
|
||||||
|
|
||||||
debug "Started libp2p node", peer = s.peerInfo
|
debug "Started libp2p node", peer = s.peerInfo
|
||||||
|
|
||||||
proc newSwitch*(peerInfo: PeerInfo,
|
proc newSwitch*(peerInfo: PeerInfo,
|
||||||
|
@ -144,7 +144,7 @@ suite "Identify":
|
|||||||
check id.protoVersion.get() == ProtoVersion
|
check id.protoVersion.get() == ProtoVersion
|
||||||
check id.agentVersion.get() == AgentVersion
|
check id.agentVersion.get() == AgentVersion
|
||||||
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
check id.protos == @["/test/proto1/1.0.0", "/test/proto2/1.0.0"]
|
||||||
check id.signedPeerRecord.get() == remotePeerInfo.signedPeerRecord.get()
|
check id.signedPeerRecord.get() == remotePeerInfo.signedPeerRecord.envelope
|
||||||
|
|
||||||
suite "handle push identify message":
|
suite "handle push identify message":
|
||||||
var
|
var
|
||||||
|
@ -31,7 +31,7 @@ suite "PeerInfo":
|
|||||||
peerInfo = PeerInfo.new(seckey, multiAddresses)
|
peerInfo = PeerInfo.new(seckey, multiAddresses)
|
||||||
|
|
||||||
let
|
let
|
||||||
env = peerInfo.signedPeerRecord.get()
|
env = peerInfo.signedPeerRecord.envelope
|
||||||
rec = PeerRecord.decode(env.payload()).tryGet()
|
rec = PeerRecord.decode(env.payload()).tryGet()
|
||||||
|
|
||||||
# Check envelope fields
|
# Check envelope fields
|
||||||
|
Loading…
x
Reference in New Issue
Block a user