From ae2b7601dfe7e9a441821151acf28a5004c589cd Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:26:40 +0530 Subject: [PATCH] fix(gas_price_service_v1): block committer api format --- .../block_committer_costs.rs | 110 ++++++++++-------- tests/tests/gas_price.rs | 8 +- 2 files changed, 64 insertions(+), 54 deletions(-) diff --git a/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs b/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs index ac888a0a70..7d304b1875 100644 --- a/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs +++ b/crates/services/gas_price_service/src/v1/da_source_service/block_committer_costs.rs @@ -19,9 +19,9 @@ pub trait BlockCommitterApi: Send + Sync { /// Used on first run to get the latest costs and seqno async fn get_latest_costs(&self) -> DaBlockCostsResult>; /// Used to get the costs for a specific seqno - async fn get_costs_by_seqno( + async fn get_costs_by_l2_block_number( &self, - number: u32, + l2_block_number: u32, ) -> DaBlockCostsResult>; /// Used to get the costs for a range of blocks (inclusive) async fn get_cost_bundles_by_range( @@ -39,29 +39,35 @@ pub struct BlockCommitterDaBlockCosts { #[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq)] pub struct RawDaBlockCosts { - /// Sequence number (Monotonically increasing nonce) pub bundle_id: u32, - /// The range of blocks that the costs apply to - pub blocks_heights: Vec, + /// The beginning of the range of blocks that the costs apply to + pub start_height: u32, + /// The end of the range of blocks that the costs apply to + pub end_height: u32, /// The DA block height of the last transaction for the range of blocks pub da_block_height: DaBlockHeight, - /// Rolling sum cost of posting blobs (wei) - pub total_cost: u128, - /// Rolling sum size of blobs (bytes) - pub total_size_bytes: u32, + /// cost of posting this blob (wei) + pub cost_wei: u128, + /// size of this blob (bytes) + pub size_bytes: u32, } impl From<&RawDaBlockCosts> for DaBlockCosts { fn from(raw_da_block_costs: &RawDaBlockCosts) -> Self { + let RawDaBlockCosts { + start_height, + end_height, + da_block_height, + cost_wei, + size_bytes, + bundle_id, + } = *raw_da_block_costs; DaBlockCosts { - bundle_id: raw_da_block_costs.bundle_id, - l2_blocks: raw_da_block_costs - .blocks_heights - .clone() - .into_iter() - .collect(), - bundle_size_bytes: raw_da_block_costs.total_size_bytes, - blob_cost_wei: raw_da_block_costs.total_cost, + bundle_id, + // construct a vec of l2 blocks from the start_height to the end_height + l2_blocks: (start_height..end_height).collect(), + bundle_size_bytes: raw_da_block_costs.size_bytes, + blob_cost_wei: raw_da_block_costs.cost_wei, } } } @@ -85,9 +91,9 @@ where &mut self, ) -> DaBlockCostsResult> { let raw_da_block_costs = match self.last_raw_da_block_costs { - Some(ref last_value) => { - self.client.get_costs_by_seqno(last_value.bundle_id + 1) - } + Some(ref last_value) => self + .client + .get_costs_by_l2_block_number(last_value.end_height + 1), _ => self.client.get_latest_costs(), } .await?; @@ -105,11 +111,11 @@ where let costs = costs.expect("Defined to be OK"); let blob_size_bytes = costs .bundle_size_bytes - .checked_sub(last_value.total_size_bytes) + .checked_sub(last_value.size_bytes) .ok_or(anyhow!("Blob size bytes underflow"))?; let blob_cost_wei = raw_da_block_costs - .total_cost - .checked_sub(last_value.total_cost) + .cost_wei + .checked_sub(last_value.cost_wei) .ok_or(anyhow!("Blob cost wei underflow"))?; Ok(DaBlockCosts { bundle_size_bytes: blob_size_bytes, @@ -122,8 +128,10 @@ where self.last_raw_da_block_costs = Some(raw_da_block_costs.clone()); Ok(Some(da_block_costs)) } + async fn set_last_value(&mut self, bundle_id: u32) -> DaBlockCostsResult<()> { - self.last_raw_da_block_costs = self.client.get_costs_by_seqno(bundle_id).await?; + self.last_raw_da_block_costs = + self.client.get_costs_by_l2_block_number(bundle_id).await?; Ok(()) } } @@ -156,14 +164,14 @@ impl BlockCommitterApi for BlockCommitterHttpApi { } } - async fn get_costs_by_seqno( + async fn get_costs_by_l2_block_number( &self, - number: u32, + l2_block_number: u32, ) -> DaBlockCostsResult> { if let Some(url) = &self.url { let val = self .client - .get(format!("{}/{}", url, number)) + .get(format!("{url}/v1/costs?from_height={l2_block_number}")) .send() .await?; tracing::warn!("val: {:?}", val); @@ -179,10 +187,15 @@ impl BlockCommitterApi for BlockCommitterHttpApi { &self, range: core::ops::Range, ) -> DaBlockCostsResult>> { + let start = range.start; + let range_len = range.len(); + if let Some(url) = &self.url { let response = self .client - .get(format!("{}/{}-{}", url, range.start, range.end)) + .get(format!( + "{url}/v1/costs?from_height={start}&limit={range_len}" + )) .send() .await? .json::>() @@ -214,23 +227,19 @@ mod tests { async fn get_latest_costs(&self) -> DaBlockCostsResult> { Ok(self.value.clone()) } - async fn get_costs_by_seqno( + async fn get_costs_by_l2_block_number( &self, - seq_no: u32, + l2_block_number: u32, ) -> DaBlockCostsResult> { // arbitrary logic to generate a new value let mut value = self.value.clone(); if let Some(value) = &mut value { - value.bundle_id = seq_no; - value.blocks_heights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - .to_vec() - .iter() - .map(|x| x * seq_no) - .collect(); + value.start_height = l2_block_number; + value.end_height = value.end_height + l2_block_number + 10; value.da_block_height = - value.da_block_height + ((seq_no + 1) as u64).into(); - value.total_cost += 1; - value.total_size_bytes += 1; + value.da_block_height + ((l2_block_number + 1) as u64).into(); + value.cost_wei += 1; + value.size_bytes += 1; } Ok(value) } @@ -245,10 +254,11 @@ mod tests { fn test_da_block_costs() -> RawDaBlockCosts { RawDaBlockCosts { bundle_id: 1, - blocks_heights: (0..10).collect(), + start_height: 1, + end_height: 10, da_block_height: 1u64.into(), - total_cost: 1, - total_size_bytes: 1, + cost_wei: 1, + size_bytes: 1, } } @@ -273,6 +283,7 @@ mod tests { ) { // given let mut da_block_costs = test_da_block_costs(); + let da_block_costs_len = da_block_costs.end_height - da_block_costs.start_height; let mock_api = MockBlockCommitterApi::new(Some(da_block_costs.clone())); let mut block_committer = BlockCommitterDaBlockCosts::new(mock_api, Some(da_block_costs.clone())); @@ -281,7 +292,7 @@ mod tests { let actual = block_committer.request_da_block_cost().await.unwrap(); // then - assert_ne!(da_block_costs.blocks_heights, actual.unwrap().l2_blocks); + assert_ne!(da_block_costs_len as usize, actual.unwrap().l2_blocks.len()); } // TODO: Do we need this? @@ -313,19 +324,18 @@ mod tests { async fn get_latest_costs(&self) -> DaBlockCostsResult> { Ok(self.value.clone()) } - async fn get_costs_by_seqno( + async fn get_costs_by_l2_block_number( &self, - seq_no: u32, + l2_block_number: u32, ) -> DaBlockCostsResult> { // arbitrary logic to generate a new value let mut value = self.value.clone(); if let Some(value) = &mut value { - value.bundle_id = seq_no; - value.blocks_heights = - value.blocks_heights.iter().map(|x| x + seq_no).collect(); + value.start_height = l2_block_number; + value.end_height = value.end_height + l2_block_number + 10; value.da_block_height = value.da_block_height + 1u64.into(); - value.total_cost -= 1; - value.total_size_bytes -= 1; + value.cost_wei -= 1; + value.size_bytes -= 1; } Ok(value) } diff --git a/tests/tests/gas_price.rs b/tests/tests/gas_price.rs index 6ac0cf772c..66eaeaad01 100644 --- a/tests/tests/gas_price.rs +++ b/tests/tests/gas_price.rs @@ -503,8 +503,8 @@ fn produce_block__l1_committed_block_effects_gas_price() { bundle_id: 1, blocks_heights: vec![1], da_block_height: DaBlockHeight(100), - total_cost: 100, - total_size_bytes: 100, + cost_wei: 100, + size_bytes: 100, }; mock.add_response(costs); @@ -683,8 +683,8 @@ fn _produce_block__algorithm_recovers_from_divergent_profit(block_delay: usize) bundle_id: 1, blocks_heights, da_block_height: DaBlockHeight(100), - total_cost: cost, - total_size_bytes, + cost_wei: cost, + size_bytes: total_size_bytes, }); let mut profits = Vec::new();