Skip to content

Commit b466e50

Browse files
committed
Fix flakiness in tenure_extend_with_other_transactions
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent cc306e5 commit b466e50

File tree

2 files changed

+53
-49
lines changed
  • stackslib/src/chainstate/stacks
  • testnet/stacks-node/src/tests/signer

2 files changed

+53
-49
lines changed

stackslib/src/chainstate/stacks/miner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,7 @@ impl StacksBlockBuilder {
22582258
..
22592259
})
22602260
) {
2261+
debug!("PRINT HERE : {:?}", tx.payload);
22612262
info!("Nakamoto miner heuristic: during tenure change blocks, produce a fast short block to begin tenure");
22622263
return Ok((false, tx_events));
22632264
}

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
@@ -3026,16 +3029,19 @@ fn tenure_extend_with_other_transactions() {
30263029
let idle_timeout = Duration::from_secs(30);
30273030
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new_with_config_modifications(
30283031
num_signers,
3029-
vec![(sender_addr, send_amt + send_fee)],
3032+
vec![(sender_addr, (send_amt + send_fee) * 2)],
30303033
|config| {
30313034
config.tenure_idle_timeout = idle_timeout;
3035+
config.tenure_idle_timeout_buffer = Duration::from_secs(1);
30323036
},
30333037
|config| {
30343038
config.miner.tenure_extend_cost_threshold = 0;
30353039
},
30363040
None,
30373041
None,
30383042
);
3043+
let miner_sk = signer_test.running_nodes.conf.miner.mining_key.unwrap();
3044+
let miner_pk = StacksPublicKey::from_private(&miner_sk);
30393045
let http_origin = format!("http://{}", &signer_test.running_nodes.conf.node.rpc_bind);
30403046

30413047
signer_test.boot_to_epoch_3();
@@ -3046,65 +3052,62 @@ fn tenure_extend_with_other_transactions() {
30463052
info!("Pause miner so it doesn't propose a block before the tenure extend");
30473053
TEST_MINE_STALL.set(true);
30483054

3049-
// Submit a transaction to be included with the tenure extend
3055+
info!("---- Trigger a block proposal but pause its broadcast ----");
3056+
let stacks_tip_height = get_chain_info(&signer_test.running_nodes.conf).stacks_tip_height;
3057+
// Submit a transaction to force a response from signers that indicate that the tenure extend timeout is exceeded
3058+
let mut sender_nonce = 0;
30503059
let transfer_tx = make_stacks_transfer(
30513060
&sender_sk,
3052-
0,
3061+
sender_nonce,
30533062
send_fee,
30543063
signer_test.running_nodes.conf.burnchain.chain_id,
30553064
&recipient,
30563065
send_amt,
30573066
);
3058-
let _tx = submit_tx(&http_origin, &transfer_tx);
3067+
let _ = submit_tx(&http_origin, &transfer_tx);
3068+
sender_nonce += 1;
3069+
3070+
TEST_BROADCAST_PROPOSAL_STALL.set(vec![miner_pk]);
3071+
TEST_MINE_STALL.set(false);
30593072

30603073
info!("---- Wait for tenure extend timeout ----");
3074+
sleep_ms(idle_timeout.as_millis() as u64 + 5);
30613075

3062-
sleep_ms(idle_timeout.as_millis() as u64 + 1000);
3076+
TEST_MINE_STALL.set(true);
3077+
TEST_BROADCAST_PROPOSAL_STALL.set(vec![]);
3078+
// Submit a transaction to be included with the tenure extend
3079+
let transfer_tx = make_stacks_transfer(
3080+
&sender_sk,
3081+
sender_nonce,
3082+
send_fee,
3083+
signer_test.running_nodes.conf.burnchain.chain_id,
3084+
&recipient,
3085+
send_amt,
3086+
);
3087+
let to_find = submit_tx(&http_origin, &transfer_tx);
30633088

3064-
info!("---- Resume miner to propose a block with the tenure extend ----");
3089+
info!("---- Resume miner to propose a block with the tenure extend and transfer tx ----");
30653090
TEST_MINE_STALL.set(false);
3066-
30673091
// Now, wait for a block with a tenure extend
3068-
wait_for(idle_timeout.as_secs() + 10, || {
3069-
let blocks = test_observer::get_blocks();
3070-
let last_block = &blocks.last().unwrap();
3071-
let transactions = last_block["transactions"].as_array().unwrap();
3072-
let (first_tx, other_txs) = transactions.split_first().unwrap();
3073-
let raw_tx = first_tx["raw_tx"].as_str().unwrap();
3074-
let tx_bytes = hex_bytes(&raw_tx[2..]).unwrap();
3075-
let parsed = StacksTransaction::consensus_deserialize(&mut &tx_bytes[..]).unwrap();
3076-
let found_tenure_extend = match &parsed.payload {
3077-
TransactionPayload::TenureChange(payload)
3078-
if payload.cause == TenureChangeCause::Extended =>
3079-
{
3080-
info!("Found tenure extend transaction: {parsed:?}");
3081-
true
3082-
}
3083-
_ => false,
3084-
};
3085-
if found_tenure_extend {
3086-
let found_transfer = other_txs.iter().any(|tx| {
3087-
let raw_tx = 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-
match &parsed.payload {
3091-
TransactionPayload::TokenTransfer(..) => true,
3092-
_ => false,
3093-
}
3094-
});
3095-
if found_transfer {
3096-
info!("Found transfer transaction");
3097-
Ok(true)
3098-
} else {
3099-
Err("No transfer transaction found together with the tenure extend".to_string())
3100-
}
3101-
} else {
3102-
info!("No tenure change transaction found");
3103-
Ok(false)
3104-
}
3105-
})
3092+
let block = wait_for_tenure_change_tx(
3093+
idle_timeout.as_secs() + 10,
3094+
TenureChangeCause::Extended,
3095+
stacks_tip_height + 2,
3096+
)
31063097
.expect("Timed out waiting for a block with a tenure extend");
3107-
3098+
let transactions = block["transactions"].as_array().unwrap();
3099+
assert_eq!(
3100+
transactions.len(),
3101+
2,
3102+
"Expected 2 transactions in the block"
3103+
);
3104+
let tx = transactions[1].as_object().unwrap();
3105+
let txid = tx["txid"].as_str().unwrap();
3106+
assert_eq!(
3107+
txid[2..],
3108+
to_find,
3109+
"Expected the transfer tx to be the second transaction in the block"
3110+
);
31083111
signer_test.shutdown();
31093112
}
31103113

0 commit comments

Comments
 (0)