mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Target needs to be truncated after next target calculation, given that serialization in the block header does it anyway.
This commit is contained in:
parent
0c52665f17
commit
1ab9001338
2 changed files with 17 additions and 11 deletions
|
@ -59,7 +59,7 @@ pub fn next_target(ts: i64, prev_ts: i64, prev_target: Target, prev_cuckoo_sz: u
|
|||
// increase the cuckoo size when the target gets lower than the soft min as
|
||||
// long as we're not at the max size already; target gets 2x to compensate for
|
||||
// increased next_target
|
||||
let soft_min = SOFT_MIN_TARGET >> (((prev_cuckoo_sz - DEFAULT_SIZESHIFT) * 8) as usize);
|
||||
let soft_min = SOFT_MIN_TARGET >> (((prev_cuckoo_sz - cmp::min(DEFAULT_SIZESHIFT, prev_cuckoo_sz)) * 8) as usize);
|
||||
let (ptarget, clen) = if prev_target < soft_min && prev_cuckoo_sz < MAX_SIZESHIFT {
|
||||
(prev_target << 1, prev_cuckoo_sz + 1)
|
||||
} else {
|
||||
|
@ -83,21 +83,21 @@ pub fn next_target(ts: i64, prev_ts: i64, prev_target: Target, prev_cuckoo_sz: u
|
|||
if new_target > MAX_TARGET {
|
||||
(MAX_TARGET, clen)
|
||||
} else {
|
||||
(new_target, clen)
|
||||
(new_target.truncate(), clen)
|
||||
}
|
||||
}
|
||||
|
||||
/// Max target hash, lowest next_target
|
||||
pub const MAX_TARGET: Target = Target([0xf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff]);
|
||||
pub const MAX_TARGET: Target = Target([0xf, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0]);
|
||||
|
||||
/// Target limit under which we start increasing the size shift on Cuckoo cycle.
|
||||
pub const SOFT_MIN_TARGET: Target = Target([0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff]);
|
||||
pub const SOFT_MIN_TARGET: Target = Target([0, 0, 0xf, 0xff, 0xff, 0xff, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0]);
|
||||
|
||||
/// Default number of blocks in the past when cross-block cut-through will start
|
||||
/// happening. Needs to be long enough to not overlap with a long reorg.
|
||||
|
|
|
@ -57,7 +57,7 @@ impl Target {
|
|||
/// Takes a u32 mantissa, bringing it to the provided exponent to build a
|
||||
/// new target.
|
||||
fn join(exp: u8, mantissa: u32) -> Result<Target, ser::Error> {
|
||||
if exp > 192 {
|
||||
if exp > 255 {
|
||||
return Err(ser::Error::CorruptedData);
|
||||
}
|
||||
let mut t = [0; 32];
|
||||
|
@ -84,6 +84,12 @@ impl Target {
|
|||
res += (mantissa[28] as u32) << 24;
|
||||
(exp, res)
|
||||
}
|
||||
|
||||
/// Truncates the target to its maximum precision.
|
||||
pub fn truncate(&self) -> Target {
|
||||
let (e, m) = self.split();
|
||||
Target::join(e, m).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Target {
|
||||
|
|
Loading…
Reference in a new issue