Skip to content

Commit

Permalink
refactor: pack both start & end updatedatas into a single binaryupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbadadare committed Dec 3, 2024
1 parent 4774835 commit 1a07a77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
21 changes: 8 additions & 13 deletions apps/hermes/server/src/api/rest/v2/latest_twaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,17 @@ where
})?;

let twap_update_data = twaps_with_update_data.update_data;
let binary: Vec<BinaryUpdate> = twap_update_data
let encoded_data = twap_update_data
.into_iter()
.map(|data_vec| {
let encoded_data = data_vec
.into_iter()
.map(|data| match params.encoding {
EncodingType::Base64 => base64_standard_engine.encode(data),
EncodingType::Hex => hex::encode(data),
})
.collect();
BinaryUpdate {
encoding: params.encoding,
data: encoded_data,
}
.map(|data| match params.encoding {
EncodingType::Base64 => base64_standard_engine.encode(data),
EncodingType::Hex => hex::encode(data),
})
.collect();
let binary = BinaryUpdate {
encoding: params.encoding,
data: encoded_data,
};

let parsed: Option<Vec<ParsedPriceFeedTwap>> = if params.parsed {
Some(
Expand Down
4 changes: 2 additions & 2 deletions apps/hermes/server/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ impl From<PriceFeedTwap> for ParsedPriceFeedTwap {

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct TwapsResponse {
/// Each BinaryUpdate contains the start & end cumulative price updates used to
/// Contains the start & end cumulative price updates used to
/// calculate a given price feed's TWAP.
pub binary: Vec<BinaryUpdate>,
pub binary: BinaryUpdate,

/// The calculated TWAPs for each price ID
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
17 changes: 8 additions & 9 deletions apps/hermes/server/src/state/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub struct PublisherStakeCapsWithUpdateData {
#[derive(Debug)]
pub struct TwapsWithUpdateData {
pub twaps: Vec<PriceFeedTwap>,
pub update_data: Vec<Vec<Vec<u8>>>,
pub update_data: Vec<Vec<u8>>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -688,12 +688,13 @@ where
}

// Construct update data.
// update_data[0] will contain the start VAA and merkle proofs
// update_data[1] will contain the end VAA and merkle proofs
let start_updates =
// update_data[0] contains the start VAA and merkle proofs
// update_data[1] contains the end VAA and merkle proofs
let mut update_data =
construct_update_data(start_messages.into_iter().map(Into::into).collect())?;
let end_updates = construct_update_data(end_messages.into_iter().map(Into::into).collect())?;
let update_data = vec![start_updates, end_updates];
update_data.extend(construct_update_data(
end_messages.into_iter().map(Into::into).collect(),
)?);

Ok(TwapsWithUpdateData { twaps, update_data })
}
Expand Down Expand Up @@ -1307,10 +1308,8 @@ mod test {
assert_eq!(twap_2.start_timestamp, 100);
assert_eq!(twap_2.end_timestamp, 200);

// Each update_data element contains a VAA and merkle proofs for both feeds
// update_data should have 2 elements, one for the start block and one for the end block.
assert_eq!(result.update_data.len(), 2);
assert_eq!(result.update_data[0].len(), 1); // Start update
assert_eq!(result.update_data[1].len(), 1); // End update
}
#[tokio::test]

Expand Down

0 comments on commit 1a07a77

Please sign in to comment.