Target needs to be truncated after next target calculation, given that serialization in the block header does it anyway.

This commit is contained in:
Ignotus Peverell 2016-11-29 20:03:43 -08:00
parent 0c52665f17
commit 1ab9001338
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
2 changed files with 17 additions and 11 deletions

View file

@ -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 // 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 // long as we're not at the max size already; target gets 2x to compensate for
// increased next_target // 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 { let (ptarget, clen) = if prev_target < soft_min && prev_cuckoo_sz < MAX_SIZESHIFT {
(prev_target << 1, prev_cuckoo_sz + 1) (prev_target << 1, prev_cuckoo_sz + 1)
} else { } 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 { if new_target > MAX_TARGET {
(MAX_TARGET, clen) (MAX_TARGET, clen)
} else { } else {
(new_target, clen) (new_target.truncate(), clen)
} }
} }
/// Max target hash, lowest next_target /// Max target hash, lowest next_target
pub const MAX_TARGET: Target = Target([0xf, 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,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0xff, 0xff, 0xff, 0xff]); 0, 0, 0, 0]);
/// Target limit under which we start increasing the size shift on Cuckoo cycle. /// 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, pub const SOFT_MIN_TARGET: Target = Target([0, 0, 0xf, 0xff, 0xff, 0xff, 0, 0, 0, 0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0xff, 0xff, 0xff, 0xff]); 0, 0, 0, 0]);
/// Default number of blocks in the past when cross-block cut-through will start /// 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. /// happening. Needs to be long enough to not overlap with a long reorg.

View file

@ -57,7 +57,7 @@ impl Target {
/// Takes a u32 mantissa, bringing it to the provided exponent to build a /// Takes a u32 mantissa, bringing it to the provided exponent to build a
/// new target. /// new target.
fn join(exp: u8, mantissa: u32) -> Result<Target, ser::Error> { fn join(exp: u8, mantissa: u32) -> Result<Target, ser::Error> {
if exp > 192 { if exp > 255 {
return Err(ser::Error::CorruptedData); return Err(ser::Error::CorruptedData);
} }
let mut t = [0; 32]; let mut t = [0; 32];
@ -84,6 +84,12 @@ impl Target {
res += (mantissa[28] as u32) << 24; res += (mantissa[28] as u32) << 24;
(exp, res) (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 { impl Index<usize> for Target {