mirror of https://github.com/status-im/nim-eth.git
Update enrs in buckets and replacement caches and fix empty ENR list in AuthResponse
This commit is contained in:
parent
ac5155394f
commit
84fd39a8f8
|
@ -103,9 +103,10 @@ proc encodeAuthHeader*(rng: var BrHmacDrbgContext,
|
||||||
var resp = AuthResponse(version: 5)
|
var resp = AuthResponse(version: 5)
|
||||||
let ln = c.localNode
|
let ln = c.localNode
|
||||||
|
|
||||||
# TODO: What goes over the wire now in case of no updated ENR?
|
|
||||||
if challenge.recordSeq < ln.record.seqNum:
|
if challenge.recordSeq < ln.record.seqNum:
|
||||||
resp.record = ln.record
|
resp.record = ln.record
|
||||||
|
# else:
|
||||||
|
# an uninitialized record which will get encoded as an empty rlp list.
|
||||||
|
|
||||||
let ephKeys = KeyPair.random(rng)
|
let ephKeys = KeyPair.random(rng)
|
||||||
let signature = signIDNonce(c.privKey, challenge.idNonce,
|
let signature = signIDNonce(c.privKey, challenge.idNonce,
|
||||||
|
@ -118,7 +119,7 @@ proc encodeAuthHeader*(rng: var BrHmacDrbgContext,
|
||||||
let respRlp = rlp.encode(resp)
|
let respRlp = rlp.encode(resp)
|
||||||
|
|
||||||
var zeroNonce: array[gcmNonceSize, byte]
|
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,
|
let header = AuthHeader(auth: nonce, idNonce: challenge.idNonce,
|
||||||
scheme: authSchemeName, ephemeralKey: ephKeys.pubkey.toRaw,
|
scheme: authSchemeName, ephemeralKey: ephKeys.pubkey.toRaw,
|
||||||
|
|
|
@ -450,12 +450,9 @@ proc `$`(f: Field): string =
|
||||||
|
|
||||||
proc `$`*(r: Record): string =
|
proc `$`*(r: Record): string =
|
||||||
result = "("
|
result = "("
|
||||||
var first = true
|
result &= $r.seqNum
|
||||||
for (k, v) in r.pairs:
|
for (k, v) in r.pairs:
|
||||||
if first:
|
result &= ", "
|
||||||
first = false
|
|
||||||
else:
|
|
||||||
result &= ", "
|
|
||||||
result &= k
|
result &= k
|
||||||
result &= ": "
|
result &= ": "
|
||||||
result &= $v
|
result &= $v
|
||||||
|
@ -472,4 +469,7 @@ proc read*(rlp: var Rlp, T: typedesc[Record]):
|
||||||
rlp.skipElem()
|
rlp.skipElem()
|
||||||
|
|
||||||
proc append*(rlpWriter: var RlpWriter, value: Record) =
|
proc append*(rlpWriter: var RlpWriter, value: Record) =
|
||||||
rlpWriter.appendRawBytes(value.raw)
|
if value.raw.len > 0:
|
||||||
|
rlpWriter.appendRawBytes(value.raw)
|
||||||
|
else:
|
||||||
|
rlpWriter.startList(0)
|
||||||
|
|
|
@ -110,6 +110,9 @@ proc add(k: KBucket, n: Node): Node =
|
||||||
k.lastUpdated = epochTime()
|
k.lastUpdated = epochTime()
|
||||||
let nodeIdx = k.nodes.find(n)
|
let nodeIdx = k.nodes.find(n)
|
||||||
if nodeIdx != -1:
|
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
|
return nil
|
||||||
elif k.len < BUCKET_SIZE:
|
elif k.len < BUCKET_SIZE:
|
||||||
k.nodes.add(n)
|
k.nodes.add(n)
|
||||||
|
@ -126,8 +129,11 @@ proc addReplacement(k: KBucket, n: Node) =
|
||||||
## to the tail.
|
## to the tail.
|
||||||
let nodeIdx = k.replacementCache.find(n)
|
let nodeIdx = k.replacementCache.find(n)
|
||||||
if nodeIdx != -1:
|
if nodeIdx != -1:
|
||||||
k.replacementCache.delete(nodeIdx)
|
if k.replacementCache[nodeIdx].record.seqNum <= n.record.seqNum:
|
||||||
k.replacementCache.add(n)
|
# 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:
|
else:
|
||||||
doAssert(k.replacementCache.len <= REPLACEMENT_CACHE_SIZE)
|
doAssert(k.replacementCache.len <= REPLACEMENT_CACHE_SIZE)
|
||||||
if k.replacementCache.len == REPLACEMENT_CACHE_SIZE:
|
if k.replacementCache.len == REPLACEMENT_CACHE_SIZE:
|
||||||
|
|
|
@ -10,7 +10,7 @@ suite "ENR":
|
||||||
var pk = PrivateKey.fromHex(
|
var pk = PrivateKey.fromHex(
|
||||||
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
|
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
|
||||||
var r = initRecord(123, pk, {"udp": 1234'u, "ip": [byte 5, 6, 7, 8]})[]
|
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()
|
let uri = r.toURI()
|
||||||
var r2: Record
|
var r2: Record
|
||||||
let sigValid = r2.fromURI(uri)
|
let sigValid = r2.fromURI(uri)
|
||||||
|
@ -22,7 +22,7 @@ suite "ENR":
|
||||||
var pk = PrivateKey.fromHex(
|
var pk = PrivateKey.fromHex(
|
||||||
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
|
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
|
||||||
var r = initRecord(123, pk, {"udp": 1234'u, "ip": [byte 5, 6, 7, 8]})[]
|
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 encoded = rlp.encode(r)
|
||||||
let decoded = rlp.decode(encoded, enr.Record)
|
let decoded = rlp.decode(encoded, enr.Record)
|
||||||
check($decoded == $r)
|
check($decoded == $r)
|
||||||
|
@ -44,7 +44,7 @@ suite "ENR":
|
||||||
var r: Record
|
var r: Record
|
||||||
let sigValid = r.fromBase64("-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8")
|
let sigValid = r.fromBase64("-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8")
|
||||||
check(sigValid)
|
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":
|
test "Bad base64":
|
||||||
var r: Record
|
var r: Record
|
||||||
|
@ -146,13 +146,13 @@ suite "ENR":
|
||||||
"z": [byte 0],
|
"z": [byte 0],
|
||||||
"123": "abc",
|
"123": "abc",
|
||||||
"a12": 1'u})[]
|
"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 newField = toFieldPair("test", 123'u)
|
||||||
let newField2 = toFieldPair("zzz", 123'u)
|
let newField2 = toFieldPair("zzz", 123'u)
|
||||||
let updated = r.update(pk, [newField, newField2])
|
let updated = r.update(pk, [newField, newField2])
|
||||||
check updated.isOk()
|
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":
|
test "ENR update size too big":
|
||||||
let pk = PrivateKey.fromHex(
|
let pk = PrivateKey.fromHex(
|
||||||
|
|
Loading…
Reference in New Issue