Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cost request returns wrong bundle when requesting from within block range #169

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions packages/adapters/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
8 changes: 8 additions & 0 deletions packages/adapters/storage/src/mappings/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ impl TryFrom<BundleCost> for services::types::BundleCost {
type Error = crate::error::Error;

fn try_from(value: BundleCost) -> Result<Self, Self::Error> {
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| {
Expand Down Expand Up @@ -474,6 +481,7 @@ impl TryFrom<BundleCost> for services::types::BundleCost {
})?;

Ok(Self {
id,
cost,
size,
da_block_height,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/storage/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,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
Expand Down
2 changes: 2 additions & 0 deletions packages/services/src/types/bundle_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading