Skip to content

Commit

Permalink
node: add ledger_txs to Ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
Neotamandua committed Dec 20, 2024
1 parent 60d4280 commit 0291335
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions node/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub trait Ledger {
fn block_exists(&self, hash: &[u8]) -> Result<bool>;

fn ledger_tx(&self, tx_id: &[u8]) -> Result<Option<SpentTransaction>>;
fn ledger_txs(
&self,
tx_ids: Vec<&[u8; 32]>,
) -> Result<Vec<SpentTransaction>>;

fn ledger_tx_exists(&self, tx_id: &[u8]) -> Result<bool>;

Expand Down
37 changes: 37 additions & 0 deletions node/src/database/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,43 @@ impl<'db, DB: DBAccess> Ledger for DBTransaction<'db, DB> {
Ok(tx)
}

/// Returns a list of transactions from the ledger
///
/// This function expects a list of transaction IDs that are in the ledger.
///
/// It will return an error if any of the transaction IDs are not found in
/// the ledger.
fn ledger_txs(
&self,
tx_ids: Vec<&[u8; 32]>,
) -> Result<Vec<SpentTransaction>> {
let cf = self.ledger_txs_cf;

let ids = tx_ids.into_iter().map(|id| (cf, id)).collect::<Vec<_>>();

let multi_get_results = self.inner.multi_get_cf(ids);

let mut spent_transactions =
Vec::with_capacity(multi_get_results.len());
for result in multi_get_results.into_iter() {
let opt_blob = result.map_err(|e| {
std::io::Error::new(std::io::ErrorKind::Other, e)
})?;

let Some(blob) = opt_blob else {
return Err(anyhow::anyhow!(
"At least one Transaction ID was not found"
));
};

let stx = SpentTransaction::read(&mut &blob[..])?;

spent_transactions.push(stx);
}

Ok(spent_transactions)
}

/// Returns true if the transaction exists in the
/// ledger
///
Expand Down

0 comments on commit 0291335

Please sign in to comment.