From 385b38545635a6d851c859eb3f9e8620e278277e Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Wed, 31 Oct 2018 13:18:59 -0700 Subject: [PATCH] Minor block.rs cleanup (#1889) --- core/src/core/block.rs | 68 ++++++++---------------------------------- 1 file changed, 13 insertions(+), 55 deletions(-) diff --git a/core/src/core/block.rs b/core/src/core/block.rs index a4b7864b..4b535722 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -147,14 +147,11 @@ fn fixed_size_of_serialized_header(_version: u16) -> usize { size += mem::size_of::(); // version size += mem::size_of::(); // height size += mem::size_of::(); // timestamp - size += mem::size_of::(); // previous - size += mem::size_of::(); // prev_root - size += mem::size_of::(); // output_root - size += mem::size_of::(); // range_proof_root - size += mem::size_of::(); // kernel_root + // previous, prev_root, output_root, range_proof_root, kernel_root + size += 5 * mem::size_of::(); size += mem::size_of::(); // total_kernel_offset - size += mem::size_of::(); // output_mmr_size - size += mem::size_of::(); // kernel_mmr_size + // output_mmr_size, kernel_mmr_size + size += 2 * mem::size_of::(); size += mem::size_of::(); // total_difficulty size += mem::size_of::(); // secondary_scaling size += mem::size_of::(); // nonce @@ -166,8 +163,7 @@ pub fn serialized_size_of_header(version: u16, edge_bits: u8) -> usize { let mut size = fixed_size_of_serialized_header(version); size += mem::size_of::(); // pow.edge_bits - let nonce_bits = edge_bits as usize; - let bitvec_len = global::proofsize() * nonce_bits; + let bitvec_len = global::proofsize() * edge_bits as usize; size += bitvec_len / 8; // pow.nonces if bitvec_len % 8 != 0 { size += 1; @@ -418,35 +414,6 @@ impl Block { Ok(block) } - /// Extract tx data from this block as a single aggregate tx. - pub fn aggregate_transaction( - &self, - prev_kernel_offset: BlindingFactor, - ) -> Result, Error> { - let inputs = self.inputs().iter().cloned().collect(); - let outputs = self - .outputs() - .iter() - .filter(|x| !x.features.contains(OutputFeatures::COINBASE_OUTPUT)) - .cloned() - .collect(); - let kernels = self - .kernels() - .iter() - .filter(|x| !x.features.contains(KernelFeatures::COINBASE_KERNEL)) - .cloned() - .collect::>(); - - let tx = if kernels.is_empty() { - None - } else { - let tx = Transaction::new(inputs, outputs, kernels) - .with_offset(self.block_kernel_offset(prev_kernel_offset)?); - Some(tx) - }; - Ok(tx) - } - /// Hydrate a block from a compact block. /// Note: caller must validate the block themselves, we do not validate it /// here. @@ -506,13 +473,12 @@ impl Block { reward_kern: TxKernel, difficulty: Difficulty, ) -> Result { - // A block is just a big transaction, aggregate as such. - let mut agg_tx = transaction::aggregate(txs)?; - - // Now add the reward output and reward kernel to the aggregate tx. - // At this point the tx is technically invalid, - // but the tx body is valid if we account for the reward (i.e. as a block). - agg_tx = agg_tx.with_output(reward_out).with_kernel(reward_kern); + // A block is just a big transaction, aggregate and add the reward output + // and reward kernel. At this point the tx is technically invalid but the + // tx body is valid if we account for the reward (i.e. as a block). + let agg_tx = transaction::aggregate(txs)? + .with_output(reward_out) + .with_kernel(reward_kern); // Now add the kernel offset of the previous block for a total let total_kernel_offset = @@ -587,12 +553,6 @@ impl Block { /// 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. - /// - /// NOTE: exclude coinbase from cut-through process - /// if a block contains a new coinbase output and - /// is a transaction spending a previous coinbase - /// we do not want to cut-through (all coinbase must be preserved) - /// pub fn cut_through(self) -> Result { let mut inputs = self.inputs().clone(); let mut outputs = self.outputs().clone(); @@ -684,10 +644,8 @@ impl Block { let secp = secp.lock(); let over_commit = secp.commit_value(reward(self.total_fees()))?; - let out_adjust_sum = secp.commit_sum( - cb_outs.iter().map(|x| x.commitment()).collect(), - vec![over_commit], - )?; + let out_adjust_sum = + secp.commit_sum(map_vec!(cb_outs, |x| x.commitment()), vec![over_commit])?; let kerns_sum = secp.commit_sum(cb_kerns.iter().map(|x| x.excess).collect(), vec![])?;