Skip to content

Commit

Permalink
ledger-rpc_api: Add wrapper in Ledger for add/get transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jun 10, 2024
1 parent 38433e9 commit 2e79b97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
19 changes: 3 additions & 16 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ impl RpcApi for Client {
txid: &bitcoin::Txid,
_block_hash: Option<&bitcoin::BlockHash>,
) -> bitcoincore_rpc::Result<bitcoin::Transaction> {
Ok(self
.database
.lock()
.unwrap()
.get_transaction(&txid.to_string())
.unwrap())
Ok(self.ledger.get_transaction(*txid))
}

fn send_raw_transaction<R: bitcoincore_rpc::RawTx>(
Expand All @@ -57,11 +52,7 @@ impl RpcApi for Client {
) -> bitcoincore_rpc::Result<bitcoin::Txid> {
let tx: Transaction = encode::deserialize_hex(&tx.raw_hex()).unwrap();

self.database
.lock()
.unwrap()
.insert_transaction_unconditionally(&tx)
.unwrap();
self.ledger.add_transaction_unconditionally(tx.clone());

Ok(tx.compute_txid())
}
Expand Down Expand Up @@ -191,11 +182,7 @@ impl RpcApi for Client {
output: vec![txout],
};

self.database
.lock()
.unwrap()
.insert_transaction_unconditionally(&tx)
.unwrap();
self.ledger.add_transaction_unconditionally(tx.clone());

for output in tx.output {
self.ledger.add_utxo(output);
Expand Down
10 changes: 9 additions & 1 deletion src/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::Ledger;
use crate::{add_item, get_item};
use bitcoin::{Transaction, TxOut};
use bitcoin::{Transaction, TxOut, Txid};

impl Ledger {
/// Adds a new UTXO to user's UTXO's.
Expand All @@ -25,6 +25,14 @@ impl Ledger {
add_item!(self.transactions, transaction);
}
/// Returns user's list of transactions.
pub fn get_transaction(&self, txid: Txid) -> Transaction {
self.database
.lock()
.unwrap()
.get_transaction(&txid.to_string())
.unwrap()
}
/// Returns user's list of transactions.
pub fn get_transactions(&self) -> Vec<Transaction> {
get_item!(self.transactions);
}
Expand Down

0 comments on commit 2e79b97

Please sign in to comment.