Skip to content

Commit 990061d

Browse files
committed
add test
1 parent 13cbc52 commit 990061d

File tree

4 files changed

+60
-36
lines changed

4 files changed

+60
-36
lines changed

applications/minotari_node/src/grpc/base_node_grpc_server.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use tari_core::{
7676
},
7777
transaction_key_manager::{create_memory_db_key_manager, TariKeyId, TransactionKeyManagerInterface, TxoStage},
7878
},
79+
validation::tari_rx_vm_key_height,
7980
};
8081
use tari_p2p::{auto_update::SoftwareUpdaterHandle, services::liveness::LivenessHandle};
8182
use tari_utilities::{hex::Hex, message_format::MessageFormat, ByteArray};
@@ -93,7 +94,6 @@ use crate::{
9394
grpc_method::GrpcMethod,
9495
BaseNodeConfig,
9596
};
96-
9797
const LOG_TARGET: &str = "minotari::base_node::grpc";
9898
const GET_TOKENS_IN_CIRCULATION_MAX_HEIGHTS: usize = 1_000_000;
9999
const GET_TOKENS_IN_CIRCULATION_PAGE_SIZE: usize = 1_000;
@@ -995,13 +995,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
995995
algo: Some(tari_rpc::PowAlgo { pow_algo: pow }),
996996
};
997997
let vm_key = *handler
998-
.get_header(
999-
new_template
1000-
.header
1001-
.height
1002-
.saturating_sub(new_template.header.height % 2048)
1003-
.saturating_sub(64),
1004-
)
998+
.get_header(tari_rx_vm_key_height(new_template.header.height))
1005999
.await
10061000
.map_err(|_| {
10071001
obscure_error_if_true(
@@ -1293,13 +1287,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
12931287
PowAlgorithm::RandomXM => new_block.header.merge_mining_hash().to_vec(),
12941288
};
12951289
let vm_key = *handler
1296-
.get_header(
1297-
new_block
1298-
.header
1299-
.height
1300-
.saturating_sub(new_block.header.height % 2048)
1301-
.saturating_sub(64),
1302-
)
1290+
.get_header(tari_rx_vm_key_height(new_block.header.height))
13031291
.await
13041292
.map_err(|_| {
13051293
obscure_error_if_true(
@@ -1568,13 +1556,7 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
15681556
algo: Some(tari_rpc::PowAlgo { pow_algo: pow }),
15691557
};
15701558
let vm_key = *handler
1571-
.get_header(
1572-
new_template
1573-
.header
1574-
.height
1575-
.saturating_sub(new_template.header.height % 2048)
1576-
.saturating_sub(64),
1577-
)
1559+
.get_header(tari_rx_vm_key_height(new_template.header.height))
15781560
.await
15791561
.map_err(|_| {
15801562
obscure_error_if_true(

base_layer/core/src/base_node/comms_interface/inbound_handlers.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::{
5656
PowError,
5757
},
5858
transactions::aggregated_body::AggregateBody,
59-
validation::{helpers, ValidationError},
59+
validation::{helpers, tari_rx_vm_key_height, ValidationError},
6060
};
6161

6262
const LOG_TARGET: &str = "c::bn::comms_interface::inbound_handler";
@@ -574,12 +574,7 @@ where B: BlockchainBackend + 'static
574574
PowAlgorithm::RandomXT => {
575575
let vm_key = *self
576576
.blockchain_db
577-
.fetch_chain_header(
578-
header
579-
.height()
580-
.saturating_sub(header.height() % 2048)
581-
.saturating_sub(64),
582-
)
577+
.fetch_chain_header(tari_rx_vm_key_height(header.height()))
583578
.await?
584579
.hash();
585580
tari_randomx_difficulty(&new_block.header, &self.randomx_factory, &vm_key)?

base_layer/core/src/validation/difficulty_calculator.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ impl DifficultyCalculator {
5353
);
5454
let gen_hash = *self.rules.get_genesis_block().hash();
5555
let vm_key = *db
56-
.fetch_chain_header_by_height(
57-
block_header
58-
.height
59-
.saturating_sub(block_header.height % 2048)
60-
.saturating_sub(64),
61-
)?
56+
.fetch_chain_header_by_height(tari_rx_vm_key_height(block_header.height))?
6257
.hash();
6358
let achieved_target = check_target_difficulty(
6459
block_header,
@@ -72,3 +67,54 @@ impl DifficultyCalculator {
7267
Ok(achieved_target)
7368
}
7469
}
70+
71+
pub fn tari_rx_vm_key_height(height: u64) -> u64 {
72+
// The VM key is calculated from the block at height - (height % 2048) - 64
73+
// This is to ensure that the VM key is not too far in the past
74+
// and that it is not too close to the current block
75+
height.saturating_sub(height % 2048).saturating_sub(64)
76+
}
77+
78+
#[cfg(test)]
79+
mod test {
80+
use super::*;
81+
82+
#[test]
83+
fn test_tari_vm_key_calc() {
84+
let height = 0;
85+
let expected = 0;
86+
assert_eq!(tari_rx_vm_key_height(height), expected);
87+
88+
let height = 1000;
89+
let expected = 0;
90+
assert_eq!(tari_rx_vm_key_height(height), expected);
91+
92+
let height = 2047;
93+
let expected = 0;
94+
assert_eq!(tari_rx_vm_key_height(height), expected);
95+
96+
let height = 2048;
97+
let expected = 1984;
98+
assert_eq!(tari_rx_vm_key_height(height), expected);
99+
100+
let height = 3048;
101+
let expected = 1984;
102+
assert_eq!(tari_rx_vm_key_height(height), expected);
103+
104+
let height = 4000;
105+
let expected = 1984;
106+
assert_eq!(tari_rx_vm_key_height(height), expected);
107+
108+
let height = 4095;
109+
let expected = 1984;
110+
assert_eq!(tari_rx_vm_key_height(height), expected);
111+
112+
let height = 4096;
113+
let expected = 4032;
114+
assert_eq!(tari_rx_vm_key_height(height), expected);
115+
116+
let height = 4097;
117+
let expected = 4032;
118+
assert_eq!(tari_rx_vm_key_height(height), expected);
119+
}
120+
}

base_layer/core/src/validation/header/header_full_validator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::{
3333
proof_of_work::{AchievedTargetDifficulty, Difficulty, PowAlgorithm, PowError},
3434
validation::{
3535
helpers::{check_header_timestamp_greater_than_median, check_target_difficulty},
36+
tari_rx_vm_key_height,
3637
DifficultyCalculator,
3738
HeaderChainLinkedValidator,
3839
ValidationError,
@@ -80,7 +81,7 @@ impl<B: BlockchainBackend> HeaderChainLinkedValidator<B> for HeaderFullValidator
8081
check_timestamp_ftl(header, &self.rules)?;
8182
check_pow_data(header)?;
8283
let vm_key = *db
83-
.fetch_chain_header_by_height(header.height.saturating_sub(header.height % 2048).saturating_sub(64))?
84+
.fetch_chain_header_by_height(tari_rx_vm_key_height(header.height))?
8485
.hash();
8586

8687
let achieved_target = if let Some(target) = target_difficulty {

0 commit comments

Comments
 (0)