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
18 changes: 4 additions & 14 deletions applications/minotari_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use tari_core::{
},
transaction_key_manager::{create_memory_db_key_manager, TariKeyId, TransactionKeyManagerInterface, TxoStage},
},
validation::tari_rx_vm_key_height,
};
use tari_p2p::{auto_update::SoftwareUpdaterHandle, services::liveness::LivenessHandle};
use tari_utilities::{hex::Hex, message_format::MessageFormat, ByteArray};
Expand All @@ -93,7 +94,6 @@ use crate::{
grpc_method::GrpcMethod,
BaseNodeConfig,
};

const LOG_TARGET: &str = "minotari::base_node::grpc";
const GET_TOKENS_IN_CIRCULATION_MAX_HEIGHTS: usize = 1_000_000;
const GET_TOKENS_IN_CIRCULATION_PAGE_SIZE: usize = 1_000;
Expand Down Expand Up @@ -995,12 +995,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
algo: Some(tari_rpc::PowAlgo { pow_algo: pow }),
};
let vm_key = *handler
.get_header(
new_template
.header
.height
.saturating_sub(new_template.header.height % 2000),
)
.get_header(tari_rx_vm_key_height(new_template.header.height))
.await
.map_err(|_| {
obscure_error_if_true(
Expand Down Expand Up @@ -1292,7 +1287,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
PowAlgorithm::RandomXM => new_block.header.merge_mining_hash().to_vec(),
};
let vm_key = *handler
.get_header(new_block.header.height.saturating_sub(new_block.header.height % 2000))
.get_header(tari_rx_vm_key_height(new_block.header.height))
.await
.map_err(|_| {
obscure_error_if_true(
Expand Down Expand Up @@ -1561,12 +1556,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
algo: Some(tari_rpc::PowAlgo { pow_algo: pow }),
};
let vm_key = *handler
.get_header(
new_template
.header
.height
.saturating_sub(new_template.header.height % 2000),
)
.get_header(tari_rx_vm_key_height(new_template.header.height))
.await
.map_err(|_| {
obscure_error_if_true(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use crate::{
PowError,
},
transactions::aggregated_body::AggregateBody,
validation::{helpers, ValidationError},
validation::{helpers, tari_rx_vm_key_height, ValidationError},
};

const LOG_TARGET: &str = "c::bn::comms_interface::inbound_handler";
Expand Down Expand Up @@ -574,7 +574,7 @@ where B: BlockchainBackend + 'static
PowAlgorithm::RandomXT => {
let vm_key = *self
.blockchain_db
.fetch_chain_header(header.height().saturating_sub(header.height() % 2000))
.fetch_chain_header(tari_rx_vm_key_height(header.height()))
.await?
.hash();
tari_randomx_difficulty(&new_block.header, &self.randomx_factory, &vm_key)?
Expand Down
57 changes: 56 additions & 1 deletion base_layer/core/src/validation/difficulty_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use crate::{
validation::{helpers::check_target_difficulty, ValidationError},
};

const TARI_RX_VM_KEY_BLOCK_SWAP: u64 = 2048;
const TARI_RX_VM_KEY_REORG_SAFETY_NUMBER: u64 = 64;

#[derive(Clone)]
pub struct DifficultyCalculator {
pub rules: ConsensusManager,
Expand All @@ -53,7 +56,7 @@ impl DifficultyCalculator {
);
let gen_hash = *self.rules.get_genesis_block().hash();
let vm_key = *db
.fetch_chain_header_by_height(block_header.height.saturating_sub(block_header.height % 2000))?
.fetch_chain_header_by_height(tari_rx_vm_key_height(block_header.height))?
.hash();
let achieved_target = check_target_difficulty(
block_header,
Expand All @@ -67,3 +70,55 @@ impl DifficultyCalculator {
Ok(achieved_target)
}
}

pub fn tari_rx_vm_key_height(height: u64) -> u64 {
if height <= TARI_RX_VM_KEY_BLOCK_SWAP + TARI_RX_VM_KEY_REORG_SAFETY_NUMBER {
0
} else {
(height - TARI_RX_VM_KEY_REORG_SAFETY_NUMBER - 1) & !(TARI_RX_VM_KEY_BLOCK_SWAP - 1)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_tari_vm_key_calc() {
let height = 0;
let expected = 0;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 1000;
let expected = 0;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 2047;
let expected = 0;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 2048;
let expected = 0;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 3048;
let expected = 2048;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 4000;
let expected = 2048;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 4159;
let expected = 2048;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 4160;
let expected = 2048;
assert_eq!(tari_rx_vm_key_height(height), expected);

let height = 4161;
let expected = 4096;
assert_eq!(tari_rx_vm_key_height(height), expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
proof_of_work::{AchievedTargetDifficulty, Difficulty, PowAlgorithm, PowError},
validation::{
helpers::{check_header_timestamp_greater_than_median, check_target_difficulty},
tari_rx_vm_key_height,
DifficultyCalculator,
HeaderChainLinkedValidator,
ValidationError,
Expand Down Expand Up @@ -80,7 +81,7 @@ impl<B: BlockchainBackend> HeaderChainLinkedValidator<B> for HeaderFullValidator
check_timestamp_ftl(header, &self.rules)?;
check_pow_data(header)?;
let vm_key = *db
.fetch_chain_header_by_height(header.height.saturating_sub(header.height % 2000))?
.fetch_chain_header_by_height(tari_rx_vm_key_height(header.height))?
.hash();

let achieved_target = if let Some(target) = target_difficulty {
Expand Down
Loading