Extra test on newProtocol

This commit is contained in:
kdeme 2020-07-08 13:13:29 +02:00
parent 95a09fdf7f
commit d3db83fa0a
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
4 changed files with 43 additions and 10 deletions

View File

@ -258,6 +258,8 @@ proc update*(record: Record, pk: PrivateKey, fieldPairs: openarray[FieldPair]):
updated = true
if updated:
if r.seqNum == high(r.seqNum): # highly unlikely
return err("Maximum sequence number reached")
r.seqNum.inc()
r.raw = ? makeEnrRaw(r.seqNum, pk, r.pairs)

View File

@ -172,7 +172,10 @@ proc nodesDiscovered*(d: Protocol): int {.inline.} = d.routingTable.len
func privKey*(d: Protocol): lent PrivateKey =
d.privateKey
proc updateEnr*(
func getRecord*(d: Protocol): Record =
d.localNode.record
proc updateRecord*(
d: Protocol, enrFields: openarray[(string, seq[byte])]): DiscResult[void] =
let fields = mapIt(enrFields, toFieldPair(it[0], it[1]))
d.localNode.record = ? d.localNode.record.update(d.privateKey, fields)
@ -699,7 +702,7 @@ proc newProtocol*(privKey: PrivateKey, db: Database,
externalIp: Option[ValidIpAddress], tcpPort, udpPort: Port,
localEnrFields: openarray[(string, seq[byte])] = [],
bootstrapRecords: openarray[Record] = [],
previousEnr = none[enr.Record](),
previousRecord = none[enr.Record](),
bindIp = IPv4_any(), rng = newRng()):
Protocol {.raises: [Defect].} =
# TODO: Tried adding bindPort = udpPort as parameter but that gave
@ -712,8 +715,8 @@ proc newProtocol*(privKey: PrivateKey, db: Database,
# TODO:
# - Defect as is now or return a result for enr errors?
# - In case incorrect key, allow for new enr based on new key (new node id)?
enr = if previousEnr.isSome():
previousEnr.get().update(privKey, externalIp, tcpPort, udpPort,
enr = if previousRecord.isSome():
previousRecord.get().update(privKey, externalIp, tcpPort, udpPort,
extraFields).expect("Record within size limits and correct key")
else:
enr.Record.init(1, privKey, externalIp, tcpPort, udpPort,

View File

@ -10,14 +10,16 @@ proc localAddress*(port: int): Address =
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
address: Address,
bootstrapRecords: openarray[Record] = [],
localEnrFields: openarray[(string, seq[byte])] = []):
localEnrFields: openarray[(string, seq[byte])] = [],
previousRecord = none[enr.Record]()):
discv5_protocol.Protocol =
var db = DiscoveryDB.init(newMemoryDB())
result = newProtocol(privKey, db,
some(address.ip),
address.port, address.port,
bootstrapRecords = bootstrapRecords,
localEnrFields = localEnrFields, rng = rng)
localEnrFields = localEnrFields,
previousRecord = previousRecord, rng = rng)
result.open()

View File

@ -1,7 +1,7 @@
import
chronos, chronicles, tables, stint, testutils/unittests,
stew/shims/net, eth/keys, bearssl,
eth/p2p/discoveryv5/[enr, node, types, routing_table, encoding],
stew/shims/net, eth/[keys, trie/db], bearssl,
eth/p2p/discoveryv5/[enr, node, types, routing_table, encoding, discovery_db],
eth/p2p/discoveryv5/protocol as discv5_protocol,
./discv5_test_helper
@ -336,7 +336,7 @@ procSuite "Discovery v5 Tests":
targetSeqNum.inc()
# need to add something to get the enr sequence number incremented
let update = targetNode.updateEnr({"addsomefield": @[byte 1]})
let update = targetNode.updateRecord({"addsomefield": @[byte 1]})
check update.isOk()
let n = await mainNode.resolve(targetId)
@ -349,7 +349,7 @@ procSuite "Discovery v5 Tests":
# close targetNode, resolve should lookup, check if we get updated ENR.
block:
targetSeqNum.inc()
let update = targetNode.updateEnr({"addsomefield": @[byte 2]})
let update = targetNode.updateRecord({"addsomefield": @[byte 2]})
# ping node so that its ENR gets added
check (await targetNode.ping(lookupNode.localNode)).isOk()
@ -392,3 +392,29 @@ procSuite "Discovery v5 Tests":
check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode)
await lookupNode.closeWait()
test "New protocol with enr":
let
privKey = PrivateKey.random(rng[])
ip = some(ValidIpAddress.init("127.0.0.1"))
port = Port(20301)
db = DiscoveryDB.init(newMemoryDB())
node = newProtocol(privKey, db, ip, port, port, rng = rng)
noUpdatesNode = newProtocol(privKey, db, ip, port, port, rng = rng,
previousRecord = some(node.getRecord()))
updatesNode = newProtocol(privKey, db, ip, port, Port(20302), rng = rng,
previousRecord = some(noUpdatesNode.getRecord()))
moreUpdatesNode = newProtocol(privKey, db, ip, port, port, rng = rng,
localEnrFields = {"addfield": @[byte 0]},
previousRecord = some(updatesNode.getRecord()))
check:
node.getRecord().seqNum == 1
noUpdatesNode.getRecord().seqNum == 1
updatesNode.getRecord().seqNum == 2
moreUpdatesNode.getRecord().seqNum == 3
# Defect (for now?) on incorrect key use
expect ResultDefect:
let incorrectKeyUpdates = newProtocol(PrivateKey.random(rng[]),
db, ip, port, port, rng = rng,
previousRecord = some(updatesNode.getRecord()))