Skip to content

Commit

Permalink
- 10bps reverted
Browse files Browse the repository at this point in the history
- PoW Hash updated (suggested by lolminer)
- Sync with latest private changes
  • Loading branch information
Pyrinpyi committed Aug 30, 2024
1 parent 7783b2a commit 198994a
Show file tree
Hide file tree
Showing 28 changed files with 291 additions and 163 deletions.
116 changes: 58 additions & 58 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ members = [

[workspace.package]
rust-version = "1.78.0"
version = "0.14.2"
version = "0.14.5"
authors = ["Pyrin developers", "Kaspa developers"]
license = "ISC"
repository = "https://github.com/Pyrinpyi/pyrin"
Expand Down
26 changes: 2 additions & 24 deletions consensus/core/src/config/bps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl<const BPS: u64> Bps<BPS> {
/// Returns the GHOSTDAG K value which was pre-computed for this BPS
/// (see [`calculate_ghostdag_k`] and [`gen_ghostdag_table`] for the full calculation)
#[rustfmt::skip]
#[inline(always)]
pub const fn ghostdag_k() -> KType {
match BPS {
1 => 18, 2 => 31, 3 => 43, 4 => 55, 5 => 67, 6 => 79, 7 => 90, 8 => 102, 9 => 113, 10 => 124,
Expand All @@ -46,7 +45,6 @@ impl<const BPS: u64> Bps<BPS> {
}
}

#[inline(always)]
/// Returns the target time per block in milliseconds
pub const fn target_time_per_block() -> u64 {
if 1000 % BPS != 0 {
Expand All @@ -55,7 +53,6 @@ impl<const BPS: u64> Bps<BPS> {
1000 / BPS
}

#[inline(always)]
/// Returns the max number of direct parents a block can have
pub const fn max_block_parents() -> u8 {
let val = (Self::ghostdag_k() / 2) as u8;
Expand All @@ -75,7 +72,6 @@ impl<const BPS: u64> Bps<BPS> {
}
}

#[inline(always)]
pub const fn mergeset_size_limit() -> u64 {
let val = Self::ghostdag_k() as u64 * 2;
if val < 180 {
Expand All @@ -89,70 +85,53 @@ impl<const BPS: u64> Bps<BPS> {
}
}

#[inline(always)]
pub const fn merge_depth_bound() -> u64 {
BPS * MERGE_DEPTH_DURATION
}

#[inline(always)]
pub const fn finality_depth() -> u64 {
BPS * NEW_FINALITY_DURATION
}

#[inline(always)]
/// Limit used to previously calculate the pruning depth.
const fn prev_mergeset_size_limit() -> u64 {
Self::ghostdag_k() as u64 * 10
}

#[inline(always)]
pub const fn pruning_depth() -> u64 {
// Based on the analysis at https://github.com/kaspanet/docs/blob/main/Reference/prunality/Prunality.pdf
// and on the decomposition of merge depth (rule R-I therein) from finality depth (φ)
// We add an additional merge depth unit as a safety margin for anticone finalization
let pruning_depth: u64 = Self::finality_depth()
Self::finality_depth()
+ Self::merge_depth_bound() * 2
+ 4 * Self::prev_mergeset_size_limit() * Self::ghostdag_k() as u64
+ 2 * Self::ghostdag_k() as u64
+ 2;

+ 2

// TODO (HF or restart of TN11):
// Return `Self::finality_depth() * 3` and assert that this value is equal or larger than the above expression.
// This will give us a round easy number to track which is not sensitive to minor changes in other related params.
let v = Self::finality_depth() * 3;

if v > pruning_depth {
v
} else {
pruning_depth
}
}

#[inline(always)]
pub const fn pruning_proof_m() -> u64 {
// No need to scale this constant with BPS since the important block levels (higher) remain logarithmically short
PRUNING_PROOF_M
}

#[inline(always)]
/// Sample rate for sampling blocks to the median time window (in block units, hence dependent on BPS)
pub const fn past_median_time_sample_rate() -> u64 {
BPS * PAST_MEDIAN_TIME_SAMPLE_INTERVAL
}

#[inline(always)]
/// Sample rate for sampling blocks to the DA window (in block units, hence dependent on BPS)
pub const fn difficulty_adjustment_sample_rate() -> u64 {
BPS * DIFFICULTY_WINDOW_SAMPLE_INTERVAL
}

#[inline(always)]
pub const fn coinbase_maturity() -> u64 {
BPS * LEGACY_COINBASE_MATURITY
}

#[inline(always)]
/// DAA score after which the pre-deflationary period switches to the deflationary period.
///
/// This number is calculated as follows:
Expand All @@ -165,7 +144,6 @@ impl<const BPS: u64> Bps<BPS> {
BPS * (15778800 - 259200)
}

#[inline(always)]
pub const fn pre_deflationary_phase_base_subsidy() -> u64 {
1700000000 / BPS
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/core/src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub mod perf {
impl PerfParams {
pub fn adjust_to_consensus_params(&mut self, consensus_params: &Params) {
// Allow caching up to 10x over the baseline
self.block_data_cache_size *= consensus_params.bps().clamp(10, 100) as usize;
self.block_data_cache_size *= consensus_params.bps().clamp(1, 10) as usize;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions consensus/core/src/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ impl From<(&Header, &'static [u8])> for GenesisBlock {
/// The genesis block of the block-DAG which serves as the public transaction ledger for the main network.
pub const GENESIS: GenesisBlock = GenesisBlock {
hash: Hash::from_bytes([
0x6c, 0xd4, 0x06, 0x81, 0x2f, 0xb1, 0x82, 0xd5, 0x6c, 0x0b, 0x70, 0x65, 0x65, 0x26, 0x66, 0x2e, 0x6e, 0x11, 0x40, 0x90, 0x43,
0x85, 0xab, 0x6a, 0x43, 0xdf, 0xaf, 0x0e, 0xed, 0xa6, 0x11, 0xf1,
0xdb, 0x28, 0xf9, 0x36, 0x55, 0xe5, 0x79, 0xb3, 0xbb, 0xc2, 0x76, 0x43, 0x37, 0x11, 0xcd, 0x21, 0x4b, 0x39, 0x7a, 0xc8, 0x46,
0x74, 0x01, 0xcb, 0x82, 0xb2, 0x8f, 0x65, 0x5d, 0x25, 0xf7, 0x62,
]),
version: 0,
hash_merkle_root: Hash::from_bytes([
0xb0, 0x49, 0x41, 0x6e, 0x64, 0x06, 0x1a, 0x0d, 0xd7, 0x61, 0xc1, 0xff, 0x02, 0x6b, 0x14, 0xf8, 0xdf, 0x0a, 0x7e, 0xbd, 0x23,
0x0a, 0x56, 0x1f, 0x30, 0xa5, 0xd0, 0x33, 0x2e, 0xc9, 0xeb, 0xd5,
0x4e, 0x44, 0xc5, 0x8e, 0xb8, 0xc2, 0x97, 0x62, 0x2a, 0x7b, 0x0a, 0x4b, 0xe3, 0x30, 0x62, 0x47, 0xa1, 0xeb, 0xef, 0x7b, 0x93,
0xea, 0xbd, 0xa7, 0x00, 0x5b, 0x53, 0xbc, 0x0f, 0x21, 0x7a, 0xfd,
]),
utxo_commitment: Blake2Hash::from_bytes([
0xe9, 0xb3, 0xab, 0x4c, 0xcc, 0x51, 0xb1, 0x92, 0x5d, 0xe4, 0x5f, 0x1a, 0xf0, 0x19, 0xb4, 0xda, 0xe0, 0x0e, 0x34, 0x33, 0x1e,
0x7d, 0xb8, 0xdc, 0x2c, 0x35, 0xb0, 0xfd, 0x48, 0xe7, 0x54, 0x38
0xd4, 0xf5, 0x74, 0x51, 0x55, 0xb1, 0x20, 0x28, 0xda, 0xeb, 0x90, 0xdb, 0x04, 0xd8, 0xfe, 0x55, 0x22, 0x5b, 0xc7, 0xab, 0x60,
0x99, 0x57, 0xaf, 0xa7, 0xe4, 0x3f, 0x2b, 0xa1, 0x70, 0xb7, 0x74,
]),
timestamp: 1724758844759,
timestamp: 1724969400000,
bits: 486652985,
nonce: 0x4e616f72,
daa_score: 23_917_744, // Checkpoint DAA score
daa_score: 25_883_292, // Checkpoint DAA score
coinbase_payload: &[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Blue score
0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, // Subsidy
Expand All @@ -107,10 +107,10 @@ pub const GENESIS: GenesisBlock = GenesisBlock {
0x00, 0x02, 0x08, 0x58, 0x0c, 0x63, 0x1e, 0x5c,
0xa5, 0x2b, 0x49, 0x09, 0x8a, 0x30, 0x13, 0x41,
0xa5, 0xc7, 0x54, 0xa0, 0xfb, 0x4c, 0xef, 0x20,
0xe9, 0xb3, 0xab, 0x4c, 0xcc, 0x51, 0xb1, 0x92, // UTXO
0x5d, 0xe4, 0x5f, 0x1a, 0xf0, 0x19, 0xb4, 0xda,
0xe0, 0x0e, 0x34, 0x33, 0x1e, 0x7d, 0xb8, 0xdc,
0x2c, 0x35, 0xb0, 0xfd, 0x48, 0xe7, 0x54, 0x38
0xd4, 0xf5, 0x74, 0x51, 0x55, 0xb1, 0x20, 0x28, // UTXO
0xda, 0xeb, 0x90, 0xdb, 0x04, 0xd8, 0xfe, 0x55,
0x22, 0x5b, 0xc7, 0xab, 0x60, 0x99, 0x57, 0xaf,
0xa7, 0xe4, 0x3f, 0x2b, 0xa1, 0x70, 0xb7, 0x74,
],
};

Expand Down
38 changes: 19 additions & 19 deletions consensus/core/src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Params {
// with significant testnet hashrate does not overwhelm the network with deep side-DAGs.
//
// We use DAA duration as baseline and scale it down with BPS (and divide by 3 for mining only when very close to current time on TN11)
let max_expected_duration_without_blocks_in_milliseconds = self.target_time_per_block * self.bps() * NEW_DIFFICULTY_WINDOW_DURATION / 3; // = DAA duration in milliseconds / bps / 3
let max_expected_duration_without_blocks_in_milliseconds = self.target_time_per_block * NEW_DIFFICULTY_WINDOW_DURATION / 3; // = DAA duration in milliseconds / bps / 3
unix_now() < sink_timestamp + max_expected_duration_without_blocks_in_milliseconds
}
}
Expand Down Expand Up @@ -293,31 +293,31 @@ impl From<NetworkId> for Params {

pub const MAINNET_PARAMS: Params = Params {
dns_seeders: &[
"seeder01-mainnet.pyrin.network",
"seeder02-mainnet.pyrin.network",
"seeder03-mainnet.pyrin.network",
"seeder04-mainnet.pyrin.network",
"seeder1-mainnet.pyrin.network",
"seeder2-mainnet.pyrin.network",
"seeder3-mainnet.pyrin.network",
"seeder4-mainnet.pyrin.network",
],
net: NetworkId::new(NetworkType::Mainnet),
genesis: GENESIS,
ghostdag_k: Bps::<10>::ghostdag_k(),
ghostdag_k: LEGACY_DEFAULT_GHOSTDAG_K,
legacy_timestamp_deviation_tolerance: LEGACY_TIMESTAMP_DEVIATION_TOLERANCE,
new_timestamp_deviation_tolerance: NEW_TIMESTAMP_DEVIATION_TOLERANCE,
past_median_time_sample_rate: Bps::<10>::past_median_time_sample_rate(),
past_median_time_sample_rate: Bps::<1>::past_median_time_sample_rate(),
past_median_time_sampled_window_size: MEDIAN_TIME_SAMPLED_WINDOW_SIZE,
target_time_per_block: Bps::<10>::target_time_per_block(),
target_time_per_block: 1000,
sampling_activation_daa_score: u64::MAX,
max_difficulty_target: MAX_DIFFICULTY_TARGET,
max_difficulty_target_f64: MAX_DIFFICULTY_TARGET_AS_F64,
difficulty_sample_rate: Bps::<10>::difficulty_adjustment_sample_rate(),
difficulty_sample_rate: Bps::<1>::difficulty_adjustment_sample_rate(),
sampled_difficulty_window_size: DIFFICULTY_SAMPLED_WINDOW_SIZE as usize,
legacy_difficulty_window_size: LEGACY_DIFFICULTY_WINDOW_SIZE,
min_difficulty_window_len: MIN_DIFFICULTY_WINDOW_LEN,
max_block_parents: Bps::<10>::max_block_parents(),
mergeset_size_limit: Bps::<10>::mergeset_size_limit(),
merge_depth: Bps::<10>::merge_depth_bound(),
finality_depth: Bps::<10>::finality_depth(),
pruning_depth: Bps::<10>::pruning_depth(),
max_block_parents: 10,
mergeset_size_limit: (LEGACY_DEFAULT_GHOSTDAG_K as u64) * 10,
merge_depth: 3600,
finality_depth: 86400,
pruning_depth: 185798,
coinbase_payload_script_public_key_max_len: 150,
max_coinbase_payload_len: 204,

Expand All @@ -344,14 +344,14 @@ pub const MAINNET_PARAMS: Params = Params {
// Half a year in seconds = 365.25 / 2 * 24 * 60 * 60 = 15778800
// The network was down for three days shortly after launch
// Three days in seconds = 3 * 24 * 60 * 60 = 259200
deflationary_phase_daa_score: Bps::<10>::deflationary_phase_daa_score(),
pre_deflationary_phase_base_subsidy: Bps::<10>::pre_deflationary_phase_base_subsidy(),
coinbase_maturity: Bps::<10>::coinbase_maturity(),
deflationary_phase_daa_score: 15778800 - 259200,
pre_deflationary_phase_base_subsidy: 1700000000,
coinbase_maturity: 100,
skip_proof_of_work: false,
max_block_level: 225,
pruning_proof_m: Bps::<10>::pruning_proof_m(),
pruning_proof_m: 1000,

hf_relaunch_daa_score: 23_917_744, // Checkpoint DAA score
hf_relaunch_daa_score: 27_037_930, // Hardfork at GMT Thursday, September 12, 2024 8:00:00 PM
};

pub const TESTNET_PARAMS: Params = Params {
Expand Down
2 changes: 1 addition & 1 deletion consensus/pow/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn bench_pow(c: &mut Criterion) {
c.bench_function("HeavyHash", |b| {
b.iter(|| {
for (matrix, &hash) in matrices.iter().zip(hashes.iter()) {
black_box(matrix.heavy_hash(hash));
black_box(matrix.heavy_hash(hash, false));
}
});
});
Expand Down
11 changes: 6 additions & 5 deletions consensus/pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::cmp::max;

use crate::matrix::Matrix;
use kaspa_consensus_core::{hashing, header::Header, BlockLevel};
use kaspa_consensus_core::config::params::MAINNET_PARAMS;
use kaspa_hashes::PowHash;
use kaspa_math::Uint256;

Expand Down Expand Up @@ -37,17 +38,17 @@ impl State {
#[inline]
#[must_use]
/// PRE_POW_HASH || TIME || 32 zero byte padding || NONCE
pub fn calculate_pow(&self, nonce: u64) -> Uint256 {
pub fn calculate_pow(&self, nonce: u64, algo_updated: bool) -> Uint256 {
// Hasher already contains PRE_POW_HASH || TIME || 32 zero byte padding; so only the NONCE is missing
let hash = self.hasher.clone().finalize_with_nonce(nonce);
let hash = self.matrix.heavy_hash(hash);
let hash = self.matrix.heavy_hash(hash, algo_updated);
Uint256::from_le_bytes(hash.as_bytes())
}

#[inline]
#[must_use]
pub fn check_pow(&self, nonce: u64) -> (bool, Uint256) {
let pow = self.calculate_pow(nonce);
pub fn check_pow(&self, nonce: u64, algo_updated: bool) -> (bool, Uint256) {
let pow = self.calculate_pow(nonce, algo_updated);
// The pow hash must be less or equal than the claimed target.
(pow <= self.target, pow)
}
Expand All @@ -59,7 +60,7 @@ pub fn calc_block_level(header: &Header, max_block_level: BlockLevel) -> BlockLe
}

let state = State::new(header);
let (_, pow) = state.check_pow(header.nonce);
let (_, pow) = state.check_pow(header.nonce, header.daa_score > MAINNET_PARAMS.hf_relaunch_daa_score);
let signed_block_level = max_block_level as i64 - pow.bits() as i64;
max(signed_block_level, 0) as BlockLevel
}
10 changes: 7 additions & 3 deletions consensus/pow/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Matrix {
rank
}

pub fn heavy_hash(&self, hash: Hash) -> Hash {
pub fn heavy_hash(&self, hash: Hash, algo_updated: bool) -> Hash {
// SAFETY: An uninitialized MaybrUninit is always safe.
let mut vec: [MaybeUninit<u8>; 64] = unsafe { MaybeUninit::uninit().assume_init() };
for (i, element) in hash.as_bytes().into_iter().enumerate() {
Expand All @@ -116,7 +116,11 @@ impl Matrix {
sum1 += self.0[2 * i][j] * (elem as u16);
sum2 += self.0[2 * i + 1][j] * (elem as u16);
}
((sum1 >> 10) << 4) as u8 | (sum2 >> 10) as u8
if algo_updated {
(((sum1 & 0xF) ^ ((sum1 >> 4) & 0xF) ^ ((sum1 >> 8) & 0xF)) << 4) as u8 | ((sum2 & 0xF) ^ ((sum2 >> 4) & 0xF) ^ ((sum2 >> 8) & 0xF)) as u8
} else {
((sum1 >> 10) << 4) as u8 | (sum2 >> 10) as u8
}
});

// Concatenate 4 LSBs back to 8 bit xor with sum1
Expand Down Expand Up @@ -238,7 +242,7 @@ mod tests {
82, 46, 212, 218, 28, 192, 143, 92, 213, 66, 86, 63, 245, 241, 155, 189, 73, 159, 229, 180, 202, 105, 159, 166, 109, 172,
128, 136, 169, 195, 97, 41,
]);
assert_eq!(test_matrix.heavy_hash(hash), expected_hash);
assert_eq!(test_matrix.heavy_hash(hash, false), expected_hash);
}
#[test]
fn test_generate_matrix() {
Expand Down
2 changes: 1 addition & 1 deletion consensus/pow/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl State {
#[wasm_bindgen(js_name=checkPow)]
pub fn check_pow(&self, nonce_jsv: JsValue) -> Result<js_sys::Array> {
let nonce = nonce_jsv.try_as_u64()?;
let (c, v) = self.inner.check_pow(nonce);
let (c, v) = self.inner.check_pow(nonce, false); // Won't be activated
let array = js_sys::Array::new();
array.push(&JsValue::from(c));
array.push(&v.to_bigint().map_err(|err| Error::Custom(format!("{err:?}")))?.into());
Expand Down
4 changes: 3 additions & 1 deletion consensus/src/consensus/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ static UTXO_DUMP_3: &[u8] = include_bytes!("utxo_data.zip.003");
static UTXO_DUMP_4: &[u8] = include_bytes!("utxo_data.zip.004");
static UTXO_DUMP_5: &[u8] = include_bytes!("utxo_data.zip.005");
static UTXO_DUMP_6: &[u8] = include_bytes!("utxo_data.zip.006");
static UTXO_DUMP_7: &[u8] = include_bytes!("utxo_data.zip.007");


fn load_utxo_dump() -> UtxoCollection {
Expand Down Expand Up @@ -459,7 +460,8 @@ fn load_utxo_dump() -> UtxoCollection {
fn read_binary_from_zip_chunks() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let chunks = [
UTXO_DUMP_1, UTXO_DUMP_2, UTXO_DUMP_3,
UTXO_DUMP_4, UTXO_DUMP_5, UTXO_DUMP_6
UTXO_DUMP_4, UTXO_DUMP_5, UTXO_DUMP_6,
UTXO_DUMP_7
];

let total_size: usize = chunks.iter().map(|chunk| chunk.len()).sum();
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/consensus/utxo_data.zip.001
Git LFS file not shown
2 changes: 1 addition & 1 deletion consensus/src/consensus/utxo_data.zip.002
Git LFS file not shown
2 changes: 1 addition & 1 deletion consensus/src/consensus/utxo_data.zip.003
Git LFS file not shown
2 changes: 1 addition & 1 deletion consensus/src/consensus/utxo_data.zip.004
Git LFS file not shown
2 changes: 1 addition & 1 deletion consensus/src/consensus/utxo_data.zip.005
Git LFS file not shown
4 changes: 2 additions & 2 deletions consensus/src/consensus/utxo_data.zip.006
Git LFS file not shown
3 changes: 3 additions & 0 deletions consensus/src/consensus/utxo_data.zip.007
Git LFS file not shown
Loading

0 comments on commit 198994a

Please sign in to comment.