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.
This commit is contained in:
parent
9b253e7ea7
commit
85cd261fcd
@ -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<BlockChainTip>;
|
||||
|
||||
/// 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<sync::Mutex<dyn BitcoinInterface + 'static>>
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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!()
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user