Skip to content

Commit 144d5a2

Browse files
committed
fix(validations): fix mismatch in superblock validations
1 parent a9b660b commit 144d5a2

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

data_structures/src/chain/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ use crate::{
3636
DataRequestError, EpochCalculationError, OutputPointerParseError, Secp256k1ConversionError,
3737
TransactionError,
3838
},
39-
get_environment, get_protocol_version,
40-
proto::{versioning::Versioned, ProtobufConvert},
39+
get_environment,
40+
proto::{
41+
versioning::{ProtocolVersion, Versioned},
42+
ProtobufConvert,
43+
},
4144
superblock::SuperBlockState,
4245
transaction::{
4346
CommitTransaction, DRTransaction, DRTransactionBody, Memoized, MintTransaction,
@@ -669,7 +672,7 @@ impl Hashable for BlockHeader {
669672
impl MemoizedHashable for Block {
670673
fn hashable_bytes(&self) -> Vec<u8> {
671674
self.block_header
672-
.to_versioned_pb_bytes(get_protocol_version())
675+
.to_versioned_pb_bytes(ProtocolVersion::guess())
673676
.unwrap()
674677
}
675678

data_structures/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,6 @@ mod tests {
161161
// If this default changes before the transition to V2 is complete, almost everything will
162162
// break because data structures change schema and, serialization changes and hash
163163
// derivation breaks too
164-
assert_eq!(get_protocol_version(), ProtocolVersion::V1_6);
164+
assert_eq!(ProtocolVersion::guess(), ProtocolVersion::V1_6);
165165
}
166166
}

data_structures/src/proto/versioning.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::fmt::Formatter;
66
use crate::proto::schema::witnet::SuperBlock;
77
use crate::{
88
chain::Hash,
9+
get_protocol_version,
910
proto::{
1011
schema::witnet::{
1112
Block, Block_BlockHeader, Block_BlockHeader_BlockMerkleRoots, Block_BlockTransactions,
@@ -30,6 +31,12 @@ pub enum ProtocolVersion {
3031
V2_0,
3132
}
3233

34+
impl ProtocolVersion {
35+
pub fn guess() -> Self {
36+
get_protocol_version()
37+
}
38+
}
39+
3340
impl fmt::Display for ProtocolVersion {
3441
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3542
let s = match self {

data_structures/src/superblock.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
PublicKeyHash, SuperBlock, SuperBlockVote,
1818
},
1919
get_environment,
20+
proto::versioning::{ProtocolVersion, VersionedHashable},
2021
};
2122

2223
/// Possible result of SuperBlockState::add_vote
@@ -269,11 +270,15 @@ impl SuperBlockState {
269270

270271
AddSuperBlockVote::DoubleVote
271272
} else {
272-
let is_same_hash =
273-
sbv.superblock_hash == self.current_superblock_beacon.hash_prev_block;
273+
let theirs = sbv.superblock_hash;
274+
let ours = self.current_superblock_beacon.hash_prev_block;
275+
let votes_for_our_tip = theirs == ours;
276+
277+
log::debug!("Superblock vote comparison:\n(theirs): {theirs}\n(ours): {ours}");
278+
274279
self.votes_mempool.insert_vote(sbv);
275280

276-
if is_same_hash {
281+
if votes_for_our_tip {
277282
AddSuperBlockVote::ValidWithSameHash
278283
} else {
279284
AddSuperBlockVote::ValidButDifferentHash
@@ -699,7 +704,7 @@ pub fn mining_build_superblock(
699704
)
700705
}
701706
Some(last_block_header) => {
702-
let last_block_hash = last_block_header.hash();
707+
let last_block_hash = last_block_header.versioned_hash(ProtocolVersion::guess());
703708
let merkle_drs: Vec<Hash> = block_headers
704709
.iter()
705710
.map(|b| b.merkle_roots.dr_hash_merkle_root)

validations/src/validations.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use witnet_data_structures::{
2929
calculate_witness_reward_before_second_hard_fork, create_tally, DataRequestPool,
3030
},
3131
error::{BlockError, DataRequestError, TransactionError},
32-
get_protocol_version,
3332
proto::versioning::ProtocolVersion,
3433
radon_report::{RadonReport, ReportContext},
3534
transaction::{
@@ -1945,7 +1944,7 @@ pub fn validate_block_transactions(
19451944

19461945
// Nullify roots for legacy protocol version
19471946
// TODO skip all staking logic if protocol version is legacy
1948-
let (st_root, ut_root) = match get_protocol_version() {
1947+
let (st_root, ut_root) = match ProtocolVersion::guess() {
19491948
ProtocolVersion::V1_6 => Default::default(),
19501949
_ => (Hash::from(st_mt.root()), Hash::from(ut_mt.root())),
19511950
};

0 commit comments

Comments
 (0)