Skip to content

Commit 8d7f7a8

Browse files
committed
feat: insert parent_burn_block_hash in signerdb
1 parent 1d28e3c commit 8d7f7a8

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

libsigner/src/events.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ pub enum SignerEvent<T: SignerEventTrait> {
214214
consensus_hash: ConsensusHash,
215215
/// the time at which this event was received by the signer's event processor
216216
received_time: SystemTime,
217+
/// the parent burn block hash for the newly processed burn block
218+
parent_burn_block_hash: BurnchainHeaderHash,
217219
},
218220
/// A new processed Stacks block was received from the node with the given block hash
219221
NewBlock {
@@ -585,6 +587,8 @@ struct BurnBlockEvent {
585587
burn_amount: u64,
586588
#[serde(with = "prefix_hex")]
587589
consensus_hash: ConsensusHash,
590+
#[serde(with = "prefix_hex")]
591+
parent_burn_block_hash: BurnchainHeaderHash,
588592
}
589593

590594
impl<T: SignerEventTrait> TryFrom<BurnBlockEvent> for SignerEvent<T> {
@@ -596,6 +600,7 @@ impl<T: SignerEventTrait> TryFrom<BurnBlockEvent> for SignerEvent<T> {
596600
received_time: SystemTime::now(),
597601
burn_header_hash: burn_block_event.burn_block_hash,
598602
consensus_hash: burn_block_event.consensus_hash,
603+
parent_burn_block_hash: burn_block_event.parent_burn_block_hash,
599604
})
600605
}
601606
}

stacks-signer/src/signerdb.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ CREATE TABLE IF NOT EXISTS signer_state_machine_updates (
532532
PRIMARY KEY (signer_addr, reward_cycle)
533533
) STRICT;"#;
534534

