Skip to content

Commit e21c0dd

Browse files
authored
Merge pull request #5922 from rdeioris/feat/signer_state_machine_message
Feat/signer state machine message
2 parents c96be77 + 9937c41 commit e21c0dd

File tree

2 files changed

+144
-3
lines changed

2 files changed

+144
-3
lines changed

libsigner/src/v0/messages.rs

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ use stacks_common::codec::{
6161
StacksMessageCodec,
6262
};
6363
use stacks_common::consts::SIGNER_SLOTS_PER_USER;
64-
use stacks_common::util::hash::Sha512Trunc256Sum;
64+
use stacks_common::types::chainstate::StacksBlockId;
65+
use stacks_common::util::hash::{Hash160, Sha512Trunc256Sum};
6566
use tiny_http::{
6667
Method as HttpMethod, Request as HttpRequest, Response as HttpResponse, Server as HttpServer,
6768
};
@@ -81,7 +82,9 @@ define_u8_enum!(
8182
/// the contract index in the signers contracts (i.e., X in signers-0-X)
8283
MessageSlotID {
8384
/// Block Response message from signers
84-
BlockResponse = 1
85+
BlockResponse = 1,
86+
/// Signer State Machine Update
87+
StateMachineUpdate = 2
8588
});
8689

8790
define_u8_enum!(
@@ -122,7 +125,9 @@ SignerMessageTypePrefix {
122125
/// Mock block signature message from Epoch 2.5 signers
123126
MockSignature = 4,
124127
/// Mock block message from Epoch 2.5 miners
125-
MockBlock = 5
128+
MockBlock = 5,
129+
/// State machine update
130+
StateMachineUpdate = 6
126131
});
127132

128133
#[cfg_attr(test, mutants::skip)]
@@ -168,6 +173,7 @@ impl From<&SignerMessage> for SignerMessageTypePrefix {
168173
SignerMessage::MockProposal(_) => SignerMessageTypePrefix::MockProposal,
169174
SignerMessage::MockSignature(_) => SignerMessageTypePrefix::MockSignature,
170175
SignerMessage::MockBlock(_) => SignerMessageTypePrefix::MockBlock,
176+
SignerMessage::StateMachineUpdate(_) => SignerMessageTypePrefix::StateMachineUpdate,
171177
}
172178
}
173179
}
@@ -187,6 +193,8 @@ pub enum SignerMessage {
187193
MockProposal(MockProposal),
188194
/// A mock block from the epoch 2.5 miners
189195
MockBlock(MockBlock),
196+
/// A state machine update
197+
StateMachineUpdate(StateMachineUpdate),
190198
}
191199

192200
impl SignerMessage {
@@ -201,6 +209,7 @@ impl SignerMessage {
201209
| Self::MockProposal(_)
202210
| Self::MockBlock(_) => None,
203211
Self::BlockResponse(_) | Self::MockSignature(_) => Some(MessageSlotID::BlockResponse), // Mock signature uses the same slot as block response since its exclusively for epoch 2.5 testing
212+
Self::StateMachineUpdate(_) => Some(MessageSlotID::StateMachineUpdate),
204213
}
205214
}
206215
}
@@ -217,6 +226,9 @@ impl StacksMessageCodec for SignerMessage {
217226
SignerMessage::MockSignature(signature) => signature.consensus_serialize(fd),
218227
SignerMessage::MockProposal(message) => message.consensus_serialize(fd),
219228
SignerMessage::MockBlock(block) => block.consensus_serialize(fd),
229+
SignerMessage::StateMachineUpdate(state_machine_update) => {
230+
state_machine_update.consensus_serialize(fd)
231+
}
220232
}?;
221233
Ok(())
222234
}
@@ -250,6 +262,10 @@ impl StacksMessageCodec for SignerMessage {
250262
let block = StacksMessageCodec::consensus_deserialize(fd)?;
251263
SignerMessage::MockBlock(block)
252264
}
265+
SignerMessageTypePrefix::StateMachineUpdate => {
266+
let state_machine_update = StacksMessageCodec::consensus_deserialize(fd)?;
267+
SignerMessage::StateMachineUpdate(state_machine_update)
268+
}
253269
};
254270
Ok(message)
255271
}
@@ -525,6 +541,54 @@ impl StacksMessageCodec for MockBlock {
525541
}
526542
}
527543

