Skip to content

Commit 888e8e3

Browse files
authored
fix: improve light client type test coverage (#1636)
1 parent add9be3 commit 888e8e3

File tree

6 files changed

+111
-134
lines changed

6 files changed

+111
-134
lines changed

crates/ethportal-api/src/types/content_value/beacon.rs

Lines changed: 110 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -548,140 +548,141 @@ impl ContentValue for BeaconContentValue {
548548

549549
#[cfg(test)]
550550
mod test {
551-
use std::fs;
551+
use std::{fs, str::FromStr};
552552

553+
use alloy::primitives::Bytes;
553554
use serde::Deserialize;
555+
use serde_yaml::Value;
554556

555557
use super::*;
556558

557-
#[test]
558-
fn light_client_bootstrap_encode_decode() {
559-
let file = fs::read_to_string(
560-
"../../test_assets/portalnet/content/beacon/light_client_bootstrap.json",
561-
)
559+
#[rstest::rstest]
560+
#[case("capella", 6718368)]
561+
#[case("deneb", 10248000)]
562+
fn light_client_bootstrap_encode_decode(#[case] fork_name: &str, #[case] expected_slot: u64) {
563+
let file = fs::read_to_string(format!(
564+
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/bootstrap.yaml",
565+
))
562566
.unwrap();
563-
let json: serde_json::Value = serde_json::from_str(&file).unwrap();
564-
let json = json.as_object().unwrap();
565-
for (slot_num, obj) in json {
566-
let slot_num: u64 = slot_num.parse().unwrap();
567-
let content_key = BeaconContentKey::deserialize(&obj["content_key"]).unwrap();
568-
let content_bytes = RawContentValue::deserialize(&obj["content_value"]).unwrap();
569-
let beacon_content = BeaconContentValue::decode(&content_key, &content_bytes).unwrap();
570-
571-
match &beacon_content {
572-
BeaconContentValue::LightClientBootstrap(value) => {
573-
assert_eq!(
574-
slot_num,
575-
value.bootstrap.header_capella().unwrap().beacon.slot
576-
);
577-
}
578-
_ => panic!("Invalid beacon content type!"),
579-
}
580567

581-
assert_eq!(content_bytes, beacon_content.encode());
568+
let value: Value = serde_yaml::from_str(&file).unwrap();
569+
let content_key: BeaconContentKey =
570+
serde_yaml::from_value(value["content_key"].clone()).unwrap();
571+
let raw_content_value = Bytes::from_str(value["content_value"].as_str().unwrap()).unwrap();
572+
let content_value = BeaconContentValue::decode(&content_key, raw_content_value.as_ref())
573+
.expect("unable to decode content value");
574+
575+
assert_str_roundtrip(content_key, content_value.clone());
582576

583-
assert_str_roundtrip(content_key, beacon_content);
577+
match content_value {
578+
BeaconContentValue::LightClientBootstrap(value) => {
579+
assert_eq!(expected_slot, value.get_slot());
580+
}
581+
_ => panic!("Invalid beacon content type!"),
584582
}
585583
}
586584

587-
#[test]
588-
fn light_client_updates_by_range_encode_decode() {
589-
let file = fs::read_to_string(
590-
"../../test_assets/portalnet/content/beacon/light_client_updates_by_range.json",
591-
)
585+
#[rstest::rstest]
586+
#[case("capella", 6684738)]
587+
#[case("deneb", 10240088)]
588+
fn light_client_updates_by_range_encode_decode(
589+
#[case] fork_name: &str,
590+
#[case] expected_slot: u64,
591+
) {
592+
let file = fs::read_to_string(format!(
593+
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/updates.yaml",
594+
))
592595
.unwrap();
593-
let json: serde_json::Value = serde_json::from_str(&file).unwrap();
594-
let json = json.as_object().unwrap();
595-
for (slot_num, obj) in json {
596-
let slot_num: u64 = slot_num.parse().unwrap();
597-
let content_key = BeaconContentKey::deserialize(&obj["content_key"]).unwrap();
598-
let content_bytes = RawContentValue::deserialize(&obj["content_value"]).unwrap();
599-
let beacon_content = BeaconContentValue::decode(&content_key, &content_bytes).unwrap();
600-
601-
match &beacon_content {
602-
BeaconContentValue::LightClientUpdatesByRange(updates) => {
603-
assert_eq!(
604-
slot_num,
605-
updates[0]
606-
.update
607-
.attested_header_capella()
608-
.unwrap()
609-
.beacon
610-
.slot
611-
);
612-
assert_eq!(updates.len(), 4)
613-
}
614-
_ => panic!("Invalid beacon content type!"),
615-
}
616596

617-
assert_eq!(content_bytes, beacon_content.encode());
597+
let value: Value = serde_yaml::from_str(&file).unwrap();
598+
let content_key: BeaconContentKey =
599+
serde_yaml::from_value(value["content_key"].clone()).unwrap();
600+
let raw_content_value = Bytes::from_str(value["content_value"].as_str().unwrap()).unwrap();
601+
let content_value = BeaconContentValue::decode(&content_key, raw_content_value.as_ref())
602+
.expect("unable to decode content value");
618603

619-
assert_str_roundtrip(content_key, beacon_content);
620-
}
621-
}
604+
assert_str_roundtrip(content_key, content_value.clone());
622605

623-
#[test]
624-
fn light_client_optimistic_update_encode_decode() {
625-
let file = fs::read_to_string(
626-
"../../test_assets/portalnet/content/beacon/light_client_optimistic_update.json",
627-
)
606+
let update = match content_value {
607+
BeaconContentValue::LightClientUpdatesByRange(value) => value[0].update.clone(),
608+
_ => panic!("Invalid beacon content type!"),
609+
};
610+
let actual_slot = match fork_name {
611+
"capella" => update.attested_header_capella().unwrap().beacon.slot,
612+
"deneb" => update.attested_header_deneb().unwrap().beacon.slot,
613+
_ => panic!("Invalid fork name!"),
614+
};
615+
assert_eq!(actual_slot, expected_slot);
616+
}
617+
618+
#[rstest::rstest]
619+
#[case("capella", 6718463)]
620+
#[case("deneb", 10248457)]
621+
fn light_client_optimistic_update_encode_decode(
622+
#[case] fork_name: &str,
623+
#[case] expected_slot: u64,
624+
) {
625+
let file = fs::read_to_string(format!(
626+
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/optimistic_update.yaml",
627+
))
628628
.unwrap();
629-
let json: serde_json::Value = serde_json::from_str(&file).unwrap();
630-
let json = json.as_object().unwrap();
631-
for (slot_num, obj) in json {
632-
let slot_num: u64 = slot_num.parse().unwrap();
633-
let content_key = BeaconContentKey::deserialize(&obj["content_key"]).unwrap();
634-
let content_bytes = RawContentValue::deserialize(&obj["content_value"]).unwrap();
635-
let beacon_content = BeaconContentValue::decode(&content_key, &content_bytes).unwrap();
636-
637-
match &beacon_content {
638-
BeaconContentValue::LightClientOptimisticUpdate(value) => {
639-
assert_eq!(
640-
slot_num,
641-
value.update.attested_header_capella().unwrap().beacon.slot
642-
);
643-
}
644-
_ => panic!("Invalid beacon content type!"),
645-
}
646629

647-
assert_eq!(content_bytes, beacon_content.encode());
630+
let value: Value = serde_yaml::from_str(&file).unwrap();
631+
let content_key: BeaconContentKey =
632+
serde_yaml::from_value(value["content_key"].clone()).unwrap();
633+
let raw_content_value = Bytes::from_str(value["content_value"].as_str().unwrap()).unwrap();
634+
let content_value = BeaconContentValue::decode(&content_key, raw_content_value.as_ref())
635+
.expect("unable to decode content value");
648636

649-
assert_str_roundtrip(content_key, beacon_content);
650-
}
651-
}
637+
assert_str_roundtrip(content_key, content_value.clone());
652638

653-
#[test]
654-
fn light_client_finality_update_encode_decode() {
655-
let file = fs::read_to_string(
656-
"../../test_assets/portalnet/content/beacon/light_client_finality_update.json",
657-
)
639+
let update = match content_value {
640+
BeaconContentValue::LightClientOptimisticUpdate(value) => value.update,
641+
_ => panic!("Invalid beacon content type!"),
642+
};
643+
let actual_slot = match fork_name {
644+
"capella" => update.attested_header_capella().unwrap().beacon.slot,
645+
"deneb" => update.attested_header_deneb().unwrap().beacon.slot,
646+
_ => panic!("Invalid fork name!"),
647+
};
648+
assert_eq!(actual_slot, expected_slot);
649+
}
650+
651+
#[rstest::rstest]
652+
#[case("capella", 6718463)]
653+
#[case("deneb", 10248453)]
654+
fn light_client_finality_update_encode_decode(
655+
#[case] fork_name: &str,
656+
#[case] expected_slot: u64,
657+
) {
658+
let file = fs::read_to_string(format!(
659+
"./../../portal-spec-tests/tests/mainnet/beacon_chain/light_client/{fork_name}/finality_update.yaml"
660+
))
658661
.unwrap();
659-
let json: serde_json::Value = serde_json::from_str(&file).unwrap();
660-
let json = json.as_object().unwrap();
661-
for (slot_num, obj) in json {
662-
let slot_num: u64 = slot_num.parse().unwrap();
663-
let content_key = BeaconContentKey::deserialize(&obj["content_key"]).unwrap();
664-
let content_bytes = RawContentValue::deserialize(&obj["content_value"]).unwrap();
665-
let beacon_content = BeaconContentValue::decode(&content_key, &content_bytes).unwrap();
666-
667-
match &beacon_content {
668-
BeaconContentValue::LightClientFinalityUpdate(value) => {
669-
assert_eq!(
670-
slot_num,
671-
value.update.attested_header_capella().unwrap().beacon.slot
672-
);
673-
}
674-
_ => panic!("Invalid beacon content type!"),
675-
}
676662

677-
assert_eq!(content_bytes, beacon_content.encode());
663+
let value: Value = serde_yaml::from_str(&file).unwrap();
664+
let content_key: BeaconContentKey =
665+
serde_yaml::from_value(value["content_key"].clone()).unwrap();
666+
let raw_content_value = Bytes::from_str(value["content_value"].as_str().unwrap()).unwrap();
667+
let content_value = BeaconContentValue::decode(&content_key, raw_content_value.as_ref())
668+
.expect("unable to decode content value");
678669

679-
assert_str_roundtrip(content_key, beacon_content);
680-
}
670+
assert_str_roundtrip(content_key, content_value.clone());
671+
672+
let update = match content_value {
673+
BeaconContentValue::LightClientFinalityUpdate(value) => value.update,
674+
_ => panic!("Invalid beacon content type!"),
675+
};
676+
let actual_slot = match fork_name {
677+
"capella" => update.attested_header_capella().unwrap().beacon.slot,
678+
"deneb" => update.attested_header_deneb().unwrap().beacon.slot,
679+
_ => panic!("Invalid fork name!"),
680+
};
681+
assert_eq!(actual_slot, expected_slot);
681682
}
682683

683684
#[test]
684-
fn historical_summaries_with_proof_encode_decode() {
685+
fn deneb_historical_summaries_with_proof_encode_decode() {
685686
let file = fs::read_to_string("./../../portal-spec-tests/tests/mainnet/beacon_chain/historical_summaries_with_proof/deneb/historical_summaries_with_proof.yaml").unwrap();
686687
let value: serde_yaml::Value = serde_yaml::from_str(&file).unwrap();
687688
let content_key = BeaconContentKey::deserialize(&value["content_key"]).unwrap();

test_assets/portalnet/content/beacon/light_client_bootstrap.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

test_assets/portalnet/content/beacon/light_client_finality_update.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

test_assets/portalnet/content/beacon/light_client_optimistic_update.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

test_assets/portalnet/content/beacon/light_client_updates_by_range.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)