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: ignore coinbase transactions when computing fee percentiles. #152

Merged
merged 2 commits into from
Mar 14, 2023
Merged
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
38 changes: 37 additions & 1 deletion canister/src/api/fee_percentiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ fn get_fees_per_byte(
if tx_i >= number_of_transactions {
break;
}
tx_i += 1;
if !tx.is_coin_base() {
tx_i += 1;
}
if let Some(fee) = get_tx_fee_per_byte(tx, unstable_blocks) {
fees.push(fee);
}
Expand Down Expand Up @@ -327,6 +329,40 @@ mod test {
assert_eq!(percentiles[81..101], [33; 20]);
}

#[test]
fn coinbase_txs_are_ignored() {
let balance = 1000;
let fee = 1;
let fee_in_millisatoshi = fee * 1000;

let tx_1 = TransactionBuilder::coinbase()
.with_output(&random_p2pkh_address(Network::Regtest), balance)
.build();
let tx_2 = TransactionBuilder::new()
.with_input(OutPoint::new(tx_1.txid(), 0))
.with_output(&random_p2pkh_address(Network::Regtest), balance - fee)
.build();

let blocks = vec![
BlockBuilder::with_prev_header(genesis_block(Network::Regtest).header())
.with_transaction(tx_1)
.with_transaction(tx_2.clone())
.build(),
];

let stability_threshold = blocks.len() as u128;
init_state(blocks, stability_threshold);

with_state_mut(|s| {
// Get the current fee percentiles for one tx. Coinbase txs are ignored,
// so the percentiles should be the fee / byte of the second transaction.
assert_eq!(
get_current_fee_percentiles_internal(s, 1),
vec![fee_in_millisatoshi / tx_2.size() as u64; PERCENTILE_BUCKETS]
);
});
}

#[test]
fn get_current_fee_percentiles_requested_number_of_txs_is_less_than_number_of_actual_txs() {
let number_of_blocks = 8;
Expand Down