Skip to content

Commit 2f398d6

Browse files
committed
errors: Add initial error module.
1 parent 65a05c4 commit 2f398d6

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ serde = { version = "1.0", default-features = false }
1010
serde_json = "1.0.108"
1111
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc.git" }
1212
bitcoin-simulator = { git = "https://github.com/chainwayxyz/bitcoin-simulator" }
13+
anyhow = "1.0.86"
14+
thiserror = "1.0.61"

src/client/rpc_api.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ impl RpcApi for Client {
3838
);
3939
}
4040

41+
fn send_raw_transaction<R: bitcoincore_rpc::RawTx>(
42+
&self,
43+
tx: R,
44+
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
45+
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();
46+
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+
}
52+
53+
self.ledger.add_transaction_unconditionally(tx.clone());
54+
55+
Ok(tx.compute_txid())
56+
}
4157
fn get_raw_transaction(
4258
&self,
4359
txid: &bitcoin::Txid,
@@ -67,22 +83,6 @@ impl RpcApi for Client {
6783
blocktime: None,
6884
})
6985
}
70-
fn send_raw_transaction<R: bitcoincore_rpc::RawTx>(
71-
&self,
72-
tx: R,
73-
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
74-
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();
75-
76-
self.ledger.add_transaction_unconditionally(tx.clone());
77-
78-
if !self.ledger.check_transaction(tx.clone()) {
79-
return Err(bitcoincore_rpc::Error::Io(std::io::Error::other(
80-
"Transaction not valid.",
81-
)));
82-
}
83-
84-
Ok(tx.compute_txid())
85-
}
8686

8787
fn get_transaction(
8888
&self,
@@ -207,19 +207,24 @@ mod tests {
207207
fn raw_transaction() {
208208
let rpc = Client::new("", bitcoincore_rpc::Auth::None).unwrap();
209209

210+
// Get some BTC.
211+
let address = rpc.get_new_address(None, None).unwrap();
212+
rpc.generate_to_address(101, address.assume_checked_ref())
213+
.unwrap();
214+
let prev_tx = rpc.ledger._get_transactions().get(0).unwrap().to_owned();
215+
210216
// Insert raw transactions to Bitcoin.
211217
let txin = TxIn {
212218
previous_output: OutPoint {
213-
txid: Txid::from_byte_array([0x45; 32]),
219+
txid: prev_tx.compute_txid(),
214220
vout: 0,
215221
},
216-
sequence: bitcoin::transaction::Sequence::ENABLE_RBF_NO_LOCKTIME,
217-
script_sig: ScriptBuf::default(),
218-
witness: Witness::new(),
222+
..Default::default()
219223
};
224+
println!("----- {:?}", txin.clone());
220225
let txout = TxOut {
221226
value: Amount::from_sat(0x1F),
222-
script_pubkey: test_common::get_temp_address().script_pubkey(),
227+
script_pubkey: address.assume_checked().script_pubkey(),
223228
};
224229
let inserted_tx1 = test_common::create_transaction(vec![txin], vec![txout]);
225230
rpc.send_raw_transaction(&inserted_tx1).unwrap();

src/ledger/errors.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! # Errors
2+
//!
3+
//! Errors that can be returned from ledger operations.
4+
5+
use thiserror::Error;
6+
7+
/// Ledger error types.
8+
#[derive(Error, Debug)]
9+
pub enum LedgerError {
10+
#[error("Database returned an error: {0}")]
11+
Database(anyhow::Error),
12+
}

src/ledger/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::{
1212
sync::{Arc, Mutex},
1313
};
1414

15+
mod errors;
1516
mod transactions;
1617

1718
/// Adds a new item to a `Vec` member, guarded by a `Cell`.

src/ledger/transactions.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Transaction Related Ledger Operations
22
3-
use super::Ledger;
3+
use super::{errors::LedgerError, Ledger};
44
use crate::{add_item, get_item};
55
use bitcoin::{Transaction, TxOut, Txid};
66

@@ -37,17 +37,16 @@ impl Ledger {
3737
get_item!(self.transactions);
3838
}
3939
/// Checks if a transaction is OK or not.
40-
pub fn check_transaction(&self, transaction: Transaction) -> bool {
41-
if let Ok(()) = self
40+
pub fn check_transaction(&self, transaction: Transaction) -> Result<(), LedgerError> {
41+
match self
4242
.database
4343
.lock()
4444
.unwrap()
4545
.verify_transaction(&transaction)
4646
{
47-
return true;
48-
};
49-
50-
false
47+
Ok(()) => Ok(()),
48+
Err(e) => Err(LedgerError::Database(e)),
49+
}
5150
}
5251
}
5352

0 commit comments

Comments
 (0)