Skip to content

Commit 1287d88

Browse files
committed
chore: remove some test flake, cleanup db migrations
1 parent cfe8fcd commit 1287d88

File tree

5 files changed

+75
-42
lines changed

5 files changed

+75
-42
lines changed

stackslib/src/chainstate/nakamoto/mod.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -234,44 +234,45 @@ lazy_static! {
234234
UPDATE db_config SET version = "4";
235235
"#.into(),
236236
];
237+
}
237238

238-
pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_2: Vec<String> = vec![
239-
NAKAMOTO_TENURES_SCHEMA_2.into(),
239+
pub static NAKAMOTO_CHAINSTATE_SCHEMA_2: &[&str] = &[
240+
NAKAMOTO_TENURES_SCHEMA_2,
240241
r#"
241242
ALTER TABLE nakamoto_block_headers
242243
ADD COLUMN timestamp INTEGER NOT NULL;
243-
"#.into(),
244+
"#,
244245
r#"
245246
UPDATE db_config SET version = "5";
246-
"#.into(),
247-
// make burn_view NULLable. We could use a default value, but NULL should be safer (because it will error).
248-
// there should be no entries in nakamoto_block_headers with a NULL entry when this column is added, because
249-
// nakamoto blocks have not been produced yet.
247+
"#,
248+
// make burn_view NULLable. We could use a default value, but NULL should be safer (because it will error).
249+
// there should be no entries in nakamoto_block_headers with a NULL entry when this column is added, because
250+
// nakamoto blocks have not been produced yet.
250251
r#"
251252
ALTER TABLE nakamoto_block_headers
252253
ADD COLUMN burn_view TEXT;
253-
"#.into(),
254-
];
254+
"#,
255+
];
255256

256-
pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_3: Vec<String> = vec![
257-
NAKAMOTO_TENURES_SCHEMA_3.into(),
257+
pub static NAKAMOTO_CHAINSTATE_SCHEMA_3: &[&str] = &[
258+
NAKAMOTO_TENURES_SCHEMA_3,
258259
r#"
259260
UPDATE db_config SET version = "6";
260-
"#.into(),
261-
// Add a `height_in_tenure` field to the block header row, so we know how high this block is
262-
// within its tenure. This is needed to process malleablized Nakamoto blocks with the same
263-
// height, as well as accidental forks that can arise from slow miners.
264-
//
265-
//
266-
//
267-
// No default value is needed because at the time of this writing, this table is actually empty.
261+
"#,
262+
// Add a `height_in_tenure` field to the block header row, so we know how high this block is
263+
// within its tenure. This is needed to process malleablized Nakamoto blocks with the same
264+
// height, as well as accidental forks that can arise from slow miners.
265+
//
266+
//
267+
//
268+
// No default value is needed because at the time of this writing, this table is actually empty.
268269
r#"
269270
ALTER TABLE nakamoto_block_headers
270271
ADD COLUMN height_in_tenure;
271-
"#.into(),
272-
];
272+
"#,
273+
];
273274

274-
pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_4: [&'static str; 2] = [
275+
pub static NAKAMOTO_CHAINSTATE_SCHEMA_4: &[&str] = &[
275276
r#"
276277
UPDATE db_config SET version = "7";
277278
"#,
@@ -289,16 +290,22 @@ lazy_static! {
289290
PRIMARY KEY(public_key,reward_cycle)
290291
);
291292
"#,
292-
];
293+
];
293294

294-
pub static ref NAKAMOTO_CHAINSTATE_SCHEMA_5: [&'static str; 2] = [
295+
pub static NAKAMOTO_CHAINSTATE_SCHEMA_5: &[&str] = &[
295296
r#"
296297
UPDATE db_config SET version = "8";
297298
"#,
298299
// Add an index for index block hash in nakamoto block headers
299300
"CREATE INDEX IF NOT EXISTS index_block_hash ON nakamoto_block_headers(index_block_hash);",
300-
];
301-
}
301+
];
302+
303+
pub static NAKAMOTO_CHAINSTATE_SCHEMA_6: &[&str] = &[
304+
// schema change is JUST a new index, but the index is on a table
305+
// created by a migration, so don't add the index to the CHAINSTATE_INDEXES
306+
r#"UPDATE db_config SET version = "10";"#,
307+
"CREATE INDEX IF NOT EXISTS nakamoto_block_headers_by_ch_bv ON nakamoto_block_headers(consensus_hash, burn_view);"
308+
];
302309

