Skip to content

Commit

Permalink
fix(gas_price_service_v1): block committer api format
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Dec 17, 2024
1 parent 78f5b94 commit ae2b760
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<RawDaBlockCosts>>;
/// 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<Option<RawDaBlockCosts>>;
/// Used to get the costs for a range of blocks (inclusive)
async fn get_cost_bundles_by_range(
Expand All @@ -39,29 +39,35 @@ pub struct BlockCommitterDaBlockCosts<BlockCommitter> {

#[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<u32>,
/// 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,
}
}
}
Expand All @@ -85,9 +91,9 @@ where
&mut self,
) -> DaBlockCostsResult<Option<DaBlockCosts>> {
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?;
Expand All @@ -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,
Expand All @@ -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(())
}
}
Expand Down Expand Up @@ -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<Option<RawDaBlockCosts>> {
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);
Expand All @@ -179,10 +187,15 @@ impl BlockCommitterApi for BlockCommitterHttpApi {
&self,
range: core::ops::Range<u32>,
) -> DaBlockCostsResult<Vec<Option<RawDaBlockCosts>>> {
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::<Vec<RawDaBlockCosts>>()
Expand Down Expand Up @@ -214,23 +227,19 @@ mod tests {
async fn get_latest_costs(&self) -> DaBlockCostsResult<Option<RawDaBlockCosts>> {
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<Option<RawDaBlockCosts>> {
// 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)
}
Expand All @@ -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,
}
}

Expand All @@ -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()));
Expand All @@ -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?
Expand Down Expand Up @@ -313,19 +324,18 @@ mod tests {
async fn get_latest_costs(&self) -> DaBlockCostsResult<Option<RawDaBlockCosts>> {
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<Option<RawDaBlockCosts>> {
// 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)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ae2b760

Please sign in to comment.