mirror of https://github.com/status-im/nim-eth.git
Extra test on newProtocol
This commit is contained in:
parent
95a09fdf7f
commit
d3db83fa0a
|
@ -258,6 +258,8 @@ proc update*(record: Record, pk: PrivateKey, fieldPairs: openarray[FieldPair]):
|
||||||
updated = true
|
updated = true
|
||||||
|
|
||||||
if updated:
|
if updated:
|
||||||
|
if r.seqNum == high(r.seqNum): # highly unlikely
|
||||||
|
return err("Maximum sequence number reached")
|
||||||
r.seqNum.inc()
|
r.seqNum.inc()
|
||||||
r.raw = ? makeEnrRaw(r.seqNum, pk, r.pairs)
|
r.raw = ? makeEnrRaw(r.seqNum, pk, r.pairs)
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,10 @@ proc nodesDiscovered*(d: Protocol): int {.inline.} = d.routingTable.len
|
||||||
func privKey*(d: Protocol): lent PrivateKey =
|
func privKey*(d: Protocol): lent PrivateKey =
|
||||||
d.privateKey
|
d.privateKey
|
||||||
|
|
||||||
proc updateEnr*(
|
func getRecord*(d: Protocol): Record =
|
||||||
|
d.localNode.record
|
||||||
|
|
||||||
|
proc updateRecord*(
|
||||||
d: Protocol, enrFields: openarray[(string, seq[byte])]): DiscResult[void] =
|
d: Protocol, enrFields: openarray[(string, seq[byte])]): DiscResult[void] =
|
||||||
let fields = mapIt(enrFields, toFieldPair(it[0], it[1]))
|
let fields = mapIt(enrFields, toFieldPair(it[0], it[1]))
|
||||||
d.localNode.record = ? d.localNode.record.update(d.privateKey, fields)
|
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,
|
externalIp: Option[ValidIpAddress], tcpPort, udpPort: Port,
|
||||||
localEnrFields: openarray[(string, seq[byte])] = [],
|
localEnrFields: openarray[(string, seq[byte])] = [],
|
||||||
bootstrapRecords: openarray[Record] = [],
|
bootstrapRecords: openarray[Record] = [],
|
||||||
previousEnr = none[enr.Record](),
|
previousRecord = none[enr.Record](),
|
||||||
bindIp = IPv4_any(), rng = newRng()):
|
bindIp = IPv4_any(), rng = newRng()):
|
||||||
Protocol {.raises: [Defect].} =
|
Protocol {.raises: [Defect].} =
|
||||||
# TODO: Tried adding bindPort = udpPort as parameter but that gave
|
# TODO: Tried adding bindPort = udpPort as parameter but that gave
|
||||||
|
@ -712,8 +715,8 @@ proc newProtocol*(privKey: PrivateKey, db: Database,
|
||||||
# TODO:
|
# TODO:
|
||||||
# - Defect as is now or return a result for enr errors?
|
# - 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)?
|
# - In case incorrect key, allow for new enr based on new key (new node id)?
|
||||||
enr = if previousEnr.isSome():
|
enr = if previousRecord.isSome():
|
||||||
previousEnr.get().update(privKey, externalIp, tcpPort, udpPort,
|
previousRecord.get().update(privKey, externalIp, tcpPort, udpPort,
|
||||||
extraFields).expect("Record within size limits and correct key")
|
extraFields).expect("Record within size limits and correct key")
|
||||||
else:
|
else:
|
||||||
enr.Record.init(1, privKey, externalIp, tcpPort, udpPort,
|
enr.Record.init(1, privKey, externalIp, tcpPort, udpPort,
|
||||||
|
|
|
@ -10,14 +10,16 @@ proc localAddress*(port: int): Address =
|
||||||
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
|
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey,
|
||||||
address: Address,
|
address: Address,
|
||||||
bootstrapRecords: openarray[Record] = [],
|
bootstrapRecords: openarray[Record] = [],
|
||||||
localEnrFields: openarray[(string, seq[byte])] = []):
|
localEnrFields: openarray[(string, seq[byte])] = [],
|
||||||
|
previousRecord = none[enr.Record]()):
|
||||||
discv5_protocol.Protocol =
|
discv5_protocol.Protocol =
|
||||||
var db = DiscoveryDB.init(newMemoryDB())
|
var db = DiscoveryDB.init(newMemoryDB())
|
||||||
result = newProtocol(privKey, db,
|
result = newProtocol(privKey, db,
|
||||||
some(address.ip),
|
some(address.ip),
|
||||||
address.port, address.port,
|
address.port, address.port,
|
||||||
bootstrapRecords = bootstrapRecords,
|
bootstrapRecords = bootstrapRecords,
|
||||||
localEnrFields = localEnrFields, rng = rng)
|
localEnrFields = localEnrFields,
|
||||||
|
previousRecord = previousRecord, rng = rng)
|
||||||
|
|
||||||
result.open()
|
result.open()
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import
|
import
|
||||||
chronos, chronicles, tables, stint, testutils/unittests,
|
chronos, chronicles, tables, stint, testutils/unittests,
|
||||||
stew/shims/net, eth/keys, bearssl,
|
stew/shims/net, eth/[keys, trie/db], bearssl,
|
||||||
eth/p2p/discoveryv5/[enr, node, types, routing_table, encoding],
|
eth/p2p/discoveryv5/[enr, node, types, routing_table, encoding, discovery_db],
|
||||||
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
||||||
./discv5_test_helper
|
./discv5_test_helper
|
||||||
|
|
||||||
|
@ -336,7 +336,7 @@ procSuite "Discovery v5 Tests":
|
||||||
|
|
||||||
targetSeqNum.inc()
|
targetSeqNum.inc()
|
||||||
# need to add something to get the enr sequence number incremented
|
# 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()
|
check update.isOk()
|
||||||
|
|
||||||
let n = await mainNode.resolve(targetId)
|
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.
|
# close targetNode, resolve should lookup, check if we get updated ENR.
|
||||||
block:
|
block:
|
||||||
targetSeqNum.inc()
|
targetSeqNum.inc()
|
||||||
let update = targetNode.updateEnr({"addsomefield": @[byte 2]})
|
let update = targetNode.updateRecord({"addsomefield": @[byte 2]})
|
||||||
|
|
||||||
# ping node so that its ENR gets added
|
# ping node so that its ENR gets added
|
||||||
check (await targetNode.ping(lookupNode.localNode)).isOk()
|
check (await targetNode.ping(lookupNode.localNode)).isOk()
|
||||||
|
@ -392,3 +392,29 @@ procSuite "Discovery v5 Tests":
|
||||||
check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode)
|
check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode)
|
||||||
|
|
||||||
await lookupNode.closeWait()
|
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()))
|
||||||
|
|
Loading…
Reference in New Issue