Skip to content

Commit

Permalink
pass slices around and not refs to vecs (#3404)
Browse files Browse the repository at this point in the history
* pass slices around and not refs to vecs

* use slice.swap()

* use inputs() not body.inputs
  • Loading branch information
antiochp authored Jul 27, 2020
1 parent 105f50b commit 80841f1
Show file tree
Hide file tree
Showing 29 changed files with 272 additions and 275 deletions.
5 changes: 3 additions & 2 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ impl<'a> Batch<'a> {

/// We maintain a "spent" index for each full block to allow the output_pos
/// to be easily reverted during rewind.
pub fn save_spent_index(&self, h: &Hash, spent: &Vec<CommitPos>) -> Result<(), Error> {
self.db.put_ser(&to_key(BLOCK_SPENT_PREFIX, h)[..], spent)?;
pub fn save_spent_index(&self, h: &Hash, spent: &[CommitPos]) -> Result<(), Error> {
self.db
.put_ser(&to_key(BLOCK_SPENT_PREFIX, h)[..], &spent.to_vec())?;
Ok(())
}

Expand Down
5 changes: 2 additions & 3 deletions chain/tests/chain_test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ where
let reward =
libtx::reward::output(keychain, &libtx::ProofBuilder::new(keychain), &pk, 0, false)
.unwrap();
let mut b =
core::core::Block::new(&prev, vec![], next_header_info.clone().difficulty, reward)
.unwrap();
let mut b = core::core::Block::new(&prev, &[], next_header_info.clone().difficulty, reward)
.unwrap();
b.header.timestamp = prev.timestamp + Duration::seconds(60);
b.header.pow.secondary_scaling = next_header_info.secondary_scaling;

Expand Down
6 changes: 3 additions & 3 deletions chain/tests/mine_nrd_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
let reward =
reward::output(keychain, &ProofBuilder::new(keychain), key_id, fee, false).unwrap();

let mut block = Block::new(&prev, txs, next_header_info.clone().difficulty, reward).unwrap();
let mut block = Block::new(&prev, &txs, next_header_info.clone().difficulty, reward).unwrap();

block.header.timestamp = prev.timestamp + Duration::seconds(60);
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
Expand Down Expand Up @@ -87,7 +87,7 @@ fn mine_block_with_nrd_kernel_and_nrd_feature_enabled() {
fee: 20000,
relative_height: NRDRelativeHeight::new(1440).unwrap(),
},
vec![
&[
build::coinbase_input(consensus::REWARD, key_id1.clone()),
build::output(consensus::REWARD - 20000, key_id2.clone()),
],
Expand Down Expand Up @@ -134,7 +134,7 @@ fn mine_invalid_block_with_nrd_kernel_and_nrd_feature_enabled_before_hf() {
fee: 20000,
relative_height: NRDRelativeHeight::new(1440).unwrap(),
},
vec![
&[
build::coinbase_input(consensus::REWARD, key_id1.clone()),
build::output(consensus::REWARD - 20000, key_id2.clone()),
],
Expand Down
35 changes: 15 additions & 20 deletions chain/tests/mine_simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ fn spend_rewind_spend() {

let tx1 = build::transaction(
KernelFeatures::Plain { fee: 20000 },
vec![
&[
build::coinbase_input(consensus::REWARD, key_id_coinbase.clone()),
build::output(consensus::REWARD - 20000, key_id30.clone()),
],
Expand All @@ -579,7 +579,7 @@ fn spend_rewind_spend() {
)
.unwrap();

let b = prepare_block_tx(&kc, &head, &chain, 6, vec![&tx1]);
let b = prepare_block_tx(&kc, &head, &chain, 6, &[tx1.clone()]);
head = b.header.clone();
chain
.process_block(b.clone(), chain::Options::SKIP_POW)
Expand All @@ -595,7 +595,7 @@ fn spend_rewind_spend() {
// Now mine a competing block also spending the same coinbase output from earlier.
// Rewind back prior to the tx that spends it to "unspend" it.
{
let b = prepare_block_tx(&kc, &rewind_to, &chain, 6, vec![&tx1]);
let b = prepare_block_tx(&kc, &rewind_to, &chain, 6, &[tx1]);
chain
.process_block(b.clone(), chain::Options::SKIP_POW)
.unwrap();
Expand Down Expand Up @@ -644,7 +644,7 @@ fn spend_in_fork_and_compact() {

let tx1 = build::transaction(
KernelFeatures::Plain { fee: 20000 },
vec![
&[
build::coinbase_input(consensus::REWARD, key_id2.clone()),
build::output(consensus::REWARD - 20000, key_id30.clone()),
],
Expand All @@ -653,7 +653,7 @@ fn spend_in_fork_and_compact() {
)
.unwrap();

let next = prepare_block_tx(&kc, &fork_head, &chain, 7, vec![&tx1]);
let next = prepare_block_tx(&kc, &fork_head, &chain, 7, &[tx1.clone()]);
let prev_main = next.header.clone();
chain
.process_block(next.clone(), chain::Options::SKIP_POW)
Expand All @@ -662,7 +662,7 @@ fn spend_in_fork_and_compact() {

let tx2 = build::transaction(
KernelFeatures::Plain { fee: 20000 },
vec![
&[
build::input(consensus::REWARD - 20000, key_id30.clone()),
build::output(consensus::REWARD - 40000, key_id31.clone()),
],
Expand All @@ -671,19 +671,19 @@ fn spend_in_fork_and_compact() {
)
.unwrap();

let next = prepare_block_tx(&kc, &prev_main, &chain, 9, vec![&tx2]);
let next = prepare_block_tx(&kc, &prev_main, &chain, 9, &[tx2.clone()]);
let prev_main = next.header.clone();
chain.process_block(next, chain::Options::SKIP_POW).unwrap();

// Full chain validation for completeness.
chain.validate(false).unwrap();

// mine 2 forked blocks from the first
let fork = prepare_block_tx(&kc, &fork_head, &chain, 6, vec![&tx1]);
let fork = prepare_block_tx(&kc, &fork_head, &chain, 6, &[tx1.clone()]);
let prev_fork = fork.header.clone();
chain.process_block(fork, chain::Options::SKIP_POW).unwrap();

let fork_next = prepare_block_tx(&kc, &prev_fork, &chain, 8, vec![&tx2]);
let fork_next = prepare_block_tx(&kc, &prev_fork, &chain, 8, &[tx2.clone()]);
let prev_fork = fork_next.header.clone();
chain
.process_block(fork_next, chain::Options::SKIP_POW)
Expand Down Expand Up @@ -771,7 +771,7 @@ fn output_header_mappings() {
.unwrap();
reward_outputs.push(reward.0.clone());
let mut b =
core::core::Block::new(&prev, vec![], next_header_info.clone().difficulty, reward)
core::core::Block::new(&prev, &[], next_header_info.clone().difficulty, reward)
.unwrap();
b.header.timestamp = prev.timestamp + Duration::seconds(60);
b.header.pow.secondary_scaling = next_header_info.secondary_scaling;
Expand Down Expand Up @@ -834,7 +834,7 @@ fn prepare_block_key_idx<K>(
where
K: Keychain,
{
let mut b = prepare_block_nosum(kc, prev, diff, key_idx, vec![]);
let mut b = prepare_block_nosum(kc, prev, diff, key_idx, &[]);
chain.set_txhashset_roots(&mut b).unwrap();
b
}
Expand All @@ -845,7 +845,7 @@ fn prepare_block_tx<K>(
prev: &BlockHeader,
chain: &Chain,
diff: u64,
txs: Vec<&Transaction>,
txs: &[Transaction],
) -> Block
where
K: Keychain,
Expand All @@ -860,7 +860,7 @@ fn prepare_block_tx_key_idx<K>(
chain: &Chain,
diff: u64,
key_idx: u32,
txs: Vec<&Transaction>,
txs: &[Transaction],
) -> Block
where
K: Keychain,
Expand All @@ -875,7 +875,7 @@ fn prepare_block_nosum<K>(
prev: &BlockHeader,
diff: u64,
key_idx: u32,
txs: Vec<&Transaction>,
txs: &[Transaction],
) -> Block
where
K: Keychain,
Expand All @@ -886,12 +886,7 @@ where
let fees = txs.iter().map(|tx| tx.fee()).sum();
let reward =
libtx::reward::output(kc, &libtx::ProofBuilder::new(kc), &key_id, fees, false).unwrap();
let mut b = match core::core::Block::new(
prev,
txs.into_iter().cloned().collect(),
Difficulty::from_num(diff),
reward,
) {
let mut b = match core::core::Block::new(prev, txs, Difficulty::from_num(diff), reward) {
Err(e) => panic!("{:?}", e),
Ok(b) => b,
};
Expand Down
14 changes: 7 additions & 7 deletions chain/tests/nrd_validation_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
let reward =
reward::output(keychain, &ProofBuilder::new(keychain), key_id, fee, false).unwrap();

let mut block = Block::new(prev, txs, next_header_info.clone().difficulty, reward)?;
let mut block = Block::new(prev, &txs, next_header_info.clone().difficulty, reward)?;

block.header.timestamp = prev.timestamp + Duration::seconds(60);
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
Expand Down Expand Up @@ -121,7 +121,7 @@ fn process_block_nrd_validation() -> Result<(), Error> {
let key_id3 = ExtKeychainPath::new(1, 3, 0, 0, 0).to_identifier();

let tx1 = build::transaction_with_kernel(
vec![
&[
build::coinbase_input(consensus::REWARD, key_id1.clone()),
build::output(consensus::REWARD - 20000, key_id2.clone()),
],
Expand All @@ -133,7 +133,7 @@ fn process_block_nrd_validation() -> Result<(), Error> {
.unwrap();

let tx2 = build::transaction_with_kernel(
vec![
&[
build::input(consensus::REWARD - 20000, key_id2.clone()),
build::output(consensus::REWARD - 40000, key_id3.clone()),
],
Expand Down Expand Up @@ -237,7 +237,7 @@ fn process_block_nrd_validation_relative_height_1() -> Result<(), Error> {
let key_id3 = ExtKeychainPath::new(1, 3, 0, 0, 0).to_identifier();

let tx1 = build::transaction_with_kernel(
vec![
&[
build::coinbase_input(consensus::REWARD, key_id1.clone()),
build::output(consensus::REWARD - 20000, key_id2.clone()),
],
Expand All @@ -249,7 +249,7 @@ fn process_block_nrd_validation_relative_height_1() -> Result<(), Error> {
.unwrap();

let tx2 = build::transaction_with_kernel(
vec![
&[
build::input(consensus::REWARD - 20000, key_id2.clone()),
build::output(consensus::REWARD - 40000, key_id3.clone()),
],
Expand Down Expand Up @@ -336,7 +336,7 @@ fn process_block_nrd_validation_fork() -> Result<(), Error> {
let key_id3 = ExtKeychainPath::new(1, 3, 0, 0, 0).to_identifier();

let tx1 = build::transaction_with_kernel(
vec![
&[
build::coinbase_input(consensus::REWARD, key_id1.clone()),
build::output(consensus::REWARD - 20000, key_id2.clone()),
],
Expand All @@ -348,7 +348,7 @@ fn process_block_nrd_validation_fork() -> Result<(), Error> {
.unwrap();

let tx2 = build::transaction_with_kernel(
vec![
&[
build::input(consensus::REWARD - 20000, key_id2.clone()),
build::output(consensus::REWARD - 40000, key_id3.clone()),
],
Expand Down
17 changes: 8 additions & 9 deletions chain/tests/test_coinbase_maturity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn test_coinbase_maturity() {

let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
let reward = libtx::reward::output(&keychain, &builder, &key_id1, 0, false).unwrap();
let mut block = core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
let mut block = core::core::Block::new(&prev, &[], Difficulty::min(), reward).unwrap();
block.header.timestamp = prev.timestamp + Duration::seconds(60);
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;

Expand Down Expand Up @@ -101,7 +101,7 @@ fn test_coinbase_maturity() {
// this is not a valid tx as the coinbase output cannot be spent yet
let coinbase_txn = build::transaction(
KernelFeatures::Plain { fee: 2 },
vec![
&[
build::coinbase_input(amount, key_id1.clone()),
build::output(amount - 2, key_id2.clone()),
],
Expand All @@ -110,7 +110,7 @@ fn test_coinbase_maturity() {
)
.unwrap();

let txs = vec![coinbase_txn.clone()];
let txs = &[coinbase_txn.clone()];
let fees = txs.iter().map(|tx| tx.fee()).sum();
let reward = libtx::reward::output(&keychain, &builder, &key_id3, fees, false).unwrap();
let mut block = core::core::Block::new(&prev, txs, Difficulty::min(), reward).unwrap();
Expand Down Expand Up @@ -149,8 +149,7 @@ fn test_coinbase_maturity() {

let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
let reward = libtx::reward::output(&keychain, &builder, &key_id1, 0, false).unwrap();
let mut block =
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
let mut block = core::core::Block::new(&prev, &[], Difficulty::min(), reward).unwrap();

block.header.timestamp = prev.timestamp + Duration::seconds(60);
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
Expand Down Expand Up @@ -184,7 +183,7 @@ fn test_coinbase_maturity() {
// this is not a valid tx as the coinbase output cannot be spent yet
let coinbase_txn = build::transaction(
KernelFeatures::Plain { fee: 2 },
vec![
&[
build::coinbase_input(amount, key_id1.clone()),
build::output(amount - 2, key_id2.clone()),
],
Expand All @@ -193,7 +192,7 @@ fn test_coinbase_maturity() {
)
.unwrap();

let txs = vec![coinbase_txn.clone()];
let txs = &[coinbase_txn.clone()];
let fees = txs.iter().map(|tx| tx.fee()).sum();
let reward = libtx::reward::output(&keychain, &builder, &key_id3, fees, false).unwrap();
let mut block = core::core::Block::new(&prev, txs, Difficulty::min(), reward).unwrap();
Expand Down Expand Up @@ -232,7 +231,7 @@ fn test_coinbase_maturity() {

let reward = libtx::reward::output(&keychain, &builder, &pk, 0, false).unwrap();
let mut block =
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
core::core::Block::new(&prev, &[], Difficulty::min(), reward).unwrap();
let next_header_info =
consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
block.header.timestamp = prev.timestamp + Duration::seconds(60);
Expand All @@ -257,7 +256,7 @@ fn test_coinbase_maturity() {
// The coinbase output has matured sufficiently based on current chain state.
chain.verify_coinbase_maturity(&coinbase_txn).unwrap();

let txs = vec![coinbase_txn];
let txs = &[coinbase_txn];
let fees = txs.iter().map(|tx| tx.fee()).sum();
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
let reward = libtx::reward::output(&keychain, &builder, &key_id4, fees, false).unwrap();
Expand Down
Loading

0 comments on commit 80841f1

Please sign in to comment.