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

multi: use RPC-based blockchain backend on regtest #68

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 39 additions & 2 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ tonic-build = "0.12.3"
[dependencies]
anyhow = "1.0.89"
async-broadcast = "0.7.1"
bdk = { version = "0.29.0", features = ["all-keys", "sqlite"] }
bdk = { version = "0.29.0", features = [
"all-keys",
"sqlite",
"rpc",
], git = "https://github.com/torkelrogstad/bdk", branch = "2024-10-30-rpc-patch" }
bincode = "1.3.3"
bitcoin = "0.32.3"
blake3 = "1.5.4"
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ async fn main() -> Result<()> {
let wallet: Option<Arc<wallet::Wallet>> = if cli.enable_wallet {
let wallet = Wallet::new(
&wallet_data_dir,
&cli.node_rpc_opts,
&cli.wallet_opts,
mainchain_client,
validator.clone(),
)
.map_err(|e| miette!("failed to create wallet: {:?}", e))
.map_err(|e| miette!("failed to create wallet: {:#}", e))
.await?;
Some(Arc::new(wallet))
} else {
Expand Down
26 changes: 24 additions & 2 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
use bdk::blockchain::rpc::Auth;
use bip300301::jsonrpsee::http_client::HttpClient;
use miette::{miette, IntoDiagnostic};
use miette::{miette, IntoDiagnostic, Result};

use crate::cli::NodeRpcConfig;

pub fn create_client(conf: &NodeRpcConfig) -> Result<HttpClient, miette::Report> {
pub fn create_auth(conf: &NodeRpcConfig) -> Result<bdk::blockchain::rpc::Auth> {
if let Some(ref cookie) = conf.cookie_path {
return Ok(Auth::Cookie {
file: cookie.into(),
});
}

let Some(ref user) = conf.user else {
return Err(miette!("RPC user must be set"));
};

let Some(ref pass) = conf.pass else {
return Err(miette!("RPC password must be set"));
};

Ok(Auth::UserPass {
username: user.clone(),
password: pass.clone(),
})
}

pub fn create_client(conf: &NodeRpcConfig) -> Result<HttpClient> {
if conf.user.is_none() != conf.pass.is_none() {
return Err(miette!("RPC user and password must be set together"));
}
Expand Down
141 changes: 141 additions & 0 deletions src/wallet/blockchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use std::{cell::RefCell, collections::HashSet};

use bdk::{
self,
bitcoin::{Network, Transaction, Txid},
blockchain::{
rpc::RpcBlockchainFactory, BlockchainFactory as _, ElectrumBlockchain, Progress,
RpcBlockchain,
},
database::BatchDatabase,
electrum_client::ConfigBuilder,
};
use miette::{miette, IntoDiagnostic as _, Result};

use crate::{
cli::{NodeRpcConfig, WalletConfig},
rpc_client,
};

// Wrapper for different blockchain backends, that allow the wallet to dynamically
// choose one at run-time.
pub(crate) enum Type {
Electrum(ElectrumBlockchain),
Rpc(RpcBlockchain),
}

impl Type {
pub(crate) fn new<T: BatchDatabase>(
network: Network,
node_config: &NodeRpcConfig,
wallet_config: &WalletConfig,
wallet: &bdk::Wallet<T>,
) -> Result<Self> {
if network == Network::Regtest {
tracing::debug!("Creating RPC blockchain client");

let factory = RpcBlockchainFactory {
url: node_config.addr.to_string(),
auth: rpc_client::create_auth(node_config)?,
network,
wallet_name_prefix: None,
sync_params: None,
default_skip_blocks: 0,
};

let blockchain = factory.build_for_wallet(wallet, None).into_diagnostic()?;

return Ok(Type::Rpc(blockchain));
}

let electrum_url = format!(
"{}:{}",
wallet_config.electrum_host, wallet_config.electrum_port
);

tracing::debug!("Creating electrum blockchain client: {electrum_url}");

// Apply a reasonably short timeout to prevent the wallet from hanging
let timeout = 5;
let config = ConfigBuilder::new().timeout(Some(timeout)).build();

let electrum_client = bdk::electrum_client::Client::from_config(&electrum_url, config)
.map_err(|err| miette!("failed to create electrum client: {err:#}"))?;

Ok(Type::Electrum(ElectrumBlockchain::from(electrum_client)))
}
}

impl bdk::blockchain::Blockchain for Type {
fn get_capabilities(&self) -> HashSet<bdk::blockchain::Capability> {
match self {
Type::Electrum(b) => b.get_capabilities(),
Type::Rpc(b) => b.get_capabilities(),
}
}

fn broadcast(&self, tx: &bdk::bitcoin::Transaction) -> Result<(), bdk::Error> {
match self {
Type::Electrum(b) => b.broadcast(tx),
Type::Rpc(b) => b.broadcast(tx),
}
}

fn estimate_fee(&self, target: usize) -> Result<bdk::FeeRate, bdk::Error> {
match self {
Type::Electrum(b) => b.estimate_fee(target),
Type::Rpc(b) => b.estimate_fee(target),
}
}
}

impl bdk::blockchain::GetBlockHash for Type {
fn get_block_hash(&self, height: u64) -> Result<bdk::bitcoin::BlockHash, bdk::Error> {
match self {
Type::Electrum(b) => b.get_block_hash(height),
Type::Rpc(b) => b.get_block_hash(height),
}
}
}

impl bdk::blockchain::GetTx for Type {
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, bdk::Error> {
match self {
Type::Electrum(b) => b.get_tx(txid),
Type::Rpc(b) => b.get_tx(txid),
}
}
}

impl bdk::blockchain::GetHeight for Type {
fn get_height(&self) -> Result<u32, bdk::Error> {
match self {
Type::Electrum(b) => b.get_height(),
Type::Rpc(b) => b.get_height(),
}
}
}

impl bdk::blockchain::WalletSync for Type {
fn wallet_setup<D: BatchDatabase>(
&self,
database: &RefCell<D>,
progress_update: Box<dyn Progress>,
) -> Result<(), bdk::Error> {
match self {
Type::Electrum(b) => b.wallet_setup(database, progress_update),
Type::Rpc(b) => b.wallet_setup(database, progress_update),
}
}

fn wallet_sync<D: BatchDatabase>(
&self,
database: &RefCell<D>,
progress_update: Box<dyn Progress>,
) -> Result<(), bdk::Error> {
match self {
Type::Electrum(b) => b.wallet_sync(database, progress_update),
Type::Rpc(b) => b.wallet_sync(database, progress_update),
}
}
}
26 changes: 7 additions & 19 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use bdk::{
consensus::Encodable as _, hashes::Hash as _, psbt::PartiallySignedTransaction,
script::PushBytesBuf, BlockHash, Network,
},
blockchain::ElectrumBlockchain,
database::SqliteDatabase,
electrum_client::ConfigBuilder,
keys::{
bip39::{Language, Mnemonic},
DerivableKey, ExtendedKey,
Expand Down Expand Up @@ -46,22 +44,23 @@ use parking_lot::{Mutex, RwLock};
use rusqlite::{Connection, Row};

use crate::{
cli::WalletConfig,
cli::{NodeRpcConfig, WalletConfig},
convert,
messages::{self, CoinbaseBuilder, M8_BMM_REQUEST_TAG},
types::{Ctip, SidechainAck, SidechainNumber, SidechainProposal},
validator::Validator,
};
use error::WalletError;

mod blockchain;
pub mod error;

pub struct Wallet {
main_client: HttpClient,
validator: Validator,
bitcoin_wallet: Arc<Mutex<bdk::Wallet<SqliteDatabase>>>,
db_connection: Arc<Mutex<rusqlite::Connection>>,
bitcoin_blockchain: ElectrumBlockchain,
bitcoin_blockchain: blockchain::Type,
_mnemonic: Mnemonic,
// seed
// sidechain number
Expand All @@ -74,7 +73,8 @@ pub struct Wallet {
impl Wallet {
pub async fn new(
data_dir: &Path,
config: &WalletConfig,
node_config: &NodeRpcConfig,
wallet_config: &WalletConfig,
main_client: HttpClient,
validator: Validator,
) -> Result<Self> {
Expand Down Expand Up @@ -116,20 +116,8 @@ impl Wallet {
)
.into_diagnostic()?;

let bitcoin_blockchain = {
let electrum_url = format!("{}:{}", config.electrum_host, config.electrum_port);

tracing::debug!("creating electrum client: {electrum_url}");

// Apply a reasonably short timeout to prevent the wallet from hanging
let timeout = 5;
let config = ConfigBuilder::new().timeout(Some(timeout)).build();

let electrum_client = bdk::electrum_client::Client::from_config(&electrum_url, config)
.map_err(|err| miette!("failed to create electrum client: {err:#}"))?;

ElectrumBlockchain::from(electrum_client)
};
let bitcoin_blockchain =
blockchain::Type::new(network, node_config, wallet_config, &bitcoin_wallet)?;

use rusqlite_migration::{Migrations, M};

Expand Down
Loading