Update enrs in buckets and replacement caches and fix empty ENR list in AuthResponse

This commit is contained in:
kdeme 2020-07-17 11:14:08 +02:00
parent ac5155394f
commit 84fd39a8f8
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
4 changed files with 22 additions and 15 deletions

View File

@ -103,9 +103,10 @@ proc encodeAuthHeader*(rng: var BrHmacDrbgContext,
var resp = AuthResponse(version: 5)
let ln = c.localNode
# TODO: What goes over the wire now in case of no updated ENR?
if challenge.recordSeq < ln.record.seqNum:
resp.record = ln.record
# else:
# an uninitialized record which will get encoded as an empty rlp list.
let ephKeys = KeyPair.random(rng)
let signature = signIDNonce(c.privKey, challenge.idNonce,
@ -118,7 +119,7 @@ proc encodeAuthHeader*(rng: var BrHmacDrbgContext,
let respRlp = rlp.encode(resp)
var zeroNonce: array[gcmNonceSize, byte]
let respEnc = encryptGCM(secrets.authRespKey, zeroNonce, respRLP, [])
let respEnc = encryptGCM(secrets.authRespKey, zeroNonce, respRlp, [])
let header = AuthHeader(auth: nonce, idNonce: challenge.idNonce,
scheme: authSchemeName, ephemeralKey: ephKeys.pubkey.toRaw,

View File

@ -450,12 +450,9 @@ proc `$`(f: Field): string =
proc `$`*(r: Record): string =
result = "("
var first = true
result &= $r.seqNum
for (k, v) in r.pairs:
if first:
first = false
else:
result &= ", "
result &= ", "
result &= k
result &= ": "
result &= $v
@ -472,4 +469,7 @@ proc read*(rlp: var Rlp, T: typedesc[Record]):
rlp.skipElem()
proc append*(rlpWriter: var RlpWriter, value: Record) =
rlpWriter.appendRawBytes(value.raw)
if value.raw.len > 0:
rlpWriter.appendRawBytes(value.raw)
else:
rlpWriter.startList(0)

View File

@ -110,6 +110,9 @@ proc add(k: KBucket, n: Node): Node =
k.lastUpdated = epochTime()
let nodeIdx = k.nodes.find(n)
if nodeIdx != -1:
if k.nodes[nodeIdx].record.seqNum < n.record.seqNum:
# In case of a newer record, it gets replaced.
k.nodes[nodeIdx].record = n.record
return nil
elif k.len < BUCKET_SIZE:
k.nodes.add(n)
@ -126,8 +129,11 @@ proc addReplacement(k: KBucket, n: Node) =
## to the tail.
let nodeIdx = k.replacementCache.find(n)
if nodeIdx != -1:
k.replacementCache.delete(nodeIdx)
k.replacementCache.add(n)
if k.replacementCache[nodeIdx].record.seqNum <= n.record.seqNum:
# In case the record sequence number is higher or the same, the node gets
# moved to the tail.
k.replacementCache.delete(nodeIdx)
k.replacementCache.add(n)
else:
doAssert(k.replacementCache.len <= REPLACEMENT_CACHE_SIZE)
if k.replacementCache.len == REPLACEMENT_CACHE_SIZE:

View File

@ -10,7 +10,7 @@ suite "ENR":
var pk = PrivateKey.fromHex(
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
var r = initRecord(123, pk, {"udp": 1234'u, "ip": [byte 5, 6, 7, 8]})[]
check($r == """(id: "v4", ip: 0x05060708, secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, udp: 1234)""")
check($r == """(123, id: "v4", ip: 0x05060708, secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, udp: 1234)""")
let uri = r.toURI()
var r2: Record
let sigValid = r2.fromURI(uri)
@ -22,7 +22,7 @@ suite "ENR":
var pk = PrivateKey.fromHex(
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
var r = initRecord(123, pk, {"udp": 1234'u, "ip": [byte 5, 6, 7, 8]})[]
check($r == """(id: "v4", ip: 0x05060708, secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, udp: 1234)""")
check($r == """(123, id: "v4", ip: 0x05060708, secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, udp: 1234)""")
let encoded = rlp.encode(r)
let decoded = rlp.decode(encoded, enr.Record)
check($decoded == $r)
@ -44,7 +44,7 @@ suite "ENR":
var r: Record
let sigValid = r.fromBase64("-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8")
check(sigValid)
check($r == """(id: "v4", ip: 0x7F000001, secp256k1: 0x03CA634CAE0D49ACB401D8A4C6B6FE8C55B70D115BF400769CC1400F3258CD3138, udp: 30303)""")
check($r == """(1, id: "v4", ip: 0x7F000001, secp256k1: 0x03CA634CAE0D49ACB401D8A4C6B6FE8C55B70D115BF400769CC1400F3258CD3138, udp: 30303)""")
test "Bad base64":
var r: Record
@ -146,13 +146,13 @@ suite "ENR":
"z": [byte 0],
"123": "abc",
"a12": 1'u})[]
check $r == """(123: "abc", a12: 1, abc: 1234, id: "v4", secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, z: 0x00)"""
check $r == """(123, 123: "abc", a12: 1, abc: 1234, id: "v4", secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, z: 0x00)"""
let newField = toFieldPair("test", 123'u)
let newField2 = toFieldPair("zzz", 123'u)
let updated = r.update(pk, [newField, newField2])
check updated.isOk()
check $r == """(123: "abc", a12: 1, abc: 1234, id: "v4", secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, test: 123, z: 0x00, zzz: 123)"""
check $r == """(124, 123: "abc", a12: 1, abc: 1234, id: "v4", secp256k1: 0x02E51EFA66628CE09F689BC2B82F165A75A9DDECBB6A804BE15AC3FDF41F3B34E7, test: 123, z: 0x00, zzz: 123)"""
test "ENR update size too big":
let pk = PrivateKey.fromHex(