Skip to content

Commit e53547c

Browse files
authored
Merge pull request #5233 from stacks-network/chore/add-0th-tenure-block-signing-test
Add signing_in_0th_tenure_of_reward_cycle test
2 parents de29c64 + e809f19 commit e53547c

File tree

2 files changed

+134
-68
lines changed

2 files changed

+134
-68
lines changed

.github/workflows/bitcoin-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ jobs:
114114
- tests::signer::v0::partial_tenure_fork
115115
- tests::signer::v0::mine_2_nakamoto_reward_cycles
116116
- tests::signer::v0::signer_set_rollover
117+
- tests::signer::v0::signing_in_0th_tenure_of_reward_cycle
117118
- tests::nakamoto_integrations::burn_ops_integration_test
118119
- tests::nakamoto_integrations::check_block_heights
119120
- tests::nakamoto_integrations::clarity_burn_state

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

Lines changed: 133 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use stacks::chainstate::stacks::db::{StacksBlockHeaderTypes, StacksChainState, S
3535
use stacks::codec::StacksMessageCodec;
3636
use stacks::core::{StacksEpochId, CHAIN_ID_TESTNET};
3737
use stacks::libstackerdb::StackerDBChunkData;
38+
use stacks::net::api::getsigner::GetSignerResponse;
3839
use stacks::net::api::postblock_proposal::{ValidateRejectCode, TEST_VALIDATE_STALL};
3940
use stacks::net::relay::fault_injection::set_ignore_block;
4041
use stacks::types::chainstate::{StacksAddress, StacksBlockId, StacksPrivateKey, StacksPublicKey};
@@ -1991,35 +1992,29 @@ fn end_of_tenure() {
19911992
std::thread::sleep(Duration::from_millis(100));
19921993
}
19931994

1994-
while signer_test.get_current_reward_cycle() != final_reward_cycle {
1995-
next_block_and(
1996-
&mut signer_test.running_nodes.btc_regtest_controller,
1997-
10,
1998-
|| Ok(true),
1999-
)
2000-
.unwrap();
2001-
assert!(
2002-
start_time.elapsed() <= short_timeout,
2003-
"Timed out waiting to enter the next reward cycle"
2004-
);
2005-
std::thread::sleep(Duration::from_millis(100));
2006-
}
1995+
wait_for(short_timeout.as_secs(), || {
1996+
let result = signer_test.get_current_reward_cycle() == final_reward_cycle;
1997+
if !result {
1998+
signer_test
1999+
.running_nodes
2000+
.btc_regtest_controller
2001+
.build_next_block(1);
2002+
}
2003+
Ok(result)
2004+
})
2005+
.expect("Timed out waiting to enter the next reward cycle");
20072006

2008-
while test_observer::get_burn_blocks()
2009-
.last()
2010-
.unwrap()
2011-
.get("burn_block_height")
2012-
.unwrap()
2013-
.as_u64()
2014-
.unwrap()
2015-
< final_reward_cycle_height_boundary + 1
2016-
{
2017-
assert!(
2018-
start_time.elapsed() <= short_timeout,
2019-
"Timed out waiting for burn block events"
2020-
);
2021-
std::thread::sleep(Duration::from_millis(100));
2022-
}
2007+
wait_for(short_timeout.as_secs(), || {
2008+
let blocks = test_observer::get_burn_blocks()
2009+
.last()
2010+
.unwrap()
2011+
.get("burn_block_height")
2012+
.unwrap()
2013+
.as_u64()
2014+
.unwrap();
2015+
Ok(blocks > final_reward_cycle_height_boundary)
2016+
})
2017+
.expect("Timed out waiting for burn block events");
20232018

20242019
signer_test.wait_for_cycle(30, final_reward_cycle);
20252020

@@ -2077,21 +2072,11 @@ fn retry_on_rejection() {
20772072
let burnchain = signer_test.running_nodes.conf.get_burnchain();
20782073
let sortdb = burnchain.open_sortition_db(true).unwrap();
20792074

2080-
loop {
2081-
next_block_and(
2082-
&mut signer_test.running_nodes.btc_regtest_controller,
2083-
60,
2084-
|| Ok(true),
2085-
)
2086-
.unwrap();
2087-
2088-
sleep_ms(10_000);
2089-
2075+
wait_for(30, || {
20902076
let tip = SortitionDB::get_canonical_burn_chain_tip(&sortdb.conn()).unwrap();
2091-
if tip.sortition {
2092-
break;
2093-
}
2094-
}
2077+
Ok(tip.sortition)
2078+
})
2079+
.expect("Timed out waiting for sortition");
20952080

20962081
// mine a nakamoto block
20972082
let mined_blocks = signer_test.running_nodes.nakamoto_blocks_mined.clone();
@@ -2533,12 +2518,10 @@ fn mock_sign_epoch_25() {
25332518
{
25342519
let mut mock_block_mesage = None;
25352520
let mock_poll_time = Instant::now();
2536-
next_block_and(
2537-
&mut signer_test.running_nodes.btc_regtest_controller,
2538-
60,
2539-
|| Ok(true),
2540-
)
2541-
.unwrap();
2521+
signer_test
2522+
.running_nodes
2523+
.btc_regtest_controller
2524+
.build_next_block(1);
25422525
let current_burn_block_height = signer_test
25432526
.running_nodes
25442527
.btc_regtest_controller
@@ -2746,12 +2729,10 @@ fn multiple_miners_mock_sign_epoch_25() {
27462729
{
27472730
let mut mock_block_mesage = None;
27482731
let mock_poll_time = Instant::now();
2749-
next_block_and(
2750-
&mut signer_test.running_nodes.btc_regtest_controller,
2751-
60,
2752-
|| Ok(true),
2753-
)
2754-
.unwrap();
2732+
signer_test
2733+
.running_nodes
2734+
.btc_regtest_controller
2735+
.build_next_block(1);
27552736
let current_burn_block_height = signer_test
27562737
.running_nodes
27572738
.btc_regtest_controller
@@ -4538,21 +4519,11 @@ fn miner_recovers_when_broadcast_block_delay_across_tenures_occurs() {
45384519
let burnchain = signer_test.running_nodes.conf.get_burnchain();
45394520
let sortdb = burnchain.open_sortition_db(true).unwrap();
45404521

4541-
loop {
4542-
next_block_and(
4543-
&mut signer_test.running_nodes.btc_regtest_controller,
4544-
60,
4545-
|| Ok(true),
4546-
)
4547-
.unwrap();
4548-
4549-
sleep_ms(10_000);
4550-
4522+
wait_for(30, || {
45514523
let tip = SortitionDB::get_canonical_burn_chain_tip(&sortdb.conn()).unwrap();
4552-
if tip.sortition {
4553-
break;
4554-
}
4555-
}
4524+
Ok(tip.sortition)
4525+
})
4526+
.expect("Timed out waiting for sortition");
45564527

45574528
// submit a tx so that the miner will mine a stacks block
45584529
let mut sender_nonce = 0;
@@ -4813,3 +4784,97 @@ fn miner_recovers_when_broadcast_block_delay_across_tenures_occurs() {
48134784
assert_eq!(info_after.stacks_tip.to_string(), block_n_2.block_hash);
48144785
assert_ne!(block_n_2, block_n);
48154786
}
4787+
4788+
#[test]
4789+
#[ignore]
4790+
/// Test that signers can successfully sign a block proposal in the 0th tenure of a reward cycle
4791+
/// This ensures there is no race condition in the /v2/pox endpoint which could prevent it from updating
4792+
/// on time, possibly triggering an "off by one" like behaviour in the 0th tenure.
4793+
///
4794+
fn signing_in_0th_tenure_of_reward_cycle() {
4795+
if env::var("BITCOIND_TEST") != Ok("1".into()) {
4796+
return;
4797+
}
4798+
4799+
tracing_subscriber::registry()
4800+
.with(fmt::layer())
4801+
.with(EnvFilter::from_default_env())
4802+
.init();
4803+
4804+
info!("------------------------- Test Setup -------------------------");
4805+
let num_signers = 5;
4806+
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new(num_signers, vec![]);
4807+
let signer_public_keys = signer_test
4808+
.signer_stacks_private_keys
4809+
.iter()
4810+
.map(StacksPublicKey::from_private)
4811+
.collect::<Vec<_>>();
4812+
let long_timeout = Duration::from_secs(200);
4813+
signer_test.boot_to_epoch_3();
4814+
let curr_reward_cycle = signer_test.get_current_reward_cycle();
4815+
let next_reward_cycle = curr_reward_cycle + 1;
4816+
// Mine until the boundary of the first full Nakamoto reward cycles (epoch 3 starts in the middle of one)
4817+
let next_reward_cycle_height_boundary = signer_test
4818+
.running_nodes
4819+
.btc_regtest_controller
4820+
.get_burnchain()
4821+
.reward_cycle_to_block_height(next_reward_cycle)
4822+
.saturating_sub(1);
4823+
4824+
info!("------------------------- Advancing to {next_reward_cycle} Boundary at Block {next_reward_cycle_height_boundary} -------------------------");
4825+
signer_test.run_until_burnchain_height_nakamoto(
4826+
long_timeout,
4827+
next_reward_cycle_height_boundary,
4828+
num_signers,
4829+
);
4830+
4831+
let http_origin = format!("http://{}", &signer_test.running_nodes.conf.node.rpc_bind);
4832+
let get_v3_signer = |pubkey: &Secp256k1PublicKey, reward_cycle: u64| {
4833+
let url = &format!(
4834+
"{http_origin}/v3/signer/{pk}/{reward_cycle}",
4835+
pk = pubkey.to_hex()
4836+
);
4837+
info!("Send request: GET {url}");
4838+
reqwest::blocking::get(url)
4839+
.unwrap_or_else(|e| panic!("GET request failed: {e}"))
4840+
.json::<GetSignerResponse>()
4841+
.unwrap()
4842+
.blocks_signed
4843+
};
4844+
4845+
assert_eq!(signer_test.get_current_reward_cycle(), curr_reward_cycle);
4846+
4847+
for signer in &signer_public_keys {
4848+
let blocks_signed = get_v3_signer(&signer, next_reward_cycle);
4849+
assert_eq!(blocks_signed, 0);
4850+
}
4851+
4852+
info!("------------------------- Enter Reward Cycle {next_reward_cycle} -------------------------");
4853+
for signer in &signer_public_keys {
4854+
let blocks_signed = get_v3_signer(&signer, next_reward_cycle);
4855+
assert_eq!(blocks_signed, 0);
4856+
}
4857+
let blocks_before = signer_test
4858+
.running_nodes
4859+
.nakamoto_blocks_mined
4860+
.load(Ordering::SeqCst);
4861+
signer_test
4862+
.running_nodes
4863+
.btc_regtest_controller
4864+
.build_next_block(1);
4865+
4866+
wait_for(30, || {
4867+
Ok(signer_test
4868+
.running_nodes
4869+
.nakamoto_blocks_mined
4870+
.load(Ordering::SeqCst)
4871+
> blocks_before)
4872+
})
4873+
.unwrap();
4874+
4875+
for signer in &signer_public_keys {
4876+
let blocks_signed = get_v3_signer(&signer, next_reward_cycle);
4877+
assert_eq!(blocks_signed, 1);
4878+
}
4879+
assert_eq!(signer_test.get_current_reward_cycle(), next_reward_cycle);
4880+
}

0 commit comments

Comments
 (0)