Skip to content

Commit

Permalink
fix: neon_node unprocessed_block behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
kantai committed Apr 12, 2023
1 parent 83a16d5 commit ebf6cf2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to the versioning scheme outlined in the [README.md](README.md).

## [2.1.0.0.3]

This is a high-priority hotfix release to address a bug in the
stacks-node miner logic which could impact miner availability. This
release's chainstate directory is compatible with chainstate
directories from 2.1.0.0.2.

## [2.1.0.0.2]

This software update is a hotfix to resolve improper unlock handling
in mempool admission. This release's chainstate directory is
compatible with chainstate directories from 2.1.0.0.2.
compatible with chainstate directories from 2.1.0.0.1.

### Fixed

Expand Down
9 changes: 0 additions & 9 deletions clarity/src/vm/database/clarity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,15 +1833,6 @@ impl<'a> ClarityDatabase<'a> {
self.burn_state_db.get_burn_block_height(sortition_id)
}

pub fn get_burn_header_hash(
&self,
height: u32,
sortition_id: &SortitionId,
) -> Option<BurnchainHeaderHash> {
self.burn_state_db
.get_burn_header_hash(height, sortition_id)
}

/// This function obtains the stacks epoch version, which is based on the burn block height.
/// Valid epochs include stacks 1.0, 2.0, 2.05, and so on.
pub fn get_stacks_epoch(&self, height: u32) -> Option<StacksEpoch> {
Expand Down
26 changes: 17 additions & 9 deletions src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3704,13 +3704,18 @@ impl StacksChainState {
Ok(count - to_write)
}

/// Check whether or not there exists a Stacks block at or higher than a given height that is
/// unprocessed. This is used by miners to determine whether or not the block-commit they're
/// about to send is about to be invalidated
pub fn has_higher_unprocessed_blocks(conn: &DBConn, height: u64) -> Result<bool, Error> {
/// Check whether or not there exists a Stacks block at or higher
/// than a given height that is unprocessed and relatively
/// new. This is used by miners to determine whether or not the
/// block-commit they're about to send is about to be invalidated.
pub fn has_higher_unprocessed_blocks(
conn: &DBConn,
height: u64,
deadline: u64,
) -> Result<bool, Error> {
let sql =
"SELECT 1 FROM staging_blocks WHERE orphaned = 0 AND processed = 0 AND height >= ?1";
let args: &[&dyn ToSql] = &[&u64_to_sql(height)?];
"SELECT 1 FROM staging_blocks WHERE orphaned = 0 AND processed = 0 AND height >= ?1 AND arrival_time >= ?2";
let args: &[&dyn ToSql] = &[&u64_to_sql(height)?, &u64_to_sql(deadline)?];
let res = conn
.query_row(sql, args, |_r| Ok(()))
.optional()
Expand All @@ -3720,10 +3725,13 @@ impl StacksChainState {

/// Get the metadata of the highest unprocessed block.
/// The block data will not be returned
pub fn get_highest_unprocessed_block(conn: &DBConn) -> Result<Option<StagingBlock>, Error> {
pub fn get_highest_unprocessed_block(
conn: &DBConn,
deadline: u64,
) -> Result<Option<StagingBlock>, Error> {
let sql =
"SELECT * FROM staging_blocks WHERE orphaned = 0 AND processed = 0 ORDER BY height DESC LIMIT 1";
let res = query_row(conn, sql, NO_PARAMS)?;
"SELECT * FROM staging_blocks WHERE orphaned = 0 AND processed = 0 AND arrival_time >= ?1 ORDER BY height DESC LIMIT 1";
let res = query_row(conn, sql, &[u64_to_sql(deadline)?])?;
Ok(res)
}

Expand Down
2 changes: 1 addition & 1 deletion src/chainstate/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use stacks_common::address::{
C32_ADDRESS_VERSION_TESTNET_MULTISIG, C32_ADDRESS_VERSION_TESTNET_SINGLESIG,
};

pub const STACKS_BLOCK_VERSION: u8 = 4;
pub const STACKS_BLOCK_VERSION: u8 = 5;
pub const STACKS_BLOCK_VERSION_AST_PRECHECK_SIZE: u8 = 1;

pub const MAX_BLOCK_LEN: u32 = 2 * 1024 * 1024;
Expand Down
2 changes: 1 addition & 1 deletion src/clarity_vm/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl BurnStateDB for SortitionDBConn<'_> {
Some(height) => height,
};

if height >= current_height {
if height > current_height {
return None;
}

Expand Down
6 changes: 6 additions & 0 deletions testnet/stacks-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ impl Config {
candidate_retry_cache_size: miner
.candidate_retry_cache_size
.unwrap_or(miner_default_config.candidate_retry_cache_size),
unprocessed_block_deadline_secs: miner
.unprocessed_block_deadline_secs
.unwrap_or(miner_default_config.unprocessed_block_deadline_secs),
},
None => miner_default_config,
};
Expand Down Expand Up @@ -1846,6 +1849,7 @@ pub struct MinerConfig {
pub wait_for_block_download: bool,
pub nonce_cache_size: u64,
pub candidate_retry_cache_size: u64,
pub unprocessed_block_deadline_secs: u64,
}

impl MinerConfig {
Expand All @@ -1861,6 +1865,7 @@ impl MinerConfig {
wait_for_block_download: true,
nonce_cache_size: 10_000,
candidate_retry_cache_size: 10_000,
unprocessed_block_deadline_secs: 30,
}
}
}
Expand Down Expand Up @@ -1976,6 +1981,7 @@ pub struct MinerConfigFile {
pub segwit: Option<bool>,
pub nonce_cache_size: Option<u64>,
pub candidate_retry_cache_size: Option<u64>,
pub unprocessed_block_deadline_secs: Option<u64>,
}

#[derive(Clone, Deserialize, Default, Debug)]
Expand Down
30 changes: 22 additions & 8 deletions testnet/stacks-node/src/neon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ impl BlockMinerThread {
burnchain: &Burnchain,
sortdb: &SortitionDB,
chainstate: &StacksChainState,
unprocessed_block_deadline: u64,
) -> bool {
let sort_tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn())
.expect("FATAL: could not query canonical sortition DB tip");
Expand All @@ -1768,13 +1769,21 @@ impl BlockMinerThread {
.get_stacks_chain_tip(sortdb)
.expect("FATAL: could not query canonical Stacks chain tip")
{
let has_unprocessed =
StacksChainState::has_higher_unprocessed_blocks(chainstate.db(), stacks_tip.height)
.expect("FATAL: failed to query staging blocks");
// if a block hasn't been processed within some deadline seconds of receipt, don't block
// mining
let process_deadline = get_epoch_time_secs() - unprocessed_block_deadline;
let has_unprocessed = StacksChainState::has_higher_unprocessed_blocks(
chainstate.db(),
stacks_tip.height,
process_deadline,
)
.expect("FATAL: failed to query staging blocks");
if has_unprocessed {
let highest_unprocessed_opt =
StacksChainState::get_highest_unprocessed_block(chainstate.db())
.expect("FATAL: failed to query staging blocks");
let highest_unprocessed_opt = StacksChainState::get_highest_unprocessed_block(
chainstate.db(),
process_deadline,
)
.expect("FATAL: failed to query staging blocks");

if let Some(highest_unprocessed) = highest_unprocessed_opt {
let highest_unprocessed_block_sn_opt =
Expand Down Expand Up @@ -2011,8 +2020,12 @@ impl BlockMinerThread {
.expect("FATAL: mutex poisoned")
.is_blocked();

let has_unprocessed =
Self::unprocessed_blocks_prevent_mining(&self.burnchain, &burn_db, &chain_state);
let has_unprocessed = Self::unprocessed_blocks_prevent_mining(
&self.burnchain,
&burn_db,
&chain_state,
self.config.miner.unprocessed_block_deadline_secs,
);
if stacks_tip.anchored_block_hash != anchored_block.header.parent_block
|| parent_block_info.parent_consensus_hash != stacks_tip.consensus_hash
|| cur_burn_chain_tip.burn_header_hash != self.burn_block.burn_header_hash
Expand Down Expand Up @@ -2977,6 +2990,7 @@ impl RelayerThread {
&self.burnchain,
self.sortdb_ref(),
self.chainstate_ref(),
self.config.miner.unprocessed_block_deadline_secs,
);
if has_unprocessed {
debug!(
Expand Down

0 comments on commit ebf6cf2

Please sign in to comment.