Skip to content

Commit 3fa16aa

Browse files
authored
Merge pull request #5844 from stacks-network/fix/tenure-extend-with-other-transactions
Fix flakiness in tenure_extend_with_other_transactions
2 parents 60bcb5c + 8475ca7 commit 3fa16aa

File tree

1 file changed

+52
-49
lines changed
  • testnet/stacks-node/src/tests/signer

1 file changed

+52
-49
lines changed

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

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl MultipleMinerTest {
711711
sortdb: &SortitionDB,
712712
cause: TenureChangeCause,
713713
timeout_secs: u64,
714-
) -> Result<(), String> {
714+
) -> Result<serde_json::Value, String> {
715715
let start = Instant::now();
716716
let stacks_height_before = self.get_peer_stacks_tip_height();
717717
self.mine_bitcoin_blocks_and_confirm(sortdb, 1, timeout_secs)?;
@@ -939,7 +939,8 @@ fn wait_for_tenure_change_tx(
939939
timeout_secs: u64,
940940
cause: TenureChangeCause,
941941
expected_height: u64,
942-
) -> Result<(), String> {
942+
) -> Result<serde_json::Value, String> {
943+
let mut result = None;
943944
wait_for(timeout_secs, || {
944945
let blocks = test_observer::get_blocks();
945946
for block in blocks {
@@ -954,14 +955,16 @@ fn wait_for_tenure_change_tx(
954955
if let TransactionPayload::TenureChange(payload) = &parsed.payload {
955956
if payload.cause == cause {
956957
info!("Found tenure change transaction: {parsed:?}");
958+
result = Some(block);
957959
return Ok(true);
958960
}
959961
}
960962
}
961963
}
962964
}
963965
Ok(false)
964-
})
966+
})?;
967+
Ok(result.unwrap())
965968
}
966969

