Skip to content

Commit 6334b2e

Browse files
committed
tests: fix most broken texts
1 parent 32289d4 commit 6334b2e

File tree

6 files changed

+116
-24
lines changed

6 files changed

+116
-24
lines changed

crypto/src/hash.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use digest::Digest;
44
#[cfg(feature = "serde")]
55
use serde::{Deserialize, Serialize};
6+
use std::fmt::Formatter;
67

78
/// Enumeration of hash-function names
89
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -13,7 +14,7 @@ pub enum HashFunction {
1314
}
1415

1516
/// Secure hashing algorithm v2
16-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
17+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
1718
pub struct Sha256(pub [u8; 32]);
1819

1920
/// Value of an empty hash
@@ -28,6 +29,12 @@ impl AsRef<[u8]> for Sha256 {
2829
}
2930
}
3031

32+
impl std::fmt::Debug for Sha256 {
33+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34+
write!(f, "{}", hex::encode(self))
35+
}
36+
}
37+
3138
/// Calculate the SHA256 hash
3239
pub fn calculate_sha256(bytes: &[u8]) -> Sha256 {
3340
let mut hasher = sha2::Sha256::new();

data_structures/src/chain/mod.rs

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,10 +1139,7 @@ pub enum Hash {
11391139

11401140
impl Default for Hash {
11411141
fn default() -> Hash {
1142-
Hash::SHA256([
1143-
227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200, 153, 111, 185, 36, 39, 174,
1144-
65, 228, 100, 155, 147, 76, 164, 149, 153, 27, 120, 82, 184, 85,
1145-
])
1142+
Hash::SHA256([0; 32])
11461143
}
11471144
}
11481145

@@ -4714,7 +4711,10 @@ mod tests {
47144711
.collect();
47154712
let proof = InclusionProof::sha256(result.index, lemma);
47164713
assert!(proof.verify(
4717-
dr_txs[index].body.data_poi_hash().into(),
4714+
dr_txs[index]
4715+
.body
4716+
.data_poi_hash(ProtocolVersion::default())
4717+
.into(),
47184718
sb.data_request_root.into()
47194719
));
47204720
}
@@ -4742,10 +4742,8 @@ mod tests {
47424742
})
47434743
.collect();
47444744
let proof = InclusionProof::sha256(result.index, lemma);
4745-
assert!(proof.verify(
4746-
tally_txs[index].data_poi_hash().into(),
4747-
sb.tally_root.into()
4748-
));
4745+
let data_poi_hash = tally_txs[index].data_poi_hash();
4746+
assert!(proof.verify(data_poi_hash.into(), sb.tally_root.into()));
47494747
}
47504748
}
47514749

@@ -4793,7 +4791,82 @@ mod tests {
47934791
tally_txs
47944792
}
47954793

4796-
#[ignore]
4794+
#[test]
4795+
fn test_block_header_serialization_across_protocol_versions() {
4796+
let block = block_example();
4797+
assert_ne!(
4798+
hex::encode(
4799+
block
4800+
.block_header
4801+
.to_versioned_pb_bytes(ProtocolVersion::V1_7)
4802+
.unwrap()
4803+
),
4804+
hex::encode(
4805+
block
4806+
.block_header
4807+
.to_versioned_pb_bytes(ProtocolVersion::V1_8)
4808+
.unwrap()
4809+
),
4810+
);
4811+
assert_eq!(
4812+
hex::encode(
4813+
block
4814+
.block_header
4815+
.to_versioned_pb_bytes(ProtocolVersion::V1_8)
4816+
.unwrap()
4817+
),
4818+
hex::encode(
4819+
block
4820+
.block_header
4821+
.to_versioned_pb_bytes(ProtocolVersion::V2_0)
4822+
.unwrap()
4823+
),
4824+
);
4825+
}
4826+
4827+
#[test]
4828+
fn test_block_header_hashing_across_protocol_versions() {
4829+
let block = block_example();
4830+
assert_ne!(
4831+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V1_7)),
4832+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V1_8)),
4833+
);
4834+
assert_eq!(
4835+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V1_8)),
4836+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V2_0)),
4837+
);
4838+
}
4839+
4840+
#[test]
4841+
fn test_block_hashing_across_protocol_versions() {
4842+
let block = block_example();
4843+
assert_ne!(
4844+
hex::encode(block.versioned_hash(ProtocolVersion::V1_7)),
4845+
hex::encode(block.versioned_hash(ProtocolVersion::V1_8)),
4846+
);
4847+
assert_eq!(
4848+
hex::encode(block.versioned_hash(ProtocolVersion::V1_8)),
4849+
hex::encode(block.versioned_hash(ProtocolVersion::V2_0)),
4850+
);
4851+
}
4852+
4853+
#[test]
4854+
fn test_block_hashing_matches_block_header_hashing() {
4855+
let block = block_example();
4856+
assert_eq!(
4857+
hex::encode(block.versioned_hash(ProtocolVersion::V1_7)),
4858+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V1_7)),
4859+
);
4860+
assert_eq!(
4861+
hex::encode(block.versioned_hash(ProtocolVersion::V1_8)),
4862+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V1_8)),
4863+
);
4864+
assert_eq!(
4865+
hex::encode(block.versioned_hash(ProtocolVersion::V2_0)),
4866+
hex::encode(block.block_header.versioned_hash(ProtocolVersion::V2_0)),
4867+
);
4868+
}
4869+
47974870
#[test]
47984871
fn test_block_hashable_trait() {
47994872
let block = block_example();
@@ -4814,7 +4887,6 @@ mod tests {
48144887
);
48154888
}
48164889

