TUI Freeze Fix - Add manually maintained hash to difficulty iterator (#3684)

* add manually managed hash to difficulty iterator

* text fix

* fix for hash-of-hash, review feedback
This commit is contained in:
Yeastplume 2022-01-07 14:23:58 +00:00 committed by GitHub
parent a3eebbc0ab
commit 78c9794d30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 18 deletions

View file

@ -441,6 +441,7 @@ pub struct DifficultyIter<'a> {
// toward the genesis block (while maintaining current state) // toward the genesis block (while maintaining current state)
header: Option<BlockHeader>, header: Option<BlockHeader>,
prev_header: Option<BlockHeader>, prev_header: Option<BlockHeader>,
prev_header_hash: Option<Hash>,
} }
impl<'a> DifficultyIter<'a> { impl<'a> DifficultyIter<'a> {
@ -453,6 +454,7 @@ impl<'a> DifficultyIter<'a> {
batch: None, batch: None,
header: None, header: None,
prev_header: None, prev_header: None,
prev_header_hash: None,
} }
} }
@ -465,6 +467,7 @@ impl<'a> DifficultyIter<'a> {
batch: Some(batch), batch: Some(batch),
header: None, header: None,
prev_header: None, prev_header: None,
prev_header_hash: None,
} }
} }
} }
@ -479,18 +482,26 @@ impl<'a> Iterator for DifficultyIter<'a> {
// Items returned by this iterator cannot be expected to correctly // Items returned by this iterator cannot be expected to correctly
// calculate their own hash - This iterator is purely for iterating through // calculate their own hash - This iterator is purely for iterating through
// difficulty information // difficulty information
self.header = if self.header.is_none() { let (cur_header, cur_header_hash) = if self.header.is_none() {
if let Some(ref batch) = self.batch { if let Some(ref batch) = self.batch {
batch.get_block_header_skip_proof(&self.start).ok() (
batch.get_block_header_skip_proof(&self.start).ok(),
Some(self.start),
)
} else if let Some(ref store) = self.store { } else if let Some(ref store) = self.store {
store.get_block_header_skip_proof(&self.start).ok() (
store.get_block_header_skip_proof(&self.start).ok(),
Some(self.start),
)
} else { } else {
None (None, None)
} }
} else { } else {
self.prev_header.clone() (self.prev_header.clone(), self.prev_header_hash)
}; };
self.header = cur_header;
// If we have a header we can do this iteration. // If we have a header we can do this iteration.
// Otherwise we are done. // Otherwise we are done.
if let Some(header) = self.header.clone() { if let Some(header) = self.header.clone() {
@ -502,6 +513,8 @@ impl<'a> Iterator for DifficultyIter<'a> {
self.prev_header = None; self.prev_header = None;
} }
self.prev_header_hash = Some(header.prev_hash);
let prev_difficulty = self let prev_difficulty = self
.prev_header .prev_header
.clone() .clone()
@ -510,6 +523,7 @@ impl<'a> Iterator for DifficultyIter<'a> {
let scaling = header.pow.secondary_scaling; let scaling = header.pow.secondary_scaling;
Some(HeaderDifficultyInfo::new( Some(HeaderDifficultyInfo::new(
cur_header_hash,
header.timestamp.timestamp() as u64, header.timestamp.timestamp() as u64,
difficulty, difficulty,
scaling, scaling,

View file

@ -19,6 +19,7 @@
//! here. //! here.
use crate::core::block::HeaderVersion; use crate::core::block::HeaderVersion;
use crate::core::hash::Hash;
use crate::global; use crate::global;
use crate::pow::Difficulty; use crate::pow::Difficulty;
use std::cmp::{max, min}; use std::cmp::{max, min};
@ -231,6 +232,8 @@ pub const INITIAL_DIFFICULTY: u64 = 1_000_000 * UNIT_DIFFICULTY;
/// the header's PoW proof nonces from being deserialized on read /// the header's PoW proof nonces from being deserialized on read
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct HeaderDifficultyInfo { pub struct HeaderDifficultyInfo {
/// Hash of this block
pub hash: Option<Hash>,
/// Timestamp of the header, 1 when not used (returned info) /// Timestamp of the header, 1 when not used (returned info)
pub timestamp: u64, pub timestamp: u64,
/// Network difficulty or next difficulty to use /// Network difficulty or next difficulty to use
@ -244,12 +247,14 @@ pub struct HeaderDifficultyInfo {
impl HeaderDifficultyInfo { impl HeaderDifficultyInfo {
/// Default constructor /// Default constructor
pub fn new( pub fn new(
hash: Option<Hash>,
timestamp: u64, timestamp: u64,
difficulty: Difficulty, difficulty: Difficulty,
secondary_scaling: u32, secondary_scaling: u32,
is_secondary: bool, is_secondary: bool,
) -> HeaderDifficultyInfo { ) -> HeaderDifficultyInfo {
HeaderDifficultyInfo { HeaderDifficultyInfo {
hash,
timestamp, timestamp,
difficulty, difficulty,
secondary_scaling, secondary_scaling,
@ -261,6 +266,7 @@ impl HeaderDifficultyInfo {
/// PoW factor /// PoW factor
pub fn from_ts_diff(timestamp: u64, difficulty: Difficulty) -> HeaderDifficultyInfo { pub fn from_ts_diff(timestamp: u64, difficulty: Difficulty) -> HeaderDifficultyInfo {
HeaderDifficultyInfo { HeaderDifficultyInfo {
hash: None,
timestamp, timestamp,
difficulty, difficulty,
secondary_scaling: global::initial_graph_weight(), secondary_scaling: global::initial_graph_weight(),
@ -276,6 +282,7 @@ impl HeaderDifficultyInfo {
secondary_scaling: u32, secondary_scaling: u32,
) -> HeaderDifficultyInfo { ) -> HeaderDifficultyInfo {
HeaderDifficultyInfo { HeaderDifficultyInfo {
hash: None,
timestamp: 1, timestamp: 1,
difficulty, difficulty,
secondary_scaling, secondary_scaling,

View file

@ -213,6 +213,7 @@ fn repeat(
pairs pairs
.map(|(t, d)| { .map(|(t, d)| {
HeaderDifficultyInfo::new( HeaderDifficultyInfo::new(
None,
cur_time + t as u64, cur_time + t as u64,
*d, *d,
diff.secondary_scaling, diff.secondary_scaling,

View file

@ -463,19 +463,7 @@ impl Server {
height += 1; height += 1;
// We need to query again for the actual block hash, as let block_hash = next.hash.unwrap_or(ZERO_HASH);
// the difficulty iterator doesn't contain enough info to
// create a hash
// The diff iterator returns 59 block headers 'before' 0 to give callers
// enough detail to calculate initial block difficulties. Ignore these.
let block_hash = if height < 0 {
ZERO_HASH
} else {
match self.chain.get_header_by_height(height as u64) {
Ok(h) => h.hash(),
Err(_) => ZERO_HASH,
}
};
DiffBlock { DiffBlock {
block_height: height, block_height: height,