From 78674818c29b21174525e3b0f3f42f673900d31e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 7 Dec 2020 10:19:15 +1100 Subject: [PATCH] Upgrade bitcoin-harness --- Cargo.lock | 27 +++++++++++++++++++++++-- swap/Cargo.toml | 2 +- swap/src/bitcoin.rs | 19 +++++++++++------ swap/src/recover.rs | 3 ++- xmr-btc/Cargo.toml | 2 +- xmr-btc/tests/harness/wallet/bitcoin.rs | 16 +++++++++++---- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index caf57c6b..40b1f224 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,14 +254,15 @@ dependencies = [ [[package]] name = "bitcoin-harness" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4cd49a4bc53eaeab332d66022a158515f0b5bb4b381a1583a07a0c667ac280" +source = "git+https://github.com/coblox/bitcoin-harness-rs?rev=864b55fcba2e770105f135781dd2e3002c503d12#864b55fcba2e770105f135781dd2e3002c503d12" dependencies = [ + "async-trait", "base64 0.12.3", "bitcoin", "bitcoincore-rpc-json", "futures", "hex 0.4.2", + "jsonrpc_client", "reqwest", "serde", "serde_json", @@ -1551,6 +1552,28 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "jsonrpc_client" +version = "0.3.0" +source = "git+https://github.com/thomaseizinger/rust-jsonrpc-client?rev=c7010817e0f86ab24b3dc10d6bb0463faa0aace4#c7010817e0f86ab24b3dc10d6bb0463faa0aace4" +dependencies = [ + "async-trait", + "jsonrpc_client_macro", + "reqwest", + "serde", + "serde_json", + "url", +] + +[[package]] +name = "jsonrpc_client_macro" +version = "0.1.0" +source = "git+https://github.com/thomaseizinger/rust-jsonrpc-client?rev=c7010817e0f86ab24b3dc10d6bb0463faa0aace4#c7010817e0f86ab24b3dc10d6bb0463faa0aace4" +dependencies = [ + "quote 1.0.7", + "syn 1.0.48", +] + [[package]] name = "keccak" version = "0.1.0" diff --git a/swap/Cargo.toml b/swap/Cargo.toml index 84feae6c..e5123577 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -13,7 +13,7 @@ atty = "0.2" backoff = { version = "0.2", features = ["tokio"] } base64 = "0.12" bitcoin = { version = "0.25", features = ["rand", "use-serde"] } -bitcoin-harness = "0.2" +bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "864b55fcba2e770105f135781dd2e3002c503d12" } conquer-once = "0.3" derivative = "2" ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", rev = "cdfbc766045ea678a41780919d6228dd5acee3be", features = ["libsecp_compat", "serde"] } diff --git a/swap/src/bitcoin.rs b/swap/src/bitcoin.rs index bcef2462..06017ba3 100644 --- a/swap/src/bitcoin.rs +++ b/swap/src/bitcoin.rs @@ -1,11 +1,10 @@ -use std::time::Duration; - -use anyhow::Result; +use anyhow::{Context, Result}; use async_trait::async_trait; use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; use bitcoin::util::psbt::PartiallySignedTransaction; -use bitcoin_harness::bitcoind_rpc::PsbtBase64; +use bitcoin_harness::{bitcoind_rpc::PsbtBase64, BitcoindRpcApi}; use reqwest::Url; +use std::time::Duration; use tokio::time::interval; use xmr_btc::{ bitcoin::{ @@ -44,7 +43,15 @@ impl Wallet { .0 .get_wallet_transaction(txid) .await - .map(|res| bitcoin::Amount::from_btc(-res.fee))??; + .map(|res| { + res.fee.map(|signed_amount| { + signed_amount + .abs() + .to_unsigned() + .expect("Absolute value is always positive") + }) + })? + .context("Rpc response did not contain a fee")?; Ok(fee) } @@ -116,7 +123,7 @@ impl GetRawTransaction for Wallet { #[async_trait] impl BlockHeight for Wallet { async fn block_height(&self) -> u32 { - (|| async { Ok(self.0.block_height().await?) }) + (|| async { Ok(self.0.client.getblockcount().await?) }) .retry(ConstantBackoff::new(Duration::from_secs(1))) .await .expect("transient errors to be retried") diff --git a/swap/src/recover.rs b/swap/src/recover.rs index aa5c2f51..bd8d40ac 100644 --- a/swap/src/recover.rs +++ b/swap/src/recover.rs @@ -16,6 +16,7 @@ use crate::{ state::{Alice, Bob, Swap}, }; use anyhow::Result; +use bitcoin_harness::BitcoindRpcApi; use ecdsa_fun::{adaptor::Adaptor, nonce::Deterministic}; use futures::{ future::{select, Either}, @@ -163,7 +164,7 @@ pub async fn alice_recover( .transaction_block_height(state.tx_lock.txid()) .await; - let block_height = bitcoin_wallet.0.block_height().await?; + let block_height = bitcoin_wallet.0.client.getblockcount().await?; let refund_absolute_expiry = tx_lock_height + state.refund_timelock; info!("Checking refund timelock"); diff --git a/xmr-btc/Cargo.toml b/xmr-btc/Cargo.toml index eced65e7..bac95877 100644 --- a/xmr-btc/Cargo.toml +++ b/xmr-btc/Cargo.toml @@ -29,7 +29,7 @@ tracing = "0.1" [dev-dependencies] backoff = { version = "0.2", features = ["tokio"] } base64 = "0.12" -bitcoin-harness = "0.2" +bitcoin-harness = { git = "https://github.com/coblox/bitcoin-harness-rs", rev = "864b55fcba2e770105f135781dd2e3002c503d12" } futures = "0.3" monero-harness = { path = "../monero-harness" } reqwest = { version = "0.10", default-features = false } diff --git a/xmr-btc/tests/harness/wallet/bitcoin.rs b/xmr-btc/tests/harness/wallet/bitcoin.rs index f9d2c91d..e2ac5faa 100644 --- a/xmr-btc/tests/harness/wallet/bitcoin.rs +++ b/xmr-btc/tests/harness/wallet/bitcoin.rs @@ -1,8 +1,8 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use async_trait::async_trait; use backoff::{backoff::Constant as ConstantBackoff, future::FutureOperation as _}; use bitcoin::{util::psbt::PartiallySignedTransaction, Address, Amount, Transaction, Txid}; -use bitcoin_harness::{bitcoind_rpc::PsbtBase64, Bitcoind}; +use bitcoin_harness::{bitcoind_rpc::PsbtBase64, Bitcoind, BitcoindRpcApi}; use reqwest::Url; use std::time::Duration; use tokio::time; @@ -35,7 +35,15 @@ impl Wallet { .0 .get_wallet_transaction(txid) .await - .map(|res| bitcoin::Amount::from_btc(-res.fee))??; + .map(|res| { + res.fee.map(|signed_amount| { + signed_amount + .abs() + .to_unsigned() + .expect("Absolute value is always positive") + }) + })? + .context("Rpc response did not contain a fee")?; Ok(fee) } @@ -121,7 +129,7 @@ impl WatchForRawTransaction for Wallet { #[async_trait] impl BlockHeight for Wallet { async fn block_height(&self) -> u32 { - (|| async { Ok(self.0.block_height().await?) }) + (|| async { Ok(self.0.client.getblockcount().await?) }) .retry(ConstantBackoff::new(Duration::from_secs(1))) .await .expect("transient errors to be retried")