Skip to content

Commit

Permalink
ledger: Add add_transaction, which combines add_transaction_unconditi…
Browse files Browse the repository at this point in the history
…onally and check_transaction.
  • Loading branch information
ceyhunsen committed Jun 12, 2024
1 parent e6cc3e9 commit 4f7deda
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ impl RpcApi for Client {
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();

self.ledger.check_transaction(tx.clone())?;

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

Ok(tx.compute_txid())
}
Expand Down
10 changes: 8 additions & 2 deletions src/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ impl Ledger {
get_item!(self.utxos);
}

/// Adds transaction to current block, after verifying.
pub fn add_transaction(&self, transaction: Transaction) -> Result<Txid, LedgerError> {
self.check_transaction(&transaction)?;

self.add_transaction_unconditionally(transaction)
}
/// Adds transaction to current block, without checking anything.
pub fn add_transaction_unconditionally(
&self,
Expand Down Expand Up @@ -45,12 +51,12 @@ impl Ledger {
/// # Panics
///
/// If mutex can't be locked, it will panic.
pub fn check_transaction(&self, transaction: Transaction) -> Result<(), LedgerError> {
pub fn check_transaction(&self, transaction: &Transaction) -> Result<(), LedgerError> {
Ok(self
.database
.lock()
.unwrap()
.verify_transaction(&transaction)?)
.verify_transaction(transaction)?)
}
}

Expand Down

0 comments on commit 4f7deda

Please sign in to comment.