Skip to content

Commit 92e404c

Browse files
authored
Merge pull request #4981 from jbencin/refactor/block-info-enum
refactor(signer): Add enum for version-specific data to `BlockInfo`
2 parents beb4795 + ef45a38 commit 92e404c

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,53 @@ use stacks_common::util::hash::Sha512Trunc256Sum;
3434
use stacks_common::{debug, error};
3535
use wsts::net::NonceRequest;
3636

37+
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
38+
/// Information specific to Signer V1
39+
pub struct BlockInfoV1 {
40+
/// The associated packet nonce request if we have one
41+
pub nonce_request: Option<NonceRequest>,
42+
}
43+
44+
impl From<NonceRequest> for BlockInfoV1 {
45+
fn from(value: NonceRequest) -> Self {
46+
Self {
47+
nonce_request: Some(value),
48+
}
49+
}
50+
}
51+
52+
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
53+
/// Store extra version-specific info in `BlockInfo`
54+
pub enum ExtraBlockInfo {
55+
#[default]
56+
/// Don't know what version
57+
None,
58+
/// Extra data for Signer V0
59+
V0,
60+
/// Extra data for Signer V1
61+
V1(BlockInfoV1),
62+
}
63+
64+
impl ExtraBlockInfo {
65+
/// Take `nonce_request` if it exists
66+
pub fn take_nonce_request(&mut self) -> Option<NonceRequest> {
67+
match self {
68+
ExtraBlockInfo::None | ExtraBlockInfo::V0 => None,
69+
ExtraBlockInfo::V1(v1) => v1.nonce_request.take(),
70+
}
71+
}
72+
/// Set `nonce_request` if it exists
73+
pub fn set_nonce_request(&mut self, value: NonceRequest) -> Result<(), &str> {
74+
match self {
75+
ExtraBlockInfo::None | ExtraBlockInfo::V0 => Err("Field doesn't exist"),
76+
ExtraBlockInfo::V1(v1) => {
77+
v1.nonce_request = Some(value);
78+
Ok(())
79+
}
80+
}
81+
}
82+
}
83+
3784
/// Additional Info about a proposed block
3885
#[derive(Serialize, Deserialize, Debug, PartialEq)]
3986
pub struct BlockInfo {
@@ -47,8 +94,6 @@ pub struct BlockInfo {
4794
pub vote: Option<NakamotoBlockVote>,
4895
/// Whether the block contents are valid
4996
pub valid: Option<bool>,
50-
/// The associated packet nonce request if we have one
51-
pub nonce_request: Option<NonceRequest>,
5297
/// Whether this block is already being signed over
5398
pub signed_over: bool,
5499
/// Time at which the proposal was received by this signer (epoch time in seconds)
@@ -57,6 +102,8 @@ pub struct BlockInfo {
57102
pub signed_self: Option<u64>,
58103
/// Time at which the proposal was signed by a threshold in the signer set (epoch time in seconds)
59104
pub signed_group: Option<u64>,
105+
/// Extra data specific to v0, v1, etc.
106+
pub ext: ExtraBlockInfo,
60107
}
61108

62109
impl From<BlockProposal> for BlockInfo {
@@ -67,19 +114,19 @@ impl From<BlockProposal> for BlockInfo {
67114
reward_cycle: value.reward_cycle,
68115
vote: None,
69116
valid: None,
70-
nonce_request: None,
71117
signed_over: false,
72118
proposed_time: get_epoch_time_secs(),
73119
signed_self: None,
74120
signed_group: None,
121+
ext: ExtraBlockInfo::default(),
75122
}
76123
}
77124
}
78125
impl BlockInfo {
79126
/// Create a new BlockInfo with an associated nonce request packet
80-
pub fn new_with_request(block_proposal: BlockProposal, nonce_request: NonceRequest) -> Self {
127+
pub fn new_v1_with_request(block_proposal: BlockProposal, nonce_request: NonceRequest) -> Self {
81128
let mut block_info = BlockInfo::from(block_proposal);
82-
block_info.nonce_request = Some(nonce_request);
129+
block_info.ext = ExtraBlockInfo::V1(BlockInfoV1::from(nonce_request));
83130
block_info.signed_over = true;
84131
block_info
85132
}
@@ -89,9 +136,7 @@ impl BlockInfo {
89136
pub fn mark_signed_and_valid(&mut self) {
90137
self.valid = Some(true);
91138
self.signed_over = true;
92-
if self.signed_self.is_none() {
93-
self.signed_self = Some(get_epoch_time_secs());
94-
}
139+
self.signed_self.get_or_insert(get_epoch_time_secs());
95140
}
96141

97142
/// Return the block's signer signature hash
@@ -115,7 +160,7 @@ CREATE TABLE IF NOT EXISTS blocks (
115160
block_info TEXT NOT NULL,
116161
consensus_hash TEXT NOT NULL,
117162
signed_over INTEGER NOT NULL,
118-
stacks_height INTEGER NOT NULL,
163+
stacks_height INTEGER NOT NULL,
119164
burn_block_height INTEGER NOT NULL,
120165
PRIMARY KEY (reward_cycle, signer_signature_hash)
121166
) STRICT";
@@ -189,7 +234,7 @@ impl SignerDb {
189234
})
190235
.optional();
191236
match result {
192-
Ok(x) => Ok(x.unwrap_or_else(|| 0)),
237+
Ok(x) => Ok(x.unwrap_or(0)),
193238
Err(e) => Err(DBError::from(e)),
194239
}
195240
}

stacks-signer/src/v1/signer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl Signer {
692692
block_info
693693
}
694694
};
695-
if let Some(mut nonce_request) = block_info.nonce_request.take() {
695+
if let Some(mut nonce_request) = block_info.ext.take_nonce_request() {
696696
debug!("{self}: Received a block validate response from the stacks node for a block we already received a nonce request for. Responding to the nonce request...");
697697
// We have received validation from the stacks node. Determine our vote and update the request message
698698
self.determine_vote(&mut block_info, &mut nonce_request);
@@ -916,7 +916,7 @@ impl Signer {
916916
"{self}: received a nonce request for a new block. Submit block for validation. ";
917917
"signer_sighash" => %signer_signature_hash,
918918
);
919-
let block_info = BlockInfo::new_with_request(block_proposal, nonce_request.clone());
919+
let block_info = BlockInfo::new_v1_with_request(block_proposal, nonce_request.clone());
920920
stacks_client
921921
.submit_block_for_validation(block_info.block.clone())
922922
.unwrap_or_else(|e| {
@@ -928,7 +928,12 @@ impl Signer {
928928
if block_info.valid.is_none() {
929929
// We have not yet received validation from the stacks node. Cache the request and wait for validation
930930
debug!("{self}: We have yet to receive validation from the stacks node for a nonce request. Cache the nonce request and wait for block validation...");
931-
block_info.nonce_request = Some(nonce_request.clone());
931+
block_info
932+
.ext
933+
.set_nonce_request(nonce_request.clone())
934+
.unwrap_or_else(|e| {
935+
warn!("{self}: Failed to set nonce_request: {e:?}",);
936+
});
932937
return Some(block_info);
933938
}
934939

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use stacks_common::util::hash::{to_hex, Hash160, Sha512Trunc256Sum};
8989
use stacks_common::util::secp256k1::{MessageSignature, Secp256k1PrivateKey, Secp256k1PublicKey};
9090
use stacks_common::util::{get_epoch_time_secs, sleep_ms};
9191
use stacks_signer::chainstate::{ProposalEvalConfig, SortitionsView};
92-
use stacks_signer::signerdb::{BlockInfo, SignerDb};
92+
use stacks_signer::signerdb::{BlockInfo, ExtraBlockInfo, SignerDb};
9393
use wsts::net::Message;
9494

9595
use super::bitcoin_regtest::BitcoinCoreController;
@@ -4714,11 +4714,11 @@ fn signer_chainstate() {
47144714
reward_cycle,
47154715
vote: None,
47164716
valid: Some(true),
4717-
nonce_request: None,
47184717
signed_over: true,
47194718
proposed_time: get_epoch_time_secs(),
47204719
signed_self: None,
47214720
signed_group: None,
4721+
ext: ExtraBlockInfo::None,
47224722
})
47234723
.unwrap();
47244724

@@ -4789,11 +4789,11 @@ fn signer_chainstate() {
47894789
reward_cycle,
47904790
vote: None,
47914791
valid: Some(true),
4792-
nonce_request: None,
47934792
signed_over: true,
47944793
proposed_time: get_epoch_time_secs(),
47954794
signed_self: None,
47964795
signed_group: None,
4796+
ext: ExtraBlockInfo::None,
47974797
})
47984798
.unwrap();
47994799

0 commit comments

Comments
 (0)