4817-
#[ignore]
48184890
#[test]
48194891
fn test_transaction_hashable_trait() {
48204892
let transaction = transaction_example();

data_structures/src/proto/versioning.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ impl Versioned for crate::transaction::UnstakeTransactionBody {
306306
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
307307
}
308308

309+
impl Versioned for crate::chain::DataRequestOutput {
310+
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
311+
}
312+
309313
pub trait AutoVersioned: ProtobufConvert {}
310314

311315
impl AutoVersioned for crate::chain::BlockHeader {}
@@ -318,6 +322,7 @@ impl AutoVersioned for crate::transaction::TallyTransaction {}
318322
impl AutoVersioned for crate::transaction::MintTransaction {}
319323
impl AutoVersioned for crate::transaction::StakeTransactionBody {}
320324
impl AutoVersioned for crate::transaction::UnstakeTransactionBody {}
325+
impl AutoVersioned for crate::chain::DataRequestOutput {}
321326

322327
pub trait VersionedHashable {
323328
fn versioned_hash(&self, version: ProtocolVersion) -> Hash;
@@ -396,6 +401,7 @@ impl VersionedHashable for crate::transaction::UnstakeTransaction {
396401
}
397402

398403
impl VersionedHashable for crate::chain::Block {
404+
#[inline]
399405
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
400406
self.block_header.versioned_hash(version)
401407
}

data_structures/src/transaction.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl DRTransaction {
397397
pub fn data_proof_of_inclusion(&self, block: &Block) -> Option<TxInclusionProof> {
398398
self.proof_of_inclusion(block, ProtocolVersion::default())
399399
.map(|mut poi| {
400-
poi.add_leave(self.body.rest_poi_hash());
400+
poi.add_leave(self.body.rest_poi_hash(ProtocolVersion::default()));
401401

402402
poi
403403
})
@@ -463,14 +463,14 @@ impl DRTransactionBody {
463463

464464
/// Specified data to be divided in a new level in the proof of inclusion
465465
/// In this case data = Hash( dr_output )
466-
pub fn data_poi_hash(&self) -> Hash {
467-
self.dr_output.hash()
466+
pub fn data_poi_hash(&self, protocol_version: ProtocolVersion) -> Hash {
467+
self.dr_output.versioned_hash(protocol_version)
468468
}
469469

470470
/// Rest of the transaction to be divided in a new level in the proof of inclusion
471471
/// In this case we choose the complete transaction
472-
pub fn rest_poi_hash(&self) -> Hash {
473-
calculate_sha256(&self.to_pb_bytes().unwrap()).into()
472+
pub fn rest_poi_hash(&self, protocol_version: ProtocolVersion) -> Hash {
473+
self.versioned_hash(protocol_version)
474474
}
475475
}
476476

@@ -905,8 +905,8 @@ impl MemoizedHashable for VTTransactionBody {
905905
}
906906
impl MemoizedHashable for DRTransactionBody {
907907
fn hashable_bytes(&self) -> Vec<u8> {
908-
let Hash::SHA256(data_bytes) = self.data_poi_hash();
909-
let Hash::SHA256(rest_bytes) = self.rest_poi_hash();
908+
let Hash::SHA256(data_bytes) = self.data_poi_hash(ProtocolVersion::V1_7);
909+
let Hash::SHA256(rest_bytes) = self.rest_poi_hash(ProtocolVersion::V1_7);
910910

911911
[data_bytes, rest_bytes].concat()
912912
}

validations/src/eligibility/current.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::{
44
ops::{Add, Div, Mul, Rem, Sub},
55
};
66

7+
use serde::Serialize;
8+
9+
use witnet_crypto::secp256k1::serde;
710
use witnet_data_structures::{chain::Hash, staking::prelude::*, wit::PrecisionLoss};
811

912
const MINING_REPLICATION_FACTOR: usize = 4;
@@ -97,7 +100,7 @@ where
97100
impl<const UNIT: u8, Address, Coins, Epoch, Power> Eligibility<Address, Coins, Epoch, Power>
98101
for Stakes<UNIT, Address, Coins, Epoch, Power>
99102
where
100-
Address: Clone + Debug + Default + Display + Ord + Sync + Send + 'static,
103+
Address: Clone + Debug + Default + Display + Ord + Sync + Send + Serialize + 'static,
101104
Coins: Copy
102105
+ Debug
103106
+ Default
@@ -115,6 +118,7 @@ where
115118
+ PrecisionLoss
116119
+ Sync
117120
+ Send
121+
+ Serialize
118122
+ Sum
119123
+ 'static,
120124
Epoch: Copy
@@ -128,6 +132,7 @@ where
128132
+ From<u32>
129133
+ Sync
130134
+ Send
135+
+ Serialize
131136
+ 'static,
132137
Power: Copy
133138
+ Default
@@ -137,6 +142,7 @@ where
137142
+ Mul<Output = Power>
138143
+ Div<Output = Power>
139144
+ From<u64>
145+
+ Serialize
140146
+ Sum
141147
+ Display,
142148
u64: From<Coins> + From<Power> + Mul<Power, Output = u64> + Div<Power, Output = u64>,

validations/src/tests/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ fn vtt_timelock() {
12071207
let epoch_constants = EpochConstants {
12081208
checkpoint_zero_timestamp: 0,
12091209
checkpoints_period: 1_000,
1210-
checkpoint_zero_timestamp_v2: 500,
1210+
checkpoint_zero_timestamp_v2: i64::MAX,
12111211
checkpoints_period_v2: 1_000,
12121212
};
12131213

@@ -2810,6 +2810,7 @@ fn data_request_miner_fee_with_too_much_change() {
28102810

28112811
#[test]
28122812
fn data_request_zero_value_output() {
2813+
let protocol_version = ProtocolVersion::default();
28132814
let mut signatures_to_verify = vec![];
28142815
let data_request = example_data_request();
28152816
let dr_output = DataRequestOutput {
@@ -2849,12 +2850,12 @@ fn data_request_zero_value_output() {
28492850
MAX_DR_WEIGHT,
28502851
REQUIRED_REWARD_COLLATERAL_RATIO,
28512852
&current_active_wips(),
2852-
None,
2853+
Some(protocol_version),
28532854
);
28542855
assert_eq!(
28552856
x.unwrap_err().downcast::<TransactionError>().unwrap(),
28562857
TransactionError::ZeroValueOutput {
2857-
tx_hash: dr_transaction.hash(),
2858+
tx_hash: dr_transaction.versioned_hash(protocol_version),
28582859
output_id: 0,
28592860
}
28602861
);
@@ -4024,7 +4025,7 @@ fn commitment_timelock() {
40244025
let epoch_constants = EpochConstants {
40254026
checkpoint_zero_timestamp: 0,
40264027
checkpoints_period: 1_000,
4027-
checkpoint_zero_timestamp_v2: 500,
4028+
checkpoint_zero_timestamp_v2: i64::MAX,
40284029
checkpoints_period_v2: 1_000,
40294030
};
40304031
let test_commit_epoch = |epoch, time_lock| {

0 commit comments

Comments
 (0)