mirror of
https://github.com/logos-storage/logos-storage-nim-dht.git
synced 2026-01-04 06:23:05 +00:00
Cleanups (#55)
* limit query to batchSize * allow initializing node from ip and port * misc cleanups
This commit is contained in:
parent
08928e57d8
commit
d6d255b4b5
@ -46,6 +46,21 @@ proc toNodeId*(pk: PublicKey): Result[NodeId, cstring] =
|
|||||||
let pid = ? PeerId.init(pk)
|
let pid = ? PeerId.init(pk)
|
||||||
ok pid.toNodeId
|
ok pid.toNodeId
|
||||||
|
|
||||||
|
func newNode*(
|
||||||
|
ip: IpAddress,
|
||||||
|
port: Port,
|
||||||
|
pk: PublicKey,
|
||||||
|
record: SignedPeerRecord): Result[Node, cstring] =
|
||||||
|
|
||||||
|
let
|
||||||
|
node = Node(
|
||||||
|
id: ? pk.toNodeId(),
|
||||||
|
pubkey: pk,
|
||||||
|
record: record,
|
||||||
|
address: Address(ip: ValidIpAddress.init(ip), port: port).some)
|
||||||
|
|
||||||
|
ok node
|
||||||
|
|
||||||
func newNode*(r: SignedPeerRecord): Result[Node, cstring] =
|
func newNode*(r: SignedPeerRecord): Result[Node, cstring] =
|
||||||
## Create a new `Node` from a `SignedPeerRecord`.
|
## Create a new `Node` from a `SignedPeerRecord`.
|
||||||
# TODO: Handle IPv6
|
# TODO: Handle IPv6
|
||||||
@ -59,17 +74,22 @@ func newNode*(r: SignedPeerRecord): Result[Node, cstring] =
|
|||||||
# Also this can not fail for a properly created record as id is checked upon
|
# Also this can not fail for a properly created record as id is checked upon
|
||||||
# deserialization.
|
# deserialization.
|
||||||
let
|
let
|
||||||
tr = ? r.toTypedRecord()
|
|
||||||
nodeId = ? pk.get().toNodeId()
|
nodeId = ? pk.get().toNodeId()
|
||||||
|
|
||||||
if tr.ip.isSome() and tr.udp.isSome():
|
if r.ip.isSome() and r.udp.isSome():
|
||||||
let a = Address(ip: ipv4(tr.ip.get()), port: Port(tr.udp.get()))
|
let a = Address(ip: ipv4(r.ip.get()), port: Port(r.udp.get()))
|
||||||
|
|
||||||
ok(Node(id: nodeId, pubkey: pk.get() , record: r,
|
ok(Node(
|
||||||
address: some(a)))
|
id: nodeId,
|
||||||
|
pubkey: pk.get(),
|
||||||
|
record: r,
|
||||||
|
address: some(a)))
|
||||||
else:
|
else:
|
||||||
ok(Node(id: nodeId, pubkey: pk.get(), record: r,
|
ok(Node(
|
||||||
address: none(Address)))
|
id: nodeId,
|
||||||
|
pubkey: pk.get(),
|
||||||
|
record: r,
|
||||||
|
address: none(Address)))
|
||||||
|
|
||||||
proc update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress],
|
proc update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress],
|
||||||
tcpPort, udpPort: Option[Port] = none[Port]()): Result[void, cstring] =
|
tcpPort, udpPort: Option[Port] = none[Port]()): Result[void, cstring] =
|
||||||
|
|||||||
@ -170,7 +170,6 @@ type
|
|||||||
ipVote: IpVote
|
ipVote: IpVote
|
||||||
enrAutoUpdate: bool
|
enrAutoUpdate: bool
|
||||||
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
|
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
|
||||||
# overkill here, use sequence
|
|
||||||
rng*: ref BrHmacDrbgContext
|
rng*: ref BrHmacDrbgContext
|
||||||
providers: ProvidersManager
|
providers: ProvidersManager
|
||||||
|
|
||||||
@ -1102,12 +1101,19 @@ proc newProtocol*(
|
|||||||
bindIp = IPv4_any(),
|
bindIp = IPv4_any(),
|
||||||
config = defaultDiscoveryConfig,
|
config = defaultDiscoveryConfig,
|
||||||
rng = newRng(),
|
rng = newRng(),
|
||||||
providers = ProvidersManager.new(
|
providers = ProvidersManager.new(SQLiteDatastore.new(Memory)
|
||||||
SQLiteDatastore.new(Memory)
|
.expect("Should not fail!"))): Protocol =
|
||||||
.expect("Should not fail!"))):
|
## Initialize DHT protocol
|
||||||
Protocol =
|
##
|
||||||
|
|
||||||
info "Discovery SPR initialized", seqNum = record.seqNum, uri = toURI(record)
|
info "Discovery SPR initialized", seqNum = record.seqNum, uri = toURI(record)
|
||||||
let node = newNode(record).expect("Properly initialized record")
|
|
||||||
|
let
|
||||||
|
node = newNode(
|
||||||
|
bindIp,
|
||||||
|
bindPort,
|
||||||
|
privKey.getPublicKey.expect("Should get public key"),
|
||||||
|
record).expect("Properly initialized record")
|
||||||
|
|
||||||
# TODO Consider whether this should be a Defect
|
# TODO Consider whether this should be a Defect
|
||||||
doAssert rng != nil, "RNG initialization failed"
|
doAssert rng != nil, "RNG initialization failed"
|
||||||
@ -1125,7 +1131,6 @@ proc newProtocol*(
|
|||||||
|
|
||||||
result.transport = newTransport(result, privKey, node, bindPort, bindIp, 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
|
||||||
|
|
||||||
|
|||||||
@ -32,7 +32,7 @@ proc cleanupExpired*(
|
|||||||
now = Moment.now()
|
now = Moment.now()
|
||||||
|
|
||||||
let
|
let
|
||||||
q = Query.init(CidKey)
|
q = Query.init(CidKey, limit = batchSize)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
without iter =? (await store.query(q)), err:
|
without iter =? (await store.query(q)), err:
|
||||||
@ -74,7 +74,7 @@ proc cleanupOrphaned*(
|
|||||||
trace "Cleaning up orphaned records"
|
trace "Cleaning up orphaned records"
|
||||||
|
|
||||||
let
|
let
|
||||||
providersQuery = Query.init(ProvidersKey)
|
providersQuery = Query.init(ProvidersKey, limit = batchSize)
|
||||||
|
|
||||||
block:
|
block:
|
||||||
without iter =? (await store.query(providersQuery)), err:
|
without iter =? (await store.query(providersQuery)), err:
|
||||||
|
|||||||
@ -184,16 +184,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] =
|
||||||
for address in r.data.addresses:
|
for address in r.data.addresses:
|
||||||
let ma = address.address
|
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
|
||||||
|
|||||||
@ -197,13 +197,12 @@ proc closeWait*(t: Transport) {.async.} =
|
|||||||
await t.transp.closeWait
|
await t.transp.closeWait
|
||||||
|
|
||||||
proc newTransport*[T](
|
proc newTransport*[T](
|
||||||
client: T,
|
client: T,
|
||||||
privKey: PrivateKey,
|
privKey: PrivateKey,
|
||||||
localNode: Node,
|
localNode: Node,
|
||||||
bindPort: Port,
|
bindPort: Port,
|
||||||
bindIp = IPv4_any(),
|
bindIp = IPv4_any(),
|
||||||
rng = newRng()):
|
rng = newRng()): Transport[T]=
|
||||||
Transport[T]=
|
|
||||||
|
|
||||||
# TODO Consider whether this should be a Defect
|
# TODO Consider whether this should be a Defect
|
||||||
doAssert rng != nil, "RNG initialization failed"
|
doAssert rng != nil, "RNG initialization failed"
|
||||||
@ -211,6 +210,8 @@ proc newTransport*[T](
|
|||||||
Transport[T](
|
Transport[T](
|
||||||
client: client,
|
client: client,
|
||||||
bindAddress: Address(ip: ValidIpAddress.init(bindIp), port: bindPort),
|
bindAddress: Address(ip: ValidIpAddress.init(bindIp), port: bindPort),
|
||||||
codec: Codec(localNode: localNode, privKey: privKey,
|
codec: Codec(
|
||||||
|
localNode: localNode,
|
||||||
|
privKey: privKey,
|
||||||
sessions: Sessions.init(256)),
|
sessions: Sessions.init(256)),
|
||||||
rng: rng)
|
rng: rng)
|
||||||
|
|||||||
@ -36,7 +36,8 @@ proc initDiscoveryNode*(
|
|||||||
let protocol = newProtocol(
|
let protocol = newProtocol(
|
||||||
privKey,
|
privKey,
|
||||||
some(address.ip),
|
some(address.ip),
|
||||||
some(address.port), some(address.port),
|
some(address.port),
|
||||||
|
some(address.port),
|
||||||
bindPort = address.port,
|
bindPort = address.port,
|
||||||
bootstrapRecords = bootstrapRecords,
|
bootstrapRecords = bootstrapRecords,
|
||||||
localEnrFields = localEnrFields,
|
localEnrFields = localEnrFields,
|
||||||
@ -54,6 +55,7 @@ proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool =
|
|||||||
|
|
||||||
proc generateNode*(privKey: PrivateKey, port: int = 20302,
|
proc generateNode*(privKey: PrivateKey, port: int = 20302,
|
||||||
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node =
|
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node =
|
||||||
|
|
||||||
let
|
let
|
||||||
port = Port(port)
|
port = Port(port)
|
||||||
spr = SignedPeerRecord.init(1, privKey, some(ip), some(port), some(port))
|
spr = SignedPeerRecord.init(1, privKey, some(ip), some(port), some(port))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user