Skip to content

Commit

Permalink
feat: update to version for PoW (#4875)
Browse files Browse the repository at this point in the history
Description
---
Updates the header version to change the PoW so we don't have to reset the chain

Motivation and Context
---
See: #4862 

How Has This Been Tested?
---
  • Loading branch information
SWvheerden committed Nov 3, 2022
1 parent d109371 commit 889118b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 72 deletions.
109 changes: 43 additions & 66 deletions base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,73 +512,50 @@ impl ConsensusConstants {
features: OutputFeaturesVersion::V0..=OutputFeaturesVersion::V0,
opcode: OpcodeVersion::V0..=OpcodeVersion::V1,
};
let consensus_constants_1 = ConsensusConstants {
effective_from_height: 0,
coinbase_lock_height: 6,
blockchain_version: 0,
valid_blockchain_version_range: 0..=0,
future_time_limit: 540,
difficulty_block_window: 90,
max_block_transaction_weight: 127_795,
median_timestamp_count: 11,
emission_initial: 18_462_816_327 * uT,
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
proof_of_work: algos,
faucet_value: (10 * 4000) * T,
transaction_weight: TransactionWeight::v1(),
max_script_byte_size: 2048,
input_version_range,
output_version_range,
kernel_version_range,
permitted_output_types: Self::current_permitted_output_types(),
};
let consensus_constants_2 = ConsensusConstants {
effective_from_height: 23000,
blockchain_version: 1,
valid_blockchain_version_range: 0..=1,
..consensus_constants_1.clone()
};
let consensus_constants_3 = ConsensusConstants {
effective_from_height: 25000,
output_version_range: output_version_2_range,
..consensus_constants_2.clone()
};
let consensus_constants_4 = ConsensusConstants {
effective_from_height: 33000,
blockchain_version: 2,
..consensus_constants_3.clone()
};

vec![
ConsensusConstants {
effective_from_height: 0,
coinbase_lock_height: 6,
blockchain_version: 0,
valid_blockchain_version_range: 0..=0,
future_time_limit: 540,
difficulty_block_window: 90,
max_block_transaction_weight: 127_795,
median_timestamp_count: 11,
emission_initial: 18_462_816_327 * uT,
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
proof_of_work: algos.clone(),
faucet_value: (10 * 4000) * T,
transaction_weight: TransactionWeight::v1(),
max_script_byte_size: 2048,
input_version_range: input_version_range.clone(),
output_version_range: output_version_range.clone(),
kernel_version_range: kernel_version_range.clone(),
permitted_output_types: Self::current_permitted_output_types(),
},
ConsensusConstants {
effective_from_height: 23000,
coinbase_lock_height: 6,
blockchain_version: 1,
valid_blockchain_version_range: 0..=1,
future_time_limit: 540,
difficulty_block_window: 90,
max_block_transaction_weight: 127_795,
median_timestamp_count: 11,
emission_initial: 18_462_816_327 * uT,
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
proof_of_work: algos.clone(),
faucet_value: (10 * 4000) * T,
transaction_weight: TransactionWeight::v1(),
max_script_byte_size: 2048,
input_version_range: input_version_range.clone(),
output_version_range,
kernel_version_range: kernel_version_range.clone(),
permitted_output_types: Self::current_permitted_output_types(),
},
ConsensusConstants {
effective_from_height: 25000,
coinbase_lock_height: 6,
blockchain_version: 1,
valid_blockchain_version_range: 0..=1,
future_time_limit: 540,
difficulty_block_window: 90,
max_block_transaction_weight: 127_795,
median_timestamp_count: 11,
emission_initial: 18_462_816_327 * uT,
emission_decay: &ESMERALDA_DECAY_PARAMS,
emission_tail: 800 * T,
max_randomx_seed_height: 3000,
proof_of_work: algos,
faucet_value: (10 * 4000) * T,
transaction_weight: TransactionWeight::v1(),
max_script_byte_size: 2048,
input_version_range,
output_version_range: output_version_2_range,
kernel_version_range,
permitted_output_types: Self::current_permitted_output_types(),
},
consensus_constants_1,
consensus_constants_2,
consensus_constants_3,
consensus_constants_4,
]
}

Expand Down
18 changes: 14 additions & 4 deletions base_layer/core/src/proof_of_work/sha3_pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use crate::{
/// Mining using this CPU version of the algorithm is unlikely to be profitable, but is included for reference and
/// can be used to mine tXTR on testnets.
pub fn sha3x_difficulty(header: &BlockHeader) -> Difficulty {
sha3x_difficulty_with_hash(header).0
match header.version {
2 => sha3x_difficulty_with_hash(header).0,
_ => old_sha3_difficulty_with_hash(header).0,
}
}

pub fn sha3_hash(header: &BlockHeader) -> Vec<u8> {
Expand All @@ -61,6 +64,13 @@ fn sha3x_difficulty_with_hash(header: &BlockHeader) -> (Difficulty, Vec<u8>) {
(difficulty, hash.to_vec())
}

fn old_sha3_difficulty_with_hash(header: &BlockHeader) -> (Difficulty, Vec<u8>) {
let hash = sha3_hash(header);
let hash = Sha3_256::digest(&hash);
let difficulty = big_endian_difficulty(&hash);
(difficulty, hash.to_vec())
}

#[cfg(test)]
pub mod test {
use chrono::{DateTime, NaiveDate, Utc};
Expand All @@ -84,7 +94,7 @@ pub mod test {
}

pub fn get_header() -> BlockHeader {
let mut header = BlockHeader::new(0);
let mut header = BlockHeader::new(2);

#[allow(clippy::cast_sign_loss)]
let epoch_secs =
Expand All @@ -97,7 +107,7 @@ pub mod test {
#[test]
fn validate_max_target() {
let mut header = get_header();
header.nonce = 1;
assert_eq!(sha3x_difficulty(&header), Difficulty::from(3));
header.nonce = 14;
assert_eq!(sha3x_difficulty(&header), Difficulty::from(25));
}
}
4 changes: 2 additions & 2 deletions base_layer/tari_mining_helper_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ mod tests {

#[test]
fn detect_change_in_consensus_encoding() {
const NONCE: u64 = 7520709748303934033;
const DIFFICULTY: Difficulty = Difficulty::from_u64(2187);
const NONCE: u64 = 1368783905506569398;
const DIFFICULTY: Difficulty = Difficulty::from_u64(3549);
unsafe {
let mut error = -1;
let error_ptr = &mut error as *mut c_int;
Expand Down

0 comments on commit 889118b

Please sign in to comment.