Skip to content

Commit

Permalink
TUI Freeze Fix - Add manually maintained hash to difficulty iterator (#…
Browse files Browse the repository at this point in the history
…3684)

* add manually managed hash to difficulty iterator

* text fix

* fix for hash-of-hash, review feedback
  • Loading branch information
yeastplume authored Jan 7, 2022
1 parent a3eebbc commit 78c9794
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
24 changes: 19 additions & 5 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ pub struct DifficultyIter<'a> {
// toward the genesis block (while maintaining current state)
header: Option<BlockHeader>,
prev_header: Option<BlockHeader>,
prev_header_hash: Option<Hash>,
}

impl<'a> DifficultyIter<'a> {
Expand All @@ -453,6 +454,7 @@ impl<'a> DifficultyIter<'a> {
batch: None,
header: None,
prev_header: None,
prev_header_hash: None,
}
}

Expand All @@ -465,6 +467,7 @@ impl<'a> DifficultyIter<'a> {
batch: Some(batch),
header: None,
prev_header: None,
prev_header_hash: None,
}
}
}
Expand All @@ -479,18 +482,26 @@ impl<'a> Iterator for DifficultyIter<'a> {
// Items returned by this iterator cannot be expected to correctly
// calculate their own hash - This iterator is purely for iterating through
// 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 {
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 {
store.get_block_header_skip_proof(&self.start).ok()
(
store.get_block_header_skip_proof(&self.start).ok(),
Some(self.start),
)
} else {
None
(None, None)
}
} 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.
// Otherwise we are done.
if let Some(header) = self.header.clone() {
Expand All @@ -502,6 +513,8 @@ impl<'a> Iterator for DifficultyIter<'a> {
self.prev_header = None;
}

self.prev_header_hash = Some(header.prev_hash);

let prev_difficulty = self
.prev_header
.clone()
Expand All @@ -510,6 +523,7 @@ impl<'a> Iterator for DifficultyIter<'a> {
let scaling = header.pow.secondary_scaling;

Some(HeaderDifficultyInfo::new(
cur_header_hash,
header.timestamp.timestamp() as u64,
difficulty,
scaling,
Expand Down
7 changes: 7 additions & 0 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! here.

use crate::core::block::HeaderVersion;
use crate::core::hash::Hash;
use crate::global;
use crate::pow::Difficulty;
use std::cmp::{max, min};
Expand Down Expand Up @@ -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
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HeaderDifficultyInfo {
/// Hash of this block
pub hash: Option<Hash>,
/// Timestamp of the header, 1 when not used (returned info)
pub timestamp: u64,
/// Network difficulty or next difficulty to use
Expand All @@ -244,12 +247,14 @@ pub struct HeaderDifficultyInfo {
impl HeaderDifficultyInfo {
/// Default constructor
pub fn new(
hash: Option<Hash>,
timestamp: u64,
difficulty: Difficulty,
secondary_scaling: u32,
is_secondary: bool,
) -> HeaderDifficultyInfo {
HeaderDifficultyInfo {
hash,
timestamp,
difficulty,
secondary_scaling,
Expand All @@ -261,6 +266,7 @@ impl HeaderDifficultyInfo {
/// PoW factor
pub fn from_ts_diff(timestamp: u64, difficulty: Difficulty) -> HeaderDifficultyInfo {
HeaderDifficultyInfo {
hash: None,
timestamp,
difficulty,
secondary_scaling: global::initial_graph_weight(),
Expand All @@ -276,6 +282,7 @@ impl HeaderDifficultyInfo {
secondary_scaling: u32,
) -> HeaderDifficultyInfo {
HeaderDifficultyInfo {
hash: None,
timestamp: 1,
difficulty,
secondary_scaling,
Expand Down
1 change: 1 addition & 0 deletions core/tests/consensus_automated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ fn repeat(
pairs
.map(|(t, d)| {
HeaderDifficultyInfo::new(
None,
cur_time + t as u64,
*d,
diff.secondary_scaling,
Expand Down
14 changes: 1 addition & 13 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,7 @@ impl Server {

height += 1;

// We need to query again for the actual block hash, as
// 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,
}
};
let block_hash = next.hash.unwrap_or(ZERO_HASH);

DiffBlock {
block_height: height,
Expand Down

0 comments on commit 78c9794

Please sign in to comment.