Skip to content

Commit

Permalink
dedupe address functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymar committed Dec 11, 2024
1 parent 995389f commit 77df8df
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 143 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion canister/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bench = false
[dev-dependencies]
assert_matches = { workspace = true }
async-std = { version = "1.12.0", features = ["attributes"] }
bitcoin = { workspace = true, features = ["rand"] } # needed for generating secp256k1 keys.
bitcoin = { workspace = true }
byteorder = { workspace = true }
candid_parser = { workspace = true }
ic-btc-test-utils = { workspace = true }
Expand Down
7 changes: 7 additions & 0 deletions canister/proptest-regressions/api/get_utxos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 4fb046840a47349fe7c8517cf3b45e933e8f90657d7050598605d8e7ad6364b4 # shrinks to num_transactions = 1, num_blocks = 1, utxo_limit = 10
8 changes: 8 additions & 0 deletions canister/proptest-regressions/tests/confirmation_counts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc d4ae143d926026c4c35660945d703ae75c588984ae466120a9be524d22e9f455 # shrinks to chain_len = 2
cc ac900a0fc39aca9433b9d13fe624a86ff91c797726eabd8ae3a2e2109f2834bf # shrinks to chain_len = 9, fork_idx = 0
7 changes: 7 additions & 0 deletions canister/proptest-regressions/utxo_set.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 7cf105bd410b167fa9eb7f08bdad6c46c1e5a1ef960837293d9c21e837891654 # shrinks to ingestion_rate = 1, tx_cardinality = 1, network = Mainnet
17 changes: 11 additions & 6 deletions canister/src/address_utxoset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ impl<'a> AddressUtxoSet<'a> {
#[cfg(test)]
mod test {
use super::*;
use crate::test_utils::{random_p2pkh_address, BlockBuilder, TransactionBuilder};
use crate::test_utils::{BlockBuilder, TransactionBuilder};
use crate::types::into_bitcoin_network;
use crate::unstable_blocks;
use ic_btc_interface::Network;
use ic_btc_test_utils::random_p2pkh_address;
use ic_btc_types::OutPoint;

#[test]
fn add_tx_to_empty_utxo() {
let network = Network::Mainnet;
let btc_network = into_bitcoin_network(network);
// Create some BTC addresses.
let address_1 = random_p2pkh_address(network);
let address_1 = random_p2pkh_address(btc_network).into();

let utxo_set = UtxoSet::new(network);

Expand Down Expand Up @@ -161,9 +164,10 @@ mod test {
#[test]
fn add_tx_then_transfer() {
let network = Network::Mainnet;
let btc_network = into_bitcoin_network(network);
// Create some BTC addresses.
let address_1 = random_p2pkh_address(network);
let address_2 = random_p2pkh_address(network);
let address_1 = random_p2pkh_address(btc_network).into();
let address_2 = random_p2pkh_address(btc_network).into();

let utxo_set = UtxoSet::new(network);

Expand Down Expand Up @@ -213,10 +217,11 @@ mod test {
#[test]
fn spending_multiple_inputs() {
let network = Network::Mainnet;
let btc_network = into_bitcoin_network(network);

// Create some BTC addresses.
let address_1 = random_p2pkh_address(network);
let address_2 = random_p2pkh_address(network);
let address_1 = random_p2pkh_address(btc_network).into();
let address_2 = random_p2pkh_address(btc_network).into();

// Create a genesis block where 2000 satoshis are given to address 1
// in two different outputs.
Expand Down
44 changes: 34 additions & 10 deletions canister/src/api/fee_percentiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ fn percentiles(mut values: Vec<u64>) -> Vec<u64> {
#[cfg(test)]
mod test {
use super::*;
use crate::types::into_bitcoin_network;
use crate::{
genesis_block, heartbeat, state,
test_utils::{random_p2pkh_address, BlockBuilder, TransactionBuilder},
test_utils::{BlockBuilder, TransactionBuilder},
with_state,
};
use async_std::task::block_on;
use bitcoin::Witness;
use ic_btc_interface::{Fees, InitConfig, Network, Satoshi};
use ic_btc_test_utils::random_p2pkh_address;
use ic_btc_types::OutPoint;
use std::iter::FromIterator;

Expand Down Expand Up @@ -246,11 +248,12 @@ mod test {
// Fee is choosen to be a multiple of transaction size to have round values of fee.
fn generate_blocks(initial_balance: Satoshi, number_of_blocks: u32) -> Vec<Block> {
let network = Network::Regtest;
let btc_network = into_bitcoin_network(network);
let mut blocks = Vec::new();

let pay: Satoshi = 1;
let address_1 = random_p2pkh_address(network);
let address_2 = random_p2pkh_address(network);
let address_1 = random_p2pkh_address(btc_network).into();
let address_2 = random_p2pkh_address(btc_network).into();

let coinbase_tx = TransactionBuilder::coinbase()
.with_output(&address_1, initial_balance)
Expand Down Expand Up @@ -355,11 +358,17 @@ mod test {
let fee_in_millisatoshi = fee * 1000;

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

let blocks = vec![
Expand Down Expand Up @@ -399,11 +408,17 @@ mod test {
let balance = 1000;

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

BlockBuilder::with_prev_header(genesis_block(network).header())
Expand Down Expand Up @@ -599,7 +614,10 @@ mod test {
let fee_in_millisatoshi = 1000;

let coinbase_tx = TransactionBuilder::coinbase()
.with_output(&random_p2pkh_address(Network::Regtest), balance)
.with_output(
&random_p2pkh_address(bitcoin::Network::Regtest).into(),
balance,
)
.build();

let witness = Witness::from_vec(vec![
Expand All @@ -610,12 +628,18 @@ mod test {
]);
let tx = TransactionBuilder::new()
.with_input_and_witness(OutPoint::new(coinbase_tx.txid(), 0), witness)
.with_output(&random_p2pkh_address(Network::Regtest), balance - fee)
.with_output(
&random_p2pkh_address(bitcoin::Network::Regtest).into(),
balance - fee,
)
.build();

let tx_without_witness = TransactionBuilder::new()
.with_input(OutPoint::new(coinbase_tx.txid(), 0))
.with_output(&random_p2pkh_address(Network::Regtest), balance - fee)
.with_output(
&random_p2pkh_address(bitcoin::Network::Regtest).into(),
balance - fee,
)
.build();

// Check that vsize() is not the same as size() of a transaction.
Expand Down
19 changes: 13 additions & 6 deletions canister/src/api/get_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ fn get_balance_private(request: GetBalanceRequest) -> Result<Satoshi, GetBalance
#[cfg(test)]
mod test {
use super::*;
use crate::types::into_bitcoin_network;
use crate::{
genesis_block, state,
test_utils::{random_p2pkh_address, BlockBuilder, TransactionBuilder},
test_utils::{BlockBuilder, TransactionBuilder},
with_state_mut,
};
use ic_btc_interface::{Fees, InitConfig, Network};
use ic_btc_test_utils::random_p2pkh_address;
use ic_btc_types::OutPoint;

#[test]
Expand Down Expand Up @@ -151,14 +153,15 @@ mod test {
#[test]
fn retrieves_the_balance_of_address() {
let network = Network::Regtest;
let btc_network = into_bitcoin_network(network);
crate::init(InitConfig {
stability_threshold: Some(2),
network: Some(network),
..Default::default()
});

// Create a block where 1000 satoshis are given to an address.
let address = random_p2pkh_address(network);
let address = random_p2pkh_address(btc_network).into();
let coinbase_tx = TransactionBuilder::coinbase()
.with_output(&address, 1000)
.build();
Expand Down Expand Up @@ -196,13 +199,14 @@ mod test {
#[test]
fn error_on_very_large_confirmations() {
let network = Network::Regtest;
let btc_network = into_bitcoin_network(network);
crate::init(InitConfig {
stability_threshold: Some(2),
network: Some(network),
..Default::default()
});

let address = random_p2pkh_address(network);
let address: Address = random_p2pkh_address(btc_network).into();

for min_confirmations in [Some(0), None, Some(1)] {
assert_eq!(
Expand All @@ -229,6 +233,7 @@ mod test {
#[test]
fn retrieves_balances_of_addresses_with_different_confirmations() {
let network = Network::Regtest;
let btc_network = into_bitcoin_network(network);

crate::init(InitConfig {
stability_threshold: Some(2),
Expand All @@ -237,8 +242,8 @@ mod test {
});

// Generate addresses.
let address_1 = random_p2pkh_address(network);
let address_2 = random_p2pkh_address(network);
let address_1 = random_p2pkh_address(btc_network).into();
let address_2 = random_p2pkh_address(btc_network).into();

// Create a chain where 1000 satoshis are given to the address_1, then
// address_1 gives 1000 satoshis to address_2.
Expand Down Expand Up @@ -308,6 +313,8 @@ mod test {

#[test]
fn charges_cycles() {
let network = Network::Regtest;
let btc_network = into_bitcoin_network(network);
crate::init(InitConfig {
fees: Some(Fees {
get_balance: 10,
Expand All @@ -317,7 +324,7 @@ mod test {
});

get_balance(GetBalanceRequest {
address: random_p2pkh_address(Network::Regtest).to_string(),
address: random_p2pkh_address(btc_network).to_string(),
min_confirmations: None,
})
.unwrap();
Expand Down
Loading

0 comments on commit 77df8df

Please sign in to comment.