Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault-magnet committed Jan 10, 2025
1 parent f9a6a75 commit 242a73d
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions packages/adapters/storage/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ impl Postgres {
da_block_height,
} in cost_per_tx
{
// 1) Fetch the number of fragments and total byte usage per bundle
let rows = sqlx::query!(
r#"
SELECT
Expand All @@ -777,49 +776,40 @@ impl Postgres {
.fetch_all(&mut *tx)
.await?;

// 2) Calculate the total number of fragments in this transaction
let total_fragments_in_tx = rows
.iter()
.map(|r| r.fragment_count.unwrap_or(0) as u64)
.sum::<u64>();

// 3) Distribute cost among all bundles based on fragment count,
// but still track the size usage.
for row in rows {
let bundle_id = row.bundle_id;

// 3a) number of fragments in this bundle
let frag_count_in_bundle = row.fragment_count.unwrap_or(0) as u64;
let total_bytes = row.total_bytes.unwrap_or(0).max(0) as u64;
let unused_bytes = row.unused_bytes.unwrap_or(0).max(0) as u64;
let used_bytes = total_bytes.saturating_sub(unused_bytes);

// 3b) fraction = how many fragments for this bundle vs. total
let fraction = if total_fragments_in_tx == 0 {
0.0
const PPM: u128 = 1_000_000;
let fraction_in_ppm = if total_fragments_in_tx == 0 {
0u128
} else {
frag_count_in_bundle as f64 / total_fragments_in_tx as f64
u128::from(frag_count_in_bundle)
.saturating_mul(PPM)
.saturating_div(u128::from(total_fragments_in_tx))
};

// 3c) proportion of the total fee to allocate
let cost_contribution = (*total_fee as f64 * fraction).round() as u128;
let cost_contribution = fraction_in_ppm
.saturating_mul(*total_fee)
.saturating_div(PPM);

// 3d) "used bytes" for size tracking
let total_bytes = row.total_bytes.unwrap_or(0).max(0) as u64;
let unused_bytes = row.unused_bytes.unwrap_or(0).max(0) as u64;
let used_bytes = total_bytes.saturating_sub(unused_bytes);

// 3e) update the aggregator
let entry = bundle_updates.entry(bundle_id).or_insert(BundleCostUpdate {
cost_contribution: 0,
size_contribution: 0,
latest_da_block_height: 0,
});

// add to cost
entry.cost_contribution = entry.cost_contribution.saturating_add(cost_contribution);

// update size usage, if you still want to accumulate it
entry.size_contribution = entry.size_contribution.saturating_add(used_bytes);

// track the most recent da block height
entry.latest_da_block_height = entry.latest_da_block_height.max(*da_block_height);
}
}
Expand Down

0 comments on commit 242a73d

Please sign in to comment.