Skip to content

Commit

Permalink
add address_tx_ids(); also index spends from addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Apr 5, 2022
1 parent eefcac1 commit bcd8d0b
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions zebra-state/src/service/finalized_state/zebra_db/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ use std::collections::{BTreeMap, BTreeSet, HashMap};

use zebra_chain::{
amount::{Amount, NonNegative},
transaction,
transparent::{self, Utxo},
};

use crate::{
service::finalized_state::{
disk_db::{DiskDb, DiskWriteBatch, ReadDisk, WriteDisk},
disk_format::transparent::{
AddressBalanceLocation, AddressLocation, AddressTransaction, AddressUnspentOutput,
OutputLocation,
disk_format::{
transparent::{
AddressBalanceLocation, AddressLocation, AddressTransaction, AddressUnspentOutput,
OutputLocation,
},
TransactionLocation,
},
zebra_db::ZebraDb,
},
Expand Down Expand Up @@ -162,6 +166,39 @@ impl ZebraDb {
addr_unspent_outputs
}

/// Returns the transaction hash for an [`TransactionLocation`].
pub fn tx_id_by_location(&self, tx_location: TransactionLocation) -> Option<transaction::Hash> {
let hash_by_tx_loc = self.db.cf_handle("hash_by_tx_loc").unwrap();

self.db.zs_get(&hash_by_tx_loc, &tx_location)
}

/// Returns the [`transaction::Hash`]es that created or spent outputs for a [`transparent::Address`],
/// in chain order, if they are in the finalized state.
#[allow(dead_code)]
pub fn address_tx_ids(
&self,
address: &transparent::Address,
) -> BTreeMap<TransactionLocation, transaction::Hash> {
let address_location = match self.address_location(address) {
Some(address_location) => address_location,
None => return BTreeMap::new(),
};

let transaction_locations = self.address_transaction_locations(address_location);

transaction_locations
.iter()
.map(|&tx_loc| {
(
tx_loc.transaction_location(),
self.tx_id_by_location(tx_loc.transaction_location())
.expect("transactions whose locations are stored must exist"),
)
})
.collect()
}

/// Returns the locations of any transactions that sent or received from a [`transparent::Address`],
/// if they are in the finalized state.
#[allow(dead_code)]
Expand Down Expand Up @@ -292,13 +329,22 @@ impl DiskWriteBatch {
address_balance_location
.spend_output(&spent_output)
.expect("balance underflow already checked");
let sending_address_location = address_balance_location.address_location();

// Delete the link from the AddressLocation to the spent OutputLocation in the database.
let address_spent_output = AddressUnspentOutput::new(
address_balance_location.address_location(),
spent_output_location,
);
self.zs_delete(&utxo_loc_by_transparent_addr_loc, address_spent_output);

// Create a link from the AddressLocation to the spent TransactionLocation in the database.
// Unlike the OutputLocation link, this will never be deleted.
let address_transaction = AddressTransaction::new(
sending_address_location,
spent_output_location.transaction_location(),
);
self.zs_insert(&tx_loc_by_transparent_addr_loc, address_transaction, ());
}

// Delete the OutputLocation, and the copy of the spent Output in the database.
Expand Down

0 comments on commit bcd8d0b

Please sign in to comment.