303310
#[cfg(test)]
304311
mod fault_injection {

stackslib/src/chainstate/stacks/db/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::chainstate::nakamoto::{
5656
HeaderTypeNames, NakamotoBlock, NakamotoBlockHeader, NakamotoChainState,
5757
NakamotoStagingBlocksConn, NAKAMOTO_CHAINSTATE_SCHEMA_1, NAKAMOTO_CHAINSTATE_SCHEMA_2,
5858
NAKAMOTO_CHAINSTATE_SCHEMA_3, NAKAMOTO_CHAINSTATE_SCHEMA_4, NAKAMOTO_CHAINSTATE_SCHEMA_5,
59+
NAKAMOTO_CHAINSTATE_SCHEMA_6,
5960
};
6061
use crate::chainstate::stacks::address::StacksAddressExtensions;
6162
use crate::chainstate::stacks::boot::*;
@@ -862,13 +863,6 @@ const CHAINSTATE_SCHEMA_4: &[&str] = &[
862863
"#,
863864
];
864865

865-
const CHAINSTATE_SCHEMA_5: &[&str] = &[
866-
// schema change is JUST a new index, but the index is on a table
867-
// created by a migration, so don't add the index to the CHAINSTATE_INDEXES
868-
r#"UPDATE db_config SET version = "10";"#,
869-
"CREATE INDEX IF NOT EXISTS nakamoto_block_headers_by_ch_bv ON nakamoto_block_headers(consensus_hash, burn_view);"
870-
];
871-
872866
const CHAINSTATE_INDEXES: &[&str] = &[
873867
"CREATE INDEX IF NOT EXISTS index_block_hash_to_primary_key ON block_headers(index_block_hash,consensus_hash,block_hash);",
874868
"CREATE INDEX IF NOT EXISTS block_headers_hash_index ON block_headers(block_hash,block_height);",
@@ -1133,7 +1127,7 @@ impl StacksChainState {
11331127
info!(
11341128
"Migrating chainstate schema from version 9 to 10: add index for nakamoto_block_headers"
11351129
);
1136-
for cmd in CHAINSTATE_SCHEMA_5.iter() {
1130+
for cmd in NAKAMOTO_CHAINSTATE_SCHEMA_6.iter() {
11371131
tx.execute_batch(cmd)?;
11381132
}
11391133
}

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@ impl RelayerThread {
16881688
last_committed.burn_tip.block_height,
16891689
tip_height,
16901690
last_committed.block_commit.burn_fee,
1691+
&last_committed.tenure_consensus_hash,
16911692
);
16921693
self.last_committed = Some(last_committed);
16931694

testnet/stacks-node/src/run_loop/neon.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use libc;
1010
use stacks::burnchains::bitcoin::address::{BitcoinAddress, LegacyBitcoinAddressType};
1111
use stacks::burnchains::{Burnchain, Error as burnchain_error};
1212
use stacks::chainstate::burn::db::sortdb::SortitionDB;
13-
use stacks::chainstate::burn::BlockSnapshot;
13+
use stacks::chainstate::burn::{BlockSnapshot, ConsensusHash};
1414
use stacks::chainstate::coordinator::comm::{CoordinatorChannels, CoordinatorReceivers};
1515
use stacks::chainstate::coordinator::{
1616
migrate_chainstate_dbs, static_get_canonical_affirmation_map,
@@ -50,6 +50,14 @@ use crate::{
5050

5151
pub const STDERR: i32 = 2;
5252

53+
#[cfg(test)]
54+
#[derive(Clone, Default)]
55+
pub struct RunLoopField<T>(pub Arc<Mutex<T>>);
56+
57+
#[cfg(not(test))]
58+
#[derive(Clone, Default)]
59+
pub struct RunLoopField<T>(pub std::marker::PhantomData<T>);
60+
5361
#[cfg(test)]
5462
#[derive(Clone)]
5563
pub struct RunLoopCounter(pub Arc<AtomicU64>);
@@ -75,6 +83,13 @@ impl Default for RunLoopCounter {
7583
}
7684
}
7785

86+
impl<T: Clone> RunLoopField<Option<T>> {
87+
#[cfg(test)]
88+
pub fn get(&self) -> T {
89+
self.0.lock().unwrap().clone().unwrap()
90+
}
91+
}
92+
7893
impl RunLoopCounter {
7994
#[cfg(test)]
8095
pub fn get(&self) -> u64 {
@@ -118,6 +133,7 @@ pub struct Counters {
118133
pub naka_miner_directives: RunLoopCounter,
119134
pub naka_submitted_commit_last_stacks_tip: RunLoopCounter,
120135
pub naka_submitted_commit_last_commit_amount: RunLoopCounter,
136+
pub naka_submitted_commit_last_parent_tenure_id: RunLoopField<Option<ConsensusHash>>,
121137

122138
pub naka_miner_current_rejections: RunLoopCounter,
123139
pub naka_miner_current_rejections_timeout_secs: RunLoopCounter,
@@ -147,6 +163,15 @@ impl Counters {
147163
#[cfg(not(test))]
148164
fn set(_ctr: &RunLoopCounter, _value: u64) {}
149165

166+
#[cfg(test)]
167+
fn update<T: Clone>(ctr: &RunLoopField<Option<T>>, value: &T) {
168+
let mut mutex = ctr.0.lock().expect("FATAL: test counter mutext poisoned");
169+
let _ = mutex.replace(value.clone());
170+
}
171+
172+
#[cfg(not(test))]
173+
fn update<T: Clone>(_ctr: &RunLoopField<Option<T>>, _value: &T) {}
174+
150175
pub fn bump_blocks_processed(&self) {
151176
Counters::inc(&self.blocks_processed);
152177
}
@@ -180,6 +205,7 @@ impl Counters {
180205
committed_burn_height: u64,
181206
committed_stacks_height: u64,
182207
committed_sats_amount: u64,
208+
committed_parent_tenure_id: &ConsensusHash,
183209
) {
184210
Counters::inc(&self.naka_submitted_commits);
185211
Counters::set(
@@ -194,6 +220,10 @@ impl Counters {
194220
&self.naka_submitted_commit_last_commit_amount,
195221
committed_sats_amount,
196222
);
223+
Counters::update(
224+
&self.naka_submitted_commit_last_parent_tenure_id,
225+
committed_parent_tenure_id,
226+
);
197227
}
198228

199229
pub fn bump_naka_mined_blocks(&self) {

testnet/stacks-node/src/tests/signer/v0.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15307,6 +15307,8 @@ fn bitcoin_reorg_extended_tenure() {
1530715307
},
1530815308
|_| {},
1530915309
|config| {
15310+
// we will interpose with the testproxy on the second node's bitcoind
15311+
// connection, so that we can shut off communication before the reorg.
1531015312
config.burnchain.rpc_port = 28132;
1531115313
config.burnchain.peer_port = 28133;
1531215314
},
@@ -15353,19 +15355,18 @@ fn bitcoin_reorg_extended_tenure() {
1535315355

1535415356
miners.boot_to_epoch_3();
1535515357

15356-
let info = get_chain_info(&conf_1);
15357-
1535815358
miners
1535915359
.signer_test
1536015360
.submit_burn_block_contract_and_wait(&miners.sender_sk)
1536115361
.expect("Timed out waiting for contract publish");
1536215362

15363+
let info = get_chain_info(&conf_1);
15364+
1536315365
wait_for(60, || {
15364-
Ok(
15365-
rl1_counters.naka_submitted_commit_last_burn_height.get() >= info.burn_block_height
15366-
&& rl1_counters.naka_submitted_commit_last_stacks_tip.get()
15367-
>= info.stacks_tip_height,
15368-
)
15366+
Ok(rl1_counters
15367+
.naka_submitted_commit_last_parent_tenure_id
15368+
.get()
15369+
== info.stacks_tip_consensus_hash)
1536915370
})
1537015371
.expect("Timed out waiting for commits from Miner 1 for Tenure 1 of the test");
1537115372

0 commit comments

Comments
 (0)