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

feat: add default fees for mainnet/testnet networks #376

Merged
merged 13 commits into from
Mar 5, 2025
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
3 changes: 2 additions & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ env:
DFX_VERSION: 0.23.0
POCKET_IC_SERVER_VERSION: 7.0.0
CARGO_TERM_COLOR: always # Force Cargo to use colors
TERM: xterm-256color

on:
push:
Expand Down Expand Up @@ -94,7 +95,7 @@ jobs:
- name: Run Tests
shell: bash
run: |
cargo test --release --all-targets --workspace --exclude benchmarks
cargo test --release --all-targets --workspace --exclude benchmarks --color always -- --color always
env:
RUST_BACKTRACE: 1

Expand Down
19 changes: 2 additions & 17 deletions bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,7 @@ $ cargo run --example upload -- \
Prepare upgrade arguments
```shell
# https://internetcomputer.org/docs/references/bitcoin-how-it-works#api-fees-and-pricing
$ TESTNET_FEES="record {
get_utxos_base = 20_000_000 : nat;
get_utxos_cycles_per_ten_instructions = 10 : nat;
get_utxos_maximum = 4_000_000_000 : nat;
get_current_fee_percentiles = 4_000_000 : nat;
get_current_fee_percentiles_maximum = 40_000_000 : nat;
get_balance = 4_000_000 : nat;
get_balance_maximum = 40_000_000 : nat;
send_transaction_base = 2_000_000_000 : nat;
send_transaction_per_byte = 8_000_000 : nat;
get_block_headers_base = 20_000_000 : nat;
get_block_headers_cycles_per_ten_instructions = 10 : nat;
get_block_headers_maximum = 4_000_000_000 : nat;
}"

$ MAINNET_FEES="record {
$ CUSTOM_FEES="record {
get_utxos_base = 50_000_000 : nat;
get_utxos_cycles_per_ten_instructions = 10 : nat;
get_utxos_maximum = 10_000_000_000 : nat;
Expand All @@ -306,7 +291,7 @@ $ ARG="(record {
syncing = opt variant { enabled };
api_access = opt variant { disabled };
watchdog_canister = opt opt principal \"$TESTNET_WATCHDOG_CANISTER_ID\";
fees = opt $TESTNET_FEES;
fees = opt $CUSTOM_FEES;
})"
```

Expand Down
35 changes: 30 additions & 5 deletions canister/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub(crate) fn is_synced() -> bool {
#[cfg(test)]
mod test {
use super::*;
use ic_btc_interface::{Network, NetworkInRequest};
use ic_btc_interface::{Fees, Network, NetworkInRequest};
use ic_btc_test_utils::build_regtest_chain;
use proptest::prelude::*;

Expand Down Expand Up @@ -595,7 +595,7 @@ mod test {
});

with_state(|s| {
assert!(s.syncing_state.syncing == Flag::Disabled);
assert_eq!(s.syncing_state.syncing, Flag::Disabled);
});

init(InitConfig {
Expand All @@ -604,7 +604,7 @@ mod test {
});

with_state(|s| {
assert!(s.syncing_state.syncing == Flag::Enabled);
assert_eq!(s.syncing_state.syncing, Flag::Enabled);
});
}

Expand All @@ -616,7 +616,7 @@ mod test {
});

with_state(|s| {
assert!(s.disable_api_if_not_fully_synced == Flag::Disabled);
assert_eq!(s.disable_api_if_not_fully_synced, Flag::Disabled);
});

init(InitConfig {
Expand All @@ -625,7 +625,32 @@ mod test {
});

with_state(|s| {
assert!(s.disable_api_if_not_fully_synced == Flag::Enabled);
assert_eq!(s.disable_api_if_not_fully_synced, Flag::Enabled);
});
}

#[test]
fn init_applies_default_fees_when_not_explicitly_provided() {
let custom = Fees {
get_utxos_base: 123,
..Default::default()
};
let test_cases = [
(Network::Testnet, None, Fees::testnet()),
(Network::Mainnet, None, Fees::mainnet()),
(Network::Regtest, None, Fees::default()),
(Network::Testnet, Some(custom.clone()), custom.clone()),
(Network::Mainnet, Some(custom.clone()), custom.clone()),
(Network::Regtest, Some(custom.clone()), custom),
];
for (network, provided_fees, expected_fees) in test_cases {
init(InitConfig {
network: Some(network),
fees: provided_fees.clone(),
..Default::default()
});

with_state(|s| assert_eq!(s.fees, expected_fees));
}
}
}
8 changes: 7 additions & 1 deletion canister/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@ impl State {
let unstable_blocks =
UnstableBlocks::new(&utxos, stability_threshold, genesis_block, network);

let fees = match network {
Network::Mainnet => Fees::mainnet(),
Network::Testnet => Fees::testnet(),
Network::Regtest => Fees::default(),
};

Self {
utxos,
unstable_blocks,
syncing_state: SyncingState::default(),
blocks_source: Principal::management_canister(),
fee_percentiles_cache: None,
stable_block_headers: BlockHeaderStore::init(),
fees: Fees::default(),
fees,
metrics: Metrics::default(),
api_access: Flag::Enabled,
disable_api_if_not_fully_synced: Flag::Enabled,
Expand Down
56 changes: 56 additions & 0 deletions interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ impl From<InitConfig> for Config {
config.syncing = syncing;
}

let fees_explicitly_set = init_config.fees.is_some();
if let Some(fees) = init_config.fees {
config.fees = fees;
}
Expand All @@ -658,6 +659,15 @@ impl From<InitConfig> for Config {
config.lazily_evaluate_fee_percentiles = lazily_evaluate_fee_percentiles;
}

// Config post-processing.
if !fees_explicitly_set {
config.fees = match config.network {
Network::Mainnet => Fees::mainnet(),
Network::Testnet => Fees::testnet(),
Network::Regtest => config.fees, // Keep unchanged for regtest.
};
}

config
}
}
Expand Down Expand Up @@ -725,6 +735,52 @@ pub struct Fees {
pub get_block_headers_maximum: u128,
}

impl Fees {
pub fn testnet() -> Self {
// https://internetcomputer.org/docs/references/bitcoin-how-it-works#bitcoin-testnet
Self {
get_utxos_base: 20_000_000,
get_utxos_cycles_per_ten_instructions: 10,
get_utxos_maximum: 4_000_000_000,

get_current_fee_percentiles: 4_000_000,
get_current_fee_percentiles_maximum: 40_000_000,

get_balance: 4_000_000,
get_balance_maximum: 40_000_000,

send_transaction_base: 2_000_000_000,
send_transaction_per_byte: 8_000_000,

get_block_headers_base: 20_000_000,
get_block_headers_cycles_per_ten_instructions: 10,
get_block_headers_maximum: 4_000_000_000,
}
}

pub fn mainnet() -> Self {
// https://internetcomputer.org/docs/references/bitcoin-how-it-works#bitcoin-mainnet
Self {
get_utxos_base: 50_000_000,
get_utxos_cycles_per_ten_instructions: 10,
get_utxos_maximum: 10_000_000_000,

get_current_fee_percentiles: 10_000_000,
get_current_fee_percentiles_maximum: 100_000_000,

get_balance: 10_000_000,
get_balance_maximum: 100_000_000,

send_transaction_base: 5_000_000_000,
send_transaction_per_byte: 20_000_000,

get_block_headers_base: 50_000_000,
get_block_headers_cycles_per_ten_instructions: 10,
get_block_headers_maximum: 10_000_000_000,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading