Skip to content

Commit

Permalink
feat(node): make 2.0 codebase able to sync to V1_6
Browse files Browse the repository at this point in the history
  • Loading branch information
aesedepece committed Nov 21, 2023
1 parent 157f0cd commit 3eed9fd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 148 deletions.
38 changes: 10 additions & 28 deletions data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ use crate::{
DataRequestError, EpochCalculationError, OutputPointerParseError, Secp256k1ConversionError,
TransactionError,
},
get_environment,
proto::{
versioning::{ProtocolVersion, VersionedHashable},
ProtobufConvert,
},
get_environment, get_protocol_version,
proto::{versioning::Versioned, ProtobufConvert},
superblock::SuperBlockState,
transaction::{
CommitTransaction, DRTransaction, DRTransactionBody, Memoized, MintTransaction,
Expand Down Expand Up @@ -536,7 +533,7 @@ impl Block {
}

pub fn is_genesis(&self, genesis: &Hash) -> bool {
self.versioned_hash(ProtocolVersion::Legacy).eq(genesis)
self.hash().eq(genesis)
}
}

Expand Down Expand Up @@ -671,7 +668,9 @@ impl Hashable for BlockHeader {

impl MemoizedHashable for Block {
fn hashable_bytes(&self) -> Vec<u8> {
self.block_header.to_pb_bytes().unwrap()
self.block_header
.to_versioned_pb_bytes(get_protocol_version())
.unwrap()
}

fn memoized_hash(&self) -> &MemoHash {
Expand Down Expand Up @@ -4572,19 +4571,17 @@ mod tests {
let block = block_example();
let expected = "70e15ac70bb00f49c7a593b2423f722dca187bbae53dc2f22647063b17608c01";
assert_eq!(
block.versioned_hash(ProtocolVersion::Legacy).to_string(),
block.versioned_hash(ProtocolVersion::V1_6).to_string(),
expected
);
let expected = "29ef68357a5c861b9dbe043d351a28472ca450edcda25de4c9b80a4560a28c0f";
assert_eq!(
block
.versioned_hash(ProtocolVersion::Transition)
.to_string(),
block.versioned_hash(ProtocolVersion::V1_7).to_string(),
expected
);
let expected = "29ef68357a5c861b9dbe043d351a28472ca450edcda25de4c9b80a4560a28c0f";
assert_eq!(
block.versioned_hash(ProtocolVersion::Final).to_string(),
block.versioned_hash(ProtocolVersion::V2_0).to_string(),
expected
);
}
Expand Down Expand Up @@ -6658,7 +6655,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2, 2];
Expand Down Expand Up @@ -6713,7 +6709,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2, 2, 8, 10, 6, 4, 6];
Expand Down Expand Up @@ -6749,7 +6744,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

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

let sb = mining_build_superblock(
&[],
&[Hash::default()],
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);
let sb = mining_build_superblock(&[], &[Hash::default()], 1, Hash::default(), 1);

let result = sb.dr_proof_of_inclusion(&[], &dr_txs[2]);
assert!(result.is_none());
Expand All @@ -6793,7 +6780,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2];
Expand Down Expand Up @@ -6832,7 +6818,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2, 2];
Expand Down Expand Up @@ -6895,7 +6880,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2, 2, 8, 10, 6, 4, 6];
Expand Down Expand Up @@ -6931,7 +6915,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let result = sb.tally_proof_of_inclusion(&[b1, b2], &tally_txs[2]);
Expand Down Expand Up @@ -6963,7 +6946,6 @@ mod tests {
1,
Hash::default(),
1,
ProtocolVersion::Legacy,
);

let expected_indices = vec![0, 2, 2];
Expand Down
41 changes: 40 additions & 1 deletion data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[macro_use]
extern crate protobuf_convert;

use crate::chain::Environment;
use crate::{chain::Environment, proto::versioning::ProtocolVersion};
use lazy_static::lazy_static;
use std::sync::RwLock;

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

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

/// Protocol version that we are running.
pub fn get_protocol_version() -> ProtocolVersion {
// This unwrap is safe as long as the lock is not poisoned.
// The lock can only become poisoned when a writer panics.
// The only writer is the one used in `set_environment`, which should only
// be used during initialization.
*PROTOCOL_VERSION.read().unwrap()
}

/// Set the protocol version that we are running.
/// This function should only be called once during initialization.
// Changing the environment in tests is not supported, as it can cause spurious failures:
// multiple tests can run in parallel and some tests might fail when the environment changes.
// But if you need to change the environment in some test, just create a separate thread-local
// variable and mock get and set.
#[cfg(not(test))]
pub fn set_protocol_version(protocol_version: ProtocolVersion) {
match PROTOCOL_VERSION.write() {
Ok(mut x) => {
*x = protocol_version;
log::debug!("Protocol version set to {}", protocol_version);
}
Err(e) => {
log::error!("Failed to set protocol version: {}", e);
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -124,4 +155,12 @@ mod tests {
// addresses serialized as Bech32 will fail
assert_eq!(get_environment(), Environment::Mainnet);
}

#[test]
fn default_protocol_version() {
// If this default changes before the transition to V2 is complete, almost everything will
// break because data structures change schema and, serialization changes and hash
// derivation breaks too
assert_eq!(get_protocol_version(), ProtocolVersion::V1_6);
}
}
Loading

0 comments on commit 3eed9fd

Please sign in to comment.