From 85cd261fcd50e87ad8c3b6a5443e7e38266ff655 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Mon, 14 Nov 2022 09:37:13 +0100 Subject: [PATCH] bitcoin: remove the BitcoinError enum, use String as error instead It looks like we'd end up only using variants that contain a String, for little benefit. Cut down on complexity for now. --- src/bitcoin/mod.rs | 28 +++++----------------------- src/commands/mod.rs | 9 ++++----- src/testutils.rs | 4 ++-- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/src/bitcoin/mod.rs b/src/bitcoin/mod.rs index d54279b6..a6531f34 100644 --- a/src/bitcoin/mod.rs +++ b/src/bitcoin/mod.rs @@ -9,28 +9,10 @@ use crate::{ descriptors, }; -use std::{collections::HashMap, error, fmt, sync}; +use std::{collections::HashMap, fmt, sync}; use miniscript::bitcoin; -/// Error occuring when querying our Bitcoin backend. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum BitcoinError { - Broadcast(String), -} - -impl fmt::Display for BitcoinError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - BitcoinError::Broadcast(reason) => { - write!(f, "Failed to broadcast transaction: '{}'", reason) - } - } - } -} - -impl error::Error for BitcoinError {} - /// Information about the best block in the chain #[derive(Debug, Clone, Eq, PartialEq, Copy)] pub struct BlockChainTip { @@ -90,7 +72,7 @@ pub trait BitcoinInterface: Send { fn common_ancestor(&self, tip: &BlockChainTip) -> Option; /// Broadcast this transaction to the Bitcoin P2P network - fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), BitcoinError>; + fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), String>; /// Trigger a rescan of the block chain for transactions related to this descriptor since /// the given date. @@ -289,10 +271,10 @@ impl BitcoinInterface for d::BitcoinD { Some(ancestor) } - fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), BitcoinError> { + fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), String> { match self.broadcast_tx(tx) { Ok(()) => Ok(()), - Err(BitcoindError::Server(e)) => Err(BitcoinError::Broadcast(e.to_string())), + Err(BitcoindError::Server(e)) => Err(e.to_string()), // We assume the Bitcoin backend doesn't fail, so it must be a JSONRPC error. Err(e) => panic!( "Unexpected Bitcoin error when broadcast transaction: '{}'.", @@ -371,7 +353,7 @@ impl BitcoinInterface for sync::Arc> self.lock().unwrap().common_ancestor(tip) } - fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), BitcoinError> { + fn broadcast_tx(&self, tx: &bitcoin::Transaction) -> Result<(), String> { self.lock().unwrap().broadcast_tx(tx) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 15323fcb..48762a64 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -5,7 +5,7 @@ mod utils; use crate::{ - bitcoin::{BitcoinError, BitcoinInterface}, + bitcoin::BitcoinInterface, database::{Coin, DatabaseInterface}, descriptors, DaemonControl, VERSION, }; @@ -499,10 +499,9 @@ impl DaemonControl { // Then, broadcast it (or try to, we never know if we are not going to hit an // error at broadcast time). let final_tx = spend_psbt.extract_tx(); - match self.bitcoin.broadcast_tx(&final_tx) { - Ok(()) => Ok(()), - Err(BitcoinError::Broadcast(e)) => Err(CommandError::TxBroadcast(e)), - } + self.bitcoin + .broadcast_tx(&final_tx) + .map_err(CommandError::TxBroadcast) } /// Trigger a rescan of the block chain for transactions involving our main descriptor between diff --git a/src/testutils.rs b/src/testutils.rs index 6201f32b..3607e25c 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -1,5 +1,5 @@ use crate::{ - bitcoin::{BitcoinError, BitcoinInterface, BlockChainTip, UTxO}, + bitcoin::{BitcoinInterface, BlockChainTip, UTxO}, config::{BitcoinConfig, Config}, database::{Coin, DatabaseConnection, DatabaseInterface, SpendBlock}, descriptors, DaemonHandle, @@ -71,7 +71,7 @@ impl BitcoinInterface for DummyBitcoind { todo!() } - fn broadcast_tx(&self, _: &bitcoin::Transaction) -> Result<(), BitcoinError> { + fn broadcast_tx(&self, _: &bitcoin::Transaction) -> Result<(), String> { todo!() }