Combine near-duplicate Utxo creation functions (#2467)

* Combine near-duplicate Utxo creation functions

* rustfmt
This commit is contained in:
teor 2021-07-09 12:40:39 +10:00 committed by GitHub
parent 64be7fddb7
commit d4cc867132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 68 deletions

View File

@ -8,7 +8,6 @@
//! verification, where it may be accepted or rejected. //! verification, where it may be accepted or rejected.
use std::{ use std::{
collections::HashMap,
future::Future, future::Future,
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
@ -25,7 +24,6 @@ use tracing::Instrument;
use zebra_chain::{ use zebra_chain::{
block::{self, Block}, block::{self, Block},
parameters::Network, parameters::Network,
transaction, transparent,
work::equihash, work::equihash,
}; };
use zebra_state as zs; use zebra_state as zs;
@ -175,7 +173,7 @@ where
let mut async_checks = FuturesUnordered::new(); 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 { for transaction in &block.transactions {
let rsp = transaction_verifier let rsp = transaction_verifier
.ready_and() .ready_and()
@ -230,33 +228,3 @@ where
.boxed() .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)
}

View File

@ -36,4 +36,4 @@ pub use error::{BoxError, CloneError, CommitBlockError, ValidateContextError};
pub use request::{FinalizedBlock, HashOrHeight, PreparedBlock, Request}; pub use request::{FinalizedBlock, HashOrHeight, PreparedBlock, Request};
pub use response::Response; pub use response::Response;
pub use service::init; pub use service::init;
pub use utxo::Utxo; pub use utxo::{new_outputs, Utxo};

View File

@ -117,27 +117,7 @@ impl From<Arc<Block>> for FinalizedBlock {
.iter() .iter()
.map(|tx| tx.hash()) .map(|tx| tx.hash())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let new_outputs = crate::utxo::new_outputs(&block, transaction_hashes.as_slice());
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,
},
);
}
}
Self { Self {
block, block,

View File

@ -20,8 +20,8 @@ impl Prepare for Arc<Block> {
let block = self; let block = self;
let hash = block.hash(); let hash = block.hash();
let height = block.coinbase_height().unwrap(); let height = block.coinbase_height().unwrap();
let transaction_hashes = block.transactions.iter().map(|tx| tx.hash()).collect(); let transaction_hashes: Vec<_> = block.transactions.iter().map(|tx| tx.hash()).collect();
let new_outputs = crate::utxo::new_outputs(&block); let new_outputs = crate::utxo::new_outputs(&block, transaction_hashes.as_slice());
PreparedBlock { PreparedBlock {
block, block,

View File

@ -1,7 +1,11 @@
// needed to make clippy happy with derive(Arbitrary) //! Unspent transparent output data structures and functions.
#![allow(clippy::unit_arg)]
use zebra_chain::{block, transparent}; use std::collections::HashMap;
use zebra_chain::{
block::{self, Block},
transaction, transparent,
};
/// An unspent `transparent::Output`, with accompanying metadata. /// An unspent `transparent::Output`, with accompanying metadata.
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -18,15 +22,19 @@ pub struct Utxo {
pub from_coinbase: bool, pub from_coinbase: bool,
} }
#[cfg(test)] /// Compute an index of newly created transparent outputs, given a block and a
pub fn new_outputs(block: &block::Block) -> std::collections::HashMap<transparent::OutPoint, Utxo> { /// list of precomputed transaction hashes.
use std::collections::HashMap; pub fn new_outputs(
block: &Block,
let height = block.coinbase_height().expect("block has coinbase height"); transaction_hashes: &[transaction::Hash],
) -> HashMap<transparent::OutPoint, Utxo> {
let mut new_outputs = HashMap::default(); let mut new_outputs = HashMap::default();
for transaction in &block.transactions { let height = block.coinbase_height().expect("block has coinbase height");
let hash = transaction.hash(); for (transaction, hash) in block
.transactions
.iter()
.zip(transaction_hashes.iter().cloned())
{
let from_coinbase = transaction.is_coinbase(); let from_coinbase = transaction.is_coinbase();
for (index, output) in transaction.outputs().iter().cloned().enumerate() { for (index, output) in transaction.outputs().iter().cloned().enumerate() {
let index = index as u32; let index = index as u32;