544+
/// Message for update the Signer State infos
545+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
546+
pub struct StateMachineUpdate {
547+
burn_block: ConsensusHash,
548+
burn_block_height: u64,
549+
current_miner_pkh: Hash160,
550+
parent_tenure_id: ConsensusHash,
551+
parent_tenure_last_block: StacksBlockId,
552+
parent_tenure_last_block_height: u64,
553+
active_signer_protocol_version: u64,
554+
local_supported_signer_protocol_version: u64,
555+
}
556+
557+
impl StacksMessageCodec for StateMachineUpdate {
558+
fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), CodecError> {
559+
write_next(fd, &self.burn_block)?;
560+
write_next(fd, &self.burn_block_height)?;
561+
write_next(fd, &self.current_miner_pkh)?;
562+
write_next(fd, &self.parent_tenure_id)?;
563+
write_next(fd, &self.parent_tenure_last_block)?;
564+
write_next(fd, &self.parent_tenure_last_block_height)?;
565+
write_next(fd, &self.active_signer_protocol_version)?;
566+
write_next(fd, &self.local_supported_signer_protocol_version)?;
567+
Ok(())
568+
}
569+
570+
fn consensus_deserialize<R: Read>(fd: &mut R) -> Result<Self, CodecError> {
571+
let burn_block = read_next::<ConsensusHash, _>(fd)?;
572+
let burn_block_height = read_next::<u64, _>(fd)?;
573+
let current_miner_pkh = read_next::<Hash160, _>(fd)?;
574+
let parent_tenure_id = read_next::<ConsensusHash, _>(fd)?;
575+
let parent_tenure_last_block = read_next::<StacksBlockId, _>(fd)?;
576+
let parent_tenure_last_block_height = read_next::<u64, _>(fd)?;
577+
let active_signer_protocol_version = read_next::<u64, _>(fd)?;
578+
let local_supported_signer_protocol_version = read_next::<u64, _>(fd)?;
579+
Ok(Self {
580+
burn_block,
581+
burn_block_height,
582+
current_miner_pkh,
583+
parent_tenure_id,
584+
parent_tenure_last_block,
585+
parent_tenure_last_block_height,
586+
active_signer_protocol_version,
587+
local_supported_signer_protocol_version,
588+
})
589+
}
590+
}
591+
528592
define_u8_enum!(
529593
/// Enum representing the reject code type prefix
530594
RejectCodeTypePrefix {
@@ -1981,4 +2045,78 @@ mod test {
19812045
RejectReason::Unknown(RejectReasonPrefix::Unknown as u8)
19822046
);
19832047
}
2048+
2049+
#[test]
2050+
fn test_deserialize_state_machine_update() {
2051+
let signer_message = StateMachineUpdate {
2052+
burn_block: ConsensusHash([0x55; 20]),
2053+
burn_block_height: 100,
2054+
current_miner_pkh: Hash160([0xab; 20]),
2055+
parent_tenure_id: ConsensusHash([0x22; 20]),
2056+
parent_tenure_last_block: StacksBlockId([0x33; 32]),
2057+
parent_tenure_last_block_height: 1,
2058+
active_signer_protocol_version: 2,
2059+
local_supported_signer_protocol_version: 3,
2060+
};
2061+
2062+
let mut bytes = vec![];
2063+
signer_message.consensus_serialize(&mut bytes).unwrap();
2064+
2065+
// check for raw content for avoiding regressions when structure changes
2066+
let raw_signer_message: Vec<&[u8]> = vec![
2067+
/* burn_block*/ &[0x55; 20],
2068+
/* burn_block_height*/ &[0, 0, 0, 0, 0, 0, 0, 100],
2069+
/* current_miner_pkh */ &[0xab; 20],
2070+
/* parent_tenure_id*/ &[0x22; 20],
2071+
/* parent_tenure_last_block */ &[0x33; 32],
2072+
/* parent_tenure_last_block_height*/ &[0, 0, 0, 0, 0, 0, 0, 1],
2073+
/* active_signer_protocol_version*/ &[0, 0, 0, 0, 0, 0, 0, 2],
2074+
/* local_supported_signer_protocol_version*/ &[0, 0, 0, 0, 0, 0, 0, 3],
2075+
];
2076+
2077+
assert_eq!(bytes, raw_signer_message.concat());
2078+
2079+
let signer_message_deserialized =
2080+
StateMachineUpdate::consensus_deserialize(&mut &bytes[..]).unwrap();
2081+
2082+
assert_eq!(
2083+
signer_message.burn_block,
2084+
signer_message_deserialized.burn_block
2085+
);
2086+
2087+
assert_eq!(
2088+
signer_message.burn_block_height,
2089+
signer_message_deserialized.burn_block_height
2090+
);
2091+
2092+
assert_eq!(
2093+
signer_message.current_miner_pkh,
2094+
signer_message_deserialized.current_miner_pkh
2095+
);
2096+
2097+
assert_eq!(
2098+
signer_message.parent_tenure_id,
2099+
signer_message_deserialized.parent_tenure_id
2100+
);
2101+
2102+
assert_eq!(
2103+
signer_message.parent_tenure_last_block,
2104+
signer_message_deserialized.parent_tenure_last_block
2105+
);
2106+
2107+
assert_eq!(
2108+
signer_message.parent_tenure_last_block_height,
2109+
signer_message_deserialized.parent_tenure_last_block_height
2110+
);
2111+
2112+
assert_eq!(
2113+
signer_message.active_signer_protocol_version,
2114+
signer_message_deserialized.active_signer_protocol_version
2115+
);
2116+
2117+
assert_eq!(
2118+
signer_message.local_supported_signer_protocol_version,
2119+
signer_message_deserialized.local_supported_signer_protocol_version
2120+
);
2121+
}
19842122
}

testnet/stacks-node/src/nakamoto_node/stackerdb_listener.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ impl StackerDBListener {
440440
| SignerMessageV0::MockBlock(_) => {
441441
debug!("Received mock message. Ignoring.");
442442
}
443+
SignerMessageV0::StateMachineUpdate(_) => {
444+
debug!("Received state machine update message. Ignoring.");
445+
}
443446
};
444447
}
445448
}

0 commit comments

Comments
 (0)