Skip to content

Commit

Permalink
simplify hydrate_from with cut_through using slices, use extend_from_…
Browse files Browse the repository at this point in the history
…slice etc.
  • Loading branch information
antiochp committed Jul 27, 2020
1 parent 95682e7 commit ba1d42f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
46 changes: 13 additions & 33 deletions core/src/core/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,37 +568,32 @@ impl Block {

let header = cb.header.clone();

let mut all_inputs: HashSet<Commitment> = HashSet::new();
let mut all_outputs = HashSet::new();
let mut all_kernels = HashSet::new();
let mut inputs: Vec<Commitment> = vec![];
let mut outputs = vec![];
let mut kernels = vec![];

// collect all the inputs, outputs and kernels from the txs
for tx in txs {
let tx_inputs: Vec<_> = tx.inputs().into();
all_inputs.extend(tx_inputs.iter());
all_outputs.extend(tx.outputs());
all_kernels.extend(tx.kernels());
inputs.extend_from_slice(tx_inputs.as_slice());
outputs.extend_from_slice(tx.outputs());
kernels.extend_from_slice(tx.kernels());
}

// include the coinbase output(s) and kernel(s) from the compact_block
{
let body: CompactBlockBody = cb.into();
all_outputs.extend(body.out_full);
all_kernels.extend(body.kern_full);
}
let (inputs, outputs) = transaction::cut_through(&mut inputs, &mut outputs)?;

// convert the sets to vecs
let all_inputs = Vec::from_iter(all_inputs);
let all_outputs = Vec::from_iter(all_outputs);
let all_kernels = Vec::from_iter(all_kernels);
// include the coinbase output(s) and kernel(s) from the compact_block
let mut outputs = outputs.to_vec();
outputs.extend_from_slice(cb.out_full());
kernels.extend_from_slice(cb.kern_full());

// Initialize a tx body and sort everything.
let body = TransactionBody::init(all_inputs.into(), &all_outputs, &all_kernels, false)?;
let body = TransactionBody::init(inputs.into(), &outputs, &kernels, false)?;

// Finally return the full block.
// Note: we have not actually validated the block here,
// caller must validate the block.
Block { header, body }.cut_through()
Ok(Block { header, body })
}

/// Build a new empty block from a specified header
Expand Down Expand Up @@ -685,21 +680,6 @@ impl Block {
self.body.fee()
}

/// Matches any output with a potential spending input, eliminating them
/// from the block. Provides a simple way to cut-through the block. The
/// elimination is stable with respect to the order of inputs and outputs.
/// Method consumes the block.
pub fn cut_through(self) -> Result<Block, Error> {
let mut inputs: Vec<_> = self.inputs().into();
let mut outputs = self.outputs().to_vec();
let (inputs, outputs) = transaction::cut_through(&mut inputs, &mut outputs)?;

// Initialize tx body and sort everything.
let body = TransactionBody::init(inputs.into(), outputs, self.kernels(), false)?;

Ok(Block { body, ..self })
}

/// "Lightweight" validation that we can perform quickly during read/deserialization.
/// Subset of full validation that skips expensive verification steps, specifically -
/// * rangeproof verification (on the body)
Expand Down
10 changes: 2 additions & 8 deletions core/tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,7 @@ fn reward_empty_block() {

let b = new_block(vec![], &keychain, &builder, &previous_header, &key_id);

b.cut_through()
.unwrap()
.validate(&BlindingFactor::zero(), verifier_cache())
b.validate(&BlindingFactor::zero(), verifier_cache())
.unwrap();
}

Expand All @@ -598,11 +596,7 @@ fn reward_with_tx_block() {
&previous_header,
&key_id,
);
block
.cut_through()
.unwrap()
.validate(&BlindingFactor::zero(), vc.clone())
.unwrap();
block.validate(&BlindingFactor::zero(), vc.clone()).unwrap();
}

#[test]
Expand Down

0 comments on commit ba1d42f

Please sign in to comment.