Skip to content

Commit

Permalink
fix: remove unimplemented Blake pow algo variant (#3047)
Browse files Browse the repository at this point in the history
BREAKING CHANGE!: This is a DB and network breaking change.
  • Loading branch information
stringhandler committed Jul 4, 2021
2 parents 5116bde + 65b91c0 commit 347973e
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 37 deletions.
5 changes: 2 additions & 3 deletions applications/tari_app_grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ message ProofOfWork {
// 1 = Blake
uint64 pow_algo = 1;
// uint64 accumulated_monero_difficulty = 2;
// uint64 accumulated_blake_difficulty = 3;
// uint64 accumulated_sha_difficulty = 3;
bytes pow_data = 4;
// uint64 target_difficulty = 5;
}
Expand All @@ -80,8 +80,7 @@ message ProofOfWork {
message PowAlgo {
enum PowAlgos {
POW_ALGOS_MONERO = 0;
POW_ALGOS_BLAKE = 1;
POW_ALGOS_SHA3 = 2;
POW_ALGOS_SHA3 = 1;
}
PowAlgos pow_algo = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_base_node/src/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ impl CommandHandler {
.consensus_constants(height)
.get_difficulty_max_block_interval(pow_algo),
);
let acc_sha3 = header.accumulated_data().accumulated_blake_difficulty;
let acc_sha3 = header.accumulated_data().accumulated_sha_difficulty;
let acc_monero = header.accumulated_data().accumulated_monero_difficulty;

writeln!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<B: BlockchainBackend + 'static> BlockSynchronizer<B> {
.total_accumulated_difficulty
.to_formatted_string(&Locale::en),
block.accumulated_data().accumulated_monero_difficulty,
block.accumulated_data().accumulated_blake_difficulty,
block.accumulated_data().accumulated_sha_difficulty,
);
current_block = Some(block);
}
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/blocks/genesis_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn get_stibbons_genesis_block() -> ChainBlock {
achieved_difficulty: 1.into(),
total_accumulated_difficulty: 1,
accumulated_monero_difficulty: 1.into(),
accumulated_blake_difficulty: 1.into(),
accumulated_sha_difficulty: 1.into(),
target_difficulty: 1.into(),
};
// NOTE: Panic is impossible, accumulated_data is created from the block
Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn get_weatherwax_genesis_block() -> ChainBlock {
achieved_difficulty: 1.into(),
total_accumulated_difficulty: 1,
accumulated_monero_difficulty: 1.into(),
accumulated_blake_difficulty: 1.into(),
accumulated_sha_difficulty: 1.into(),
target_difficulty: 1.into(),
};
ChainBlock::try_construct(Arc::new(block), accumulated_data).unwrap()
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn get_ridcully_genesis_block() -> ChainBlock {
achieved_difficulty: 1.into(),
total_accumulated_difficulty: 1,
accumulated_monero_difficulty: 1.into(),
accumulated_blake_difficulty: 1.into(),
accumulated_sha_difficulty: 1.into(),
target_difficulty: 1.into(),
};
// NOTE: Panic is impossible, accumulated_data hash is set from block
Expand Down
18 changes: 9 additions & 9 deletions base_layer/core/src/chain_storage/accumulated_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,11 @@ impl BlockHeaderAccumulatedDataBuilder<'_> {
let (monero_diff, blake_diff) = match achieved_target.pow_algo() {
PowAlgorithm::Monero => (
previous_accum.accumulated_monero_difficulty + achieved_target.achieved(),
previous_accum.accumulated_blake_difficulty,
previous_accum.accumulated_sha_difficulty,
),
PowAlgorithm::Blake => unimplemented!(),
PowAlgorithm::Sha3 => (
previous_accum.accumulated_monero_difficulty,
previous_accum.accumulated_blake_difficulty + achieved_target.achieved(),
previous_accum.accumulated_sha_difficulty + achieved_target.achieved(),
),
};

Expand All @@ -259,15 +258,15 @@ impl BlockHeaderAccumulatedDataBuilder<'_> {
achieved_difficulty: achieved_target.achieved(),
total_accumulated_difficulty: monero_diff.as_u64() as u128 * blake_diff.as_u64() as u128,
accumulated_monero_difficulty: monero_diff,
accumulated_blake_difficulty: blake_diff,
accumulated_sha_difficulty: blake_diff,
target_difficulty: achieved_target.target(),
};
trace!(
target: LOG_TARGET,
"Calculated: Tot_acc_diff {}, Monero {}, SHA3 {}",
result.total_accumulated_difficulty.to_formatted_string(&Locale::en),
result.accumulated_monero_difficulty,
result.accumulated_blake_difficulty,
result.accumulated_sha_difficulty,
);
Ok(result)
}
Expand All @@ -280,11 +279,12 @@ pub struct BlockHeaderAccumulatedData {
pub total_kernel_offset: BlindingFactor,
pub achieved_difficulty: Difficulty,
pub total_accumulated_difficulty: u128,
/// The total accumulated difficulty for each proof of work algorithms for all blocks since Genesis,
/// The total accumulated difficulty for monero proof of work for all blocks since Genesis,
/// but not including this block, tracked separately.
pub accumulated_monero_difficulty: Difficulty,
// TODO: Rename #testnetreset
pub accumulated_blake_difficulty: Difficulty,
/// The total accumulated difficulty for SHA3 proof of work for all blocks since Genesis,
/// but not including this block, tracked separately.
pub accumulated_sha_difficulty: Difficulty,
/// The target difficulty for solving the current block using the specified proof of work algorithm.
pub target_difficulty: Difficulty,
}
Expand All @@ -305,7 +305,7 @@ impl Display for BlockHeaderAccumulatedData {
"Accumulated monero difficulty: {}",
self.accumulated_monero_difficulty
)?;
writeln!(f, "Accumulated sha3 difficulty: {}", self.accumulated_blake_difficulty)?;
writeln!(f, "Accumulated sha3 difficulty: {}", self.accumulated_sha_difficulty)?;
writeln!(f, "Target difficulty: {}", self.target_difficulty)?;
Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions base_layer/core/src/chain_storage/target_difficulties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ impl TargetDifficulties {
use PowAlgorithm::*;
match algo {
Monero => &self.monero,
// TODO: remove
Blake => unimplemented!(),
Sha3 => &self.sha3,
}
}
Expand All @@ -72,8 +70,6 @@ impl TargetDifficulties {
use PowAlgorithm::*;
match algo {
Monero => &mut self.monero,
// TODO: remove
Blake => unimplemented!(),
Sha3 => &mut self.sha3,
}
}
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/consensus/chain_strength_comparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub struct Sha3DifficultyComparer {}
impl ChainStrengthComparer for Sha3DifficultyComparer {
fn compare(&self, a: &ChainHeader, b: &ChainHeader) -> Ordering {
a.accumulated_data()
.accumulated_blake_difficulty
.cmp(&b.accumulated_data().accumulated_blake_difficulty)
.accumulated_sha_difficulty
.cmp(&b.accumulated_data().accumulated_sha_difficulty)
}
}

Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/proof_of_work/proof_of_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ impl Display for PowAlgorithm {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
let algo = match self {
PowAlgorithm::Monero => "Monero",
PowAlgorithm::Blake => "Blake",
PowAlgorithm::Sha3 => "Sha3",
};
fmt.write_str(&algo.to_string())
Expand Down Expand Up @@ -104,6 +103,6 @@ mod test {
pow_algo: PowAlgorithm::Sha3,
..Default::default()
};
assert_eq!(pow.to_bytes(), vec![2]);
assert_eq!(pow.to_bytes(), vec![1]);
}
}
6 changes: 2 additions & 4 deletions base_layer/core/src/proof_of_work/proof_of_work_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use std::convert::TryFrom;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Hash, Eq)]
pub enum PowAlgorithm {
Monero = 0,
// TODO: remove #testnetreset
Blake = 1,
Sha3 = 2,
Sha3 = 1,
}

