Skip to content

Commit

Permalink
fix: handle inconsistent checkpoint provider encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
ncitron committed Mar 6, 2025
1 parent 77013dd commit 0ab6a35
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions ethereum/src/config/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub struct RawSlotResponseData {

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Slot {
#[serde(deserialize_with = "deserialize_slot")]
#[serde(deserialize_with = "deserialize_number")]
pub slot: u64,
pub block_root: Option<B256>,
pub state_root: Option<B256>,
#[serde(deserialize_with = "deserialize_number")]
pub epoch: u64,
pub time: StartEndTime,
}
Expand Down Expand Up @@ -168,6 +169,7 @@ impl CheckpointFallback {
let service = service.clone();
match Self::query_service(&service.endpoint).await {
Some(raw) => {
println!("{:?}", raw);
if raw.data.slots.is_empty() {
return Err(eyre::eyre!("no slots"));
}
Expand Down Expand Up @@ -196,6 +198,8 @@ impl CheckpointFallback {
.filter(|s| s.block_root.is_some())
.collect::<Vec<_>>();

println!("{:#?}", slots);

// Get the max epoch
let max_epoch_slot = slots.iter().max_by_key(|x| x.epoch).ok_or(eyre::eyre!(
"Failed to find max epoch from checkpoint slots"
Expand Down Expand Up @@ -299,16 +303,20 @@ impl CheckpointFallback {
}
}

fn deserialize_slot<'de, D>(deserializer: D) -> Result<u64, D::Error>
fn deserialize_number<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: de::Deserializer<'de>,
{
let s: &str = de::Deserialize::deserialize(deserializer)?;
let s = if s.starts_with('"') {
&s[1..s.len() - 1]
} else {
s
};

s.parse().map_err(D::Error::custom)
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrNumber {
String(String),
Number(u64),
}

let value: StringOrNumber = de::Deserialize::deserialize(deserializer)?;
match value {
StringOrNumber::String(s) => s.parse().map_err(D::Error::custom),
StringOrNumber::Number(v) => Ok(v),
}
}

0 comments on commit 0ab6a35

Please sign in to comment.