Skip to content

Commit

Permalink
Move nonce to first in sha hash
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Oct 5, 2022
1 parent 978ee8f commit b060fcd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
22 changes: 2 additions & 20 deletions applications/tari_miner/src/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::convert::TryInto;

use sha3::{Digest, Sha3_256};
use tari_core::proof_of_work::sha3_difficulty;
use tari_app_grpc::tari_rpc::BlockHeader as grpc_header;
use tari_core::{blocks::BlockHeader, large_ints::U256};
use tari_utilities::epoch_time::EpochTime;
Expand All @@ -34,7 +33,6 @@ pub type Difficulty = u64;
#[derive(Clone)]
pub struct BlockHeaderSha3 {
pub header: BlockHeader,
hash_merge_mining: Sha3_256,
pub hashes: u64,
}

Expand All @@ -43,29 +41,19 @@ impl BlockHeaderSha3 {
#[allow(clippy::cast_sign_loss)]
pub fn new(header: grpc_header) -> Result<Self, MinerError> {
let header: BlockHeader = header.try_into().map_err(MinerError::BlockHeader)?;

let hash_merge_mining = Sha3_256::new().chain(header.mining_hash());

Ok(Self {
hash_merge_mining,
header,
hashes: 0,
})
}

#[inline]
fn get_hash_before_nonce(&self) -> Sha3_256 {
self.hash_merge_mining.clone()
}

/// This function will update the timestamp of the header, but only if the new timestamp is greater than the current
/// one.
pub fn set_forward_timestamp(&mut self, timestamp: u64) {
// if the timestamp has been advanced by the base_node due to the median time we should not reverse it but we
// should only change the timestamp if we move it forward.
if timestamp > self.header.timestamp.as_u64() {
self.header.timestamp = EpochTime::from(timestamp);
self.hash_merge_mining = Sha3_256::new().chain(self.header.mining_hash());
}
}

Expand All @@ -82,13 +70,7 @@ impl BlockHeaderSha3 {
#[inline]
pub fn difficulty(&mut self) -> Difficulty {
self.hashes = self.hashes.saturating_add(1);
let hash = self
.get_hash_before_nonce()
.chain(self.header.nonce.to_le_bytes())
.chain(self.header.pow.to_bytes())
.finalize();
let hash = Sha3_256::digest(&hash);
big_endian_difficulty(&hash)
sha3_difficulty(&self.header).into()
}

#[allow(clippy::cast_possible_wrap)]
Expand Down
27 changes: 26 additions & 1 deletion base_layer/core/src/consensus/consensus_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ impl ConsensusConstants {
let (input_version_range, output_version_range, kernel_version_range) = version_zero();
vec![ConsensusConstants {
effective_from_height: 0,
// Todo fix after test
coinbase_lock_height: 6,
blockchain_version: 0,
valid_blockchain_version_range: 0..=0,
Expand All @@ -519,6 +518,27 @@ impl ConsensusConstants {
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: 19000,
coinbase_lock_height: 6,
blockchain_version: 1,
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(),
Expand Down Expand Up @@ -653,6 +673,11 @@ impl ConsensusConstantsBuilder {
self
}

pub fn with_blockchain_version(mut self, version: u16) -> Self {
self.consensus.blockchain_version = version;
self
}

pub fn build(self) -> ConsensusConstants {
self.consensus
}
Expand Down
19 changes: 13 additions & 6 deletions base_layer/core/src/proof_of_work/sha3_pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ pub fn sha3_difficulty(header: &BlockHeader) -> Difficulty {
}

pub fn sha3_hash(header: &BlockHeader) -> Vec<u8> {
Sha3_256::new()
.chain(header.mining_hash())
.chain(header.nonce.to_le_bytes())
.chain(header.pow.to_bytes())
.finalize()
.to_vec()
let sha = Sha3_256::new();
match header.version {
0 => sha
.chain(header.mining_hash())
.chain(header.nonce.to_le_bytes())
.chain(header.pow.to_bytes()),
_ => sha
.chain(header.nonce.to_le_bytes())
.chain(header.mining_hash())
.chain(header.pow.to_bytes()),
}
.finalize()
.to_vec()
}

fn sha3_difficulty_with_hash(header: &BlockHeader) -> (Difficulty, Vec<u8>) {
Expand Down
1 change: 1 addition & 0 deletions base_layer/core/tests/block_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn test_monero_blocks() {
max_difficulty: 1.into(),
target_time: 200,
})
.with_blockchain_version(0)
.build();
let cm = ConsensusManager::builder(network).add_consensus_constants(cc).build();
let header_validator = HeaderValidator::new(cm.clone());
Expand Down

0 comments on commit b060fcd

Please sign in to comment.