Skip to content

Commit d2f974f

Browse files
authored
feat(portal-bridge(beacon)): generate and gossip HistoricalSummariesWithProof content (#1389)
1 parent 8523913 commit d2f974f

File tree

8 files changed

+91555
-110
lines changed

8 files changed

+91555
-110
lines changed

ethportal-api/src/types/consensus/beacon_state.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct BeaconState {
6464
#[superstruct(getter(copy))]
6565
pub genesis_validators_root: B256,
6666
#[superstruct(getter(copy))]
67+
#[serde(deserialize_with = "as_u64")]
6768
pub slot: u64,
6869
#[superstruct(getter(copy))]
6970
pub fork: Fork,
@@ -84,12 +85,14 @@ pub struct BeaconState {
8485

8586
// Registry
8687
pub validators: VariableList<Validator, ValidatorRegistryLimit>,
88+
#[serde(deserialize_with = "ssz_types::serde_utils::quoted_u64_var_list::deserialize")]
8789
pub balances: VariableList<u64, ValidatorRegistryLimit>,
8890

8991
// Randomness
9092
pub randao_mixes: FixedVector<B256, EpochsPerHistoricalVector>,
9193

9294
// Slashings
95+
#[serde(deserialize_with = "ssz_types::serde_utils::quoted_u64_fixed_vec::deserialize")]
9396
pub slashings: FixedVector<u64, EpochsPerSlashingsVector>,
9497

9598
// Participation (Altair and later)
@@ -109,6 +112,7 @@ pub struct BeaconState {
109112

110113
// Inactivity
111114
#[superstruct(only(Bellatrix, Capella, Deneb))]
115+
#[serde(deserialize_with = "ssz_types::serde_utils::quoted_u64_var_list::deserialize")]
112116
pub inactivity_scores: VariableList<u64, ValidatorRegistryLimit>,
113117

114118
// Light-client sync committees
@@ -366,9 +370,7 @@ mod test {
366370
"../test_assets/beacon/bellatrix/BeaconState/ssz_random/{case}/value.yaml"
367371
))
368372
.expect("cannot find test asset");
369-
let value: Value = serde_yaml::from_str(&value).unwrap();
370-
let content: BeaconStateBellatrix = serde_json::from_value(value).unwrap();
371-
373+
let content: BeaconStateBellatrix = serde_yaml::from_str(&value).unwrap();
372374
let compressed = std::fs::read(format!(
373375
"../test_assets/beacon/bellatrix/BeaconState/ssz_random/{case}/serialized.ssz_snappy"
374376
))
@@ -401,9 +403,7 @@ mod test {
401403
"../test_assets/beacon/capella/BeaconState/ssz_random/{case}/value.yaml"
402404
))
403405
.expect("cannot find test asset");
404-
let value: Value = serde_yaml::from_str(&value).unwrap();
405-
let content: BeaconStateCapella = serde_json::from_value(value).unwrap();
406-
406+
let content: BeaconStateCapella = serde_yaml::from_str(&value).unwrap();
407407
let compressed = std::fs::read(format!(
408408
"../test_assets/beacon/capella/BeaconState/ssz_random/{case}/serialized.ssz_snappy"
409409
))
@@ -432,8 +432,13 @@ mod test {
432432
"../test_assets/beacon/deneb/BeaconState/ssz_random/case_0/value.yaml",
433433
)
434434
.expect("cannot find test asset");
435-
let value: Value = serde_yaml::from_str(&value).unwrap();
436-
let content: BeaconStateDeneb = serde_json::from_value(value).unwrap();
435+
let content: BeaconStateDeneb = serde_yaml::from_str(&value).unwrap();
436+
let pandaops_value = std::fs::read_to_string(
437+
"../test_assets/beacon/deneb/BeaconState/ssz_random/case_0/pandaops_value.yaml",
438+
)
439+
.expect("cannot find test asset");
440+
let pandaops_content: BeaconStateDeneb = serde_yaml::from_str(&pandaops_value).unwrap();
441+
assert_eq!(content, pandaops_content);
437442

438443
let compressed = std::fs::read(
439444
"../test_assets/beacon/deneb/BeaconState/ssz_random/case_0/serialized.ssz_snappy",
@@ -467,9 +472,7 @@ mod test {
467472
"../test_assets/beacon/bellatrix/HistoricalBatch/ssz_random/{case}/value.yaml"
468473
))
469474
.expect("cannot find test asset");
470-
let value: Value = serde_yaml::from_str(&value).unwrap();
471-
let content: HistoricalBatch = serde_json::from_value(value).unwrap();
472-
475+
let content: HistoricalBatch = serde_yaml::from_str(&value).unwrap();
473476
let compressed = std::fs::read(format!(
474477
"../test_assets/beacon/bellatrix/HistoricalBatch/ssz_random/{case}/serialized.ssz_snappy"
475478
))
@@ -486,9 +489,7 @@ mod test {
486489
"../test_assets/beacon/bellatrix/HistoricalBatch/ssz_random/case_0/value.yaml",
487490
)
488491
.expect("cannot find test asset");
489-
let value: Value = serde_yaml::from_str(&value).unwrap();
490-
let content: HistoricalBatch = serde_json::from_value(value).unwrap();
491-
492+
let content: HistoricalBatch = serde_yaml::from_str(&value).unwrap();
492493
let expected_proof = [
493494
"0xad500369fa624b7bb451bf1c5119bb6e5e623bab76a0d06948d04a38f35a740d",
494495
"0x222151dcdfbace03dd6f2428ee1a12acffaa1ce03e6966aefd4a48282a776e8e",
@@ -519,8 +520,7 @@ mod test {
519520
"../test_assets/beacon/deneb/BeaconState/ssz_random/case_0/value.yaml",
520521
)
521522
.expect("cannot find test asset");
522-
let value: Value = serde_yaml::from_str(&value).unwrap();
523-
let beacon_state: BeaconStateDeneb = serde_json::from_value(value).unwrap();
523+
let beacon_state: BeaconStateDeneb = serde_yaml::from_str(&value).unwrap();
524524
let historical_summaries_proof = beacon_state.build_historical_summaries_proof();
525525
assert_eq!(historical_summaries_proof.len(), 5);
526526
}

ethportal-api/src/types/consensus/body.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ pub struct VoluntaryExit {
213213
#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize, Decode, Encode, TreeHash)]
214214
pub struct Eth1Data {
215215
pub deposit_root: B256,
216+
#[serde(deserialize_with = "as_u64")]
216217
pub deposit_count: u64,
217218
pub block_hash: B256,
218219
}

ethportal-api/src/types/consensus/participation_flags.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tree_hash::{Hash256, PackedEncoding, TreeHash, TreeHashType};
66
#[derive(Debug, Default, Clone, Copy, PartialEq, Deserialize, Serialize)]
77
#[serde(transparent)]
88
pub struct ParticipationFlags {
9+
#[serde(deserialize_with = "serde_utils::quoted_u8::deserialize")]
910
bits: u8,
1011
}
1112

portal-bridge/src/api/consensus.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ impl ConsensusApi {
3030
Ok(Self { primary, fallback })
3131
}
3232

33+
/// Requests the `BeaconState` structure corresponding to the current head of the beacon chain.
34+
pub async fn get_beacon_state(&self) -> anyhow::Result<String> {
35+
let endpoint = "/eth/v2/debug/beacon/states/finalized".to_string();
36+
self.request(endpoint).await
37+
}
38+
39+
/// Request the finalized root of the beacon state.
40+
pub async fn get_beacon_state_finalized_root(&self) -> anyhow::Result<String> {
41+
let endpoint = "/eth/v1/beacon/states/finalized/root".to_string();
42+
self.request(endpoint).await
43+
}
44+
3345
/// Requests the `LightClientBootstrap` structure corresponding to a given post-Altair beacon
3446
/// block root.
3547
pub async fn get_lc_bootstrap<S: AsRef<str> + Display>(

0 commit comments

Comments
 (0)