Skip to content

Commit

Permalink
fix: manually parse listaddressgroupings
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Jul 20, 2023
1 parent 189f7e2 commit 9406775
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
default = []
regtest-manual-mining = []
cli = ["clap"]
uses-bitcoind = []
uses-bitcoind = ["regtest-manual-mining"]
light-client = []

[dependencies]
Expand Down
31 changes: 17 additions & 14 deletions bitcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub use sp_core::H256;
use std::{
convert::TryInto,
future::Future,
str::FromStr,
sync::Arc,
time::{Duration, Instant},
};
Expand Down Expand Up @@ -533,10 +534,15 @@ impl BitcoinCore {
}

#[cfg(feature = "regtest-manual-mining")]
pub fn mine_block(&self) -> Result<BlockHash, Error> {
Ok(self
.rpc
.generate_to_address(1, &self.rpc.get_new_address(None, Some(AddressType::Bech32))?)?[0])
pub fn mine_blocks(&self, block_num: u64, maybe_address: Option<Address>) -> BlockHash {
let address =
maybe_address.unwrap_or_else(|| self.rpc.get_new_address(None, Some(AddressType::Bech32)).unwrap());
self.rpc
.generate_to_address(block_num, &address)
.unwrap()
.last()
.unwrap()
.clone()
}

async fn with_retry_on_timeout<F, R, T>(&self, call: F) -> Result<T, Error>
Expand Down Expand Up @@ -696,19 +702,16 @@ impl BitcoinCoreApi for BitcoinCore {

// TODO: remove this once the wallet migration has completed
fn list_addresses(&self) -> Result<Vec<Address>, Error> {
#[derive(Clone, PartialEq, Eq, Debug, serde::Deserialize)]
pub struct ListAddressGroupingResult {
pub address: Address,
#[serde(with = "bitcoincore_rpc::bitcoin::util::amount::serde::as_btc")]
pub amount: SignedAmount,
pub label: Option<String>,
}

// Lists groups of addresses which have had their common ownership
// made public by common use as inputs or as the resulting change
// in past transactions
let groupings: Vec<Vec<ListAddressGroupingResult>> = self.rpc.call("listaddressgroupings", &[])?;
Ok(groupings.into_iter().flatten().map(|group| group.address).collect())
let groupings: Vec<Vec<Vec<serde_json::Value>>> = self.rpc.call("listaddressgroupings", &[])?;
let addresses = groupings
.into_iter()
.flatten()
.filter_map(|group| group.get(0).and_then(|v| v.as_str()).map(Address::from_str)?.ok())
.collect::<Vec<_>>();
Ok(addresses)
}

/// Get the raw transaction identified by `Txid` and stored
Expand Down
16 changes: 16 additions & 0 deletions bitcoin/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ async fn should_get_new_address() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn should_list_addresses() -> Result<(), Error> {
let btc_rpc = new_bitcoin_core(Some("Alice".to_string()))?;
btc_rpc.create_or_load_wallet().await?;

let address = btc_rpc.get_new_address().await?;
btc_rpc.mine_blocks(101, Some(address.clone()));

assert!(
btc_rpc.list_addresses()?.contains(&address),
"Address not found in groupings"
);

Ok(())
}

#[tokio::test]
async fn should_get_new_public_key() -> Result<(), Error> {
let btc_rpc = new_bitcoin_core(Some("Bob".to_string()))?;
Expand Down
8 changes: 3 additions & 5 deletions vault/tests/vault_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,7 @@ mod test_with_bitcoind {
ret.create_or_load_wallet().await.unwrap();

// fund the wallet by mining blocks
for _ in 0..102 {
ret.mine_block().unwrap();
}
ret.mine_blocks(102, None);

ret
}
Expand Down Expand Up @@ -1090,7 +1088,7 @@ mod test_with_bitcoind {
}));

tracing::trace!("Step 4: mine bitcoin block");
let block_hash = btc_rpc.mine_block().unwrap();
let block_hash = btc_rpc.mine_blocks(1, None);

tracing::info!("Step 5: check that tx got included without changes");
btc_rpc
Expand Down Expand Up @@ -1160,7 +1158,7 @@ mod test_with_bitcoind {
assert!(btc_rpc.fee_rate(new_tx.txid()).await.unwrap().0 >= 10);

tracing::trace!("Step 5: mine bitcoin block");
let block_hash = btc_rpc.mine_block().unwrap();
let block_hash = btc_rpc.mine_blocks(1, None);

tracing::trace!("Step 6: check that only new tx got included");
btc_rpc.get_transaction(&new_tx.txid(), Some(block_hash)).await.unwrap();
Expand Down

0 comments on commit 9406775

Please sign in to comment.