Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add block_hash to head_info so we can easily use them later #3060

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl<'a> Iterator for DifficultyIter<'a> {
let scaling = header.pow.secondary_scaling;

Some(HeaderInfo::new(
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 @@ -21,6 +21,7 @@
use std::cmp::{max, min};

use crate::core::block::HeaderVersion;
use crate::core::hash::{Hash, ZERO_HASH};
use crate::global;
use crate::pow::Difficulty;

Expand Down Expand Up @@ -219,6 +220,8 @@ pub const INITIAL_DIFFICULTY: u64 = 1_000_000 * UNIT_DIFFICULTY;
/// take place
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HeaderInfo {
/// Block hash, ZERO_HASH when this is a sythetic entry.
pub block_hash: Hash,
/// Timestamp of the header, 1 when not used (returned info)
pub timestamp: u64,
/// Network difficulty or next difficulty to use
Expand All @@ -232,12 +235,14 @@ pub struct HeaderInfo {
impl HeaderInfo {
/// Default constructor
pub fn new(
block_hash: Hash,
timestamp: u64,
difficulty: Difficulty,
secondary_scaling: u32,
is_secondary: bool,
) -> HeaderInfo {
HeaderInfo {
block_hash,
timestamp,
difficulty,
secondary_scaling,
Expand All @@ -249,6 +254,7 @@ impl HeaderInfo {
/// PoW factor
pub fn from_ts_diff(timestamp: u64, difficulty: Difficulty) -> HeaderInfo {
HeaderInfo {
block_hash: ZERO_HASH,
timestamp,
difficulty,
secondary_scaling: global::initial_graph_weight(),
Expand All @@ -261,6 +267,7 @@ impl HeaderInfo {
/// timestamp
pub fn from_diff_scaling(difficulty: Difficulty, secondary_scaling: u32) -> HeaderInfo {
HeaderInfo {
block_hash: ZERO_HASH,
timestamp: 1,
difficulty,
secondary_scaling,
Expand Down
1 change: 1 addition & 0 deletions core/tests/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn repeat(interval: u64, diff: HeaderInfo, len: u64, cur_time: Option<u64>) -> V
pairs
.map(|(t, d)| {
HeaderInfo::new(
diff.block_hash,
cur_time + t as u64,
d.clone(),
diff.secondary_scaling,
Expand Down
19 changes: 2 additions & 17 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::common::stats::{
ChainStats, DiffBlock, DiffStats, PeerStats, ServerStateInfo, ServerStats, TxStats,
};
use crate::common::types::{Error, ServerConfig, StratumServerConfig};
use crate::core::core::hash::{Hashed, ZERO_HASH};
use crate::core::core::hash::Hashed;
use crate::core::core::verifier_cache::{LruVerifierCache, VerifierCache};
use crate::core::ser::ProtocolVersion;
use crate::core::{consensus, genesis, global, pow};
Expand Down Expand Up @@ -436,9 +436,6 @@ impl Server {
let tip_height = self.head()?.height as i64;
let mut height = tip_height as i64 - last_blocks.len() as i64 + 1;

let header_pmmr = self.chain.header_pmmr();
let header_pmmr = header_pmmr.read();

let diff_entries: Vec<DiffBlock> = last_blocks
.windows(2)
.map(|pair| {
Expand All @@ -447,21 +444,9 @@ impl Server {

height += 1;

// Use header hash if real header.
// Default to "zero" hash if synthetic header_info.
let hash = if height >= 0 {
if let Ok(hash) = header_pmmr.get_header_hash_by_height(height as u64) {
hash
} else {
ZERO_HASH
}
} else {
ZERO_HASH
};

DiffBlock {
block_height: height,
block_hash: hash,
block_hash: next.block_hash,
difficulty: next.difficulty.to_num(),
time: next.timestamp,
duration: next.timestamp - prev.timestamp,
Expand Down