Skip to content

Commit c8c59f9

Browse files
committed
refactor: create method BlockMerkleRoots::from_transactions
1 parent d88dbd8 commit c8c59f9

File tree

4 files changed

+44
-65
lines changed

4 files changed

+44
-65
lines changed

data_structures/src/chain.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,36 @@ pub struct BlockMerkleRoots {
668668
pub tally_hash_merkle_root: Hash,
669669
}
670670

671+
/// Function to calculate a merkle tree from a transaction vector
672+
pub fn merkle_tree_root<T>(transactions: &[T]) -> Hash
673+
where
674+
T: Hashable,
675+
{
676+
let transactions_hashes: Vec<Sha256> = transactions
677+
.iter()
678+
.map(|x| match x.hash() {
679+
Hash::SHA256(x) => Sha256(x),
680+
})
681+
.collect();
682+
683+
Hash::from(witnet_crypto::merkle::merkle_tree_root(
684+
&transactions_hashes,
685+
))
686+
}
687+
688+
impl BlockMerkleRoots {
689+
pub fn from_transactions(txns: &BlockTransactions) -> Self {
690+
BlockMerkleRoots {
691+
mint_hash: txns.mint.hash(),
692+
vt_hash_merkle_root: merkle_tree_root(&txns.value_transfer_txns),
693+
dr_hash_merkle_root: merkle_tree_root(&txns.data_request_txns),
694+
commit_hash_merkle_root: merkle_tree_root(&txns.commit_txns),
695+
reveal_hash_merkle_root: merkle_tree_root(&txns.reveal_txns),
696+
tally_hash_merkle_root: merkle_tree_root(&txns.tally_txns),
697+
}
698+
}
699+
}
700+
671701
/// `SuperBlock` abridges the tally and data request information that happened during a
672702
/// `superblock_period` number of Witnet epochs as well the ARS members merkle root
673703
/// as of the last block in that period.

data_structures/tests/inclusion_proofs.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
use std::convert::TryFrom;
22
use witnet_crypto::{
33
hash::Sha256,
4-
merkle::{merkle_tree_root as crypto_merkle_tree_root, sha256_concat, InclusionProof},
4+
merkle::{sha256_concat, InclusionProof},
55
};
66
use witnet_data_structures::{chain::*, transaction::*};
77

8-
/// Function to calculate a merkle tree from a transaction vector
9-
pub fn merkle_tree_root<T>(transactions: &[T]) -> Hash
10-
where
11-
T: Hashable,
12-
{
13-
let transactions_hashes: Vec<Sha256> = transactions
14-
.iter()
15-
.map(|x| match x.hash() {
16-
Hash::SHA256(x) => Sha256(x),
17-
})
18-
.collect();
19-
20-
Hash::from(crypto_merkle_tree_root(&transactions_hashes))
21-
}
22-
23-
fn build_merkle_tree(block_header: &mut BlockHeader, txns: &BlockTransactions) {
24-
let merkle_roots = BlockMerkleRoots {
25-
mint_hash: txns.mint.hash(),
26-
vt_hash_merkle_root: merkle_tree_root(&txns.value_transfer_txns),
27-
dr_hash_merkle_root: merkle_tree_root(&txns.data_request_txns),
28-
commit_hash_merkle_root: merkle_tree_root(&txns.commit_txns),
29-
reveal_hash_merkle_root: merkle_tree_root(&txns.reveal_txns),
30-
tally_hash_merkle_root: merkle_tree_root(&txns.tally_txns),
31-
};
32-
block_header.merkle_roots = merkle_roots;
33-
}
34-
358
fn h(left: Hash, right: Hash) -> Hash {
369
let left = match left {
3710
Hash::SHA256(x) => Sha256(x),
@@ -53,7 +26,7 @@ fn example_block(txns: BlockTransactions) -> Block {
5326
hash_prev_block: last_block_hash,
5427
};
5528
let mut block_header = BlockHeader::default();
56-
build_merkle_tree(&mut block_header, &txns);
29+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
5730
block_header.beacon = block_beacon;
5831

5932
let block_sig = KeyedSignature::default();

node/src/actors/chain_manager/mod.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,7 +2958,7 @@ mod tests {
29582958
vrf::BlockEligibilityClaim,
29592959
};
29602960
use witnet_protected::Protected;
2961-
use witnet_validations::validations::{block_reward, merkle_tree_root};
2961+
use witnet_validations::validations::block_reward;
29622962

29632963
use secp256k1::{
29642964
PublicKey as Secp256k1_PublicKey, Secp256k1, SecretKey as Secp256k1_SecretKey,
@@ -3380,18 +3380,6 @@ mod tests {
33803380
static PRIV_KEY_1: [u8; 32] = [0xcd; 32];
33813381
static PRIV_KEY_2: [u8; 32] = [0x43; 32];
33823382

3383-
fn build_merkle_tree(block_header: &mut BlockHeader, txns: &BlockTransactions) {
3384-
let merkle_roots = BlockMerkleRoots {
3385-
mint_hash: txns.mint.hash(),
3386-
vt_hash_merkle_root: merkle_tree_root(&txns.value_transfer_txns),
3387-
dr_hash_merkle_root: merkle_tree_root(&txns.data_request_txns),
3388-
commit_hash_merkle_root: merkle_tree_root(&txns.commit_txns),
3389-
reveal_hash_merkle_root: merkle_tree_root(&txns.reveal_txns),
3390-
tally_hash_merkle_root: merkle_tree_root(&txns.tally_txns),
3391-
};
3392-
block_header.merkle_roots = merkle_roots;
3393-
}
3394-
33953383
fn sign_tx<H: Hashable>(mk: [u8; 32], tx: &H) -> KeyedSignature {
33963384
let Hash::SHA256(data) = tx.hash();
33973385

@@ -3460,7 +3448,7 @@ mod tests {
34603448
};
34613449

34623450
let mut block_header = BlockHeader::default();
3463-
build_merkle_tree(&mut block_header, &txns);
3451+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
34643452
block_header.beacon = block_beacon;
34653453
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
34663454

validations/src/tests/mod.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7655,7 +7655,7 @@ fn test_block_with_drpool_and_utxo_set<F: FnMut(&mut Block) -> bool>(
76557655
};
76567656

76577657
let mut block_header = BlockHeader::default();
7658-
build_merkle_tree(&mut block_header, &txns);
7658+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
76597659
block_header.beacon = block_beacon;
76607660
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
76617661

@@ -7698,18 +7698,6 @@ fn test_block_with_drpool_and_utxo_set<F: FnMut(&mut Block) -> bool>(
76987698
Ok(())
76997699
}
77007700

7701-
fn build_merkle_tree(block_header: &mut BlockHeader, txns: &BlockTransactions) {
7702-
let merkle_roots = BlockMerkleRoots {
7703-
mint_hash: txns.mint.hash(),
7704-
vt_hash_merkle_root: merkle_tree_root(&txns.value_transfer_txns),
7705-
dr_hash_merkle_root: merkle_tree_root(&txns.data_request_txns),
7706-
commit_hash_merkle_root: merkle_tree_root(&txns.commit_txns),
7707-
reveal_hash_merkle_root: merkle_tree_root(&txns.reveal_txns),
7708-
tally_hash_merkle_root: merkle_tree_root(&txns.tally_txns),
7709-
};
7710-
block_header.merkle_roots = merkle_roots;
7711-
}
7712-
77137701
///////////////////////////////////////////////////////////////////////////////
77147702
// Block tests: one block
77157703
///////////////////////////////////////////////////////////////////////////////
@@ -7942,7 +7930,7 @@ fn block_difficult_proof() {
79427930
};
79437931

79447932
let mut block_header = BlockHeader::default();
7945-
build_merkle_tree(&mut block_header, &txns);
7933+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
79467934
block_header.beacon = block_beacon;
79477935
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
79487936

@@ -8163,7 +8151,7 @@ fn block_duplicated_commits() {
81638151
}],
81648152
);
81658153

8166-
build_merkle_tree(&mut b.block_header, &b.txns);
8154+
b.block_header.merkle_roots = BlockMerkleRoots::from_transactions(&b.txns);
81678155

81688156
true
81698157
},
@@ -8271,7 +8259,7 @@ fn block_duplicated_reveals() {
82718259
}],
82728260
);
82738261

