Skip to content

Commit 04b1f50

Browse files
committed
errors: Add LedgerError to bitcoincore_rpc::Error converter.
1 parent 807a5ec commit 04b1f50

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/client/rpc_api.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ impl RpcApi for Client {
4444
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
4545
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();
4646

47-
if let Err(e) = self.ledger.check_transaction(tx.clone()) {
48-
return Err(bitcoincore_rpc::Error::Io(std::io::Error::other(format!(
49-
"{e}"
50-
))));
51-
}
47+
self.ledger.check_transaction(tx.clone())?;
5248

53-
self.ledger.add_transaction_unconditionally(tx.clone());
49+
self.ledger.add_transaction_unconditionally(tx.clone())?;
5450

5551
Ok(tx.compute_txid())
5652
}
@@ -186,7 +182,7 @@ impl RpcApi for Client {
186182
output: vec![txout],
187183
};
188184

189-
self.ledger.add_transaction_unconditionally(tx.clone());
185+
self.ledger.add_transaction_unconditionally(tx.clone())?;
190186

191187
for output in tx.output {
192188
self.ledger.add_utxo(output);

src/ledger/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ pub enum LedgerError {
1010
#[error("Database returned an error: {0}")]
1111
Database(anyhow::Error),
1212
}
13+
14+
impl From<LedgerError> for bitcoincore_rpc::Error {
15+
fn from(error: LedgerError) -> Self {
16+
bitcoincore_rpc::Error::ReturnedError(error.to_string())
17+
}
18+
}

src/ledger/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ pub struct Ledger {
3333

3434
impl Ledger {
3535
/// Creates a new empty ledger.
36+
///
37+
/// # Panics
38+
///
39+
/// If database connection cannot be established in bitcoin-simulator, it
40+
/// will panic.
3641
pub fn new() -> Self {
3742
Self {
3843
database: Arc::new(Mutex::new(Database::connect_temporary_database().unwrap())),

src/ledger/transactions.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ impl Ledger {
1515
}
1616

1717
/// Adds transaction to current block, without checking anything.
18-
pub fn add_transaction_unconditionally(&self, transaction: Transaction) {
19-
self.database
18+
pub fn add_transaction_unconditionally(
19+
&self,
20+
transaction: Transaction,
21+
) -> Result<(), LedgerError> {
22+
if let Err(e) = self
23+
.database
2024
.lock()
2125
.unwrap()
2226
.insert_transaction_unconditionally(&transaction)
23-
.unwrap();
27+
{
28+
return Err(LedgerError::Database(e));
29+
};
2430

2531
add_item!(self.transactions, transaction);
32+
33+
Ok(())
2634
}
2735
/// Returns user's list of transactions.
2836
pub fn get_transaction(&self, txid: Txid) -> Transaction {

0 commit comments

Comments
 (0)