Skip to content

Commit 0d016a9

Browse files
authored
Merge pull request #5933 from kantai/feat/local-signer-state-machine
Implementation of local signer state machine
2 parents ba81886 + c2df444 commit 0d016a9

File tree

19 files changed

+2013
-752
lines changed

19 files changed

+2013
-752
lines changed

libsigner/src/events.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ use blockstack_lib::chainstate::stacks::StacksTransaction;
2929
use blockstack_lib::net::api::postblock_proposal::{
3030
BlockValidateReject, BlockValidateResponse, ValidateRejectCode,
3131
};
32+
use blockstack_lib::net::api::{prefix_hex, prefix_opt_hex};
3233
use blockstack_lib::net::stackerdb::MINER_SLOT_COUNT;
3334
use blockstack_lib::util_lib::boot::boot_code_id;
3435
use blockstack_lib::version_string;
36+
use clarity::types::chainstate::StacksBlockId;
3537
use clarity::vm::types::serialization::SerializationError;
3638
use clarity::vm::types::QualifiedContractIdentifier;
3739
use serde::{Deserialize, Serialize};
@@ -202,13 +204,21 @@ pub enum SignerEvent<T: SignerEventTrait> {
202204
burn_height: u64,
203205
/// the burn hash for the newly processed burn block
204206
burn_header_hash: BurnchainHeaderHash,
207+
/// the consensus hash for the newly processed burn block
208+
consensus_hash: ConsensusHash,
205209
/// the time at which this event was received by the signer's event processor
206210
received_time: SystemTime,
207211
},
208212
/// A new processed Stacks block was received from the node with the given block hash
209213
NewBlock {
210-
/// The block header hash for the newly processed stacks block
211-
block_hash: Sha512Trunc256Sum,
214+
/// The stacks block ID (or index block hash) of the new block
215+
block_id: StacksBlockId,
216+
/// The consensus hash of the block (either the tenure it was produced during for Stacks 3.0
217+
/// or the burn block that won the sortition in Stacks 2.0)
218+
consensus_hash: ConsensusHash,
219+
/// The signer sighash for the newly processed stacks block. If the newly processed block is a 2.0
220+
/// block, there is *no* signer sighash
221+
signer_sighash: Option<Sha512Trunc256Sum>,
212222
/// The block height for the newly processed stacks block
213223
block_height: u64,
214224
},
@@ -551,54 +561,50 @@ impl<T: SignerEventTrait> TryFrom<BlockValidateResponse> for SignerEvent<T> {
551561

552562
#[derive(Debug, Deserialize)]
553563
struct BurnBlockEvent {
554-
burn_block_hash: String,
564+
#[serde(with = "prefix_hex")]
565+
burn_block_hash: BurnchainHeaderHash,
555566
burn_block_height: u64,
556567
reward_recipients: Vec<serde_json::Value>,
557568
reward_slot_holders: Vec<String>,
558569
burn_amount: u64,
570+
#[serde(with = "prefix_hex")]
571+
consensus_hash: ConsensusHash,
559572
}
560573

561574
impl<T: SignerEventTrait> TryFrom<BurnBlockEvent> for SignerEvent<T> {
562575
type Error = EventError;
563576

564577
fn try_from(burn_block_event: BurnBlockEvent) -> Result<Self, Self::Error> {
565-
let burn_header_hash = burn_block_event
566-
.burn_block_hash
567-
.get(2..)
568-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
569-
.and_then(|hex| {
570-
BurnchainHeaderHash::from_hex(hex)
571-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
572-
})?;
573-
574578
Ok(SignerEvent::NewBurnBlock {
575579
burn_height: burn_block_event.burn_block_height,
576580
received_time: SystemTime::now(),
577-
burn_header_hash,
581+
burn_header_hash: burn_block_event.burn_block_hash,
582+
consensus_hash: burn_block_event.consensus_hash,
578583
})
579584
}
580585
}
581586

582587
#[derive(Debug, Deserialize)]
583588
struct BlockEvent {
584-
block_hash: String,
589+
#[serde(with = "prefix_hex")]
590+
index_block_hash: StacksBlockId,
591+
#[serde(with = "prefix_opt_hex")]
592+
signer_signature_hash: Option<Sha512Trunc256Sum>,
593+
#[serde(with = "prefix_hex")]
594+
consensus_hash: ConsensusHash,
595+
#[serde(with = "prefix_hex")]
596+
block_hash: BlockHeaderHash,
585597
block_height: u64,
586598
}
587599

588600
impl<T: SignerEventTrait> TryFrom<BlockEvent> for SignerEvent<T> {
589601
type Error = EventError;
590602

591603
fn try_from(block_event: BlockEvent) -> Result<Self, Self::Error> {
592-
let block_hash: Sha512Trunc256Sum = block_event
593-
.block_hash
594-
.get(2..)
595-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
596-
.and_then(|hex| {
597-
Sha512Trunc256Sum::from_hex(hex)
598-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
599-
})?;
600604
Ok(SignerEvent::NewBlock {
601-
block_hash,
605+
signer_sighash: block_event.signer_signature_hash,
606+
block_id: block_event.index_block_hash,
607+
consensus_hash: block_event.consensus_hash,
602608
block_height: block_event.block_height,
603609
})
604610
}

0 commit comments

Comments
 (0)