535+
static ADD_PARENT_BURN_BLOCK_HASH: &str = r#"
536+
ALTER TABLE burn_blocks
537+
ADD COLUMN parent_burn_block_hash TEXT;
538+
"#;
539+
540+
static ADD_PARENT_BURN_BLOCK_HASH_INDEX: &str = r#"
541+
CREATE INDEX IF NOT EXISTS burn_blocks_parent_burn_block_hash_idx on burn_blocks (parent_burn_block_hash);
542+
"#;
543+
535544
static SCHEMA_1: &[&str] = &[
536545
DROP_SCHEMA_0,
537546
CREATE_DB_CONFIG,
@@ -613,9 +622,15 @@ static SCHEMA_11: &[&str] = &[
613622
"INSERT INTO db_config (version) VALUES (11);",
614623
];
615624

625+
static SCHEMA_12: &[&str] = &[
626+
ADD_PARENT_BURN_BLOCK_HASH,
627+
ADD_PARENT_BURN_BLOCK_HASH_INDEX,
628+
"INSERT INTO db_config (version) VALUES (12);",
629+
];
630+
616631
impl SignerDb {
617632
/// The current schema version used in this build of the signer binary.
618-
pub const SCHEMA_VERSION: u32 = 11;
633+
pub const SCHEMA_VERSION: u32 = 12;
619634

620635
/// Create a new `SignerState` instance.
621636
/// This will create a new SQLite database at the given path
@@ -799,6 +814,20 @@ impl SignerDb {
799814
Ok(())
800815
}
801816

817+
/// Migrate from schema 11 to schema 12
818+
fn schema_12_migration(tx: &Transaction) -> Result<(), DBError> {
819+
if Self::get_schema_version(tx)? >= 12 {
820+
// no migration necessary
821+
return Ok(());
822+
}
823+
824+
for statement in SCHEMA_12.iter() {
825+
tx.execute_batch(statement)?;
826+
}
827+
828+
Ok(())
829+
}
830+
802831
/// Register custom scalar functions used by the database
803832
fn register_scalar_functions(&self) -> Result<(), DBError> {
804833
// Register helper function for determining if a block is a tenure change transaction
@@ -843,7 +872,8 @@ impl SignerDb {
843872
8 => Self::schema_9_migration(&sql_tx)?,
844873
9 => Self::schema_10_migration(&sql_tx)?,
845874
10 => Self::schema_11_migration(&sql_tx)?,
846-
11 => break,
875+
11 => Self::schema_12_migration(&sql_tx)?,
876+
12 => break,
847877
x => return Err(DBError::Other(format!(
848878
"Database schema is newer than supported by this binary. Expected version = {}, Database version = {x}",
849879
Self::SCHEMA_VERSION,
@@ -978,19 +1008,27 @@ impl SignerDb {
9781008
consensus_hash: &ConsensusHash,
9791009
burn_height: u64,
9801010
received_time: &SystemTime,
1011+
parent_burn_block_hash: &BurnchainHeaderHash,
9811012
) -> Result<(), DBError> {
9821013
let received_ts = received_time
9831014
.duration_since(std::time::UNIX_EPOCH)
9841015
.map_err(|e| DBError::Other(format!("Bad system time: {e}")))?
9851016
.as_secs();
986-
debug!("Inserting burn block info"; "burn_block_height" => burn_height, "burn_hash" => %burn_hash, "received" => received_ts, "ch" => %consensus_hash);
1017+
debug!("Inserting burn block info";
1018+
"burn_block_height" => burn_height,
1019+
"burn_hash" => %burn_hash,
1020+
"received" => received_ts,
1021+
"ch" => %consensus_hash,
1022+
"parent_burn_block_hash" => %parent_burn_block_hash
1023+
);
9871024
self.db.execute(
988-
"INSERT OR REPLACE INTO burn_blocks (block_hash, consensus_hash, block_height, received_time) VALUES (?1, ?2, ?3, ?4)",
1025+
"INSERT OR REPLACE INTO burn_blocks (block_hash, consensus_hash, block_height, received_time, parent_burn_block_hash) VALUES (?1, ?2, ?3, ?4, ?5)",
9891026
params![
9901027
burn_hash,
9911028
consensus_hash,
9921029
u64_to_sql(burn_height)?,
9931030
u64_to_sql(received_ts)?,
1031+
parent_burn_block_hash,
9941032
],
9951033
)?;
9961034
Ok(())
@@ -1641,8 +1679,14 @@ pub mod tests {
16411679
.duration_since(SystemTime::UNIX_EPOCH)
16421680
.unwrap()
16431681
.as_secs();
1644-
db.insert_burn_block(&test_burn_hash, &test_consensus_hash, 10, &stime)
1645-
.unwrap();
1682+
db.insert_burn_block(
1683+
&test_burn_hash,
1684+
&test_consensus_hash,
1685+
10,
1686+
&stime,
1687+
&test_burn_hash,
1688+
)
1689+
.unwrap();
16461690

16471691
let stored_time = db
16481692
.get_burn_block_receive_time(&test_burn_hash)

stacks-signer/src/tests/chainstate.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ fn reorg_timing_testing(
257257
&view.cur_sortition.consensus_hash,
258258
3,
259259
&sortition_time,
260+
&view.last_sortition.as_ref().unwrap().burn_block_hash,
260261
)
261262
.unwrap();
262263

@@ -395,7 +396,13 @@ fn check_block_proposal_timeout() {
395396
let burn_height = 1;
396397
let received_time = SystemTime::now();
397398
signer_db
398-
.insert_burn_block(&burn_hash, &consensus_hash, burn_height, &received_time)
399+
.insert_burn_block(
400+
&burn_hash,
401+
&consensus_hash,
402+
burn_height,
403+
&received_time,
404+
&view.last_sortition.as_ref().unwrap().burn_block_hash,
405+
)
399406
.unwrap();
400407

401408
view.check_proposal(
@@ -467,7 +474,13 @@ fn check_sortition_timeout() {
467474
let burn_height = 1;
468475
let received_time = SystemTime::now();
469476
signer_db
470-
.insert_burn_block(&burn_hash, &consensus_hash, burn_height, &received_time)
477+
.insert_burn_block(
478+
&burn_hash,
479+
&consensus_hash,
480+
burn_height,
481+
&received_time,
482+
&BurnchainHeaderHash([0; 32]),
483+
)
471484
.unwrap();
472485

473486
std::thread::sleep(Duration::from_secs(1));

stacks-signer/src/v0/signer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ impl SignerTrait<SignerMessage> for Signer {
349349
burn_header_hash,
350350
consensus_hash,
351351
received_time,
352+
parent_burn_block_hash,
352353
} => {
353354
info!("{self}: Received a new burn block event for block height {burn_height}");
354355
self.signer_db
@@ -357,6 +358,7 @@ impl SignerTrait<SignerMessage> for Signer {
357358
consensus_hash,
358359
*burn_height,
359360
received_time,
361+
parent_burn_block_hash,
360362
)
361363
.unwrap_or_else(|e| {
362364
error!(

0 commit comments

Comments
 (0)