Skip to content

Commit dc3d1b0

Browse files
committed
feat(validations): fix versioned hashing for most transaction types
1 parent 88fe985 commit dc3d1b0

File tree

3 files changed

+162
-30
lines changed

3 files changed

+162
-30
lines changed

data_structures/src/proto/versioning.rs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,29 @@ impl Versioned for crate::transaction::DRTransactionBody {
278278
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
279279
}
280280
impl Versioned for crate::transaction::CommitTransactionBody {
281+
// FIXME: implement proper versioning here for commit transactions
281282
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
282283
}
283284
impl Versioned for crate::transaction::RevealTransaction {
285+
// FIXME: implement proper versioning here for reveal transactions
284286
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
285287
}
286288
impl Versioned for crate::transaction::RevealTransactionBody {
287289
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
288290
}
291+
impl Versioned for crate::transaction::TallyTransaction {
292+
// FIXME: implement proper versioning here for tally transactions
293+
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
294+
}
295+
impl Versioned for crate::transaction::MintTransaction {
296+
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
297+
}
298+
impl Versioned for crate::transaction::StakeTransactionBody {
299+
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
300+
}
301+
impl Versioned for crate::transaction::UnstakeTransactionBody {
302+
type LegacyType = <Self as ProtobufConvert>::ProtoStruct;
303+
}
289304

290305
pub trait AutoVersioned: ProtobufConvert {}
291306

@@ -294,8 +309,11 @@ impl AutoVersioned for crate::chain::SuperBlock {}
294309
impl AutoVersioned for crate::transaction::VTTransactionBody {}
295310
impl AutoVersioned for crate::transaction::DRTransactionBody {}
296311
impl AutoVersioned for crate::transaction::CommitTransactionBody {}
297-
impl AutoVersioned for crate::transaction::RevealTransaction {}
298312
impl AutoVersioned for crate::transaction::RevealTransactionBody {}
313+
impl AutoVersioned for crate::transaction::TallyTransaction {}
314+
impl AutoVersioned for crate::transaction::MintTransaction {}
315+
impl AutoVersioned for crate::transaction::StakeTransactionBody {}
316+
impl AutoVersioned for crate::transaction::UnstakeTransactionBody {}
299317

300318
pub trait VersionedHashable {
301319
fn versioned_hash(&self, version: ProtocolVersion) -> Hash;
@@ -316,11 +334,17 @@ where
316334

317335
impl VersionedHashable for crate::transaction::Transaction {
318336
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
337+
use crate::transaction::Transaction::*;
338+
319339
match self {
320-
crate::transaction::Transaction::ValueTransfer(tx) => tx.versioned_hash(version),
321-
_ => {
322-
todo!();
323-
}
340+
ValueTransfer(tx) => tx.versioned_hash(version),
341+
DataRequest(tx) => tx.versioned_hash(version),
342+
Commit(tx) => tx.versioned_hash(version),
343+
Reveal(tx) => tx.versioned_hash(version),
344+
Tally(tx) => tx.versioned_hash(version),
345+
Mint(tx) => tx.versioned_hash(version),
346+
Stake(tx) => tx.versioned_hash(version),
347+
Unstake(tx) => tx.versioned_hash(version),
324348
}
325349
}
326350
}
@@ -332,8 +356,42 @@ impl VersionedHashable for crate::transaction::VTTransaction {
332356
}
333357
}
334358

