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,16 +74,21 @@ 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(
|
||||||
|
id: nodeId,
|
||||||
|
pubkey: pk.get(),
|
||||||
|
record: r,
|
||||||
address: some(a)))
|
address: some(a)))
|
||||||
else:
|
else:
|
||||||
ok(Node(id: nodeId, pubkey: pk.get(), record: r,
|
ok(Node(
|
||||||
|
id: nodeId,
|
||||||
|
pubkey: pk.get(),
|
||||||
|
record: r,
|
||||||
address: none(Address)))
|
address: none(Address)))
|
||||||
|
|
||||||
proc update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress],
|
proc update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress],
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -202,8 +202,7 @@ proc newTransport*[T](
|
||||||
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…
Reference in New Issue