Skip to content

Commit

Permalink
migrate schema of nakamoto tenure table to STRICT
Browse files Browse the repository at this point in the history
  • Loading branch information
ASuciuX committed Jul 5, 2024
1 parent 18fd4a5 commit cdf3398
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -240,6 +241,13 @@ lazy_static! {
ADD COLUMN burn_view TEXT;
"#.into(),
];

pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_3: Vec<String> = vec![
NAKAMOTO_TENURES_SCHEMA_3.into(),
r#"
UPDATE db_config SET version = "6";
"#.into(),
];
}

/// Matured miner reward schedules
Expand Down
40 changes: 40 additions & 0 deletions stackslib/src/chainstate/nakamoto/tenure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions stackslib/src/chainstate/stacks/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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 {}",
Expand Down

0 comments on commit cdf3398

Please sign in to comment.