Export some private APIs needed in nim-beacon-node

This commit is contained in:
Zahary Karadjov 2019-12-17 23:20:26 +02:00 committed by zah
parent eda6c2906c
commit 54bb1adac9
2 changed files with 16 additions and 8 deletions

View File

@ -8,6 +8,8 @@ type
raw*: seq[byte] # RLP encoded record
pairs: seq[(string, Field)] # sorted list of all key/value pairs
EnrUri* = distinct string
FieldKind = enum
kString,
kNum,
@ -197,6 +199,9 @@ proc fromURI*(r: var Record, s: string): bool =
if s.startsWith(prefix):
result = r.fromBase64(s[prefix.len .. ^1])
template fromURI*(r: var Record, url: EnrUri): bool =
fromURI(r, string(url))
proc toBase64*(r: Record): string =
result = Base64Url.encode(r.raw)

View File

@ -267,7 +267,7 @@ proc lookup(p: Protocol, target: NodeId): Future[seq[Node]] {.async.} =
if result.len < BUCKET_SIZE:
result.add(n)
proc lookupRandom(p: Protocol): Future[seq[Node]] =
proc lookupRandom*(p: Protocol): Future[seq[Node]] =
var id: NodeId
discard randomBytes(addr id, sizeof(id))
p.lookup(id)
@ -293,6 +293,15 @@ proc open*(d: Protocol) =
let ta = initTAddress(IPv4_any(), d.localNode.node.address.udpPort)
d.transp = newDatagramTransport(processClient, udata = d, local = ta)
proc addNode*(d: Protocol, r: Record) =
discard d.routingTable.addNode(newNode(r))
proc addNode*(d: Protocol, enr: EnrUri) =
var r: Record
let res = r.fromUri(enr)
doAssert(res)
discard d.routingTable.addNode(newNode(r))
when isMainModule:
import discovery_db
import eth/trie/db
@ -310,12 +319,6 @@ when isMainModule:
d.open()
result.add(d)
proc addNode(d: Protocol, enr: string) =
var r: Record
let res = r.fromUri(enr)
doAssert(res)
discard d.routingTable.addNode(newNode(r))
proc addNode(d: openarray[Protocol], enr: string) =
for dd in d: dd.addNode(enr)
@ -341,4 +344,4 @@ when isMainModule:
# echo "NODES ", i, ": ", nodes
waitFor test()
runForever()
runForever()