Skip to content

Commit 382dd28

Browse files
committed
allow talking to older nodes
1 parent b6029f8 commit 382dd28

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

base_layer/core/src/base_node/proto/chain_metadata.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ message ChainMetadata {
1212
uint64 best_block_height = 1;
1313
// The block hash of the current tip of the longest valid chain, or `None` for an empty chain
1414
bytes best_block_hash = 2;
15-
// The current geometric mean of the pow of the chain tip, or `None` if there is no chain
16-
bytes accumulated_difficulty = 5;
15+
// 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
16+
bytes accumulated_difficulty_low = 5;
17+
bytes accumulated_difficulty_high = 8;
1718
// The effective height of the pruning horizon. This indicates from what height
1819
// a full block can be provided (exclusive).
1920
// If `pruned_height` is equal to the `best_block_height` no blocks can be provided.

base_layer/core/src/base_node/proto/chain_metadata.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,29 @@ use tari_common_types::{chain_metadata::ChainMetadata, types::FixedHash};
2727

2828
use crate::proto::base_node as proto;
2929

30-
const ACCUMULATED_DIFFICULTY_BYTE_SIZE: usize = 64;
30+
const ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256: usize = 32;
31+
const ACCUMULATED_DIFFICULTY_BYTE_SIZE_U512: usize = 64;
3132
impl TryFrom<proto::ChainMetadata> for ChainMetadata {
3233
type Error = String;
3334

3435
fn try_from(metadata: proto::ChainMetadata) -> Result<Self, Self::Error> {
35-
if metadata.accumulated_difficulty.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE {
36+
if metadata.accumulated_difficulty_low.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256 {
3637
return Err(format!(
37-
"Invalid accumulated difficulty byte length. {} was expected but the actual length was {}",
38-
ACCUMULATED_DIFFICULTY_BYTE_SIZE,
39-
metadata.accumulated_difficulty.len()
38+
"Invalid accumulated difficulty low byte length. {} was expected but the actual length was {}",
39+
ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256,
40+
metadata.accumulated_difficulty_low.len()
4041
));
4142
}
42-
43-
let accumulated_difficulty = U512::from_big_endian(&metadata.accumulated_difficulty);
43+
if metadata.accumulated_difficulty_high.len() > ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256 {
44+
return Err(format!(
45+
"Invalid accumulated difficulty byte high length. {} was expected but the actual length was {}",
46+
ACCUMULATED_DIFFICULTY_BYTE_SIZE_U256,
47+
metadata.accumulated_difficulty_high.len()
48+
));
49+
}
50+
let mut bytes = metadata.accumulated_difficulty_low.to_vec();
51+
bytes.extend_from_slice(&metadata.accumulated_difficulty_high);
52+
let accumulated_difficulty = U512::from_big_endian(&bytes);
4453
let best_block_height = metadata.best_block_height;
4554

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

7281
impl From<ChainMetadata> for proto::ChainMetadata {
7382
fn from(metadata: ChainMetadata) -> Self {
74-
let mut accumulated_difficulty = [0u8; ACCUMULATED_DIFFICULTY_BYTE_SIZE];
83+
let mut accumulated_difficulty = [0u8; ACCUMULATED_DIFFICULTY_BYTE_SIZE_U512];
7584
metadata
7685
.accumulated_difficulty()
7786
.to_big_endian(&mut accumulated_difficulty);
7887
Self {
7988
best_block_height: metadata.best_block_height(),
8089
best_block_hash: metadata.best_block_hash().to_vec(),
8190
pruned_height: metadata.pruned_height(),
82-
accumulated_difficulty: accumulated_difficulty.to_vec(),
91+
accumulated_difficulty_low: accumulated_difficulty[0..32].to_vec(),
92+
accumulated_difficulty_high: accumulated_difficulty[33..64].to_vec(),
8393
timestamp: metadata.timestamp(),
8494
}
8595
}

0 commit comments

Comments
 (0)