2024-05-03 09:50:18 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-10-24 12:16:40 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2024-02-28 17:31:45 +00:00
|
|
|
unittest2,
|
|
|
|
stew/byteutils,
|
|
|
|
stew/io2,
|
|
|
|
stew/results,
|
2023-07-08 15:01:33 +00:00
|
|
|
beacon_chain/networking/network_metadata,
|
2022-10-24 12:16:40 +00:00
|
|
|
beacon_chain/spec/forks,
|
|
|
|
beacon_chain/spec/datatypes/altair,
|
2023-10-20 10:06:25 +00:00
|
|
|
../../network/beacon/beacon_content,
|
2024-05-03 09:50:18 +00:00
|
|
|
../../eth_data/yaml_utils,
|
2023-10-20 10:06:25 +00:00
|
|
|
"."/light_client_test_data
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
suite "Beacon Content Encodings - Mainnet":
|
2023-07-08 15:01:33 +00:00
|
|
|
# These test vectors are generated by eth_data_exporter. The content is taken
|
|
|
|
# from mainnet and encoded as it would be transmitted on Portal Network,
|
|
|
|
# including also the content key.
|
|
|
|
const testVectorDir =
|
|
|
|
"./vendor/portal-spec-tests/tests/mainnet/beacon_chain/light_client/"
|
|
|
|
|
|
|
|
let
|
|
|
|
metadata = getMetadataForNetwork("mainnet")
|
|
|
|
genesisState =
|
|
|
|
try:
|
2024-02-28 17:31:45 +00:00
|
|
|
template genesisData(): auto =
|
|
|
|
metadata.genesis.bakedBytes
|
|
|
|
|
|
|
|
newClone(
|
|
|
|
readSszForkedHashedBeaconState(
|
|
|
|
metadata.cfg, genesisData.toOpenArray(genesisData.low, genesisData.high)
|
|
|
|
)
|
|
|
|
)
|
2023-07-08 15:01:33 +00:00
|
|
|
except CatchableError as err:
|
|
|
|
raiseAssert "Invalid baked-in state: " & err.msg
|
2024-02-28 17:31:45 +00:00
|
|
|
genesis_validators_root = getStateField(genesisState[], genesis_validators_root)
|
2023-07-08 15:01:33 +00:00
|
|
|
forkDigests = newClone ForkDigests.init(metadata.cfg, genesis_validators_root)
|
|
|
|
|
|
|
|
test "LightClientBootstrap":
|
2024-03-29 16:12:23 +00:00
|
|
|
const file = testVectorDir & "bootstrap.yaml"
|
|
|
|
let
|
|
|
|
c = YamlPortalContent.loadFromYaml(file).valueOr:
|
|
|
|
raiseAssert "Invalid test vector file: " & error
|
|
|
|
|
|
|
|
contentKeyEncoded = c.content_key.hexToSeqByte()
|
|
|
|
contentValueEncoded = c.content_value.hexToSeqByte()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
# Decode content and content key
|
2024-03-29 16:12:23 +00:00
|
|
|
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
|
|
|
contentValue =
|
|
|
|
decodeLightClientBootstrapForked(forkDigests[], contentValueEncoded)
|
|
|
|
check:
|
|
|
|
contentKey.isOk()
|
|
|
|
contentValue.isOk()
|
|
|
|
|
|
|
|
let bootstrap = contentValue.value()
|
|
|
|
let key = contentKey.value()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
withForkyObject(bootstrap):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
let blockRoot = hash_tree_root(forkyObject.header.beacon)
|
|
|
|
check blockRoot == key.lightClientBootstrapKey.blockHash
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
# re-encode content and content key
|
|
|
|
let encoded = encodeForkedLightClientObject(bootstrap, forkDigests.capella)
|
|
|
|
|
|
|
|
check encoded == contentValueEncoded
|
|
|
|
check encode(key).asSeq() == contentKeyEncoded
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
test "LightClientUpdates":
|
2024-03-29 16:12:23 +00:00
|
|
|
const file = testVectorDir & "updates.yaml"
|
|
|
|
let
|
|
|
|
c = YamlPortalContent.loadFromYaml(file).valueOr:
|
|
|
|
raiseAssert "Invalid test vector file: " & error
|
|
|
|
|
|
|
|
contentKeyEncoded = c.content_key.hexToSeqByte()
|
|
|
|
contentValueEncoded = c.content_value.hexToSeqByte()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
# Decode content and content key
|
2024-03-29 16:12:23 +00:00
|
|
|
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
|
|
|
contentValue = decodeLightClientUpdatesByRange(forkDigests[], contentValueEncoded)
|
|
|
|
check:
|
|
|
|
contentKey.isOk()
|
|
|
|
contentValue.isOk()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
let updates = contentValue.value()
|
|
|
|
let key = contentKey.value()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
check key.lightClientUpdateKey.count == uint64(updates.len())
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
for i, update in updates:
|
|
|
|
withForkyObject(update):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
check forkyObject.finalized_header.beacon.slot div
|
|
|
|
(SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD) ==
|
|
|
|
key.lightClientUpdateKey.startPeriod + uint64(i)
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
# re-encode content and content key
|
|
|
|
let encoded = encodeLightClientUpdatesForked(forkDigests.capella, updates.asSeq())
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
check encoded == contentValueEncoded
|
|
|
|
check encode(key).asSeq() == contentKeyEncoded
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
test "LightClientFinalityUpdate":
|
2024-03-29 16:12:23 +00:00
|
|
|
const file = testVectorDir & "finality_update.yaml"
|
|
|
|
let
|
|
|
|
c = YamlPortalContent.loadFromYaml(file).valueOr:
|
|
|
|
raiseAssert "Invalid test vector file: " & error
|
|
|
|
|
|
|
|
contentKeyEncoded = c.content_key.hexToSeqByte()
|
|
|
|
contentValueEncoded = c.content_value.hexToSeqByte()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
# Decode content and content key
|
2024-03-29 16:12:23 +00:00
|
|
|
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
|
|
|
contentValue =
|
|
|
|
decodeLightClientFinalityUpdateForked(forkDigests[], contentValueEncoded)
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
check:
|
|
|
|
contentKey.isOk()
|
|
|
|
contentValue.isOk()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
let update = contentValue.value()
|
|
|
|
let key = contentKey.value()
|
|
|
|
withForkyObject(update):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
check forkyObject.finalized_header.beacon.slot ==
|
|
|
|
key.lightClientFinalityUpdateKey.finalizedSlot
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
# re-encode content and content key
|
|
|
|
let encoded = encodeForkedLightClientObject(update, forkDigests.capella)
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
check encoded == contentValueEncoded
|
|
|
|
check encode(key).asSeq() == contentKeyEncoded
|
2023-07-08 15:01:33 +00:00
|
|
|
|
|
|
|
test "LightClientOptimisticUpdate":
|
2024-03-29 16:12:23 +00:00
|
|
|
const file = testVectorDir & "optimistic_update.yaml"
|
|
|
|
let
|
|
|
|
c = YamlPortalContent.loadFromYaml(file).valueOr:
|
|
|
|
raiseAssert "Invalid test vector file: " & error
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
contentKeyEncoded = c.content_key.hexToSeqByte()
|
|
|
|
contentValueEncoded = c.content_value.hexToSeqByte()
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2024-03-29 16:12:23 +00:00
|
|
|
# Decode content and content key
|
|
|
|
contentKey = decodeSsz(contentKeyEncoded, ContentKey)
|
|
|
|
contentValue =
|
|
|
|
decodeLightClientOptimisticUpdateForked(forkDigests[], contentValueEncoded)
|
|
|
|
|
|
|
|
check:
|
|
|
|
contentKey.isOk()
|
|
|
|
contentValue.isOk()
|
|
|
|
|
|
|
|
let update = contentValue.value()
|
|
|
|
let key = contentKey.value()
|
|
|
|
withForkyObject(update):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
check forkyObject.signature_slot ==
|
|
|
|
key.lightClientOptimisticUpdateKey.optimisticSlot
|
|
|
|
|
|
|
|
# re-encode content and content key
|
|
|
|
let encoded = encodeForkedLightClientObject(update, forkDigests.capella)
|
|
|
|
|
|
|
|
check encoded == contentValueEncoded
|
|
|
|
check encode(key).asSeq() == contentKeyEncoded
|
2023-07-08 15:01:33 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
suite "Beacon Content Encodings":
|
2023-07-08 15:01:33 +00:00
|
|
|
# TODO: These tests are less useful now and should instead be altered to
|
|
|
|
# use the consensus test vectors to simply test if encoding / decoding works
|
|
|
|
# fine for the different forks.
|
2024-02-28 17:31:45 +00:00
|
|
|
const forkDigests = ForkDigests(
|
|
|
|
phase0: ForkDigest([0'u8, 0, 0, 1]),
|
|
|
|
altair: ForkDigest([0'u8, 0, 0, 2]),
|
|
|
|
bellatrix: ForkDigest([0'u8, 0, 0, 3]),
|
|
|
|
capella: ForkDigest([0'u8, 0, 0, 4]),
|
|
|
|
deneb: ForkDigest([0'u8, 0, 0, 5]),
|
|
|
|
)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "LightClientBootstrap":
|
2022-10-24 12:16:40 +00:00
|
|
|
let
|
2023-02-26 18:18:03 +00:00
|
|
|
altairData = SSZ.decode(bootstrapBytes, altair.LightClientBootstrap)
|
|
|
|
bootstrap = ForkedLightClientBootstrap(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
encoded = encodeForkedLightClientObject(bootstrap, forkDigests.altair)
|
|
|
|
decoded = decodeLightClientBootstrapForked(forkDigests, encoded)
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().kind == LightClientDataFork.Altair
|
|
|
|
decoded.get().altairData == altairData
|
2022-10-24 12:16:40 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "LightClientUpdate":
|
2022-11-03 08:12:32 +00:00
|
|
|
let
|
2023-02-26 18:18:03 +00:00
|
|
|
altairData = SSZ.decode(lightClientUpdateBytes, altair.LightClientUpdate)
|
|
|
|
update = ForkedLightClientUpdate(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
encoded = encodeForkedLightClientObject(update, forkDigests.altair)
|
|
|
|
decoded = decodeLightClientUpdateForked(forkDigests, encoded)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().kind == LightClientDataFork.Altair
|
|
|
|
decoded.get().altairData == altairData
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "LightClientUpdateList":
|
2022-11-03 08:12:32 +00:00
|
|
|
let
|
2023-02-26 18:18:03 +00:00
|
|
|
altairData = SSZ.decode(lightClientUpdateBytes, altair.LightClientUpdate)
|
|
|
|
update = ForkedLightClientUpdate(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2022-11-03 08:12:32 +00:00
|
|
|
updateList = @[update, update]
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
encoded = encodeLightClientUpdatesForked(forkDigests.altair, updateList)
|
|
|
|
decoded = decodeLightClientUpdatesByRange(forkDigests, encoded)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().asSeq()[0].altairData == updateList[0].altairData
|
|
|
|
decoded.get().asSeq()[1].altairData == updateList[1].altairData
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "LightClientFinalityUpdate":
|
2022-11-03 08:12:32 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
altairData =
|
|
|
|
SSZ.decode(lightClientFinalityUpdateBytes, altair.LightClientFinalityUpdate)
|
2023-02-26 18:18:03 +00:00
|
|
|
update = ForkedLightClientFinalityUpdate(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
encoded = encodeForkedLightClientObject(update, forkDigests.altair)
|
|
|
|
decoded = decodeLightClientFinalityUpdateForked(forkDigests, encoded)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().kind == LightClientDataFork.Altair
|
|
|
|
decoded.get().altairData == altairData
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "LightClientOptimisticUpdate":
|
2022-11-03 08:12:32 +00:00
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
altairData =
|
|
|
|
SSZ.decode(lightClientOptimisticUpdateBytes, altair.LightClientOptimisticUpdate)
|
2023-02-26 18:18:03 +00:00
|
|
|
update = ForkedLightClientOptimisticUpdate(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
|
|
|
encoded = encodeForkedLightClientObject(update, forkDigests.altair)
|
|
|
|
decoded = decodeLightClientOptimisticUpdateForked(forkDigests, encoded)
|
2022-11-03 08:12:32 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decoded.isOk()
|
|
|
|
decoded.get().kind == LightClientDataFork.Altair
|
|
|
|
decoded.get().altairData == altairData
|
2022-11-03 08:12:32 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
test "Invalid LightClientBootstrap":
|
2022-10-24 12:16:40 +00:00
|
|
|
let
|
2023-02-26 18:18:03 +00:00
|
|
|
altairData = SSZ.decode(bootstrapBytes, altair.LightClientBootstrap)
|
|
|
|
# TODO: This doesn't make much sense with current API
|
|
|
|
bootstrap = ForkedLightClientBootstrap(
|
2024-02-28 17:31:45 +00:00
|
|
|
kind: LightClientDataFork.Altair, altairData: altairData
|
|
|
|
)
|
2023-02-26 18:18:03 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
encodedTooEarlyFork = encodeForkedLightClientObject(bootstrap, forkDigests.phase0)
|
|
|
|
encodedUnknownFork =
|
|
|
|
encodeForkedLightClientObject(bootstrap, ForkDigest([0'u8, 0, 0, 6]))
|
2022-10-24 12:16:40 +00:00
|
|
|
|
|
|
|
check:
|
2023-02-26 18:18:03 +00:00
|
|
|
decodeLightClientBootstrapForked(forkDigests, @[]).isErr()
|
|
|
|
decodeLightClientBootstrapForked(forkDigests, encodedTooEarlyFork).isErr()
|
|
|
|
decodeLightClientBootstrapForked(forkDigests, encodedUnknownFork).isErr()
|
2023-12-19 18:59:38 +00:00
|
|
|
|
|
|
|
suite "Beacon ContentKey Encodings ":
|
|
|
|
test "Invalid prefix - 0 value":
|
2024-02-28 17:31:45 +00:00
|
|
|
let encoded = ByteList.init(@[byte 0x00])
|
2023-12-19 18:59:38 +00:00
|
|
|
let decoded = decode(encoded)
|
|
|
|
|
|
|
|
check decoded.isNone()
|
|
|
|
|
|
|
|
test "Invalid prefix - before valid range":
|
|
|
|
let encoded = ByteList.init(@[byte 0x01])
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
|
|
|
|
check decoded.isNone()
|
|
|
|
|
|
|
|
test "Invalid prefix - after valid range":
|
|
|
|
let encoded = ByteList.init(@[byte 0x14])
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
|
|
|
|
check decoded.isNone()
|
|
|
|
|
|
|
|
test "Invalid key - empty input":
|
|
|
|
let encoded = ByteList.init(@[])
|
|
|
|
let decoded = decode(encoded)
|
|
|
|
|
|
|
|
check decoded.isNone()
|