Skip to content

Commit

Permalink
feat: add config parameters for burn block start
Browse files Browse the repository at this point in the history
  • Loading branch information
obycode committed Mar 18, 2024
1 parent 3a94eb3 commit 1e6b1cc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
22 changes: 19 additions & 3 deletions stackslib/src/chainstate/stacks/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,7 @@ pub mod test {
let block_txs = vec![coinbase_tx];

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -2697,6 +2698,10 @@ pub mod test {

#[test]
fn test_lockups() {
let mut burnchain = Burnchain::default_unittest(
0,
&BurnchainHeaderHash::from_hex(BITCOIN_REGTEST_FIRST_BLOCK_HASH).unwrap(),
);
let mut peer_config = TestPeerConfig::new(function_name!(), 2000, 2001);
let alice = StacksAddress::from_string("STVK1K405H6SK9NKJAP32GHYHDJ98MMNP8Y6Z9N0").unwrap();
let bob = StacksAddress::from_string("ST76D2FMXZ7D2719PNE4N71KPSX84XCCNCMYC940").unwrap();
Expand Down Expand Up @@ -2783,6 +2788,7 @@ pub mod test {
let block_txs = vec![coinbase_tx];

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -2877,7 +2883,8 @@ pub mod test {
block_txs.push(tx);
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(&parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let block_builder = StacksBlockBuilder::make_regtest_block_builder(&burnchain,
&parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let (anchored_block, _size, _cost) = StacksBlockBuilder::make_anchored_block_from_txs(block_builder, chainstate, &sortdb.index_conn(), block_txs).unwrap();
(anchored_block, vec![])
});
Expand Down Expand Up @@ -2973,6 +2980,7 @@ pub mod test {
let block_txs = vec![coinbase_tx, burn_tx];

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -3083,6 +3091,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -3299,6 +3308,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_block_builder(
&burnchain,
false,
&parent_tip,
vrf_proof,
Expand Down Expand Up @@ -3557,6 +3567,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -3831,6 +3842,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -4083,7 +4095,8 @@ pub mod test {
block_txs.push(charlie_test_tx);
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(&parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let block_builder = StacksBlockBuilder::make_regtest_block_builder(&burnchain,
&parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let (anchored_block, _size, _cost) = StacksBlockBuilder::make_anchored_block_from_txs(block_builder, chainstate, &sortdb.index_conn(), block_txs).unwrap();
(anchored_block, vec![])
});
Expand Down Expand Up @@ -4246,6 +4259,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -4544,6 +4558,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -5124,6 +5139,7 @@ pub mod test {
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(
&burnchain,
&parent_tip,
vrf_proof,
tip.total_burn,
Expand Down Expand Up @@ -5502,7 +5518,7 @@ pub mod test {
block_txs.push(charlie_reject);
}

let block_builder = StacksBlockBuilder::make_regtest_block_builder(&parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let block_builder = StacksBlockBuilder::make_regtest_block_builder(&burnchain, &parent_tip, vrf_proof, tip.total_burn, microblock_pubkeyhash).unwrap();
let (anchored_block, _size, _cost) = StacksBlockBuilder::make_anchored_block_from_txs(block_builder, chainstate, &sortdb.index_conn(), block_txs).unwrap();

if tenure_id == 2 {
Expand Down
4 changes: 4 additions & 0 deletions stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10194,6 +10194,7 @@ pub mod test {
&coinbase_tx,
BlockBuilderSettings::max_value(),
None,
&peer_config.burnchain,
)
.unwrap();

Expand Down Expand Up @@ -10444,6 +10445,7 @@ pub mod test {
&coinbase_tx,
BlockBuilderSettings::max_value(),
None,
&peer_config.burnchain,
)
.unwrap();
(anchored_block.0, vec![])
Expand Down Expand Up @@ -10998,6 +11000,7 @@ pub mod test {
&coinbase_tx,
BlockBuilderSettings::max_value(),
None,
&peer_config.burnchain,
)
.unwrap();

Expand Down Expand Up @@ -11319,6 +11322,7 @@ pub mod test {
&coinbase_tx,
BlockBuilderSettings::max_value(),
None,
&peer_config.burnchain,
)
.unwrap();

Expand Down
25 changes: 13 additions & 12 deletions stackslib/src/chainstate/stacks/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use stacks_common::util::hash::{MerkleTree, Sha512Trunc256Sum};
use stacks_common::util::secp256k1::{MessageSignature, Secp256k1PrivateKey};
use stacks_common::util::vrf::*;

use crate::burnchains::{PrivateKey, PublicKey};
use crate::burnchains::{Burnchain, PrivateKey, PublicKey};
use crate::chainstate::burn::db::sortdb::{SortitionDB, SortitionDBConn, SortitionHandleTx};
use crate::chainstate::burn::operations::*;
use crate::chainstate::burn::*;
Expand Down Expand Up @@ -2041,27 +2041,27 @@ impl StacksBlockBuilder {

/// Create a block builder for mining
pub fn make_block_builder(
burnchain: &Burnchain,
mainnet: bool,
stacks_parent_header: &StacksHeaderInfo,
proof: VRFProof,
total_burn: u64,
pubkey_hash: Hash160,
) -> Result<StacksBlockBuilder, Error> {
let builder = if stacks_parent_header.consensus_hash == FIRST_BURNCHAIN_CONSENSUS_HASH {
let (first_block_hash_hex, first_block_height, first_block_ts) = if mainnet {
let (first_block_hash, first_block_height, first_block_ts) = if mainnet {
(
BITCOIN_MAINNET_FIRST_BLOCK_HASH,
BurnchainHeaderHash::from_hex(BITCOIN_MAINNET_FIRST_BLOCK_HASH).unwrap(),
BITCOIN_MAINNET_FIRST_BLOCK_HEIGHT,
BITCOIN_MAINNET_FIRST_BLOCK_TIMESTAMP,
)
} else {
(
BITCOIN_TESTNET_FIRST_BLOCK_HASH,
BITCOIN_TESTNET_FIRST_BLOCK_HEIGHT,
BITCOIN_TESTNET_FIRST_BLOCK_TIMESTAMP,
burnchain.first_block_hash,
burnchain.first_block_height,
burnchain.first_block_timestamp,
)
};
let first_block_hash = BurnchainHeaderHash::from_hex(first_block_hash_hex).unwrap();
StacksBlockBuilder::first_pubkey_hash(
0,
&FIRST_BURNCHAIN_CONSENSUS_HASH,
Expand Down Expand Up @@ -2095,21 +2095,20 @@ impl StacksBlockBuilder {

/// Create a block builder for regtest mining
pub fn make_regtest_block_builder(
burnchain: &Burnchain,
stacks_parent_header: &StacksHeaderInfo,
proof: VRFProof,
total_burn: u64,
pubkey_hash: Hash160,
) -> Result<StacksBlockBuilder, Error> {
let builder = if stacks_parent_header.consensus_hash == FIRST_BURNCHAIN_CONSENSUS_HASH {
let first_block_hash =
BurnchainHeaderHash::from_hex(BITCOIN_REGTEST_FIRST_BLOCK_HASH).unwrap();
StacksBlockBuilder::first_pubkey_hash(
0,
&FIRST_BURNCHAIN_CONSENSUS_HASH,
&first_block_hash,
u32::try_from(BITCOIN_REGTEST_FIRST_BLOCK_HEIGHT)
&burnchain.first_block_hash,
u32::try_from(burnchain.first_block_height)
.expect("first regtest bitcoin block is over 2^32"),
u64::try_from(BITCOIN_REGTEST_FIRST_BLOCK_TIMESTAMP)
u64::try_from(burnchain.first_block_timestamp)
.expect("first regtest bitcoin block timestamp is over 2^64"),
&proof,
pubkey_hash,
Expand Down Expand Up @@ -2387,6 +2386,7 @@ impl StacksBlockBuilder {
coinbase_tx: &StacksTransaction,
settings: BlockBuilderSettings,
event_observer: Option<&dyn MemPoolEventDispatcher>,
burnchain: &Burnchain,
) -> Result<(StacksBlock, ExecutionCost, u64), Error> {
if let TransactionPayload::Coinbase(..) = coinbase_tx.payload {
} else {
Expand All @@ -2409,6 +2409,7 @@ impl StacksBlockBuilder {
let (mut chainstate, _) = chainstate_handle.reopen()?;

let mut builder = StacksBlockBuilder::make_block_builder(
burnchain,
chainstate.mainnet,
parent_stacks_header,
proof,
Expand Down
53 changes: 52 additions & 1 deletion testnet/stacks-node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use stacks::cost_estimates::{CostEstimator, FeeEstimator, PessimisticEstimator,
use stacks::net::atlas::AtlasConfig;
use stacks::net::connection::ConnectionOptions;
use stacks::net::{Neighbor, NeighborKey};
use stacks::types::chainstate::BurnchainHeaderHash;
use stacks::util_lib::boot::boot_code_id;
use stacks::util_lib::db::Error as DBError;
use stacks_common::consts::SIGNER_SLOTS_PER_USER;
Expand Down Expand Up @@ -463,6 +464,31 @@ impl Config {
return;
}

if let Some(first_burn_block_height) = self.burnchain.first_burn_block_height {
debug!(
"Override first_block_height from {} to {}",
burnchain.first_block_height, first_burn_block_height
);
burnchain.first_block_height = first_burn_block_height;
}

if let Some(first_burn_block_timestamp) = self.burnchain.first_burn_block_timestamp {
debug!(
"Override first_block_timestamp from {} to {}",
burnchain.first_block_timestamp, first_burn_block_timestamp
);
burnchain.first_block_timestamp = first_burn_block_timestamp;
}

if let Some(first_burn_block_hash) = &self.burnchain.first_burn_block_hash {
debug!(
"Override first_burn_block_hash from {} to {}",
burnchain.first_block_hash, first_burn_block_hash
);
burnchain.first_block_hash = BurnchainHeaderHash::from_hex(&first_burn_block_hash)
.expect("Invalid first_burn_block_hash");
}

if let Some(pox_prepare_length) = self.burnchain.pox_prepare_length {
debug!("Override pox_prepare_length to {pox_prepare_length}");
burnchain.pox_constants.prepare_length = pox_prepare_length;
Expand Down Expand Up @@ -710,7 +736,6 @@ impl Config {
);
}

// epochs must be a prefix of [1.0, 2.0, 2.05, 2.1]
let expected_list = [
StacksEpochId::Epoch10,
StacksEpochId::Epoch20,
Expand Down Expand Up @@ -1173,6 +1198,9 @@ pub struct BurnchainConfig {
pub leader_key_tx_estimated_size: u64,
pub block_commit_tx_estimated_size: u64,
pub rbf_fee_increment: u64,
pub first_burn_block_height: Option<u64>,
pub first_burn_block_timestamp: Option<u32>,
pub first_burn_block_hash: Option<String>,
/// Custom override for the definitions of the epochs. This will only be applied for testnet and
/// regtest nodes.
pub epochs: Option<Vec<StacksEpoch>>,
Expand Down Expand Up @@ -1210,6 +1238,9 @@ impl BurnchainConfig {
leader_key_tx_estimated_size: LEADER_KEY_TX_ESTIM_SIZE,
block_commit_tx_estimated_size: BLOCK_COMMIT_TX_ESTIM_SIZE,
rbf_fee_increment: DEFAULT_RBF_FEE_RATE_INCREMENT,
first_burn_block_height: None,
first_burn_block_timestamp: None,
first_burn_block_hash: None,
epochs: None,
pox_2_activation: None,
pox_prepare_length: None,
Expand Down Expand Up @@ -1294,6 +1325,9 @@ pub struct BurnchainConfigFile {
pub block_commit_tx_estimated_size: Option<u64>,
pub rbf_fee_increment: Option<u64>,
pub max_rbf: Option<u64>,
pub first_burn_block_height: Option<u64>,
pub first_burn_block_timestamp: Option<u32>,
pub first_burn_block_hash: Option<String>,
pub epochs: Option<Vec<StacksEpochConfigFile>>,
pub pox_prepare_length: Option<u32>,
pub pox_reward_length: Option<u32>,
Expand Down Expand Up @@ -1403,6 +1437,16 @@ impl BurnchainConfigFile {
rbf_fee_increment: self
.rbf_fee_increment
.unwrap_or(default_burnchain_config.rbf_fee_increment),
first_burn_block_height: self
.first_burn_block_height
.or(default_burnchain_config.first_burn_block_height),
first_burn_block_timestamp: self
.first_burn_block_timestamp
.or(default_burnchain_config.first_burn_block_timestamp),
first_burn_block_hash: self
.first_burn_block_hash
.clone()
.or(default_burnchain_config.first_burn_block_hash.clone()),
// will be overwritten below
epochs: default_burnchain_config.epochs,
ast_precheck_size_height: self.ast_precheck_size_height,
Expand Down Expand Up @@ -1430,6 +1474,13 @@ impl BurnchainConfigFile {
{
return Err("PoX-2 parameters are not configurable in mainnet".into());
}
// Check that the first burn block options are not set in mainnet
if config.first_burn_block_height.is_some()
|| config.first_burn_block_timestamp.is_some()
|| config.first_burn_block_hash.is_some()
{
return Err("First burn block parameters are not configurable in mainnet".into());
}
}

if let Some(ref conf_epochs) = self.epochs {
Expand Down
2 changes: 2 additions & 0 deletions testnet/stacks-node/src/neon_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,7 @@ impl BlockMinerThread {
self.globals.get_miner_status(),
),
Some(&self.event_dispatcher),
&self.burnchain,
) {
Ok(block) => block,
Err(ChainstateError::InvalidStacksMicroblock(msg, mblock_header_hash)) => {
Expand Down Expand Up @@ -2345,6 +2346,7 @@ impl BlockMinerThread {
self.globals.get_miner_status(),
),
Some(&self.event_dispatcher),
&self.burnchain,
) {
Ok(block) => block,
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions testnet/stacks-node/src/tenure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl<'a> Tenure {
&self.coinbase_tx,
BlockBuilderSettings::limited(),
None,
&self.config.get_burnchain(),
)
.unwrap();

Expand Down

0 comments on commit 1e6b1cc

Please sign in to comment.