2022-08-21 17:36:46 +00:00
|
|
|
# beacon_chain
|
2023-06-19 22:43:50 +00:00
|
|
|
# Copyright (c) 2021-2023 Status Research & Development GmbH
|
2022-08-21 17:36:46 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-03-29 19:17:48 +00:00
|
|
|
{.used.}
|
|
|
|
|
2021-03-24 10:48:53 +00:00
|
|
|
import
|
2021-12-22 12:37:31 +00:00
|
|
|
testutils/unittests,
|
2021-03-24 10:48:53 +00:00
|
|
|
chronos, stew/shims/net, eth/keys, eth/p2p/discoveryv5/enr,
|
2022-08-21 17:36:46 +00:00
|
|
|
../beacon_chain/spec/[forks, network],
|
2021-03-24 10:48:53 +00:00
|
|
|
../beacon_chain/networking/[eth2_network, eth2_discovery],
|
|
|
|
./testutil
|
|
|
|
|
2021-08-18 13:58:43 +00:00
|
|
|
proc new(T: type Eth2DiscoveryProtocol,
|
2021-03-24 10:48:53 +00:00
|
|
|
pk: keys.PrivateKey,
|
2023-10-26 23:16:15 +00:00
|
|
|
enrIp: Option[IpAddress], enrTcpPort, enrUdpPort: Option[Port],
|
|
|
|
bindPort: Port, bindIp: IpAddress,
|
2021-03-24 10:48:53 +00:00
|
|
|
enrFields: openArray[(string, seq[byte])] = [],
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext):
|
2023-08-25 09:29:07 +00:00
|
|
|
T {.raises: [CatchableError].} =
|
2023-10-26 23:16:15 +00:00
|
|
|
let optValidIpAddress =
|
|
|
|
if enrIp.isSome:
|
|
|
|
some ValidIpAddress.init enrIp.get
|
|
|
|
else:
|
|
|
|
none ValidIpAddress
|
2021-03-24 10:48:53 +00:00
|
|
|
|
2023-10-26 23:16:15 +00:00
|
|
|
newProtocol(pk, optValidIpAddress, enrTcpPort, enrUdpPort, enrFields,
|
2021-03-24 10:48:53 +00:00
|
|
|
bindPort = bindPort, bindIp = bindIp, rng = rng)
|
|
|
|
|
2022-06-21 08:29:16 +00:00
|
|
|
proc generateNode(rng: ref HmacDrbgContext, port: Port,
|
2021-03-24 10:48:53 +00:00
|
|
|
enrFields: openArray[(string, seq[byte])] = []): Eth2DiscoveryProtocol =
|
2023-10-26 23:16:15 +00:00
|
|
|
let ip = parseIpAddress("127.0.0.1")
|
2021-03-24 10:48:53 +00:00
|
|
|
Eth2DiscoveryProtocol.new(keys.PrivateKey.random(rng[]),
|
|
|
|
some(ip), some(port), some(port), port, ip, enrFields, rng = rng)
|
|
|
|
|
2021-09-21 22:25:49 +00:00
|
|
|
# TODO: Add tests with a syncnets preference
|
2021-10-21 13:09:19 +00:00
|
|
|
const noSyncnetsPreference = SyncnetBits()
|
2021-09-21 22:25:49 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
procSuite "Eth2 specific discovery tests":
|
2021-03-24 10:48:53 +00:00
|
|
|
let
|
2023-06-19 22:43:50 +00:00
|
|
|
rng = HmacDrbgContext.new()
|
2021-03-24 10:48:53 +00:00
|
|
|
enrForkId = ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0))
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
asyncTest "Subnet query":
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnets: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnets.setBit(34)
|
|
|
|
|
|
|
|
let
|
|
|
|
node1 = generateNode(rng, Port(5000))
|
2021-08-10 20:46:35 +00:00
|
|
|
node2 = generateNode(rng, Port(5001), {
|
|
|
|
enrForkIdField: SSZ.encode(enrForkId),
|
|
|
|
enrAttestationSubnetsField: SSZ.encode(attnets)
|
|
|
|
})
|
2021-03-24 10:48:53 +00:00
|
|
|
|
|
|
|
node1.open()
|
|
|
|
node2.open()
|
|
|
|
|
|
|
|
# ping in one direction to add node2 to routing table of node1
|
|
|
|
check (await node2.ping(node1.localNode)).isOk()
|
|
|
|
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnetsSelected: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnetsSelected.setBit(42)
|
|
|
|
attnetsSelected.setBit(34)
|
|
|
|
|
2021-09-21 22:25:49 +00:00
|
|
|
let discovered = await node1.queryRandom(
|
2022-03-11 10:51:53 +00:00
|
|
|
enrForkId, attnetsSelected, noSyncnetsPreference, 1)
|
2021-03-24 10:48:53 +00:00
|
|
|
check discovered.len == 1
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
asyncTest "Invalid attnets field":
|
2022-09-19 09:07:46 +00:00
|
|
|
var invalidAttnets: BitArray[ATTESTATION_SUBNET_COUNT.int div 2]
|
2021-03-24 10:48:53 +00:00
|
|
|
invalidAttnets.setBit(15)
|
|
|
|
# TODO: This doesn't fail actually.
|
2022-09-19 09:07:46 +00:00
|
|
|
# var invalidAttnets2: BitArray[ATTESTATION_SUBNET_COUNT.int * 2]
|
2021-03-24 10:48:53 +00:00
|
|
|
# invalidAttnets2.setBit(15)
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnets: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnets.setBit(15)
|
|
|
|
|
|
|
|
let
|
|
|
|
node1 = generateNode(rng, Port(5000))
|
2021-08-10 20:46:35 +00:00
|
|
|
node2 = generateNode(rng, Port(5001), {
|
|
|
|
enrForkIdField: SSZ.encode(enrForkId),
|
|
|
|
enrAttestationSubnetsField: SSZ.encode(invalidAttnets)
|
|
|
|
})
|
|
|
|
node3 = generateNode(rng, Port(5002), {
|
|
|
|
enrForkIdField: SSZ.encode(enrForkId),
|
|
|
|
enrAttestationSubnetsField: SSZ.encode(attnets)
|
|
|
|
})
|
2021-03-24 10:48:53 +00:00
|
|
|
|
|
|
|
node1.open()
|
|
|
|
node2.open()
|
|
|
|
node3.open()
|
|
|
|
|
|
|
|
check (await node2.ping(node1.localNode)).isOk()
|
|
|
|
check (await node3.ping(node1.localNode)).isOk()
|
|
|
|
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnetsSelected: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnetsSelected.setBit(15)
|
|
|
|
attnetsSelected.setBit(42)
|
|
|
|
|
2021-09-21 22:25:49 +00:00
|
|
|
let discovered = await node1.queryRandom(
|
2022-03-11 10:51:53 +00:00
|
|
|
enrForkId, attnetsSelected, noSyncnetsPreference, 1)
|
2021-03-24 10:48:53 +00:00
|
|
|
check discovered.len == 1
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
|
|
|
await node3.closeWait()
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
asyncTest "Subnet query after ENR update":
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnets: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnets.setBit(1)
|
|
|
|
|
|
|
|
let
|
|
|
|
node1 = generateNode(rng, Port(5000))
|
2021-08-10 20:46:35 +00:00
|
|
|
node2 = generateNode(rng, Port(5001), {
|
|
|
|
enrForkIdField: SSZ.encode(enrForkId),
|
|
|
|
enrAttestationSubnetsField: SSZ.encode(attnets)
|
|
|
|
})
|
2021-03-24 10:48:53 +00:00
|
|
|
|
|
|
|
node1.open()
|
|
|
|
node2.open()
|
|
|
|
|
|
|
|
check (await node2.ping(node1.localNode)).isOk()
|
|
|
|
|
2021-10-21 13:09:19 +00:00
|
|
|
var attnetsSelected: AttnetBits
|
2021-03-24 10:48:53 +00:00
|
|
|
attnetsSelected.setBit(2)
|
|
|
|
|
|
|
|
block:
|
2021-09-21 22:25:49 +00:00
|
|
|
let discovered = await node1.queryRandom(
|
2022-03-11 10:51:53 +00:00
|
|
|
enrForkId, attnetsSelected, noSyncnetsPreference, 1)
|
2021-03-24 10:48:53 +00:00
|
|
|
check discovered.len == 0
|
|
|
|
|
|
|
|
block:
|
|
|
|
attnets.setBit(2)
|
2021-08-10 20:46:35 +00:00
|
|
|
check node2.updateRecord({
|
|
|
|
enrForkIdField: SSZ.encode(enrForkId),
|
|
|
|
enrAttestationSubnetsField: SSZ.encode(attnets)
|
|
|
|
}).isOk()
|
2021-03-24 10:48:53 +00:00
|
|
|
|
2021-07-19 12:25:11 +00:00
|
|
|
let nodes = await node1.findNode(node2.localNode, @[0'u16])
|
2021-03-24 10:48:53 +00:00
|
|
|
check nodes.isOk() and nodes[].len > 0
|
|
|
|
discard node1.addNode(nodes[][0])
|
|
|
|
|
2021-09-21 22:25:49 +00:00
|
|
|
let discovered = await node1.queryRandom(
|
2022-03-11 10:51:53 +00:00
|
|
|
enrForkId, attnetsSelected, noSyncnetsPreference, 1)
|
2021-03-24 10:48:53 +00:00
|
|
|
check discovered.len == 1
|
|
|
|
|
|
|
|
await node1.closeWait()
|
|
|
|
await node2.closeWait()
|
2021-09-29 11:06:16 +00:00
|
|
|
|
|
|
|
suite "Fork id compatibility test":
|
|
|
|
test "Digest check":
|
|
|
|
check false == isCompatibleForkId(
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)),
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 9, 9, 9, 9]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)))
|
|
|
|
|
|
|
|
check true == isCompatibleForkId(
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)),
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)))
|
|
|
|
|
|
|
|
test "Fork check":
|
|
|
|
# Future fork should work
|
|
|
|
check true == isCompatibleForkId(
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)),
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 2, 2, 2, 2]),
|
|
|
|
next_fork_epoch: Epoch(2)))
|
|
|
|
|
|
|
|
# Past fork should fail
|
|
|
|
check false == isCompatibleForkId(
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 1]),
|
|
|
|
next_fork_epoch: Epoch(0)),
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)))
|
|
|
|
|
|
|
|
test "Next fork epoch check":
|
|
|
|
# Same fork should check next_fork_epoch
|
|
|
|
check false == isCompatibleForkId(
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(0)),
|
|
|
|
ENRForkID(
|
|
|
|
fork_digest: ForkDigest([byte 0, 1, 2, 3]),
|
|
|
|
next_fork_version: Version([byte 0, 0, 0, 0]),
|
|
|
|
next_fork_epoch: Epoch(2)))
|
2022-08-21 17:36:46 +00:00
|
|
|
|
|
|
|
suite "Discovery fork ID":
|
|
|
|
test "Expected fork IDs":
|
|
|
|
let genesis_validators_root = ZERO_HASH
|
|
|
|
var cfg = defaultRuntimeConfig
|
|
|
|
cfg.ALTAIR_FORK_EPOCH = 5.Epoch
|
|
|
|
cfg.BELLATRIX_FORK_EPOCH = 10.Epoch
|
|
|
|
|
|
|
|
let
|
|
|
|
# Phase 0
|
|
|
|
phase0ForkId = block:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.GENESIS_FORK_VERSION
|
|
|
|
next_fork_version = current_fork_version
|
|
|
|
fork_digest = compute_fork_digest(
|
|
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
forkId = ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
|
|
|
next_fork_version: next_fork_version,
|
|
|
|
next_fork_epoch: FAR_FUTURE_EPOCH)
|
|
|
|
for epoch in GENESIS_EPOCH ..< cfg.ALTAIR_FORK_EPOCH - 1:
|
|
|
|
check cfg.getDiscoveryForkID(epoch, genesis_validators_root) == forkId
|
|
|
|
forkId
|
|
|
|
|
|
|
|
# Altair should become visible 1 epoch before the fork
|
|
|
|
phase0AltairForkId = block:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.GENESIS_FORK_VERSION
|
|
|
|
next_fork_version = cfg.ALTAIR_FORK_VERSION
|
|
|
|
fork_digest = compute_fork_digest(
|
|
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
forkId = ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
|
|
|
next_fork_version: next_fork_version,
|
|
|
|
next_fork_epoch: cfg.ALTAIR_FORK_EPOCH)
|
|
|
|
for epoch in cfg.ALTAIR_FORK_EPOCH - 1 ..< cfg.ALTAIR_FORK_EPOCH:
|
|
|
|
check cfg.getDiscoveryForkID(epoch, genesis_validators_root) == forkId
|
|
|
|
forkId
|
|
|
|
|
|
|
|
# Altair
|
|
|
|
altairForkId = block:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.ALTAIR_FORK_VERSION
|
|
|
|
next_fork_version = current_fork_version
|
|
|
|
fork_digest = compute_fork_digest(
|
|
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
forkId = ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
|
|
|
next_fork_version: next_fork_version,
|
|
|
|
next_fork_epoch: FAR_FUTURE_EPOCH)
|
|
|
|
for epoch in cfg.ALTAIR_FORK_EPOCH ..< cfg.BELLATRIX_FORK_EPOCH - 1:
|
|
|
|
check cfg.getDiscoveryForkID(epoch, genesis_validators_root) == forkId
|
|
|
|
forkId
|
|
|
|
|
|
|
|
# Bellatrix should become visible 1 epoch before the fork
|
|
|
|
altairBellatrixForkId = block:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.ALTAIR_FORK_VERSION
|
|
|
|
next_fork_version = cfg.BELLATRIX_FORK_VERSION
|
|
|
|
fork_digest = compute_fork_digest(
|
|
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
forkId = ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
|
|
|
next_fork_version: next_fork_version,
|
|
|
|
next_fork_epoch: cfg.BELLATRIX_FORK_EPOCH)
|
|
|
|
for epoch in cfg.BELLATRIX_FORK_EPOCH - 1 ..< cfg.BELLATRIX_FORK_EPOCH:
|
|
|
|
check cfg.getDiscoveryForkID(epoch, genesis_validators_root) == forkId
|
|
|
|
forkId
|
|
|
|
|
|
|
|
# Bellatrix
|
|
|
|
bellatrixForkId = block:
|
|
|
|
let
|
|
|
|
current_fork_version = cfg.BELLATRIX_FORK_VERSION
|
|
|
|
next_fork_version = current_fork_version
|
|
|
|
fork_digest = compute_fork_digest(
|
|
|
|
current_fork_version, genesis_validators_root)
|
|
|
|
forkId = ENRForkID(
|
|
|
|
fork_digest: fork_digest,
|
|
|
|
next_fork_version: next_fork_version,
|
|
|
|
next_fork_epoch: FAR_FUTURE_EPOCH)
|
|
|
|
for epoch in cfg.BELLATRIX_FORK_EPOCH ..< cfg.BELLATRIX_FORK_EPOCH + 5:
|
|
|
|
check cfg.getDiscoveryForkID(epoch, genesis_validators_root) == forkId
|
|
|
|
forkId
|
|
|
|
|
|
|
|
check: # isCompatibleForkId(ourForkId, peerForkId)
|
|
|
|
isCompatibleForkId(phase0ForkId, phase0ForkId)
|
|
|
|
isCompatibleForkId(phase0ForkId, phase0AltairForkId)
|
|
|
|
not isCompatibleForkId(phase0ForkId, altairForkId)
|
|
|
|
not isCompatibleForkId(phase0ForkId, altairBellatrixForkId)
|
|
|
|
not isCompatibleForkId(phase0ForkId, bellatrixForkId)
|
|
|
|
|
|
|
|
not isCompatibleForkId(phase0AltairForkId, phase0ForkId) # fork -1 epoch
|
|
|
|
isCompatibleForkId(phase0AltairForkId, phase0AltairForkId)
|
|
|
|
not isCompatibleForkId(phase0AltairForkId, altairForkId)
|
|
|
|
not isCompatibleForkId(phase0AltairForkId, altairBellatrixForkId)
|
|
|
|
not isCompatibleForkId(phase0AltairForkId, bellatrixForkId)
|
|
|
|
|
|
|
|
not isCompatibleForkId(altairForkId, phase0ForkId)
|
|
|
|
not isCompatibleForkId(altairForkId, phase0AltairForkId)
|
|
|
|
isCompatibleForkId(altairForkId, altairForkId)
|
|
|
|
isCompatibleForkId(altairForkId, altairBellatrixForkId)
|
|
|
|
not isCompatibleForkId(altairForkId, bellatrixForkId)
|
|
|
|
|
|
|
|
not isCompatibleForkId(altairBellatrixForkId, phase0ForkId)
|
|
|
|
not isCompatibleForkId(altairBellatrixForkId, phase0AltairForkId)
|
|
|
|
not isCompatibleForkId(altairBellatrixForkId, altairForkId) # fork -1 ep
|
|
|
|
isCompatibleForkId(altairBellatrixForkId, altairBellatrixForkId)
|
|
|
|
not isCompatibleForkId(altairBellatrixForkId, bellatrixForkId)
|
|
|
|
|
|
|
|
not isCompatibleForkId(bellatrixForkId, phase0ForkId)
|
|
|
|
not isCompatibleForkId(bellatrixForkId, phase0AltairForkId)
|
|
|
|
not isCompatibleForkId(bellatrixForkId, altairForkId)
|
|
|
|
not isCompatibleForkId(bellatrixForkId, altairBellatrixForkId)
|
|
|
|
isCompatibleForkId(bellatrixForkId, bellatrixForkId)
|