Skip to content

Commit 3eed9fd

Browse files
committed
feat(node): make 2.0 codebase able to sync to V1_6
1 parent 157f0cd commit 3eed9fd

File tree

8 files changed

+113
-148
lines changed

8 files changed

+113
-148
lines changed

data_structures/src/chain/mod.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ use crate::{
3636
DataRequestError, EpochCalculationError, OutputPointerParseError, Secp256k1ConversionError,
3737
TransactionError,
3838
},
39-
get_environment,
40-
proto::{
41-
versioning::{ProtocolVersion, VersionedHashable},
42-
ProtobufConvert,
43-
},
39+
get_environment, get_protocol_version,
40+
proto::{versioning::Versioned, ProtobufConvert},
4441
superblock::SuperBlockState,
4542
transaction::{
4643
CommitTransaction, DRTransaction, DRTransactionBody, Memoized, MintTransaction,
@@ -536,7 +533,7 @@ impl Block {
536533
}
537534

538535
pub fn is_genesis(&self, genesis: &Hash) -> bool {
539-
self.versioned_hash(ProtocolVersion::Legacy).eq(genesis)
536+
self.hash().eq(genesis)
540537
}
541538
}
542539

@@ -671,7 +668,9 @@ impl Hashable for BlockHeader {
671668

672669
impl MemoizedHashable for Block {
673670
fn hashable_bytes(&self) -> Vec<u8> {
674-
self.block_header.to_pb_bytes().unwrap()
671+
self.block_header
672+
.to_versioned_pb_bytes(get_protocol_version())
673+
.unwrap()
675674
}
676675

677676
fn memoized_hash(&self) -> &MemoHash {
@@ -4572,19 +4571,17 @@ mod tests {
45724571
let block = block_example();
45734572
let expected = "70e15ac70bb00f49c7a593b2423f722dca187bbae53dc2f22647063b17608c01";
45744573
assert_eq!(
4575-
block.versioned_hash(ProtocolVersion::Legacy).to_string(),
4574+
block.versioned_hash(ProtocolVersion::V1_6).to_string(),
45764575
expected
45774576
);
45784577
let expected = "29ef68357a5c861b9dbe043d351a28472ca450edcda25de4c9b80a4560a28c0f";
45794578
assert_eq!(
4580-
block
4581-
.versioned_hash(ProtocolVersion::Transition)
4582-
.to_string(),
4579+
block.versioned_hash(ProtocolVersion::V1_7).to_string(),
45834580
expected
45844581
);
45854582
let expected = "29ef68357a5c861b9dbe043d351a28472ca450edcda25de4c9b80a4560a28c0f";
45864583
assert_eq!(
4587-
block.versioned_hash(ProtocolVersion::Final).to_string(),
4584+
block.versioned_hash(ProtocolVersion::V2_0).to_string(),
45884585
expected
45894586
);
45904587
}
@@ -6658,7 +6655,6 @@ mod tests {
66586655
1,
66596656
Hash::default(),
66606657
1,
6661-
ProtocolVersion::Legacy,
66626658
);
66636659

66646660
let expected_indices = vec![0, 2, 2];
@@ -6713,7 +6709,6 @@ mod tests {
67136709
1,
67146710
Hash::default(),
67156711
1,
6716-
ProtocolVersion::Legacy,
67176712
);
67186713

67196714
let expected_indices = vec![0, 2, 2, 8, 10, 6, 4, 6];
@@ -6749,7 +6744,6 @@ mod tests {
67496744
1,
67506745
Hash::default(),
67516746
1,
6752-
ProtocolVersion::Legacy,
67536747
);
67546748

67556749
let result = sb.dr_proof_of_inclusion(&[b1, b2], &dr_txs[2]);
@@ -6760,14 +6754,7 @@ mod tests {
67606754
fn test_dr_merkle_root_no_block() {
67616755
let dr_txs = build_test_dr_txs(3);
67626756

6763-
let sb = mining_build_superblock(
6764-
&[],
6765-
&[Hash::default()],
6766-
1,
6767-
Hash::default(),
6768-
1,
6769-
ProtocolVersion::Legacy,
6770-
);
6757+
let sb = mining_build_superblock(&[], &[Hash::default()], 1, Hash::default(), 1);
67716758

67726759
let result = sb.dr_proof_of_inclusion(&[], &dr_txs[2]);
67736760
assert!(result.is_none());
@@ -6793,7 +6780,6 @@ mod tests {
67936780
1,
67946781
Hash::default(),
67956782
1,
6796-
ProtocolVersion::Legacy,
67976783
);
67986784

67996785
let expected_indices = vec![0, 2];
@@ -6832,7 +6818,6 @@ mod tests {
68326818
1,
68336819
Hash::default(),
68346820
1,
6835-
ProtocolVersion::Legacy,
68366821
);
68376822

68386823
let expected_indices = vec![0, 2, 2];
@@ -6895,7 +6880,6 @@ mod tests {
68956880
1,
68966881
Hash::default(),
68976882
1,
6898-
ProtocolVersion::Legacy,
68996883
);
69006884

69016885
let expected_indices = vec![0, 2, 2, 8, 10, 6, 4, 6];
@@ -6931,7 +6915,6 @@ mod tests {
69316915
1,
69326916
Hash::default(),
69336917
1,
6934-
ProtocolVersion::Legacy,
69356918
);
69366919

69376920
let result = sb.tally_proof_of_inclusion(&[b1, b2], &tally_txs[2]);
@@ -6963,7 +6946,6 @@ mod tests {
69636946
1,
69646947
Hash::default(),
69656948
1,
6966-
ProtocolVersion::Legacy,
69676949
);
69686950

69696951
let expected_indices = vec![0, 2, 2];

data_structures/src/lib.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[macro_use]
1414
extern crate protobuf_convert;
1515

16-
use crate::chain::Environment;
16+
use crate::{chain::Environment, proto::versioning::ProtocolVersion};
1717
use lazy_static::lazy_static;
1818
use std::sync::RwLock;
1919

@@ -82,6 +82,9 @@ lazy_static! {
8282
// can work without having to manually set the environment.
8383
// The default environment will also be used in tests.
8484
static ref ENVIRONMENT: RwLock<Environment> = RwLock::new(Environment::Mainnet);
85+
/// Protocol version that we are running.
86+
/// default to legacy for now — it's the v2 bootstrapping module's responsibility to upgrade it.
87+
static ref PROTOCOL_VERSION: RwLock<ProtocolVersion> = RwLock::new(ProtocolVersion::V1_6);
8588
}
8689

8790
/// Environment in which we are running: mainnet or testnet.
@@ -114,6 +117,34 @@ pub fn set_environment(environment: Environment) {
114117
}
115118
}
116119

120+
/// Protocol version that we are running.
121+
pub fn get_protocol_version() -> ProtocolVersion {
122+
// This unwrap is safe as long as the lock is not poisoned.
123+
// The lock can only become poisoned when a writer panics.
124+
// The only writer is the one used in `set_environment`, which should only
125+
// be used during initialization.
126+
*PROTOCOL_VERSION.read().unwrap()
127+
}
128+
129+
/// Set the protocol version that we are running.
130+
/// This function should only be called once during initialization.
131+
// Changing the environment in tests is not supported, as it can cause spurious failures:
132+
// multiple tests can run in parallel and some tests might fail when the environment changes.
133+
// But if you need to change the environment in some test, just create a separate thread-local
134+
// variable and mock get and set.
135+
#[cfg(not(test))]
136+
pub fn set_protocol_version(protocol_version: ProtocolVersion) {
137+
match PROTOCOL_VERSION.write() {
138+
Ok(mut x) => {
139+
*x = protocol_version;
140+
log::debug!("Protocol version set to {}", protocol_version);
141+
}
142+
Err(e) => {
143+
log::error!("Failed to set protocol version: {}", e);
144+
}
145+
}
146+
}
147+
117148
#[cfg(test)]
118149
mod tests {
119150
use super::*;
@@ -124,4 +155,12 @@ mod tests {
124155
// addresses serialized as Bech32 will fail
125156
assert_eq!(get_environment(), Environment::Mainnet);
126157
}
158+
159+
#[test]
160+
fn default_protocol_version() {
161+
// If this default changes before the transition to V2 is complete, almost everything will
162+
// break because data structures change schema and, serialization changes and hash
163+
// derivation breaks too
164+
assert_eq!(get_protocol_version(), ProtocolVersion::V1_6);
165+
}
127166
}

0 commit comments

Comments
 (0)