8274-
build_merkle_tree(&mut b.block_header, &b.txns);
8262+
b.block_header.merkle_roots = BlockMerkleRoots::from_transactions(&b.txns);
82758263

82768264
true
82778265
},
@@ -8338,7 +8326,7 @@ fn block_duplicated_tallies() {
83388326
}],
83398327
);
83408328

8341-
build_merkle_tree(&mut b.block_header, &b.txns);
8329+
b.block_header.merkle_roots = BlockMerkleRoots::from_transactions(&b.txns);
83428330

83438331
true
83448332
},
@@ -8395,7 +8383,7 @@ fn block_before_and_after_hard_fork() {
83958383
}],
83968384
);
83978385

8398-
build_merkle_tree(&mut b.block_header, &b.txns);
8386+
b.block_header.merkle_roots = BlockMerkleRoots::from_transactions(&b.txns);
83998387

84008388
true
84018389
},
@@ -8418,7 +8406,7 @@ fn block_before_and_after_hard_fork() {
84188406
}],
84198407
);
84208408

8421-
build_merkle_tree(&mut b.block_header, &b.txns);
8409+
b.block_header.merkle_roots = BlockMerkleRoots::from_transactions(&b.txns);
84228410

84238411
true
84248412
},
@@ -8643,7 +8631,7 @@ fn test_blocks_with_limits(
86438631
hash_prev_block: last_block_hash,
86448632
};
86458633
let mut block_header = BlockHeader::default();
8646-
build_merkle_tree(&mut block_header, &txns);
8634+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
86478635
block_header.beacon = block_beacon;
86488636
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
86498637

@@ -9422,7 +9410,7 @@ fn validate_block_transactions_uses_block_number_in_utxo_diff() {
94229410
};
94239411

94249412
let mut block_header = BlockHeader::default();
9425-
build_merkle_tree(&mut block_header, &txns);
9413+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
94269414
block_header.beacon = block_beacon;
94279415
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
94289416

@@ -9585,7 +9573,7 @@ fn validate_commit_transactions_included_in_utxo_diff() {
95859573
txns.commit_txns.push(c_tx);
95869574

95879575
let mut block_header = BlockHeader::default();
9588-
build_merkle_tree(&mut block_header, &txns);
9576+
block_header.merkle_roots = BlockMerkleRoots::from_transactions(&txns);
95899577
block_header.beacon = block_beacon;
95909578
block_header.proof = BlockEligibilityClaim::create(vrf, &secret_key, vrf_input).unwrap();
95919579

0 commit comments

Comments
 (0)