967970
/// Waits for a block proposal to be observed in the test_observer stackerdb chunks at the expected height
@@ -3040,16 +3043,19 @@ fn tenure_extend_with_other_transactions() {
30403043
let idle_timeout = Duration::from_secs(30);
30413044
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new_with_config_modifications(
30423045
num_signers,
3043-
vec![(sender_addr, send_amt + send_fee)],
3046+
vec![(sender_addr, (send_amt + send_fee) * 2)],
30443047
|config| {
30453048
config.tenure_idle_timeout = idle_timeout;
3049+
config.tenure_idle_timeout_buffer = Duration::from_secs(1);
30463050
},
30473051
|config| {
30483052
config.miner.tenure_extend_cost_threshold = 0;
30493053
},
30503054
None,
30513055
None,
30523056
);
3057+
let miner_sk = signer_test.running_nodes.conf.miner.mining_key.unwrap();
3058+
let miner_pk = StacksPublicKey::from_private(&miner_sk);
30533059
let http_origin = format!("http://{}", &signer_test.running_nodes.conf.node.rpc_bind);
30543060

30553061
signer_test.boot_to_epoch_3();
@@ -3060,65 +3066,62 @@ fn tenure_extend_with_other_transactions() {
30603066
info!("Pause miner so it doesn't propose a block before the tenure extend");
30613067
TEST_MINE_STALL.set(true);
30623068

3063-
// Submit a transaction to be included with the tenure extend
3069+
info!("---- Trigger a block proposal but pause its broadcast ----");
3070+
let stacks_tip_height = get_chain_info(&signer_test.running_nodes.conf).stacks_tip_height;
3071+
// Submit a transaction to force a response from signers that indicate that the tenure extend timeout is exceeded
3072+
let mut sender_nonce = 0;
30643073
let transfer_tx = make_stacks_transfer(
30653074
&sender_sk,
3066-
0,
3075+
sender_nonce,
30673076
send_fee,
30683077
signer_test.running_nodes.conf.burnchain.chain_id,
30693078
&recipient,
30703079
send_amt,
30713080
);
3072-
let _tx = submit_tx(&http_origin, &transfer_tx);
3081+
let _ = submit_tx(&http_origin, &transfer_tx);
3082+
sender_nonce += 1;
3083+
3084+
TEST_BROADCAST_PROPOSAL_STALL.set(vec![miner_pk]);
3085+
TEST_MINE_STALL.set(false);
30733086

30743087
info!("---- Wait for tenure extend timeout ----");
3088+
sleep_ms(idle_timeout.as_millis() as u64 + 5);
30753089

3076-
sleep_ms(idle_timeout.as_millis() as u64 + 1000);
3090+
TEST_MINE_STALL.set(true);
3091+
TEST_BROADCAST_PROPOSAL_STALL.set(vec![]);
3092+
// Submit a transaction to be included with the tenure extend
3093+
let transfer_tx = make_stacks_transfer(
3094+
&sender_sk,
3095+
sender_nonce,
3096+
send_fee,
3097+
signer_test.running_nodes.conf.burnchain.chain_id,
3098+
&recipient,
3099+
send_amt,
3100+
);
3101+
let to_find = submit_tx(&http_origin, &transfer_tx);
30773102

3078-
info!("---- Resume miner to propose a block with the tenure extend ----");
3103+
info!("---- Resume miner to propose a block with the tenure extend and transfer tx ----");
30793104
TEST_MINE_STALL.set(false);
3080-
30813105
// Now, wait for a block with a tenure extend
3082-
wait_for(idle_timeout.as_secs() + 10, || {
3083-
let blocks = test_observer::get_blocks();
3084-
let last_block = &blocks.last().unwrap();
3085-
let transactions = last_block["transactions"].as_array().unwrap();
3086-
let (first_tx, other_txs) = transactions.split_first().unwrap();
3087-
let raw_tx = first_tx["raw_tx"].as_str().unwrap();
3088-
let tx_bytes = hex_bytes(&raw_tx[2..]).unwrap();
3089-
let parsed = StacksTransaction::consensus_deserialize(&mut &tx_bytes[..]).unwrap();
3090-
let found_tenure_extend = match &parsed.payload {
3091-
TransactionPayload::TenureChange(payload)
3092-
if payload.cause == TenureChangeCause::Extended =>
3093-
{
3094-
info!("Found tenure extend transaction: {parsed:?}");
3095-
true
3096-
}
3097-
_ => false,
3098-
};
3099-
if found_tenure_extend {
3100-
let found_transfer = other_txs.iter().any(|tx| {
3101-
let raw_tx = tx["raw_tx"].as_str().unwrap();
3102-
let tx_bytes = hex_bytes(&raw_tx[2..]).unwrap();
3103-
let parsed = StacksTransaction::consensus_deserialize(&mut &tx_bytes[..]).unwrap();
3104-
match &parsed.payload {
3105-
TransactionPayload::TokenTransfer(..) => true,
3106-
_ => false,
3107-
}
3108-
});
3109-
if found_transfer {
3110-
info!("Found transfer transaction");
3111-
Ok(true)
3112-
} else {
3113-
Err("No transfer transaction found together with the tenure extend".to_string())
3114-
}
3115-
} else {
3116-
info!("No tenure change transaction found");
3117-
Ok(false)
3118-
}
3119-
})
3106+
let block = wait_for_tenure_change_tx(
3107+
idle_timeout.as_secs() + 10,
3108+
TenureChangeCause::Extended,
3109+
stacks_tip_height + 2,
3110+
)
31203111
.expect("Timed out waiting for a block with a tenure extend");
3121-
3112+
let transactions = block["transactions"].as_array().unwrap();
3113+
assert!(
3114+
transactions.len() > 1,
3115+
"Expected at least 2 transactions in the block"
3116+
);
3117+
assert!(
3118+
transactions.iter().any(|tx| {
3119+
let tx = tx.as_object().unwrap();
3120+
let txid = tx["txid"].as_str().unwrap();
3121+
txid[2..] == to_find
3122+
}),
3123+
"Failed to find the transfer tx in the block"
3124+
);
31223125
signer_test.shutdown();
31233126
}
31243127

0 commit comments

Comments
 (0)