Skip to content

Commit

Permalink
Avoids returning genesis coinbase tx hash when indexes are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Oct 18, 2024
1 parent 252f6b7 commit fd2bcca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
12 changes: 11 additions & 1 deletion zebra-state/src/service/finalized_state/disk_format/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl IntoDisk for TransactionIndex {

impl FromDisk for TransactionIndex {
fn from_bytes(disk_bytes: impl AsRef<[u8]>) -> Self {
let disk_bytes = disk_bytes.as_ref().try_into().unwrap_or_default();
let disk_bytes = disk_bytes.as_ref().try_into().unwrap();

TransactionIndex::from_index(u16::from_be_bytes(disk_bytes))
}
Expand All @@ -331,6 +331,16 @@ impl IntoDisk for TransactionLocation {
}
}

impl FromDisk for Option<TransactionLocation> {
fn from_bytes(disk_bytes: impl AsRef<[u8]>) -> Self {
if disk_bytes.as_ref().is_empty() {
Some(TransactionLocation::from_bytes(disk_bytes))
} else {
None
}
}
}

impl FromDisk for TransactionLocation {
fn from_bytes(disk_bytes: impl AsRef<[u8]>) -> Self {
let (height_bytes, index_bytes) = disk_bytes.as_ref().split_at(HEIGHT_DISK_BYTES);
Expand Down
15 changes: 9 additions & 6 deletions zebra-state/src/service/finalized_state/zebra_db/shielded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,39 @@ impl ZebraDb {
}

/// Returns the [`TransactionLocation`] of the transaction that revealed
/// the given [`sprout::Nullifier`], if it is revealed in the finalized state.
/// the given [`sprout::Nullifier`], if it is revealed in the finalized state and its
/// spending transaction hash has been indexed.
#[allow(clippy::unwrap_in_result)]
pub fn sprout_revealing_tx_loc(
&self,
sprout_nullifier: &sprout::Nullifier,
) -> Option<TransactionLocation> {
let sprout_nullifiers = self.db.cf_handle("sprout_nullifiers").unwrap();
self.db.zs_get(&sprout_nullifiers, &sprout_nullifier)
self.db.zs_get(&sprout_nullifiers, &sprout_nullifier)?
}

/// Returns the [`TransactionLocation`] of the transaction that revealed
/// the given [`sapling::Nullifier`], if it is revealed in the finalized state.
/// the given [`sapling::Nullifier`], if it is revealed in the finalized state and its
/// spending transaction hash has been indexed.
#[allow(clippy::unwrap_in_result)]
pub fn sapling_revealing_tx_loc(
&self,
sapling_nullifier: &sapling::Nullifier,
) -> Option<TransactionLocation> {
let sapling_nullifiers = self.db.cf_handle("sapling_nullifiers").unwrap();
self.db.zs_get(&sapling_nullifiers, &sapling_nullifier)
self.db.zs_get(&sapling_nullifiers, &sapling_nullifier)?
}

/// Returns the [`TransactionLocation`] of the transaction that revealed
/// the given [`orchard::Nullifier`], if it is revealed in the finalized state.
/// the given [`orchard::Nullifier`], if it is revealed in the finalized state and its
/// spending transaction hash has been indexed.
#[allow(clippy::unwrap_in_result)]
pub fn orchard_revealing_tx_loc(
&self,
orchard_nullifier: &orchard::Nullifier,
) -> Option<TransactionLocation> {
let orchard_nullifiers = self.db.cf_handle("orchard_nullifiers").unwrap();
self.db.zs_get(&orchard_nullifiers, &orchard_nullifier)
self.db.zs_get(&orchard_nullifiers, &orchard_nullifier)?
}

/// Returns `true` if the finalized state contains `sprout_anchor`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ impl ZebraDb {
}

/// Returns the [`TransactionLocation`] of the transaction that spent the given
/// [`transparent::OutPoint`], if it is unspent in the finalized state.
/// [`transparent::OutPoint`], if it is unspent in the finalized state and its
/// spending transaction hash has been indexed.
pub fn spending_tx_loc(&self, outpoint: &transparent::OutPoint) -> Option<TransactionLocation> {
let output_location = self.output_location(outpoint)?;
self.tx_location_by_spent_output_location(&output_location)
Expand Down
3 changes: 2 additions & 1 deletion zebra-state/src/service/read/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ where

/// Returns the [`Hash`](transaction::Hash) of the transaction that spent an output at
/// the provided [`transparent::OutPoint`] or revealed the provided nullifier, if it exists
/// and is spent or revealed in the non-finalized `chain` or finalized `db`.
/// and is spent or revealed in the non-finalized `chain` or finalized `db` and its
/// spending transaction hash has been indexed.
pub fn spending_transaction_hash<C>(
chain: Option<C>,
db: &ZebraDb,
Expand Down

0 comments on commit fd2bcca

Please sign in to comment.