diff --git a/.sqlx/query-f1ac90602eca2f5966383dc436593f3050820931aeea93d35239f980a22ced1f.json b/.sqlx/query-365408c350d49ba41303b49b319a0fb317ced0dc5bae79b8a491b0807cf97958.json similarity index 85% rename from .sqlx/query-f1ac90602eca2f5966383dc436593f3050820931aeea93d35239f980a22ced1f.json rename to .sqlx/query-365408c350d49ba41303b49b319a0fb317ced0dc5bae79b8a491b0807cf97958.json index f7ab6859..9fdddd9a 100644 --- a/.sqlx/query-f1ac90602eca2f5966383dc436593f3050820931aeea93d35239f980a22ced1f.json +++ b/.sqlx/query-365408c350d49ba41303b49b319a0fb317ced0dc5bae79b8a491b0807cf97958.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n bc.bundle_id,\n bc.cost,\n bc.size,\n bc.da_block_height,\n bc.is_finalized,\n b.start_height,\n b.end_height\n FROM\n bundle_cost bc\n JOIN bundles b ON bc.bundle_id = b.id\n WHERE\n b.start_height >= $1 AND bc.is_finalized = TRUE\n ORDER BY\n b.start_height ASC\n LIMIT $2\n ", + "query": "\n SELECT\n bc.bundle_id,\n bc.cost,\n bc.size,\n bc.da_block_height,\n bc.is_finalized,\n b.start_height,\n b.end_height\n FROM\n bundle_cost bc\n JOIN bundles b ON bc.bundle_id = b.id\n WHERE\n b.end_height >= $1 AND bc.is_finalized = TRUE\n ORDER BY\n b.start_height ASC\n LIMIT $2\n ", "describe": { "columns": [ { @@ -55,5 +55,5 @@ false ] }, - "hash": "f1ac90602eca2f5966383dc436593f3050820931aeea93d35239f980a22ced1f" + "hash": "365408c350d49ba41303b49b319a0fb317ced0dc5bae79b8a491b0807cf97958" } diff --git a/packages/adapters/storage/src/lib.rs b/packages/adapters/storage/src/lib.rs index f1eeead1..8ea34bbc 100644 --- a/packages/adapters/storage/src/lib.rs +++ b/packages/adapters/storage/src/lib.rs @@ -1125,4 +1125,42 @@ mod tests { Ok(()) } + + #[tokio::test] + async fn get_finalized_costs_from_middle_of_range() -> Result<()> { + use services::cost_reporter::port::Storage; + + // given + let storage = start_db().await; + + for i in 0..5 { + let start_height = i * 10 + 1; + let end_height = start_height + 9; + let block_range = start_height..=end_height; + + ensure_finalized_fragments_exist_in_the_db( + storage.clone(), + block_range, + 1000u128, + 5000u64, + ) + .await; + } + + // when + let from_block_height = 25; + let limit = 3; + let finalized_costs = storage + .get_finalized_costs(from_block_height, limit) + .await?; + + // then + assert_eq!(finalized_costs.len(), 3); + + assert_eq!(finalized_costs[0].start_height, 21); + assert_eq!(finalized_costs[1].start_height, 31); + assert_eq!(finalized_costs[2].start_height, 41); + + Ok(()) + } } diff --git a/packages/adapters/storage/src/mappings/tables.rs b/packages/adapters/storage/src/mappings/tables.rs index ea419785..626d431a 100644 --- a/packages/adapters/storage/src/mappings/tables.rs +++ b/packages/adapters/storage/src/mappings/tables.rs @@ -452,6 +452,13 @@ impl TryFrom for services::types::BundleCost { type Error = crate::error::Error; fn try_from(value: BundleCost) -> Result { + let id = value.bundle_id.try_into().map_err(|e| { + crate::error::Error::Conversion(format!( + "Invalid db `bundle_id` ({}). Reason: {e}", + value.bundle_id + )) + })?; + let cost = bigdecimal_to_u128(value.cost)?; let size = value.size.try_into().map_err(|e| { @@ -483,6 +490,7 @@ impl TryFrom for services::types::BundleCost { })?; Ok(Self { + id, cost, size, da_block_height, diff --git a/packages/adapters/storage/src/postgres.rs b/packages/adapters/storage/src/postgres.rs index b836e4d9..fb19bdc7 100644 --- a/packages/adapters/storage/src/postgres.rs +++ b/packages/adapters/storage/src/postgres.rs @@ -850,7 +850,7 @@ impl Postgres { bundle_cost bc JOIN bundles b ON bc.bundle_id = b.id WHERE - b.start_height >= $1 AND bc.is_finalized = TRUE + b.end_height >= $1 AND bc.is_finalized = TRUE ORDER BY b.start_height ASC LIMIT $2 diff --git a/packages/services/src/types/bundle_cost.rs b/packages/services/src/types/bundle_cost.rs index 6f45ae0b..eae58e31 100644 --- a/packages/services/src/types/bundle_cost.rs +++ b/packages/services/src/types/bundle_cost.rs @@ -9,6 +9,8 @@ pub struct TransactionCostUpdate { #[derive(Debug, Serialize, Deserialize)] pub struct BundleCost { + // the bundle id + pub id: u64, // total cost of the bundle pub cost: u128, // total size of the data contained in the bundle