Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mod test {
use std::convert::TryInto;

use futures::StreamExt;
use primitive_types::U256;
use primitive_types::U512;
use tari_comms::{peer_manager::NodeId, test_utils::mocks::create_connectivity_mock};
use tari_p2p::services::liveness::{
mock::{create_p2p_liveness_mock, LivenessMockState},
Expand Down Expand Up @@ -246,8 +246,8 @@ mod test {
}

fn create_sample_proto_chain_metadata() -> proto::ChainMetadata {
let diff: U256 = 1.into();
let mut bytes = [0u8; 32];
let diff: U512 = 1.into();
let mut bytes = [0u8; 64];
diff.to_big_endian(&mut bytes);
proto::ChainMetadata {
best_block_height: 1,
Expand All @@ -256,7 +256,8 @@ mod test {
28, 29, 30, 31,
],
pruned_height: 0,
accumulated_difficulty: bytes.to_vec(),
accumulated_difficulty_low: bytes[32..=63].to_vec(),
accumulated_difficulty_high: bytes[0..=31].to_vec(),
timestamp: EpochTime::now().as_u64(),
}
}
Expand Down
5 changes: 3 additions & 2 deletions base_layer/core/src/base_node/proto/chain_metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ message ChainMetadata {
uint64 best_block_height = 1;
// The block hash of the current tip of the longest valid chain, or `None` for an empty chain
bytes best_block_hash = 2;
// The current geometric mean of the pow of the chain tip, or `None` if there is no chain
bytes accumulated_difficulty = 5;
// The current geometric mean of the pow of the chain tip, or `None` if there is no chain, split its into u256_low and u256 high for old node compatibility
bytes accumulated_difficulty_low = 5;
bytes accumulated_difficulty_high = 8;
// The effective height of the pruning horizon. This indicates from what height
// a full block can be provided (exclusive).
// If `pruned_height` is equal to the `best_block_height` no blocks can be provided.
Expand Down
28 changes: 19 additions & 9 deletions base_layer/core/src/base_node/proto/chain_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ use tari_common_types::{chain_metadata::ChainMetadata, types::FixedHash};

use crate::proto::base_node as proto;

const ACCUMULATED_DIFFICULTY_BYTE_SIZE: usize = 64;
const ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256: usize = 32;
const ACCUMULATED_DIFFICULTY_BYTE_SIZE_U512: usize = 64;
impl TryFrom<proto::ChainMetadata> for ChainMetadata {
type Error = String;

fn try_from(metadata: proto::ChainMetadata) -> Result<Self, Self::Error> {
if metadata.accumulated_difficulty.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE {
if metadata.accumulated_difficulty_low.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256 {
return Err(format!(
"Invalid accumulated difficulty byte length. {} was expected but the actual length was {}",
ACCUMULATED_DIFFICULTY_BYTE_SIZE,
metadata.accumulated_difficulty.len()
"Invalid accumulated difficulty low byte length. {} was expected but the actual length was {}",
ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256,
metadata.accumulated_difficulty_low.len()
));
}

let accumulated_difficulty = U512::from_big_endian(&metadata.accumulated_difficulty);
if metadata.accumulated_difficulty_high.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256 {
return Err(format!(
"Invalid accumulated difficulty byte high length. {} was expected but the actual length was {}",
ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256,
metadata.accumulated_difficulty_high.len()
));
}
let mut bytes = metadata.accumulated_difficulty_high.to_vec();
bytes.extend_from_slice(&metadata.accumulated_difficulty_low);
let accumulated_difficulty = U512::from_big_endian(&bytes);
let best_block_height = metadata.best_block_height;

let pruning_horizon = if metadata.pruned_height == 0 {
Expand Down Expand Up @@ -71,15 +80,16 @@ impl TryFrom<proto::ChainMetadata> for ChainMetadata {

impl From<ChainMetadata> for proto::ChainMetadata {
fn from(metadata: ChainMetadata) -> Self {
let mut accumulated_difficulty = [0u8; ACCUMULATED_DIFFICULTY_BYTE_SIZE];
let mut accumulated_difficulty = [0u8; ACCUMULATED_DIFFICULTY_BYTE_SIZE_U512];
metadata
.accumulated_difficulty()
.to_big_endian(&mut accumulated_difficulty);
Self {
best_block_height: metadata.best_block_height(),
best_block_hash: metadata.best_block_hash().to_vec(),
pruned_height: metadata.pruned_height(),
accumulated_difficulty: accumulated_difficulty.to_vec(),
accumulated_difficulty_low: accumulated_difficulty[32..=63].to_vec(),
accumulated_difficulty_high: accumulated_difficulty[0..=31].to_vec(),
timestamp: metadata.timestamp(),
}
}
Expand Down
46 changes: 44 additions & 2 deletions base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,29 @@ impl ConsensusConstants {
let mut con_2 = con_1.clone();
con_2.coinbase_min_maturity = 120;
con_2.effective_from_height = 30 * 24 * 2; // 2 days
let mut con_3 = con_2.clone();
con_3.effective_from_height = 1500;
let mut algos = HashMap::new();
algos.insert(PowAlgorithm::Sha3x, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(150_000_000_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
algos.insert(PowAlgorithm::RandomXM, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(1_200_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
algos.insert(PowAlgorithm::RandomXT, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(1_200_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
con_3.blockchain_version = 1;
con_3.valid_blockchain_version_range = 1..=1;
con_3.proof_of_work = algos;

let consensus_constants = vec![con_1, con_2];
let consensus_constants = vec![con_1, con_2, con_3];
consensus_constants
}

Expand Down Expand Up @@ -730,9 +751,30 @@ impl ConsensusConstants {
let mut con_3 = con_2.clone();
con_3.coinbase_min_maturity = 360;
con_3.effective_from_height = 30 * 24 * 7 * 2; // 2 weeks

let mut con_4 = con_3.clone();
con_4.effective_from_height = 15_000;
con_4.coinbase_min_maturity = 180; // 6 hours
con_4.effective_from_height = 30 * 24 * 7 * 3; // 3 weeks
let mut algos = HashMap::new();
algos.insert(PowAlgorithm::Sha3x, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(150_000_000_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
algos.insert(PowAlgorithm::RandomXM, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(1_200_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
algos.insert(PowAlgorithm::RandomXT, PowAlgorithmConstants {
min_difficulty: Difficulty::from_u64(1_200_000).expect("valid difficulty"),
max_difficulty: Difficulty::max(),
target_time: 360,
});
con_4.blockchain_version = 1;
con_4.valid_blockchain_version_range = 1..=1;
con_4.proof_of_work = algos;

let consensus_constants = vec![con_1, con_2, con_3, con_4];
consensus_constants
}
Expand Down
13 changes: 13 additions & 0 deletions base_layer/core/src/proof_of_work/monero_rx/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ mod test {
TxIn,
TxOut,
};
use serial_test::serial;
use tari_common::configuration::Network;
use tari_test_utils::unpack_enum;
use tari_utilities::{
Expand Down Expand Up @@ -1294,7 +1295,19 @@ mod test {
}

#[test]
#[serial]
fn test_tari_randomx_difficulty() {
let network = Network::Esmeralda;
if std::env::var("TARI_NETWORK").is_err() {
std::env::set_var("TARI_NETWORK", network.as_key_str());
}
if Network::get_current_or_user_setting_or_default() != network {
let _ = Network::set_current(network);
}
let current_network = Network::get_current_or_user_setting_or_default();
if current_network != network {
panic!("could not set network");
}
let randomx_factory = RandomXFactory::new(1);
let vm_key = from_hex("920647f8f10b8b484649adf83599c5b86bba137e7eb22e95dd8739d98528f677").unwrap();

Expand Down
6 changes: 4 additions & 2 deletions base_layer/wallet/tests/support/comms_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ impl BaseNodeWalletRpcMockState {
metadata: Some(ChainMetadataProto {
best_block_height: i64::MAX as u64,
best_block_hash: FixedHash::zero().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: EpochTime::now().as_u64(),
}),
Expand Down Expand Up @@ -932,7 +933,8 @@ mod test {
let chain_metadata = ChainMetadata {
best_block_height: 444,
best_block_hash: vec![],
accumulated_difficulty: vec![],
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: EpochTime::now().as_u64(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::{

use blake2::Blake2b;
use chacha20poly1305::{Key, KeyInit, XChaCha20Poly1305};
use chrono::{DateTime, Days, Duration as ChronoDuration, Utc};
use chrono::{Days, Duration as ChronoDuration, Utc};
use digest::consts::U32;
use futures::{
channel::{mpsc, mpsc::Sender},
Expand Down
24 changes: 16 additions & 8 deletions base_layer/wallet/tests/utxo_scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ async fn test_utxo_scanner_recovery() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS - 1,
best_block_hash: block_headers.get(&(NUM_BLOCKS - 1)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -426,7 +427,8 @@ async fn test_utxo_scanner_recovery_with_restart() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS - 1,
best_block_hash: block_headers.get(&(NUM_BLOCKS - 1)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -563,7 +565,8 @@ async fn test_utxo_scanner_recovery_with_restart_and_reorg() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS - 1,
best_block_hash: block_headers.get(&(NUM_BLOCKS - 1)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -643,7 +646,8 @@ async fn test_utxo_scanner_recovery_with_restart_and_reorg() {
let chain_metadata = ChainMetadata {
best_block_height: 9,
best_block_hash: block_headers.get(&9).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -773,7 +777,8 @@ async fn test_utxo_scanner_scanned_block_cache_clearing() {
.clone()
.hash()
.to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -871,7 +876,8 @@ async fn test_utxo_scanner_one_sided_payments() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS - 1,
best_block_hash: block_headers.get(&(NUM_BLOCKS - 1)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -976,7 +982,8 @@ async fn test_utxo_scanner_one_sided_payments() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS,
best_block_hash: block_headers.get(&(NUM_BLOCKS)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down Expand Up @@ -1047,7 +1054,8 @@ async fn test_birthday_timestamp_over_chain() {
let chain_metadata = ChainMetadata {
best_block_height: NUM_BLOCKS - 1,
best_block_hash: block_headers.get(&(NUM_BLOCKS - 1)).unwrap().clone().hash().to_vec(),
accumulated_difficulty: Vec::new(),
accumulated_difficulty_low: Vec::new(),
accumulated_difficulty_high: Vec::new(),
pruned_height: 0,
timestamp: 0,
};
Expand Down
Loading