fix: deserialize wrapped lc headers correctly (#169)

* fix: deserialize wrapped lc headers correctly

* clippy
This commit is contained in:
Noah Citron 2023-01-18 19:42:56 -05:00 committed by GitHub
parent 819ee702e8
commit 69b8108dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -188,6 +188,7 @@ pub struct Eth1Data {
#[derive(serde::Deserialize, Debug)]
pub struct Bootstrap {
#[serde(deserialize_with = "header_deserialize")]
pub header: Header,
pub current_sync_committee: SyncCommittee,
#[serde(deserialize_with = "branch_deserialize")]
@ -196,10 +197,12 @@ pub struct Bootstrap {
#[derive(serde::Deserialize, Debug, Clone)]
pub struct Update {
#[serde(deserialize_with = "header_deserialize")]
pub attested_header: Header,
pub next_sync_committee: SyncCommittee,
#[serde(deserialize_with = "branch_deserialize")]
pub next_sync_committee_branch: Vec<Bytes32>,
#[serde(deserialize_with = "header_deserialize")]
pub finalized_header: Header,
#[serde(deserialize_with = "branch_deserialize")]
pub finality_branch: Vec<Bytes32>,
@ -210,7 +213,9 @@ pub struct Update {
#[derive(serde::Deserialize, Debug)]
pub struct FinalityUpdate {
#[serde(deserialize_with = "header_deserialize")]
pub attested_header: Header,
#[serde(deserialize_with = "header_deserialize")]
pub finalized_header: Header,
#[serde(deserialize_with = "branch_deserialize")]
pub finality_branch: Vec<Bytes32>,
@ -221,6 +226,7 @@ pub struct FinalityUpdate {
#[derive(serde::Deserialize, Debug)]
pub struct OptimisticUpdate {
#[serde(deserialize_with = "header_deserialize")]
pub attested_header: Header,
pub sync_aggregate: SyncAggregate,
#[serde(deserialize_with = "u64_deserialize")]
@ -453,3 +459,27 @@ where
Ok(attesting_indices)
}
fn header_deserialize<'de, D>(deserializer: D) -> Result<Header, D::Error>
where
D: serde::Deserializer<'de>,
{
let header: LightClientHeader = serde::Deserialize::deserialize(deserializer)?;
Ok(match header {
LightClientHeader::Unwrapped(header) => header,
LightClientHeader::Wrapped(header) => header.beacon,
})
}
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum LightClientHeader {
Unwrapped(Header),
Wrapped(Beacon),
}
#[derive(serde::Deserialize)]
struct Beacon {
beacon: Header,
}