mirror of
https://github.com/logos-storage/logos-storage-nim-dht.git
synced 2026-01-07 16:03:09 +00:00
fix updateRecord
- support incrementing seqNo - support updating with new record
This commit is contained in:
parent
d90f0a03b9
commit
2d93fa9e69
@ -222,16 +222,29 @@ func getRecord*(d: Protocol): SignedPeerRecord =
|
|||||||
## Get the SPR of the local node.
|
## Get the SPR of the local node.
|
||||||
d.localNode.record
|
d.localNode.record
|
||||||
|
|
||||||
proc updateRecord*(d: Protocol, newSpr: SignedPeerRecord): DiscResult[void] =
|
proc updateRecord*(
|
||||||
|
d: Protocol,
|
||||||
|
spr: Option[SignedPeerRecord] = SignedPeerRecord.none): DiscResult[void] =
|
||||||
## Update the ENR of the local node with provided `enrFields` k:v pairs.
|
## Update the ENR of the local node with provided `enrFields` k:v pairs.
|
||||||
|
##
|
||||||
|
|
||||||
info "Updated discovery SPR", uri=toURI(newSpr)
|
if spr.isSome:
|
||||||
d.localNode.record = newSpr
|
let
|
||||||
ok()
|
newSpr = spr.get()
|
||||||
|
seqNo = d.localNode.record.seqNum
|
||||||
|
|
||||||
|
info "Updated discovery SPR", uri = toURI(newSpr)
|
||||||
|
|
||||||
|
d.localNode.record = newSpr
|
||||||
|
d.localNode.record.data.seqNo = seqNo
|
||||||
|
|
||||||
|
? d.localNode.record.incSeqNo(d.privateKey)
|
||||||
|
|
||||||
# TODO: Would it make sense to actively ping ("broadcast") to all the peers
|
# TODO: Would it make sense to actively ping ("broadcast") to all the peers
|
||||||
# we stored a handshake with in order to get that ENR updated?
|
# we stored a handshake with in order to get that ENR updated?
|
||||||
|
|
||||||
|
ok()
|
||||||
|
|
||||||
proc sendResponse(d: Protocol, dstId: NodeId, dstAddr: Address,
|
proc sendResponse(d: Protocol, dstId: NodeId, dstAddr: Address,
|
||||||
message: SomeMessage, reqId: RequestId) =
|
message: SomeMessage, reqId: RequestId) =
|
||||||
## send Response using the specifid reqId
|
## send Response using the specifid reqId
|
||||||
|
|||||||
@ -31,7 +31,6 @@ proc seqNum*(r: SignedPeerRecord): uint64 =
|
|||||||
r.data.seqNo
|
r.data.seqNo
|
||||||
|
|
||||||
proc fromBytes(r: var SignedPeerRecord, s: openArray[byte]): bool =
|
proc fromBytes(r: var SignedPeerRecord, s: openArray[byte]): bool =
|
||||||
|
|
||||||
let decoded = SignedPeerRecord.decode(@s)
|
let decoded = SignedPeerRecord.decode(@s)
|
||||||
if decoded.isErr:
|
if decoded.isErr:
|
||||||
error "Error decoding SignedPeerRecord", error = decoded.error
|
error "Error decoding SignedPeerRecord", error = decoded.error
|
||||||
@ -47,22 +46,21 @@ proc get*(r: SignedPeerRecord, T: type PublicKey): Option[T] =
|
|||||||
r.envelope.publicKey.some
|
r.envelope.publicKey.some
|
||||||
|
|
||||||
proc incSeqNo*(
|
proc incSeqNo*(
|
||||||
r: var SignedPeerRecord,
|
r: var SignedPeerRecord,
|
||||||
pk: PrivateKey): RecordResult[void] =
|
pk: PrivateKey): RecordResult[void] =
|
||||||
|
|
||||||
r.data.seqNo.inc()
|
r.data.seqNo.inc()
|
||||||
r = ? SignedPeerRecord.init(pk, r.data).mapErr(
|
r = ? SignedPeerRecord.init(pk, r.data).mapErr(
|
||||||
(e: CryptoError) =>
|
(e: CryptoError) =>
|
||||||
("Error initialising SignedPeerRecord with incremented seqNo: " &
|
("Error initializing SignedPeerRecord with incremented seqNo: " & $e).cstring)
|
||||||
$e).cstring
|
|
||||||
)
|
|
||||||
ok()
|
ok()
|
||||||
|
|
||||||
|
proc update*(
|
||||||
proc update*(r: var SignedPeerRecord, pk: crypto.PrivateKey,
|
r: var SignedPeerRecord,
|
||||||
ip: Option[ValidIpAddress],
|
pk: crypto.PrivateKey,
|
||||||
tcpPort, udpPort: Option[Port] = none[Port]()):
|
ip: Option[ValidIpAddress],
|
||||||
RecordResult[void] =
|
tcpPort, udpPort: Option[Port] = none[Port]()):
|
||||||
|
RecordResult[void] =
|
||||||
## Update a `SignedPeerRecord` with given ip address, tcp port, udp port and optional
|
## Update a `SignedPeerRecord` with given ip address, tcp port, udp port and optional
|
||||||
## custom k:v pairs.
|
## custom k:v pairs.
|
||||||
##
|
##
|
||||||
@ -110,7 +108,6 @@ proc update*(r: var SignedPeerRecord, pk: crypto.PrivateKey,
|
|||||||
transProtoPort = udpPort.get
|
transProtoPort = udpPort.get
|
||||||
|
|
||||||
updated = MultiAddress.init(ipAddr, transProto, transProtoPort)
|
updated = MultiAddress.init(ipAddr, transProto, transProtoPort)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
let
|
let
|
||||||
existing = r.data.addresses[0].address
|
existing = r.data.addresses[0].address
|
||||||
@ -158,8 +155,7 @@ proc update*(r: var SignedPeerRecord, pk: crypto.PrivateKey,
|
|||||||
|
|
||||||
r = ? SignedPeerRecord.init(pk, r.data)
|
r = ? SignedPeerRecord.init(pk, r.data)
|
||||||
.mapErr((e: CryptoError) =>
|
.mapErr((e: CryptoError) =>
|
||||||
("Failed to update SignedPeerRecord: " & $e).cstring
|
("Failed to update SignedPeerRecord: " & $e).cstring)
|
||||||
)
|
|
||||||
|
|
||||||
return ok()
|
return ok()
|
||||||
|
|
||||||
@ -223,11 +219,13 @@ proc toBase64*(r: SignedPeerRecord): string =
|
|||||||
|
|
||||||
proc toURI*(r: SignedPeerRecord): string = "spr:" & r.toBase64
|
proc toURI*(r: SignedPeerRecord): string = "spr:" & r.toBase64
|
||||||
|
|
||||||
proc init*(T: type SignedPeerRecord, seqNum: uint64,
|
proc init*(
|
||||||
pk: PrivateKey,
|
T: type SignedPeerRecord,
|
||||||
ip: Option[ValidIpAddress],
|
seqNum: uint64,
|
||||||
tcpPort, udpPort: Option[Port]):
|
pk: PrivateKey,
|
||||||
RecordResult[T] =
|
ip: Option[ValidIpAddress],
|
||||||
|
tcpPort, udpPort: Option[Port]):
|
||||||
|
RecordResult[T] =
|
||||||
## Initialize a `SignedPeerRecord` with given sequence number, private key, optional
|
## Initialize a `SignedPeerRecord` with given sequence number, private key, optional
|
||||||
## ip address, tcp port, udp port, and optional custom k:v pairs.
|
## ip address, tcp port, udp port, and optional custom k:v pairs.
|
||||||
##
|
##
|
||||||
@ -268,7 +266,9 @@ proc init*(T: type SignedPeerRecord, seqNum: uint64,
|
|||||||
let ma = MultiAddress.init(ipAddr, proto, protoPort)
|
let ma = MultiAddress.init(ipAddr, proto, protoPort)
|
||||||
|
|
||||||
let pr = PeerRecord.init(peerId, @[ma], seqNum)
|
let pr = PeerRecord.init(peerId, @[ma], seqNum)
|
||||||
SignedPeerRecord.init(pk, pr).mapErr((e: CryptoError) => ("Failed to init SignedPeerRecord: " & $e).cstring)
|
SignedPeerRecord.init(pk, pr)
|
||||||
|
.mapErr(
|
||||||
|
(e: CryptoError) => ("Failed to init SignedPeerRecord: " & $e).cstring)
|
||||||
|
|
||||||
proc contains*(r: SignedPeerRecord, fp: (string, seq[byte])): bool =
|
proc contains*(r: SignedPeerRecord, fp: (string, seq[byte])): bool =
|
||||||
# TODO: use FieldPair for this, but that is a bit cumbersome. Perhaps the
|
# TODO: use FieldPair for this, but that is a bit cumbersome. Perhaps the
|
||||||
@ -281,4 +281,5 @@ proc contains*(r: SignedPeerRecord, fp: (string, seq[byte])): bool =
|
|||||||
debugEcho "`contains` is not yet implemented for SignedPeerRecords"
|
debugEcho "`contains` is not yet implemented for SignedPeerRecords"
|
||||||
return false
|
return false
|
||||||
|
|
||||||
proc `==`*(a, b: SignedPeerRecord): bool = a.data == b.data
|
proc `==`*(a, b: SignedPeerRecord): bool =
|
||||||
|
a.data == b.data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user