mirror of
https://github.com/status-im/nim-libp2p-dht.git
synced 2025-02-24 01:58:28 +00:00
Fixes for integration
This commit is contained in:
parent
e8177aa7eb
commit
bde7a26f9d
@ -222,12 +222,12 @@ 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): DiscResult[void] =
|
proc updateRecord*(d: Protocol, newSpr: SignedPeerRecord): 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.
|
||||||
|
|
||||||
# TODO: Do we need this proc? This simply serves so that seqNo will be
|
info "Updated discovery SPR", uri=toURI(newSpr)
|
||||||
# incremented to satisfy the tests...
|
d.localNode.record = newSpr
|
||||||
d.localNode.record.incSeqNo(d.privateKey)
|
ok()
|
||||||
|
|
||||||
# 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?
|
||||||
@ -889,6 +889,7 @@ proc refreshLoop(d: Protocol) {.async.} =
|
|||||||
trace "refreshLoop canceled"
|
trace "refreshLoop canceled"
|
||||||
|
|
||||||
proc ipMajorityLoop(d: Protocol) {.async.} =
|
proc ipMajorityLoop(d: Protocol) {.async.} =
|
||||||
|
#TODO this should be handled by libp2p, not the DHT
|
||||||
## When `enrAutoUpdate` is enabled, the IP:port combination returned
|
## When `enrAutoUpdate` is enabled, the IP:port combination returned
|
||||||
## by the majority will be used to update the local SPR.
|
## by the majority will be used to update the local SPR.
|
||||||
## This should be safe as long as the routing table is not overwhelmed by
|
## This should be safe as long as the routing table is not overwhelmed by
|
||||||
@ -1008,6 +1009,33 @@ proc newProtocol*(
|
|||||||
|
|
||||||
result.transport = newTransport(result, privKey, node, bindPort, bindIp, rng)
|
result.transport = newTransport(result, privKey, node, bindPort, bindIp, rng)
|
||||||
|
|
||||||
|
proc newProtocol*(
|
||||||
|
privKey: PrivateKey,
|
||||||
|
bindPort: Port,
|
||||||
|
record: SignedPeerRecord,
|
||||||
|
bootstrapRecords: openArray[SignedPeerRecord] = [],
|
||||||
|
bindIp = IPv4_any(),
|
||||||
|
config = defaultDiscoveryConfig,
|
||||||
|
rng = newRng()):
|
||||||
|
Protocol =
|
||||||
|
info "Discovery SPR initialized", seqNum = record.seqNum, uri = toURI(record)
|
||||||
|
let node = newNode(record).expect("Properly initialized record")
|
||||||
|
|
||||||
|
# TODO Consider whether this should be a Defect
|
||||||
|
doAssert rng != nil, "RNG initialization failed"
|
||||||
|
|
||||||
|
result = Protocol(
|
||||||
|
privateKey: privKey,
|
||||||
|
localNode: node,
|
||||||
|
bootstrapRecords: @bootstrapRecords,
|
||||||
|
ipVote: IpVote.init(),
|
||||||
|
enrAutoUpdate: false, #TODO this should be removed from nim-libp2p-dht
|
||||||
|
routingTable: RoutingTable.init(
|
||||||
|
node, config.bitsPerHop, config.tableIpLimits, rng),
|
||||||
|
rng: rng)
|
||||||
|
|
||||||
|
result.transport = newTransport(result, privKey, node, bindPort, bindIp, rng)
|
||||||
|
|
||||||
|
|
||||||
proc open*(d: Protocol) {.raises: [Defect, CatchableError].} =
|
proc open*(d: Protocol) {.raises: [Defect, CatchableError].} =
|
||||||
info "Starting discovery node", node = d.localNode
|
info "Starting discovery node", node = d.localNode
|
||||||
|
@ -166,14 +166,14 @@ proc update*(r: var SignedPeerRecord, pk: crypto.PrivateKey,
|
|||||||
proc toTypedRecord*(r: SignedPeerRecord) : RecordResult[SignedPeerRecord] = ok(r)
|
proc toTypedRecord*(r: SignedPeerRecord) : RecordResult[SignedPeerRecord] = ok(r)
|
||||||
|
|
||||||
proc ip*(r: SignedPeerRecord): Option[array[4, byte]] =
|
proc ip*(r: SignedPeerRecord): Option[array[4, byte]] =
|
||||||
let ma = r.data.addresses[0].address
|
for address in r.data.addresses:
|
||||||
|
let ma = address.address
|
||||||
let code = ma[0].get.protoCode()
|
let code = ma[0].get.protoCode()
|
||||||
if code.isOk and code.get == multiCodec("ip4"):
|
if code.isOk and code.get == multiCodec("ip4"):
|
||||||
var ipbuf: array[4, byte]
|
var ipbuf: array[4, byte]
|
||||||
let res = ma[0].get.protoArgument(ipbuf)
|
let res = ma[0].get.protoArgument(ipbuf)
|
||||||
if res.isOk:
|
if res.isOk:
|
||||||
return some(ipbuf)
|
return some(ipbuf)
|
||||||
|
|
||||||
# err("Incorrect IPv4 address")
|
# err("Incorrect IPv4 address")
|
||||||
# else:
|
# else:
|
||||||
@ -188,15 +188,16 @@ proc ip*(r: SignedPeerRecord): Option[array[4, byte]] =
|
|||||||
# err("MultiAddress must be wire address (tcp, udp or unix)")
|
# err("MultiAddress must be wire address (tcp, udp or unix)")
|
||||||
|
|
||||||
proc udp*(r: SignedPeerRecord): Option[int] =
|
proc udp*(r: SignedPeerRecord): Option[int] =
|
||||||
let ma = r.data.addresses[0].address
|
for address in r.data.addresses:
|
||||||
|
let ma = address.address
|
||||||
|
|
||||||
let code = ma[1].get.protoCode()
|
let code = ma[1].get.protoCode()
|
||||||
if code.isOk and code.get == multiCodec("udp"):
|
if code.isOk and code.get == multiCodec("udp"):
|
||||||
var pbuf: array[2, byte]
|
var pbuf: array[2, byte]
|
||||||
let res = ma[1].get.protoArgument(pbuf)
|
let res = ma[1].get.protoArgument(pbuf)
|
||||||
if res.isOk:
|
if res.isOk:
|
||||||
let p = fromBytesBE(uint16, pbuf)
|
let p = fromBytesBE(uint16, pbuf)
|
||||||
return some(p.int)
|
return some(p.int)
|
||||||
|
|
||||||
proc fromBase64*(r: var SignedPeerRecord, s: string): bool =
|
proc fromBase64*(r: var SignedPeerRecord, s: string): bool =
|
||||||
## Loads SPR from base64-encoded protobuf-encoded bytes, and validates the
|
## Loads SPR from base64-encoded protobuf-encoded bytes, and validates the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user