impl PowAlgorithm {
Expand All @@ -51,7 +49,7 @@ impl TryFrom<u64> for PowAlgorithm {
fn try_from(v: u64) -> Result<Self, Self::Error> {
match v {
0 => Ok(PowAlgorithm::Monero),
2 => Ok(PowAlgorithm::Sha3),
1 => Ok(PowAlgorithm::Sha3),
_ => Err("Invalid PoWAlgorithm".into()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ message HistoricalBlock {
message BlockHeaderAccumulatedData {
uint64 achieved_difficulty = 1;
uint64 accumulated_monero_difficulty = 2;
uint64 accumulated_blake_difficulty = 3;
uint64 accumulated_sha_difficulty = 3;
uint64 target_difficulty = 4;
bytes total_kernel_offset = 5;
bytes hash = 6;
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/proto/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl From<BlockHeaderAccumulatedData> for proto::BlockHeaderAccumulatedData {
Self {
achieved_difficulty: source.achieved_difficulty.into(),
accumulated_monero_difficulty: source.accumulated_monero_difficulty.into(),
accumulated_blake_difficulty: source.accumulated_blake_difficulty.into(),
accumulated_sha_difficulty: source.accumulated_sha_difficulty.into(),
target_difficulty: source.target_difficulty.into(),
total_kernel_offset: source.total_kernel_offset.to_vec(),
hash: source.hash,
Expand All @@ -135,7 +135,7 @@ impl TryFrom<proto::BlockHeaderAccumulatedData> for BlockHeaderAccumulatedData {
achieved_difficulty: source.achieved_difficulty.into(),
total_accumulated_difficulty: accumulated_difficulty,
accumulated_monero_difficulty: source.accumulated_monero_difficulty.into(),
accumulated_blake_difficulty: source.accumulated_blake_difficulty.into(),
accumulated_sha_difficulty: source.accumulated_sha_difficulty.into(),
target_difficulty: source.target_difficulty.into(),
total_kernel_offset: BlindingFactor::from_bytes(source.total_kernel_offset.as_slice())
.map_err(|err| format!("Invalid value for total_kernel_offset: {}", err))?,
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub fn check_pow_data<B: BlockchainBackend>(

Ok(())
},
Blake | Sha3 => {
Sha3 => {
if !block_header.pow.pow_data.is_empty() {
return Err(ValidationError::CustomError(
"Proof of work data must be empty for Sha3 blocks".to_string(),
Expand All @@ -157,7 +157,6 @@ pub fn check_target_difficulty(
) -> Result<AchievedTargetDifficulty, ValidationError> {
let achieved = match block_header.pow_algo() {
PowAlgorithm::Monero => monero_difficulty(block_header, randomx_factory)?,
PowAlgorithm::Blake => unimplemented!(),
PowAlgorithm::Sha3 => sha3_difficulty(block_header),
};

Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/tests/helpers/block_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub fn create_genesis_block_with_coinbase_value(
achieved_difficulty: 1.into(),
total_accumulated_difficulty: 1,
accumulated_monero_difficulty: 1.into(),
accumulated_blake_difficulty: 1.into(),
accumulated_sha_difficulty: 1.into(),
target_difficulty: 1.into(),
})
.unwrap(),
Expand Down Expand Up @@ -225,7 +225,7 @@ pub fn create_genesis_block_with_utxos(
achieved_difficulty: 1.into(),
total_accumulated_difficulty: 1,
accumulated_monero_difficulty: 1.into(),
accumulated_blake_difficulty: 1.into(),
accumulated_sha_difficulty: 1.into(),
target_difficulty: 1.into(),
})
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/helpers/baseNodeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class BaseNodeClient {
toLittleEndian(parseInt(header.pow.accumulated_monero_difficulty), 64)
);
hash.update(
toLittleEndian(parseInt(header.pow.accumulated_blake_difficulty), 64)
toLittleEndian(parseInt(header.pow.accumulated_sha_difficulty), 64)
);
hash.update(header.pow.pow_data);
const first_round = hash.digest();
Expand Down

0 comments on commit 347973e

Please sign in to comment.