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

fix(rpc): Mine standard and minimum difficulty blocks on testnet #5747

Merged
merged 6 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ where

size_limit: MAX_BLOCK_BYTES,

cur_time: chain_info.current_system_time.timestamp(),
cur_time: chain_info.cur_time.timestamp(),

bits: format!("{:#010x}", chain_info.expected_difficulty.to_value())
.drain(2..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub async fn test_responses<State, ReadState>(
.respond(ReadResponse::ChainInfo(Some(GetBlockTemplateChainInfo {
expected_difficulty: CompactDifficulty::from(ExpandedDifficulty::from(U256::one())),
tip: (fake_tip_height, fake_tip_hash),
current_system_time: fake_cur_time,
cur_time: fake_cur_time,
min_time: fake_min_time,
max_time: fake_max_time,
})));
Expand Down
2 changes: 1 addition & 1 deletion zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ async fn rpc_getblocktemplate() {
.respond(ReadResponse::ChainInfo(Some(GetBlockTemplateChainInfo {
expected_difficulty: CompactDifficulty::from(ExpandedDifficulty::from(U256::one())),
tip: (fake_tip_height, fake_tip_hash),
current_system_time: fake_cur_time,
cur_time: fake_cur_time,
min_time: fake_min_time,
max_time: fake_max_time,
})));
Expand Down
2 changes: 1 addition & 1 deletion zebra-state/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct GetBlockTemplateChainInfo {
pub expected_difficulty: CompactDifficulty,

/// The current system time, adjusted to fit within `min_time` and `max_time`.
pub current_system_time: chrono::DateTime<chrono::Utc>,
pub cur_time: chrono::DateTime<chrono::Utc>,

/// The mininimum time the miner can use in this block.
pub min_time: chrono::DateTime<chrono::Utc>,
Expand Down
18 changes: 7 additions & 11 deletions zebra-state/src/service/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use chrono::Duration;
use zebra_chain::{
block::{self, Block, ChainHistoryBlockTxAuthCommitmentHash, CommitmentError},
history_tree::HistoryTree,
parameters::POW_AVERAGING_WINDOW,
parameters::{Network, NetworkUpgrade},
work::difficulty::CompactDifficulty,
};

use crate::{
service::{
block_iter::any_ancestor_blocks, finalized_state::FinalizedState,
non_finalized_state::NonFinalizedState,
block_iter::any_ancestor_blocks, check::difficulty::POW_ADJUSTMENT_BLOCK_SPAN,
finalized_state::FinalizedState, non_finalized_state::NonFinalizedState,
},
BoxError, PreparedBlock, ValidateContextError,
};
Expand All @@ -35,7 +34,7 @@ pub(crate) mod utxo;
#[cfg(test)]
mod tests;

pub(crate) use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN};
pub(crate) use difficulty::AdjustedDifficulty;

/// Check that the `prepared` block is contextually valid for `network`, based
/// on the `finalized_tip_height` and `relevant_chain`.
Expand All @@ -48,8 +47,7 @@ pub(crate) use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN};
///
/// # Panics
///
/// If the state contains less than 28
/// (`POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN`) blocks.
/// If the state contains less than 28 ([`POW_ADJUSTMENT_BLOCK_SPAN`]) blocks.
#[tracing::instrument(skip(prepared, finalized_tip_height, relevant_chain))]
pub(crate) fn block_is_valid_for_recent_chain<C>(
prepared: &PreparedBlock,
Expand All @@ -66,11 +64,9 @@ where
.expect("finalized state must contain at least one block to do contextual validation");
check::block_is_not_orphaned(finalized_tip_height, prepared.height)?;

// The maximum number of blocks used by contextual checks
const MAX_CONTEXT_BLOCKS: usize = POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN;
let relevant_chain: Vec<_> = relevant_chain
.into_iter()
.take(MAX_CONTEXT_BLOCKS)
.take(POW_ADJUSTMENT_BLOCK_SPAN)
.collect();

let parent_block = relevant_chain
Expand All @@ -84,14 +80,14 @@ where

// skip this check during tests if we don't have enough blocks in the chain
#[cfg(test)]
if relevant_chain.len() < POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN {
if relevant_chain.len() < POW_ADJUSTMENT_BLOCK_SPAN {
return Ok(());
}
// process_queued also checks the chain length, so we can skip this assertion during testing
// (tests that want to check this code should use the correct number of blocks)
assert_eq!(
relevant_chain.len(),
POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN,
POW_ADJUSTMENT_BLOCK_SPAN,
"state must contain enough blocks to do proof of work contextual validation, \
and validation must receive the exact number of required blocks"
);
Expand Down
16 changes: 10 additions & 6 deletions zebra-state/src/service/check/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//! * the Testnet minimum difficulty adjustment from ZIPs 205 and 208, and
//! * `median-time-past`.

use chrono::{DateTime, Duration, Utc};

use std::{cmp::max, cmp::min, convert::TryInto};

use chrono::{DateTime, Duration, Utc};

use zebra_chain::{
block,
block::Block,
Expand All @@ -24,6 +24,11 @@ use zebra_chain::{
/// `PoWMedianBlockSpan` in the Zcash specification.
pub const POW_MEDIAN_BLOCK_SPAN: usize = 11;

/// The overall block span used for adjusting Zcash block difficulty.
///
/// `PoWAveragingWindow + PoWMedianBlockSpan` in the Zcash specification.
pub const POW_ADJUSTMENT_BLOCK_SPAN: usize = POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN;

/// The damping factor for median timespan variance.
///
/// `PoWDampingFactor` in the Zcash specification.
Expand Down Expand Up @@ -59,15 +64,14 @@ pub(crate) struct AdjustedDifficulty {
/// The `header.difficulty_threshold`s from the previous
/// `PoWAveragingWindow + PoWMedianBlockSpan` (28) blocks, in reverse height
/// order.
relevant_difficulty_thresholds:
[CompactDifficulty; POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN],
relevant_difficulty_thresholds: [CompactDifficulty; POW_ADJUSTMENT_BLOCK_SPAN],
/// The `header.time`s from the previous
/// `PoWAveragingWindow + PoWMedianBlockSpan` (28) blocks, in reverse height
/// order.
///
/// Only the first and last `PoWMedianBlockSpan` times are used. Times
/// `11..=16` are ignored.
relevant_times: [DateTime<Utc>; POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN],
relevant_times: [DateTime<Utc>; POW_ADJUSTMENT_BLOCK_SPAN],
}

impl AdjustedDifficulty {
Expand Down Expand Up @@ -131,7 +135,7 @@ impl AdjustedDifficulty {

let (relevant_difficulty_thresholds, relevant_times) = context
.into_iter()
.take(POW_AVERAGING_WINDOW + POW_MEDIAN_BLOCK_SPAN)
.take(POW_ADJUSTMENT_BLOCK_SPAN)
.unzip::<_, _, Vec<_>, Vec<_>>();

let relevant_difficulty_thresholds = relevant_difficulty_thresholds
Expand Down
Loading