From ecbeeb843f5c6dd1c48d54e747e6d4e8859e6b34 Mon Sep 17 00:00:00 2001 From: Sander Bosma Date: Wed, 19 Jul 2023 17:53:27 +0200 Subject: [PATCH 1/2] feat: bitcoin core v25 support --- Cargo.lock | 4 ++-- bitcoin/Cargo.toml | 2 +- bitcoin/src/lib.rs | 8 +++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43eb513de..1fb08192e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,7 +406,7 @@ dependencies = [ [[package]] name = "bitcoincore-rpc" version = "0.17.0" -source = "git+https://github.com/rust-bitcoin/rust-bitcoincore-rpc?rev=7bd815f1e1ae721404719ee8e6867064b7c68494#7bd815f1e1ae721404719ee8e6867064b7c68494" +source = "git+https://github.com/interlay/rust-bitcoincore-rpc?rev=671a78f638179c8951c9932061fd2bb348313a2c#671a78f638179c8951c9932061fd2bb348313a2c" dependencies = [ "bitcoin-private", "bitcoincore-rpc-json", @@ -419,7 +419,7 @@ dependencies = [ [[package]] name = "bitcoincore-rpc-json" version = "0.17.0" -source = "git+https://github.com/rust-bitcoin/rust-bitcoincore-rpc?rev=7bd815f1e1ae721404719ee8e6867064b7c68494#7bd815f1e1ae721404719ee8e6867064b7c68494" +source = "git+https://github.com/interlay/rust-bitcoincore-rpc?rev=671a78f638179c8951c9932061fd2bb348313a2c#671a78f638179c8951c9932061fd2bb348313a2c" dependencies = [ "bitcoin 0.30.1", "bitcoin-private", diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index e4a0195ea..253422993 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -13,7 +13,7 @@ light-client = [] [dependencies] thiserror = "1.0" -bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc", rev = "7bd815f1e1ae721404719ee8e6867064b7c68494" } +bitcoincore-rpc = { git = "https://github.com/interlay/rust-bitcoincore-rpc", rev = "671a78f638179c8951c9932061fd2bb348313a2c" } hex = "0.4.2" async-trait = "0.1.40" tokio = { version = "1.0", features = ["full"] } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index fe5148c30..e2c7719d1 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -74,7 +74,7 @@ pub const DEFAULT_MAX_TX_COUNT: usize = 100_000_000; /// the bitcoin core version. /// See https://github.com/bitcoin/bitcoin/blob/833add0f48b0fad84d7b8cf9373a349e7aef20b4/src/rpc/net.cpp#L627 /// and https://github.com/bitcoin/bitcoin/blob/833add0f48b0fad84d7b8cf9373a349e7aef20b4/src/clientversion.h#L33-L37 -pub const BITCOIN_CORE_VERSION_23: usize = 230_000; +pub const BITCOIN_CORE_VERSION_26: usize = 260_000; const NOT_IN_MEMPOOL_ERROR_CODE: i32 = BitcoinRpcError::RpcInvalidAddressOrKey as i32; // Time to sleep before retry on startup. @@ -283,7 +283,7 @@ async fn connect(rpc: &Client, connection_timeout: Duration) -> Result= BITCOIN_CORE_VERSION_23 { + if version >= BITCOIN_CORE_VERSION_26 { return Err(Error::IncompatibleVersion(version)) } @@ -1052,7 +1052,9 @@ impl BitcoinCoreApi for BitcoinCore { } else { info!("Creating wallet {wallet_name}..."); // wallet does not exist, create - let result = self.rpc.create_wallet(wallet_name, None, None, None, None)?; + let result = self + .rpc + .create_wallet(wallet_name, None, None, None, None, Some(false))?; if let Some(warning) = result.warning { if !warning.is_empty() { warn!("Received warning while creating wallet {wallet_name}: {warning}"); From c9ec98016d1b991678460a06be8f8fd3055a0479 Mon Sep 17 00:00:00 2001 From: Sander Bosma Date: Wed, 9 Aug 2023 19:48:31 +0200 Subject: [PATCH 2/2] hack: get-proof command for contracts testing --- bitcoin/src/cli.rs | 12 +++++++++++ runtime/src/lib.rs | 6 +++--- runtime/src/rpc.rs | 5 +++++ runtime/src/utils/account_id.rs | 1 - vault/src/main.rs | 38 ++++++++++++++++++++++++++++++++- 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/bitcoin/src/cli.rs b/bitcoin/src/cli.rs index 3b82fb0f8..d4c07a0ff 100644 --- a/bitcoin/src/cli.rs +++ b/bitcoin/src/cli.rs @@ -109,6 +109,18 @@ impl BitcoinOpts { } } + pub async fn new_walletless( + &self, + wallet_name: Option, + ) -> Result, Error> { + let bitcoin_core = self + .new_client_builder(wallet_name) + .build_and_connect(Duration::from_millis(self.bitcoin_connection_timeout_ms)) + .await?; + bitcoin_core.sync().await?; + Ok(Arc::new(bitcoin_core)) + } + pub fn new_client_with_network( &self, wallet_name: Option, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d5dd78e0a..880b88dd0 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -30,9 +30,9 @@ pub use retry::{notify_retry, RetryPolicy}; #[cfg(feature = "testing-utils")] pub use rpc::SudoPallet; pub use rpc::{ - BtcRelayPallet, CollateralBalancesPallet, FeePallet, FeeRateUpdateReceiver, InterBtcParachain, IssuePallet, - OraclePallet, RedeemPallet, ReplacePallet, SecurityPallet, TimestampPallet, UtilFuncs, VaultRegistryPallet, - DEFAULT_SPEC_NAME, SS58_PREFIX, + encoded_tx_proof, BtcRelayPallet, CollateralBalancesPallet, FeePallet, FeeRateUpdateReceiver, InterBtcParachain, + IssuePallet, OraclePallet, RedeemPallet, ReplacePallet, SecurityPallet, TimestampPallet, UtilFuncs, + VaultRegistryPallet, DEFAULT_SPEC_NAME, SS58_PREFIX, }; pub use shutdown::{ShutdownReceiver, ShutdownSender}; pub use sp_arithmetic::{traits as FixedPointTraits, FixedI128, FixedPointNumber, FixedU128}; diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index f9b7e43dc..a379a91aa 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -1937,6 +1937,11 @@ impl SudoPallet for InterBtcParachain { } } +pub fn encoded_tx_proof(raw_proof: &RawTransactionProof) -> Result, Error> { + let proof = build_full_tx_proof(raw_proof)?; + Ok(proof.encode()) +} + pub fn build_full_tx_proof(raw_proof: &RawTransactionProof) -> Result, Error> { Ok(Static(FullTransactionProof { user_tx_proof: PartialTransactionProof { diff --git a/runtime/src/utils/account_id.rs b/runtime/src/utils/account_id.rs index 6ae04ea82..ad5762102 100644 --- a/runtime/src/utils/account_id.rs +++ b/runtime/src/utils/account_id.rs @@ -7,7 +7,6 @@ use codec::{Decode, Encode}; use serde::{Deserialize, Serialize}; use sp_core::crypto::{AccountId32 as Sp_AccountId32, Ss58Codec}; use subxt::utils::Static; - #[derive( Hash, Clone, diff --git a/vault/src/main.rs b/vault/src/main.rs index 90561b772..a89d05d4e 100644 --- a/vault/src/main.rs +++ b/vault/src/main.rs @@ -1,4 +1,4 @@ -use bitcoin::{Network, PrivateKey}; +use bitcoin::{BitcoinCoreApi, BlockHash, Network, PrivateKey, Txid}; use clap::Parser; use futures::Future; use runtime::{ @@ -12,6 +12,8 @@ use std::{ io::Write, net::{Ipv4Addr, SocketAddr}, path::PathBuf, + str::FromStr, + sync::Arc, }; use sysinfo::{System, SystemExt}; use tokio_stream::StreamExt; @@ -38,6 +40,8 @@ enum Commands { GenerateBitcoinKey(GenerateBitcoinKeyOpts), /// Generate the sr25519 parachain key pair. GenerateParachainKey(GenerateParachainKeyOpts), + /// Get proof for a given txid + GetProof(GetProofOpts), /// Run the Vault client (default). #[clap(name = "run")] RunVault(Box), @@ -80,6 +84,35 @@ impl GenerateBitcoinKeyOpts { } } +#[derive(Debug, Parser, Clone)] +struct GetProofOpts { + #[clap(long)] + txid: String, + #[clap(long)] + num_confirmations: u32, + /// Connection settings for Bitcoin Core. + #[clap(flatten)] + pub bitcoin: bitcoin::cli::BitcoinOpts, +} + +impl GetProofOpts { + pub async fn print_proof(&self) -> Result<(), Error> { + let bitcoin_rpc = self.bitcoin.new_walletless(None).await?; + let txid = Txid::from_str(&self.txid).unwrap(); + + let raw_proof = bitcoin_rpc + .wait_for_transaction_metadata(txid, self.num_confirmations, None, false) + .await? + .proof; + println!("user tx: {}", hex::encode(&raw_proof.raw_user_tx)); + println!("coinbase tx: {}", hex::encode(&raw_proof.raw_coinbase_tx)); + + let interlay_proof = runtime::encoded_tx_proof(&raw_proof)?; + println!("full proof: {}", hex::encode(interlay_proof)); + Ok(()) + } +} + #[derive(Debug, Parser, Clone)] struct GenerateParachainKeyOpts { /// Output file name or stdout if unspecified. @@ -161,6 +194,9 @@ async fn start() -> Result<(), Error> { Some(Commands::GenerateParachainKey(opts)) => { return opts.generate_and_write(); } + Some(Commands::GetProof(sub_opts)) => { + return sub_opts.print_proof().await; + } _ => (), }