Skip to content

Commit

Permalink
Hard fork tryout: better PoW encapsulation in block header (mimblewim…
Browse files Browse the repository at this point in the history
…ble#1478)

* Improve encapsulation with ProofOfWork struct
* Add dual pow scaling factor, fix test
* Fix pre_pow serialization, chain tests
* Adjust header serialized size calc
* Hard fork handling, version-based serialization
  • Loading branch information
ignopeverell authored Sep 10, 2018
1 parent 48857b7 commit ecf2060
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 128 deletions.
8 changes: 4 additions & 4 deletions api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,10 @@ impl BlockHeaderPrintable {
output_root: util::to_hex(h.output_root.to_vec()),
range_proof_root: util::to_hex(h.range_proof_root.to_vec()),
kernel_root: util::to_hex(h.kernel_root.to_vec()),
nonce: h.nonce,
cuckoo_size: h.pow.cuckoo_sizeshift,
cuckoo_solution: h.pow.nonces.clone(),
total_difficulty: h.total_difficulty.to_num(),
nonce: h.pow.nonce,
cuckoo_size: h.pow.cuckoo_sizeshift(),
cuckoo_solution: h.pow.proof.nonces.clone(),
total_difficulty: h.pow.total_difficulty.to_num(),
total_kernel_offset: h.total_kernel_offset.to_hex(),
}
}
Expand Down
10 changes: 5 additions & 5 deletions chain/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E
}

if !ctx.opts.contains(Options::SKIP_POW) {
if global::min_sizeshift() > header.pow.cuckoo_sizeshift {
if global::min_sizeshift() > header.pow.cuckoo_sizeshift() {
return Err(ErrorKind::LowSizeshift.into());
}
if !(ctx.pow_verifier)(header, header.pow.cuckoo_sizeshift) {
if !(ctx.pow_verifier)(header, header.pow.cuckoo_sizeshift()) {
error!(
LOGGER,
"pipe: validate_header failed for cuckoo shift size {}",
header.pow.cuckoo_sizeshift,
header.pow.cuckoo_sizeshift()
);
return Err(ErrorKind::InvalidPow.into());
}
Expand Down Expand Up @@ -436,11 +436,11 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E
// check the pow hash shows a difficulty at least as large
// as the target difficulty
if !ctx.opts.contains(Options::SKIP_POW) {
if header.total_difficulty.clone() <= prev.total_difficulty.clone() {
if header.total_difficulty() <= prev.total_difficulty() {
return Err(ErrorKind::DifficultyTooLow.into());
}

let target_difficulty = header.total_difficulty.clone() - prev.total_difficulty.clone();
let target_difficulty = header.total_difficulty() - prev.total_difficulty();

if header.pow.to_difficulty() < target_difficulty {
return Err(ErrorKind::DifficultyTooLow.into());
Expand Down
4 changes: 2 additions & 2 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ impl Iterator for DifficultyIter {
let prev_difficulty = self
.prev_header
.clone()
.map_or(Difficulty::zero(), |x| x.total_difficulty);
let difficulty = header.total_difficulty - prev_difficulty;
.map_or(Difficulty::zero(), |x| x.total_difficulty());
let difficulty = header.total_difficulty() - prev_difficulty;

Some(Ok((header.timestamp.timestamp() as u64, difficulty)))
} else {
Expand Down
2 changes: 1 addition & 1 deletion chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Tip {
height: bh.height,
last_block_h: bh.hash(),
prev_block_h: bh.previous,
total_difficulty: bh.total_difficulty.clone(),
total_difficulty: bh.total_difficulty(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion chain/tests/data_file_integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,6 @@ fn _prepare_block_nosum(
Ok(b) => b,
};
b.header.timestamp = prev.timestamp + Duration::seconds(60);
b.header.total_difficulty = Difficulty::from_num(diff);
b.header.pow.total_difficulty = Difficulty::from_num(diff);
b
}
12 changes: 6 additions & 6 deletions chain/tests/mine_simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ fn mine_empty_chain() {
} else {
global::min_sizeshift()
};
b.header.pow.cuckoo_sizeshift = sizeshift;
b.header.pow.proof.cuckoo_sizeshift = sizeshift;
pow::pow_size(&mut b.header, difficulty, global::proofsize(), sizeshift).unwrap();
b.header.pow.cuckoo_sizeshift = sizeshift;
b.header.pow.proof.cuckoo_sizeshift = sizeshift;

let bhash = b.hash();
chain.process_block(b, chain::Options::MINE).unwrap();
Expand Down Expand Up @@ -390,9 +390,9 @@ fn output_header_mappings() {
} else {
global::min_sizeshift()
};
b.header.pow.cuckoo_sizeshift = sizeshift;
b.header.pow.proof.cuckoo_sizeshift = sizeshift;
pow::pow_size(&mut b.header, difficulty, global::proofsize(), sizeshift).unwrap();
b.header.pow.cuckoo_sizeshift = sizeshift;
b.header.pow.proof.cuckoo_sizeshift = sizeshift;

chain.process_block(b, chain::Options::MINE).unwrap();

Expand Down Expand Up @@ -479,8 +479,8 @@ where
Ok(b) => b,
};
b.header.timestamp = prev.timestamp + Duration::seconds(60);
b.header.total_difficulty = prev.total_difficulty.clone() + Difficulty::from_num(diff);
b.header.pow = core::core::Proof::random(proof_size);
b.header.pow.total_difficulty = prev.total_difficulty() + Difficulty::from_num(diff);
b.header.pow.proof = core::core::Proof::random(proof_size);
b
}

Expand Down
22 changes: 12 additions & 10 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ pub const HARD_FORK_INTERVAL: u64 = 250_000;
/// 6 months interval scheduled hard forks for the first 2 years.
pub fn valid_header_version(height: u64, version: u16) -> bool {
// uncomment below as we go from hard fork to hard fork
if height <= HARD_FORK_INTERVAL && version == 1 {
true
/* } else if height <= 2 * HARD_FORK_INTERVAL && version == 2 {
true */
/* } else if height <= 3 * HARD_FORK_INTERVAL && version == 3 {
true */
/* } else if height <= 4 * HARD_FORK_INTERVAL && version == 4 {
true */
/* } else if height > 4 * HARD_FORK_INTERVAL && version > 4 {
true */
if height < HEADER_V2_HARD_FORK {
version == 1
} else if height < HARD_FORK_INTERVAL {
version == 2
} else if height < 2 * HARD_FORK_INTERVAL {
version == 3
/* } else if height < 3 * HARD_FORK_INTERVAL {
version == 4 */
/* } else if height >= 4 * HARD_FORK_INTERVAL {
version > 4 */
} else {
false
}
Expand Down Expand Up @@ -249,3 +249,5 @@ pub trait VerifySortOrder<T> {
/// Verify a collection of items is sorted as required.
fn verify_sort_order(&self) -> Result<(), Error>;
}

pub const HEADER_V2_HARD_FORK: u64 = 95_000;
Loading

0 comments on commit ecf2060

Please sign in to comment.