335-
impl VersionedHashable for crate::chain::Block {
359+
impl VersionedHashable for crate::transaction::DRTransaction {
360+
#[inline]
361+
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
362+
self.body.versioned_hash(version)
363+
}
364+
}
365+
366+
impl VersionedHashable for crate::transaction::CommitTransaction {
367+
#[inline]
368+
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
369+
self.body.versioned_hash(version)
370+
}
371+
}
372+
373+
impl VersionedHashable for crate::transaction::RevealTransaction {
336374
#[inline]
375+
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
376+
self.body.versioned_hash(version)
377+
}
378+
}
379+
380+
impl VersionedHashable for crate::transaction::StakeTransaction {
381+
#[inline]
382+
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
383+
self.body.versioned_hash(version)
384+
}
385+
}
386+
387+
impl VersionedHashable for crate::transaction::UnstakeTransaction {
388+
#[inline]
389+
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
390+
self.body.versioned_hash(version)
391+
}
392+
}
393+
394+
impl VersionedHashable for crate::chain::Block {
337395
fn versioned_hash(&self, version: ProtocolVersion) -> Hash {
338396
self.block_header.versioned_hash(version)
339397
}

validations/src/tests/mod.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use witnet_data_structures::{
2323
calculate_tally_change, calculate_witness_reward, create_tally, DataRequestPool,
2424
},
2525
error::{BlockError, DataRequestError, Secp256k1ConversionError, TransactionError},
26-
proto::versioning::ProtocolVersion,
26+
proto::versioning::{ProtocolVersion, VersionedHashable},
2727
radon_error::RadonError,
2828
radon_report::{RadonReport, ReportContext, TypeLike},
2929
staking::stakes::StakesTracker,
@@ -1497,6 +1497,7 @@ fn data_request_no_inputs() {
14971497
MAX_DR_WEIGHT,
14981498
REQUIRED_REWARD_COLLATERAL_RATIO,
14991499
&current_active_wips(),
1500+
None,
15001501
);
15011502
assert_eq!(
15021503
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1534,6 +1535,7 @@ fn data_request_no_inputs_but_one_signature() {
15341535
MAX_DR_WEIGHT,
15351536
REQUIRED_REWARD_COLLATERAL_RATIO,
15361537
&current_active_wips(),
1538+
None,
15371539
);
15381540
assert_eq!(
15391541
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1580,6 +1582,7 @@ fn data_request_one_input_but_no_signature() {
15801582
MAX_DR_WEIGHT,
15811583
REQUIRED_REWARD_COLLATERAL_RATIO,
15821584
&current_active_wips(),
1585+
None,
15831586
);
15841587
assert_eq!(
15851588
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1627,6 +1630,7 @@ fn data_request_one_input_signatures() {
16271630
MAX_DR_WEIGHT,
16281631
REQUIRED_REWARD_COLLATERAL_RATIO,
16291632
&current_active_wips(),
1633+
None,
16301634
)?;
16311635
verify_signatures_test(signatures_to_verify)?;
16321636

@@ -1670,6 +1674,7 @@ fn data_request_input_double_spend() {
16701674
MAX_DR_WEIGHT,
16711675
REQUIRED_REWARD_COLLATERAL_RATIO,
16721676
&current_active_wips(),
1677+
None,
16731678
);
16741679
assert_eq!(
16751680
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1710,6 +1715,7 @@ fn data_request_input_not_in_utxo() {
17101715
MAX_DR_WEIGHT,
17111716
REQUIRED_REWARD_COLLATERAL_RATIO,
17121717
&current_active_wips(),
1718+
None,
17131719
);
17141720
assert_eq!(
17151721
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1755,6 +1761,7 @@ fn data_request_input_not_enough_value() {
17551761
MAX_DR_WEIGHT,
17561762
REQUIRED_REWARD_COLLATERAL_RATIO,
17571763
&current_active_wips(),
1764+
None,
17581765
);
17591766
assert_eq!(
17601767
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1824,6 +1831,7 @@ fn data_request_output_value_overflow() {
18241831
MAX_DR_WEIGHT,
18251832
REQUIRED_REWARD_COLLATERAL_RATIO,
18261833
&current_active_wips(),
1834+
None,
18271835
);
18281836
assert_eq!(
18291837
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -1861,6 +1869,7 @@ fn test_drtx(dr_output: DataRequestOutput) -> Result<(), failure::Error> {
18611869
u32::MAX,
18621870
REQUIRED_REWARD_COLLATERAL_RATIO,
18631871
&all_wips_active(),
1872+
None,
18641873
)
18651874
.map(|_| ())
18661875
}
@@ -2263,6 +2272,7 @@ fn data_request_http_post_before_wip_activation() {
22632272
u32::MAX,
22642273
REQUIRED_REWARD_COLLATERAL_RATIO,
22652274
&active_wips,
2275+
None,
22662276
)
22672277
.map(|_| ())
22682278
};
@@ -2331,6 +2341,7 @@ fn data_request_http_get_with_headers_before_wip_activation() {
23312341
u32::MAX,
23322342
REQUIRED_REWARD_COLLATERAL_RATIO,
23332343
&active_wips,
2344+
None,
23342345
)
23352346
.map(|_| ())
23362347
};
@@ -2389,6 +2400,7 @@ fn data_request_parse_xml_before_wip_activation() {
23892400
u32::MAX,
23902401
REQUIRED_REWARD_COLLATERAL_RATIO,
23912402
&active_wips,
2403+
None,
23922404
)
23932405
.map(|_| ())
23942406
};
@@ -2445,6 +2457,7 @@ fn data_request_parse_xml_after_wip_activation() {
24452457
u32::MAX,
24462458
REQUIRED_REWARD_COLLATERAL_RATIO,
24472459
&active_wips,
2460+
None,
24482461
)
24492462
.map(|_| ())
24502463
};
@@ -2478,6 +2491,7 @@ fn dr_validation_weight_limit_exceeded() {
24782491
1625 - 1,
24792492
REQUIRED_REWARD_COLLATERAL_RATIO,
24802493
&current_active_wips(),
2494+
None,
24812495
);
24822496

24832497
assert_eq!(
@@ -2566,6 +2580,7 @@ fn data_request_miner_fee() {
25662580
MAX_DR_WEIGHT,
25672581
REQUIRED_REWARD_COLLATERAL_RATIO,
25682582
&current_active_wips(),
2583+
None,
25692584
)
25702585
.map(|(_, _, fee)| fee)
25712586
.unwrap();
@@ -2617,6 +2632,7 @@ fn data_request_miner_fee_with_change() {
26172632
MAX_DR_WEIGHT,
26182633
REQUIRED_REWARD_COLLATERAL_RATIO,
26192634
&current_active_wips(),
2635+
None,
26202636
)
26212637
.map(|(_, _, fee)| fee)
26222638
.unwrap();
@@ -2668,6 +2684,7 @@ fn data_request_change_to_different_pkh() {
26682684
MAX_DR_WEIGHT,
26692685
REQUIRED_REWARD_COLLATERAL_RATIO,
26702686
&current_active_wips(),
2687+
None,
26712688
);
26722689

26732690
assert_eq!(
@@ -2729,6 +2746,7 @@ fn data_request_two_change_outputs() {
27292746
MAX_DR_WEIGHT,
27302747
REQUIRED_REWARD_COLLATERAL_RATIO,
27312748
&current_active_wips(),
2749+
None,
27322750
);
27332751

27342752
assert_eq!(
@@ -2782,6 +2800,7 @@ fn data_request_miner_fee_with_too_much_change() {
27822800
MAX_DR_WEIGHT,
27832801
REQUIRED_REWARD_COLLATERAL_RATIO,
27842802
&current_active_wips(),
2803+
None,
27852804
);
27862805
assert_eq!(
27872806
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -2830,6 +2849,7 @@ fn data_request_zero_value_output() {
28302849
MAX_DR_WEIGHT,
28312850
REQUIRED_REWARD_COLLATERAL_RATIO,
28322851
&current_active_wips(),
2852+
None,
28332853
);
28342854
assert_eq!(
28352855
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -2881,6 +2901,7 @@ fn data_request_reward_collateral_ratio_wip() {
28812901
MAX_DR_WEIGHT,
28822902
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO,
28832903
&active_wips,
2904+
None,
28842905
);
28852906
x.unwrap();
28862907

@@ -2897,6 +2918,7 @@ fn data_request_reward_collateral_ratio_wip() {
28972918
MAX_DR_WEIGHT,
28982919
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO,
28992920
&active_wips,
2921+
None,
29002922
);
29012923
assert_eq!(
29022924
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -2952,6 +2974,7 @@ fn data_request_reward_collateral_ratio_limit() {
29522974
MAX_DR_WEIGHT,
29532975
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO,
29542976
&active_wips,
2977+
None,
29552978
);
29562979
x.unwrap();
29572980

@@ -2979,6 +3002,7 @@ fn data_request_reward_collateral_ratio_limit() {
29793002
MAX_DR_WEIGHT,
29803003
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO,
29813004
&active_wips,
3005+
None,
29823006
);
29833007
assert_eq!(
29843008
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -10002,6 +10026,7 @@ fn test_blocks(txns: Vec<(BlockTransactions, u64)>) -> Result<(), failure::Error
1000210026
MAX_VT_WEIGHT,
1000310027
MAX_DR_WEIGHT,
1000410028
GENESIS_BLOCK_HASH.parse().unwrap(),
10029+
None,
1000510030
)
1000610031
}
1000710032

@@ -10010,6 +10035,7 @@ fn test_blocks_with_limits(
1001010035
max_vt_weight: u32,
1001110036
max_dr_weight: u32,
1001210037
genesis_block_hash: Hash,
10038+
protocol_version: Option<ProtocolVersion>,
1001310039
) -> Result<(), failure::Error> {
1001410040
if txns.len() > 1 {
1001510041
// FIXME(#685): add sequence validations
@@ -10022,7 +10048,7 @@ fn test_blocks_with_limits(
1002210048
let mut utxo_set = UnspentOutputsPool::default();
1002310049
let block_number = 0;
1002410050
let stakes = StakesTracker::default();
10025-
let protocol_version = ProtocolVersion::default();
10051+
let protocol_version = protocol_version.unwrap_or_default();
1002610052

1002710053
let consensus_constants = ConsensusConstants {
1002810054
checkpoint_zero_timestamp: 0,
@@ -11305,6 +11331,7 @@ fn validate_vt_weight_overflow() {
1130511331
2 * 493 - 1,
1130611332
0,
1130711333
GENESIS_BLOCK_HASH.parse().unwrap(),
11334+
None,
1130811335
);
1130911336
assert_eq!(
1131011337
x.unwrap_err().downcast::<BlockError>().unwrap(),
@@ -11343,7 +11370,13 @@ fn validate_vt_weight_valid() {
1134311370
DEFAULT_INPUT_VALUE - 2 * 10,
1134411371
)
1134511372
};
11346-
let x = test_blocks_with_limits(vec![t0], 2 * 493, 0, GENESIS_BLOCK_HASH.parse().unwrap());
11373+
let x = test_blocks_with_limits(
11374+
vec![t0],
11375+
2 * 493,
11376+
0,
11377+
GENESIS_BLOCK_HASH.parse().unwrap(),
11378+
None,
11379+
);
1134711380
x.unwrap();
1134811381
}
1134911382

@@ -11370,7 +11403,7 @@ fn validate_vt_weight_genesis_valid() {
1137011403
1_000_000 - 10,
1137111404
)
1137211405
};
11373-
let x = test_blocks_with_limits(vec![t0], 0, 0, new_genesis.parse().unwrap());
11406+
let x = test_blocks_with_limits(vec![t0], 0, 0, new_genesis.parse().unwrap(), None);
1137411407
x.unwrap();
1137511408
}
1137611409

@@ -11406,6 +11439,7 @@ fn validate_dr_weight_overflow() {
1140611439
0,
1140711440
2 * 1589 - 1,
1140811441
GENESIS_BLOCK_HASH.parse().unwrap(),
11442+
None,
1140911443
);
1141011444
assert_eq!(
1141111445
x.unwrap_err().downcast::<BlockError>().unwrap(),
@@ -11445,6 +11479,7 @@ fn validate_dr_weight_overflow_126_witnesses() {
1144511479
0,
1144611480
MAX_DR_WEIGHT,
1144711481
GENESIS_BLOCK_HASH.parse().unwrap(),
11482+
None,
1144811483
);
1144911484
assert_eq!(
1145011485
x.unwrap_err().downcast::<TransactionError>().unwrap(),
@@ -11483,7 +11518,13 @@ fn validate_dr_weight_valid() {
1148311518
DEFAULT_INPUT_VALUE - 2 * dr_value,
1148411519
)
1148511520
};
11486-
let x = test_blocks_with_limits(vec![t0], 0, 2 * 1605, GENESIS_BLOCK_HASH.parse().unwrap());
11521+
let x = test_blocks_with_limits(
11522+
vec![t0],
11523+
0,
11524+
2 * 1605,
11525+
GENESIS_BLOCK_HASH.parse().unwrap(),
11526+
None,
11527+
);
1148711528
x.unwrap();
1148811529
}
1148911530

0 commit comments

Comments
 (0)