Skip to content

Conversation

hstove
Copy link
Contributor

@hstove hstove commented Sep 18, 2025

This adds logic to an edge case where:

  • a miner wins a sortition
  • the miner's commit points to a stale tip
  • the miner also won the previous sortition

Previously, the miner would issue a normal tenure change, which would attempt (and fail) to reorg itself. This PR fixes the logic such that the miner will issue an extension.

kantai
kantai previously approved these changes Sep 18, 2025
Copy link
Contributor

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'll update with latest develop so it should pass the clippy checks.

obycode
obycode previously approved these changes Sep 18, 2025
Copy link
Contributor

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@hstove hstove dismissed stale reviews from obycode and kantai via 8c828f6 September 19, 2025 00:49
@hstove
Copy link
Contributor Author

hstove commented Sep 19, 2025

Pulling in develop led to a compile error due to a return type changing - fixed in 8c828f6!

@hstove hstove requested review from obycode and kantai September 19, 2025 00:50
jferrant
jferrant previously approved these changes Sep 19, 2025
obycode
obycode previously approved these changes Sep 19, 2025
kantai
kantai previously approved these changes Sep 19, 2025
@jferrant jferrant added this pull request to the merge queue Sep 19, 2025
@hstove hstove removed this pull request from the merge queue due to a manual request Sep 19, 2025
@hstove
Copy link
Contributor Author

hstove commented Sep 19, 2025

These integration tests aren't just flaky, looks like I actually broke them. Let me spend some time on this. Sorry!

jcnelson
jcnelson previously approved these changes Sep 22, 2025
@jcnelson
Copy link
Member

LGTM once the tests are fixed :)

@hstove hstove dismissed stale reviews from jcnelson, kantai, obycode, and jferrant via 7110865 September 23, 2025 18:57
"blocks_before" => blocks_before,
"blocks_processed" => blocks_processed,
"blocks_processed_before" => blocks_processed_before,
"block_in_tenure" => block_in_tenure,
Copy link
Contributor

@jferrant jferrant Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you are waiting on Aaron's feedback , but I think the miner views Tenure C as invalid and now builds off of block b because it was the prior miner. So its no longer seeing block b as a fork as this test is trying to do. It sees it as the last valid mined block and so it extends its prior tenure B rather than mining Tenure C. i.e. I don't think its really a forking test anymore. in a multi miner setup, I would expect the prior miner to extend Tenure B and the new incoming miner be unable to mine at all. This makes me wonder if we even should have the timeout anymore for signers to enable an incoming miner to reorg a poorly timed block. Or does that depend on signer global state being merged to remove this timeout?

use clarity::boot_util::boot_code_addr;
use clarity::vm::costs::{ExecutionCost, LimitedCostTracker};
use clarity::vm::representations::ContractName;
use clarity::vm::types::{PrincipalData, QualifiedContractIdentifier, StandardPrincipalData};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, in the "Files changed" view, this shows on the correct line.

@hstove
Copy link
Contributor Author

hstove commented Sep 30, 2025

Reviewers: I've added a new integration test, which is a MultipleMinersTest that more robustly checks the behavior we're verifying here. I haven't removed any of the older tests - the single miner test I think is actually helpful (and closer to the bug that triggered this ticket originally). The nakamoto_integrations test could be removed, I think, but I'd want another opinion first.

/// - Miner B wins tenure B, with 2 blocks
/// - Miner A wins tenure C, but with a block commit to tip A
/// - We verify that Miner B extends Tenure B
fn tenure_extend_after_stale_commit_different_miner() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might already actually have this test in tenure_extend_after_bad_commit? Not 100% sure. Your test seems more clear though so not against replacing it, but prob would delete the old test (though the old one also confirms that Miner A can win the following one after the extended Tenure B and correctly builds so you might want to expand this test if you do decide to replace)

@jferrant
Copy link
Contributor

jferrant commented Sep 30, 2025

I think the nakamoto test is kinda nice to have in place even since it removes the signers from the equation and just test the miner functionality solo basically. But I think if you keep it, it needs to have its description updated since it now confirms that tenure_C builds actually on b_0 NOT a_x. Also, it prob should be renamed to "bad_commit_does_not_trigger_fork" or something like that? EDIT: granted I guess a bad commit COULD trigger a fork in a multiple miner set up (see allow_reorg_within_first_proposal_burn_block_timing_secs) Can't wait for global signer state. Wonder if at that point we could remove first_proposal_burn_block_timing entirely and ensure we never fork unless bitcoin forks...

obycode
obycode previously approved these changes Oct 1, 2025
Copy link
Contributor

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@hstove
Copy link
Contributor Author

hstove commented Oct 2, 2025

@jferrant agreed regarding renaming the test (and fixing the description) - updated!

jferrant
jferrant previously approved these changes Oct 2, 2025
@jferrant jferrant self-requested a review October 2, 2025 18:24
@jferrant
Copy link
Contributor

jferrant commented Oct 2, 2025

@jferrant agreed regarding renaming the test (and fixing the description) - updated!

I still think you have the duplicate test, but will approve once that is updated/removed. See tenure_extend_after_bad_commit vs your new tenure_extend_after_stale_commit_different_miner

Copy link

@aaronb-stacks aaronb-stacks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM!

The changes to the forked_tenure_is_ignored() tests make sense to me. The scenario those tested are now handled differently (i.e., we expect a tenure-extend). The assertion that those tests were making (that the miner always tries to build off of their committed-to tenure) is now covered by your new test tenure_extend_after_stale_commit_different_miner (by "Miner 1's proposal for C is rejected"), which I think is the right place for it.

@hstove
Copy link
Contributor Author

hstove commented Oct 6, 2025

@jferrant I realized that the tests were doing two slightly separate things - one was verifying that we wouldn't reorg based on there being 2 blocks in a tenure, and another is based on the reorg attempt being longer than first_proposal_burn_block_timing. I've updated the test descriptions to reflect this

@hstove hstove requested a review from aaronb-stacks October 6, 2025 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Miner should extend tenure N after winning tenure N+1 if tenure N+1 attempts to reorg N
6 participants