Skip to content

Commit

Permalink
errors: Add initial error module.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jun 11, 2024
1 parent 65a05c4 commit 2f398d6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ serde = { version = "1.0", default-features = false }
serde_json = "1.0.108"
bitcoincore-rpc = { git = "https://github.com/rust-bitcoin/rust-bitcoincore-rpc.git" }
bitcoin-simulator = { git = "https://github.com/chainwayxyz/bitcoin-simulator" }
anyhow = "1.0.86"
thiserror = "1.0.61"
47 changes: 26 additions & 21 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ impl RpcApi for Client {
);
}

fn send_raw_transaction<R: bitcoincore_rpc::RawTx>(
&self,
tx: R,
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();

if let Err(e) = self.ledger.check_transaction(tx.clone()) {
return Err(bitcoincore_rpc::Error::Io(std::io::Error::other(format!(
"{e}"
))));
}

self.ledger.add_transaction_unconditionally(tx.clone());

Ok(tx.compute_txid())
}
fn get_raw_transaction(
&self,
txid: &bitcoin::Txid,
Expand Down Expand Up @@ -67,22 +83,6 @@ impl RpcApi for Client {
blocktime: None,
})
}
fn send_raw_transaction<R: bitcoincore_rpc::RawTx>(
&self,
tx: R,
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();

self.ledger.add_transaction_unconditionally(tx.clone());

if !self.ledger.check_transaction(tx.clone()) {
return Err(bitcoincore_rpc::Error::Io(std::io::Error::other(
"Transaction not valid.",
)));
}

Ok(tx.compute_txid())
}

fn get_transaction(
&self,
Expand Down Expand Up @@ -207,19 +207,24 @@ mod tests {
fn raw_transaction() {
let rpc = Client::new("", bitcoincore_rpc::Auth::None).unwrap();

// Get some BTC.
let address = rpc.get_new_address(None, None).unwrap();
rpc.generate_to_address(101, address.assume_checked_ref())
.unwrap();
let prev_tx = rpc.ledger._get_transactions().get(0).unwrap().to_owned();

// Insert raw transactions to Bitcoin.
let txin = TxIn {
previous_output: OutPoint {
txid: Txid::from_byte_array([0x45; 32]),
txid: prev_tx.compute_txid(),
vout: 0,
},
sequence: bitcoin::transaction::Sequence::ENABLE_RBF_NO_LOCKTIME,
script_sig: ScriptBuf::default(),
witness: Witness::new(),
..Default::default()
};
println!("----- {:?}", txin.clone());
let txout = TxOut {
value: Amount::from_sat(0x1F),
script_pubkey: test_common::get_temp_address().script_pubkey(),
script_pubkey: address.assume_checked().script_pubkey(),
};
let inserted_tx1 = test_common::create_transaction(vec![txin], vec![txout]);
rpc.send_raw_transaction(&inserted_tx1).unwrap();
Expand Down
12 changes: 12 additions & 0 deletions src/ledger/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! # Errors
//!
//! Errors that can be returned from ledger operations.
use thiserror::Error;

/// Ledger error types.
#[derive(Error, Debug)]
pub enum LedgerError {
#[error("Database returned an error: {0}")]
Database(anyhow::Error),
}
1 change: 1 addition & 0 deletions src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
sync::{Arc, Mutex},
};

mod errors;
mod transactions;

/// Adds a new item to a `Vec` member, guarded by a `Cell`.
Expand Down
13 changes: 6 additions & 7 deletions src/ledger/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Transaction Related Ledger Operations
use super::Ledger;
use super::{errors::LedgerError, Ledger};
use crate::{add_item, get_item};
use bitcoin::{Transaction, TxOut, Txid};

Expand Down Expand Up @@ -37,17 +37,16 @@ impl Ledger {
get_item!(self.transactions);
}
/// Checks if a transaction is OK or not.
pub fn check_transaction(&self, transaction: Transaction) -> bool {
if let Ok(()) = self
pub fn check_transaction(&self, transaction: Transaction) -> Result<(), LedgerError> {
match self
.database
.lock()
.unwrap()
.verify_transaction(&transaction)
{
return true;
};

false
Ok(()) => Ok(()),
Err(e) => Err(LedgerError::Database(e)),
}
}
}

Expand Down

0 comments on commit 2f398d6

Please sign in to comment.