Update SPR when the PeerInfo changes (#711)
This commit is contained in:
parent
c97befb387
commit
9ba5c069c8
|
@ -28,7 +28,7 @@ type
|
|||
agentVersion*: string
|
||||
privateKey*: PrivateKey
|
||||
publicKey*: PublicKey
|
||||
signedPeerRecord*: Option[Envelope]
|
||||
signedPeerRecord*: SignedPeerRecord
|
||||
|
||||
func shortLog*(p: PeerInfo): auto =
|
||||
(
|
||||
|
@ -40,6 +40,17 @@ func shortLog*(p: PeerInfo): auto =
|
|||
)
|
||||
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*(
|
||||
p: typedesc[PeerInfo],
|
||||
key: PrivateKey,
|
||||
|
@ -56,15 +67,6 @@ proc new*(
|
|||
|
||||
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(
|
||||
peerId: peerId,
|
||||
publicKey: pubkey,
|
||||
|
@ -73,6 +75,8 @@ proc new*(
|
|||
agentVersion: agentVersion,
|
||||
addrs: @addrs,
|
||||
protocols: @protocols,
|
||||
signedPeerRecord: spr)
|
||||
)
|
||||
|
||||
peerInfo.update()
|
||||
|
||||
return peerInfo
|
||||
|
|
|
@ -96,8 +96,8 @@ proc encodeMsg(peerInfo: PeerInfo, observedAddr: MultiAddress, sendSpr: bool): P
|
|||
|
||||
## Optionally populate signedPeerRecord field.
|
||||
## See https://github.com/libp2p/go-libp2p/blob/ddf96ce1cfa9e19564feb9bd3e8269958bbc0aba/p2p/protocol/identify/pb/identify.proto for reference.
|
||||
if peerInfo.signedPeerRecord.isSome() and sendSpr:
|
||||
let sprBuff = peerInfo.signedPeerRecord.get().encode()
|
||||
if sendSpr:
|
||||
let sprBuff = peerInfo.signedPeerRecord.envelope.encode()
|
||||
if sprBuff.isOk():
|
||||
result.write(8, sprBuff.get())
|
||||
|
||||
|
|
|
@ -265,6 +265,8 @@ proc start*(s: Switch) {.async, gcsafe.} =
|
|||
s.acceptFuts.add(s.accept(t))
|
||||
s.peerInfo.addrs &= t.addrs
|
||||
|
||||
s.peerInfo.update()
|
||||
|
||||
debug "Started libp2p node", peer = s.peerInfo
|
||||
|
||||
proc newSwitch*(peerInfo: PeerInfo,
|
||||
|
|
|
@ -144,7 +144,7 @@ suite "Identify":
|
|||
check id.protoVersion.get() == ProtoVersion
|
||||
check id.agentVersion.get() == AgentVersion
|
||||
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":
|
||||
var
|
||||
|
|
|
@ -31,7 +31,7 @@ suite "PeerInfo":
|
|||
peerInfo = PeerInfo.new(seckey, multiAddresses)
|
||||
|
||||
let
|
||||
env = peerInfo.signedPeerRecord.get()
|
||||
env = peerInfo.signedPeerRecord.envelope
|
||||
rec = PeerRecord.decode(env.payload()).tryGet()
|
||||
|
||||
# Check envelope fields
|
||||
|
|
Loading…
Reference in New Issue