From cdf339850a7ceae2be85b2ba9f6b485b1c0d51b2 Mon Sep 17 00:00:00 2001 From: ASuciuX Date: Fri, 5 Jul 2024 16:21:03 +0300 Subject: [PATCH] migrate schema of nakamoto tenure table to STRICT --- stackslib/src/chainstate/nakamoto/mod.rs | 8 +++++ stackslib/src/chainstate/nakamoto/tenure.rs | 40 +++++++++++++++++++++ stackslib/src/chainstate/stacks/db/mod.rs | 8 +++++ 3 files changed, 56 insertions(+) diff --git a/stackslib/src/chainstate/nakamoto/mod.rs b/stackslib/src/chainstate/nakamoto/mod.rs index 0252e2ec193..04eabe3708d 100644 --- a/stackslib/src/chainstate/nakamoto/mod.rs +++ b/stackslib/src/chainstate/nakamoto/mod.rs @@ -51,6 +51,7 @@ use stacks_common::util::retry::BoundReader; use stacks_common::util::secp256k1::MessageSignature; use stacks_common::util::vrf::{VRFProof, VRFPublicKey, VRF}; use stacks_common::util::{get_epoch_time_secs, sleep_ms}; +use tenure::NAKAMOTO_TENURES_SCHEMA_3; use wsts::curve::point::Point; use self::signer_set::SignerCalculation; @@ -240,6 +241,13 @@ lazy_static! { ADD COLUMN burn_view TEXT; "#.into(), ]; + + pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_3: Vec = vec![ + NAKAMOTO_TENURES_SCHEMA_3.into(), + r#" + UPDATE db_config SET version = "6"; + "#.into(), + ]; } /// Matured miner reward schedules diff --git a/stackslib/src/chainstate/nakamoto/tenure.rs b/stackslib/src/chainstate/nakamoto/tenure.rs index 51b824077b1..3553a2f8f1d 100644 --- a/stackslib/src/chainstate/nakamoto/tenure.rs +++ b/stackslib/src/chainstate/nakamoto/tenure.rs @@ -198,6 +198,46 @@ pub static NAKAMOTO_TENURES_SCHEMA_2: &'static str = r#" CREATE INDEX nakamoto_tenures_by_parent ON nakamoto_tenures(tenure_id_consensus_hash,prev_tenure_id_consensus_hash); "#; +pub static NAKAMOTO_TENURES_SCHEMA_3: &'static str = r#" + -- Drop the nakamoto_tenures table if it exists + DROP TABLE IF EXISTS nakamoto_tenures; + + CREATE TABLE nakamoto_tenures STRICT ( + -- consensus hash of start-tenure block (i.e. the consensus hash of the sortition in which the miner's block-commit + -- was mined) + tenure_id_consensus_hash TEXT NOT NULL, + -- consensus hash of the previous tenure's start-tenure block + prev_tenure_id_consensus_hash TEXT NOT NULL, + -- consensus hash of the last-processed sortition + burn_view_consensus_hash TEXT NOT NULL, + -- whether or not this tenure was triggered by a sortition (as opposed to a tenure-extension). + -- this is equal to the `cause` field in a TenureChange + cause INTEGER NOT NULL, + -- block hash of start-tenure block + block_hash TEXT NOT NULL, + -- block ID of this start block (this is the StacksBlockId of the above tenure_id_consensus_hash and block_hash) + block_id TEXT NOT NULL, + -- this field is the total number of _sortition-induced_ tenures in the chain history (including this tenure), + -- as of the _end_ of this block. A tenure can contain multiple TenureChanges; if so, then this + -- is the height of the _sortition-induced_ TenureChange that created it. + coinbase_height INTEGER NOT NULL, + -- number of blocks this tenure. + -- * for tenure-changes induced by sortitions, this is the number of blocks in the previous tenure + -- * for tenure-changes induced by extension, this is the number of blocks in the current tenure so far. + num_blocks_confirmed INTEGER NOT NULL, + -- this is the ith tenure transaction in its respective Nakamoto chain history. + tenure_index INTEGER NOT NULL, + + PRIMARY KEY(burn_view_consensus_hash,tenure_index) + ); + CREATE INDEX nakamoto_tenures_by_block_id ON nakamoto_tenures(block_id); + CREATE INDEX nakamoto_tenures_by_tenure_id ON nakamoto_tenures(tenure_id_consensus_hash); + CREATE INDEX nakamoto_tenures_by_block_and_consensus_hashes ON nakamoto_tenures(tenure_id_consensus_hash,block_hash); + CREATE INDEX nakamoto_tenures_by_burn_view_consensus_hash ON nakamoto_tenures(burn_view_consensus_hash); + CREATE INDEX nakamoto_tenures_by_tenure_index ON nakamoto_tenures(tenure_index); + CREATE INDEX nakamoto_tenures_by_parent ON nakamoto_tenures(tenure_id_consensus_hash,prev_tenure_id_consensus_hash); +"#; + #[derive(Debug, Clone, PartialEq)] pub struct NakamotoTenure { /// consensus hash of start-tenure block diff --git a/stackslib/src/chainstate/stacks/db/mod.rs b/stackslib/src/chainstate/stacks/db/mod.rs index 9d5979466fd..c5218680d25 100644 --- a/stackslib/src/chainstate/stacks/db/mod.rs +++ b/stackslib/src/chainstate/stacks/db/mod.rs @@ -55,6 +55,7 @@ use crate::chainstate::burn::{ConsensusHash, ConsensusHashExtensions}; use crate::chainstate::nakamoto::{ HeaderTypeNames, NakamotoBlock, NakamotoBlockHeader, NakamotoChainState, NakamotoStagingBlocksConn, NAKAMOTO_CHAINSTATE_SCHEMA_1, NAKAMOTO_CHAINSTATE_SCHEMA_2, + NAKAMOTO_CHAINSTATE_SCHEMA_3, }; use crate::chainstate::stacks::address::StacksAddressExtensions; use crate::chainstate::stacks::boot::*; @@ -1111,6 +1112,13 @@ impl StacksChainState { tx.execute_batch(cmd)?; } } + "5" => { + // migrate to nakamoto 3 + info!("Migrating chainstate schema from version 5 to 6: add STRICT to nakamoto tenure table"); + for cmd in NAKAMOTO_CHAINSTATE_SCHEMA_3.iter() { + tx.execute_batch(cmd)?; + } + } _ => { error!( "Invalid chain state database: expected version = {}, got {}",