Skip to content

Commit 8c8e44f

Browse files
committed
Add SignerMessage::BlockPreCommit struct
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 4f3676f commit 8c8e44f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

libsigner/src/v0/messages.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ MessageSlotID {
7171
/// Block Response message from signers
7272
BlockResponse = 1,
7373
/// Signer State Machine Update
74-
StateMachineUpdate = 2
74+
StateMachineUpdate = 2,
75+
/// Block Pre-commit message from signers before they commit to a block response
76+
BlockPreCommit = 3
7577
});
7678

7779
define_u8_enum!(
@@ -114,7 +116,9 @@ SignerMessageTypePrefix {
114116
/// Mock block message from Epoch 2.5 miners
115117
MockBlock = 5,
116118
/// State machine update
117-
StateMachineUpdate = 6
119+
StateMachineUpdate = 6,
120+
/// Block Pre-commit message
121+
BlockPreCommit = 7
118122
});
119123

120124
#[cfg_attr(test, mutants::skip)]
@@ -137,7 +141,7 @@ impl MessageSlotID {
137141
#[cfg_attr(test, mutants::skip)]
138142
impl Display for MessageSlotID {
139143
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140-
write!(f, "{:?}({})", self, self.to_u8())
144+
write!(f, "{self:?}({})", self.to_u8())
141145
}
142146
}
143147

@@ -161,6 +165,7 @@ impl From<&SignerMessage> for SignerMessageTypePrefix {
161165
SignerMessage::MockSignature(_) => SignerMessageTypePrefix::MockSignature,
162166
SignerMessage::MockBlock(_) => SignerMessageTypePrefix::MockBlock,
163167
SignerMessage::StateMachineUpdate(_) => SignerMessageTypePrefix::StateMachineUpdate,
168+
SignerMessage::BlockPreCommit(_) => SignerMessageTypePrefix::BlockPreCommit,
164169
}
165170
}
166171
}
@@ -182,6 +187,8 @@ pub enum SignerMessage {
182187
MockBlock(MockBlock),
183188
/// A state machine update
184189
StateMachineUpdate(StateMachineUpdate),
190+
/// The pre commit message from signers for other signers to observe
191+
BlockPreCommit(Sha512Trunc256Sum),
185192
}
186193

187194
impl SignerMessage {
@@ -197,6 +204,7 @@ impl SignerMessage {
197204
| Self::MockBlock(_) => None,
198205
Self::BlockResponse(_) | Self::MockSignature(_) => Some(MessageSlotID::BlockResponse), // Mock signature uses the same slot as block response since its exclusively for epoch 2.5 testing
199206
Self::StateMachineUpdate(_) => Some(MessageSlotID::StateMachineUpdate),
207+
Self::BlockPreCommit(_) => Some(MessageSlotID::BlockPreCommit),
200208
}
201209
}
202210
}
@@ -216,6 +224,9 @@ impl StacksMessageCodec for SignerMessage {
216224
SignerMessage::StateMachineUpdate(state_machine_update) => {
217225
state_machine_update.consensus_serialize(fd)
218226
}
227+
SignerMessage::BlockPreCommit(block_pre_commit) => {
228+
block_pre_commit.consensus_serialize(fd)
229+
}
219230
}?;
220231
Ok(())
221232
}
@@ -253,6 +264,10 @@ impl StacksMessageCodec for SignerMessage {
253264
let state_machine_update = StacksMessageCodec::consensus_deserialize(fd)?;
254265
SignerMessage::StateMachineUpdate(state_machine_update)
255266
}
267+
SignerMessageTypePrefix::BlockPreCommit => {
268+
let signer_signature_hash = StacksMessageCodec::consensus_deserialize(fd)?;
269+
SignerMessage::BlockPreCommit(signer_signature_hash)
270+
}
256271
};
257272
Ok(message)
258273
}
@@ -2604,4 +2619,14 @@ mod test {
26042619

26052620
assert_eq!(signer_message, signer_message_deserialized);
26062621
}
2622+
2623+
#[test]
2624+
fn serde_block_signer_message_pre_commit() {
2625+
let pre_commit = SignerMessage::BlockPreCommit(Sha512Trunc256Sum([0u8; 32]));
2626+
let serialized_pre_commit = pre_commit.serialize_to_vec();
2627+
let deserialized_pre_commit =
2628+
read_next::<SignerMessage, _>(&mut &serialized_pre_commit[..])
2629+
.expect("Failed to deserialize pre-commit");
2630+
assert_eq!(pre_commit, deserialized_pre_commit);
2631+
}
26072632
}

stacks-node/src/nakamoto_node/stackerdb_listener.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ impl StackerDBListener {
500500
SignerMessageV0::StateMachineUpdate(update) => {
501501
self.update_global_state_evaluator(&signer_pubkey, update);
502502
}
503+
SignerMessageV0::BlockPreCommit(_) => {
504+
debug!("Received block pre commit message. Ignoring.");
505+
}
503506
};
504507
}
505508
}

stacks-signer/src/v0/signer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ impl Signer {
518518
),
519519
SignerMessage::StateMachineUpdate(update) => self
520520
.handle_state_machine_update(signer_public_key, update, received_time),
521+
SignerMessage::BlockPreCommit(signer_signature_hash) => {
522+
todo!("Unable to handle block precommit message from {signer_public_key:?}: {signer_signature_hash}");
523+
}
521524
_ => {}
522525
}
523526
}

0 commit comments

Comments
 (0)