Skip to content

Commit 10e66b9

Browse files
committed
Blockchain, GetTx and GetHeight traits for Pruned RPC blockchain backend
1 parent e6c2bb0 commit 10e66b9

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/blockchain/pruned_rpc.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
//! ```no run
77
//! ```
88
9-
use crate::bitcoin::{Address, Network};
9+
use crate::bitcoin::{Address, Network, Transaction, Txid};
1010
use crate::blockchain::*;
11+
use crate::database::BatchDatabase;
12+
use crate::{Error, FeeRate};
1113
use bitcoincore_rpc::Auth as RpcAuth;
12-
use bitcoincore_rpc::Client;
14+
use bitcoincore_rpc::{Client, RpcApi};
1315
use serde::{Deserialize, Serialize};
1416
use std::collections::HashSet;
1517
use std::path::PathBuf;
@@ -18,11 +20,11 @@ use std::path::PathBuf;
1820
#[derive(Debug)]
1921
pub struct PrunedRpcBlockchain {
2022
/// Rpc client to the node, includes the wallet name
21-
_client: Client,
23+
client: Client,
2224
/// Whether the wallet is a "descriptor" or "legacy" wallet in Core
2325
_is_descriptors: bool,
2426
/// Blockchain capabilities, cached here at startup
25-
_capabilities: HashSet<Capability>,
27+
capabilities: HashSet<Capability>,
2628
/// This is a fixed Address used as a hack key to store information on the node
2729
_storage_address: Address,
2830
}
@@ -78,27 +80,34 @@ impl PrunedRpcBlockchain {}
7880

7981
impl Blockchain for PrunedRpcBlockchain {
8082
fn get_capabilities(&self) -> HashSet<Capability> {
81-
todo!()
83+
self.capabilities.clone()
8284
}
8385

84-
fn broadcast(&self, _tx: &Transaction) -> Result<(), Error> {
85-
todo!()
86+
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
87+
Ok(self.client.send_raw_transaction(tx).map(|_| ())?)
8688
}
8789

88-
fn estimate_fee(&self, _target: usize) -> Result<FeeRate, Error> {
89-
todo!()
90+
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
91+
let sat_per_kb = self
92+
.client
93+
.estimate_smart_fee(target as u16, None)?
94+
.fee_rate
95+
.ok_or(Error::FeeRateUnavailable)?
96+
.as_sat() as f64;
97+
98+
Ok(FeeRate::from_sat_per_vb((sat_per_kb / 1000f64) as f32))
9099
}
91100
}
92101

93102
impl GetTx for PrunedRpcBlockchain {
94-
fn get_tx(&self, _txid: &Txid) -> Result<Option<Transaction>, Error> {
95-
todo!()
103+
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
104+
Ok(Some(self.client.get_raw_transaction(txid, None)?))
96105
}
97106
}
98107

99108
impl GetHeight for PrunedRpcBlockchain {
100109
fn get_height(&self) -> Result<u32, Error> {
101-
todo!()
110+
Ok(self.client.get_blockchain_info().map(|i| i.blocks as u32)?)
102111
}
103112
}
104113

0 commit comments

Comments
 (0)