Skip to content

Commit a83067d

Browse files
committed
Merge branch 'develop' of https://github.com/stacks-network/stacks-core into fix/clippy-ci-stacks-lib-int-plus-one
2 parents 5445fc6 + 8c23c0e commit a83067d

File tree

120 files changed

+1651
-958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1651
-958
lines changed

.github/workflows/bitcoin-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ jobs:
135135
- tests::signer::v0::tenure_extend_after_bad_commit
136136
- tests::signer::v0::block_proposal_max_age_rejections
137137
- tests::signer::v0::global_acceptance_depends_on_block_announcement
138+
- tests::signer::v0::no_reorg_due_to_successive_block_validation_ok
138139
- tests::nakamoto_integrations::burn_ops_integration_test
139140
- tests::nakamoto_integrations::check_block_heights
140141
- tests::nakamoto_integrations::clarity_burn_state

libsigner/src/v0/messages.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,22 @@ impl BlockResponse {
686686
BlockResponse::Rejected(rejection) => rejection.signer_signature_hash,
687687
}
688688
}
689+
690+
/// Get the block accept data from the block response
691+
pub fn as_block_accepted(&self) -> Option<&BlockAccepted> {
692+
match self {
693+
BlockResponse::Accepted(accepted) => Some(accepted),
694+
_ => None,
695+
}
696+
}
697+
698+
/// Get the block accept data from the block response
699+
pub fn as_block_rejection(&self) -> Option<&BlockRejection> {
700+
match self {
701+
BlockResponse::Rejected(rejection) => Some(rejection),
702+
_ => None,
703+
}
704+
}
689705
}
690706

691707
impl StacksMessageCodec for BlockResponse {

stacks-signer/src/chainstate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl SortitionsView {
505505

506506
/// Get the last block from the given tenure
507507
/// Returns the last locally accepted block if it is not timed out, otherwise it will return the last globally accepted block.
508-
fn get_tenure_last_block_info(
508+
pub fn get_tenure_last_block_info(
509509
consensus_hash: &ConsensusHash,
510510
signer_db: &SignerDb,
511511
tenure_last_block_proposal_timeout: Duration,
@@ -517,7 +517,7 @@ impl SortitionsView {
517517

518518
if let Some(local_info) = last_locally_accepted_block {
519519
if let Some(signed_over_time) = local_info.signed_self {
520-
if signed_over_time + tenure_last_block_proposal_timeout.as_secs()
520+
if signed_over_time.saturating_add(tenure_last_block_proposal_timeout.as_secs())
521521
> get_epoch_time_secs()
522522
{
523523
// The last locally accepted block is not timed out, return it

stacks-signer/src/signerdb.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,18 @@ impl SignerDb {
733733
try_deserialize(result)
734734
}
735735

736+
/// Return the last accepted block the signer (highest stacks height). It will tie break a match based on which was more recently signed.
737+
pub fn get_signer_last_accepted_block(&self) -> Result<Option<BlockInfo>, DBError> {
738+
let query = "SELECT block_info FROM blocks WHERE state IN (?1, ?2) ORDER BY stacks_height DESC, signed_group DESC, signed_self DESC LIMIT 1";
739+
let args = params![
740+
&BlockState::GloballyAccepted.to_string(),
741+
&BlockState::LocallyAccepted.to_string()
742+
];
743+
let result: Option<String> = query_row(&self.db, query, args)?;
744+
745+
try_deserialize(result)
746+
}
747+
736748
/// Return the last accepted block in a tenure (identified by its consensus hash).
737749
pub fn get_last_accepted_block(
738750
&self,
@@ -1757,4 +1769,69 @@ mod tests {
17571769
< block_infos[0].proposed_time
17581770
);
17591771
}
1772+
1773+
#[test]
1774+
fn signer_last_accepted_block() {
1775+
let db_path = tmp_db_path();
1776+
let mut db = SignerDb::new(db_path).expect("Failed to create signer db");
1777+
1778+
let (mut block_info_1, _block_proposal_1) = create_block_override(|b| {
1779+
b.block.header.miner_signature = MessageSignature([0x01; 65]);
1780+
b.block.header.chain_length = 1;
1781+
b.burn_height = 1;
1782+
});
1783+
1784+
let (mut block_info_2, _block_proposal_2) = create_block_override(|b| {
1785+
b.block.header.miner_signature = MessageSignature([0x02; 65]);
1786+
b.block.header.chain_length = 2;
1787+
b.burn_height = 1;
1788+
});
1789+
1790+
let (mut block_info_3, _block_proposal_3) = create_block_override(|b| {
1791+
b.block.header.miner_signature = MessageSignature([0x02; 65]);
1792+
b.block.header.chain_length = 2;
1793+
b.burn_height = 4;
1794+
});
1795+
block_info_3
1796+
.mark_locally_accepted(false)
1797+
.expect("Failed to mark block as locally accepted");
1798+
1799+
db.insert_block(&block_info_1)
1800+
.expect("Unable to insert block into db");
1801+
db.insert_block(&block_info_2)
1802+
.expect("Unable to insert block into db");
1803+
1804+
assert!(db.get_signer_last_accepted_block().unwrap().is_none());
1805+
1806+
block_info_1
1807+
.mark_globally_accepted()
1808+
.expect("Failed to mark block as globally accepted");
1809+
db.insert_block(&block_info_1)
1810+
.expect("Unable to insert block into db");
1811+
1812+
assert_eq!(
1813+
db.get_signer_last_accepted_block().unwrap().unwrap(),
1814+
block_info_1
1815+
);
1816+
1817+
block_info_2
1818+
.mark_globally_accepted()
1819+
.expect("Failed to mark block as globally accepted");
1820+
block_info_2.signed_self = Some(get_epoch_time_secs());
1821+
db.insert_block(&block_info_2)
1822+
.expect("Unable to insert block into db");
1823+
1824+
assert_eq!(
1825+
db.get_signer_last_accepted_block().unwrap().unwrap(),
1826+
block_info_2
1827+
);
1828+
1829+
db.insert_block(&block_info_3)
1830+
.expect("Unable to insert block into db");
1831+
1832+
assert_eq!(
1833+
db.get_signer_last_accepted_block().unwrap().unwrap(),
1834+
block_info_3
1835+
);
1836+
}
17601837
}

stacks-signer/src/tests/chainstate.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,12 @@ fn check_sortition_timeout() {
452452
fs::create_dir_all(signer_db_dir).unwrap();
453453
let mut signer_db = SignerDb::new(signer_db_path).unwrap();
454454

455+
let block_sk = StacksPrivateKey::from_seed(&[0, 1]);
456+
let block_pk = StacksPublicKey::from_private(&block_sk);
457+
let block_pkh = Hash160::from_node_public_key(&block_pk);
458+
455459
let mut sortition = SortitionState {
456-
miner_pkh: Hash160([0; 20]),
460+
miner_pkh: block_pkh,
457461
miner_pubkey: None,
458462
prior_sortition: ConsensusHash([0; 20]),
459463
parent_tenure_id: ConsensusHash([0; 20]),

0 commit comments

Comments
 (0)