1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

remove Arc wrapper around daemon, instead pass directly into TxBroadcaster::broadcast

This commit is contained in:
conduition 2024-08-12 15:54:57 +00:00
parent 21d4d1cd3c
commit 2dd759d267
No known key found for this signature in database
GPG Key ID: 78296CED84F6B3C5
2 changed files with 7 additions and 14 deletions

View File

@ -13,7 +13,6 @@ use std::collections::{hash_map::Entry, HashMap};
use std::fmt;
use std::iter::FromIterator;
use std::str::FromStr;
use std::sync::Arc;
use crate::{
cache::Cache,
@ -125,7 +124,7 @@ pub struct Rpc {
tracker: Tracker,
cache: Cache,
rpc_duration: Histogram,
daemon: Arc<Daemon>,
daemon: Daemon,
tx_broadcaster: TxBroadcaster,
signal: Signal,
banner: String,
@ -144,15 +143,11 @@ impl Rpc {
let tracker = Tracker::new(config, metrics)?;
let signal = Signal::new();
let daemon = Arc::new(Daemon::connect(
config,
signal.exit_flag(),
tracker.metrics(),
)?);
let daemon = Daemon::connect(config, signal.exit_flag(), tracker.metrics())?;
let cache = Cache::new(tracker.metrics());
let tx_broadcaster = match &config.tx_broadcast_method {
TxBroadcastMethod::BitcoinRPC => TxBroadcaster::BitcoinRPC(daemon.clone()),
TxBroadcastMethod::BitcoinRPC => TxBroadcaster::BitcoinRPC,
TxBroadcastMethod::PushtxClear => TxBroadcaster::PushtxClear,
TxBroadcastMethod::PushtxTor => TxBroadcaster::PushtxTor,
TxBroadcastMethod::Script(script_path) => TxBroadcaster::Script(script_path.clone()),
@ -376,7 +371,7 @@ impl Rpc {
fn transaction_broadcast(&self, (tx_hex,): &(String,)) -> Result<Value> {
let tx_bytes = Vec::from_hex(tx_hex).context("non-hex transaction")?;
let tx = deserialize(&tx_bytes).context("invalid transaction")?;
let txid = self.tx_broadcaster.broadcast(&tx)?;
let txid = self.tx_broadcaster.broadcast(&self.daemon, &tx)?;
Ok(json!(txid))
}

View File

@ -1,13 +1,11 @@
use anyhow::{bail, Result};
use bitcoin::{Transaction, Txid};
use std::sync::Arc;
use crate::daemon::Daemon;
/// Represents one of many possible ways of broadcasting transactions.
pub enum TxBroadcaster {
BitcoinRPC(Arc<Daemon>),
BitcoinRPC,
PushtxClear,
PushtxTor,
Script(String),
@ -67,9 +65,9 @@ fn broadcast_with_pushtx(tx: &Transaction, opts: pushtx::Opts) -> Result<Txid> {
}
impl TxBroadcaster {
pub fn broadcast(&self, tx: &Transaction) -> Result<Txid> {
pub fn broadcast(&self, daemon: &Daemon, tx: &Transaction) -> Result<Txid> {
match self {
TxBroadcaster::BitcoinRPC(daemon) => daemon.broadcast(tx),
TxBroadcaster::BitcoinRPC => daemon.broadcast(tx),
TxBroadcaster::PushtxClear => broadcast_with_pushtx(
tx,
pushtx::Opts {