Skip to content

Commit

Permalink
Combine near-duplicate Utxo creation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jul 8, 2021
1 parent f817df6 commit c7e1a0f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 66 deletions.
34 changes: 1 addition & 33 deletions zebra-consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! verification, where it may be accepted or rejected.
use std::{
collections::HashMap,
future::Future,
pin::Pin,
sync::Arc,
Expand All @@ -25,7 +24,6 @@ use tracing::Instrument;
use zebra_chain::{
block::{self, Block},
parameters::Network,
transaction, transparent,
work::equihash,
};
use zebra_state as zs;
Expand Down Expand Up @@ -175,7 +173,7 @@ where

let mut async_checks = FuturesUnordered::new();

let known_utxos = new_outputs(&block, &transaction_hashes);
let known_utxos = Arc::new(zs::new_outputs(&block, &transaction_hashes));
for transaction in &block.transactions {
let rsp = transaction_verifier
.ready_and()
Expand Down Expand Up @@ -230,33 +228,3 @@ where
.boxed()
}
}

/// Compute an index of newly created transparent outputs, given a block and a
/// list of precomputed transaction hashes.
fn new_outputs(
block: &Block,
transaction_hashes: &[transaction::Hash],
) -> Arc<HashMap<transparent::OutPoint, zs::Utxo>> {
let mut new_outputs = HashMap::default();
let height = block.coinbase_height().expect("block has coinbase height");
for (transaction, hash) in block
.transactions
.iter()
.zip(transaction_hashes.iter().cloned())
{
let from_coinbase = transaction.is_coinbase();
for (index, output) in transaction.outputs().iter().cloned().enumerate() {
let index = index as u32;
new_outputs.insert(
transparent::OutPoint { hash, index },
zs::Utxo {
output,
height,
from_coinbase,
},
);
}
}

Arc::new(new_outputs)
}
2 changes: 1 addition & 1 deletion zebra-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ pub use error::{BoxError, CloneError, CommitBlockError, ValidateContextError};
pub use request::{FinalizedBlock, HashOrHeight, PreparedBlock, Request};
pub use response::Response;
pub use service::init;
pub use utxo::Utxo;
pub use utxo::{new_outputs, Utxo};
22 changes: 1 addition & 21 deletions zebra-state/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,7 @@ impl From<Arc<Block>> for FinalizedBlock {
.iter()
.map(|tx| tx.hash())
.collect::<Vec<_>>();

let mut new_outputs = HashMap::default();

for (transaction, hash) in block
.transactions
.iter()
.zip(transaction_hashes.iter().cloned())
{
let from_coinbase = transaction.is_coinbase();
for (index, output) in transaction.outputs().iter().cloned().enumerate() {
let index = index as u32;
new_outputs.insert(
transparent::OutPoint { hash, index },
Utxo {
output,
height,
from_coinbase,
},
);
}
}
let new_outputs = crate::utxo::new_outputs(&block, transaction_hashes.as_slice());

Self {
block,
Expand Down
4 changes: 2 additions & 2 deletions zebra-state/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl Prepare for Arc<Block> {
let block = self;
let hash = block.hash();
let height = block.coinbase_height().unwrap();
let transaction_hashes = block.transactions.iter().map(|tx| tx.hash()).collect();
let new_outputs = crate::utxo::new_outputs(&block);
let transaction_hashes: Vec<_> = block.transactions.iter().map(|tx| tx.hash()).collect();
let new_outputs = crate::utxo::new_outputs(&block, transaction_hashes.as_slice());

PreparedBlock {
block,
Expand Down
28 changes: 19 additions & 9 deletions zebra-state/src/utxo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// needed to make clippy happy with derive(Arbitrary)
#![allow(clippy::unit_arg)]
//! Unspent transparent output data structures and functions.
use zebra_chain::{block, transparent};
use std::collections::HashMap;

use zebra_chain::{
block::{self, Block},
transaction, transparent,
};

/// An unspent `transparent::Output`, with accompanying metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -18,15 +22,21 @@ pub struct Utxo {
pub from_coinbase: bool,
}

#[cfg(test)]
pub fn new_outputs(block: &block::Block) -> std::collections::HashMap<transparent::OutPoint, Utxo> {
use std::collections::HashMap;

let height = block.coinbase_height().expect("block has coinbase height");

/// Compute an index of newly created transparent outputs, given a block and a
/// list of precomputed transaction hashes.
pub fn new_outputs(
block: &Block,
transaction_hashes: &[transaction::Hash],
) -> HashMap<transparent::OutPoint, OrderedUtxo> {
let mut new_outputs = HashMap::default();
for transaction in &block.transactions {
let hash = transaction.hash();
let height = block.coinbase_height().expect("block has coinbase height");
for (transaction, hash) in block
.transactions
.iter()
.zip(transaction_hashes.iter().cloned())
{
let from_coinbase = transaction.is_coinbase();
for (index, output) in transaction.outputs().iter().cloned().enumerate() {
let index = index as u32;
Expand Down

0 comments on commit c7e1a0f

Please sign in to comment.