@@ -61,7 +61,8 @@ use stacks_common::codec::{
61
61
StacksMessageCodec ,
62
62
} ;
63
63
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 } ;
65
66
use tiny_http:: {
66
67
Method as HttpMethod , Request as HttpRequest , Response as HttpResponse , Server as HttpServer ,
67
68
} ;
@@ -81,7 +82,9 @@ define_u8_enum!(
81
82
/// the contract index in the signers contracts (i.e., X in signers-0-X)
82
83
MessageSlotID {
83
84
/// Block Response message from signers
84
- BlockResponse = 1
85
+ BlockResponse = 1 ,
86
+ /// Signer State Machine Update
87
+ StateMachineUpdate = 2
85
88
} ) ;
86
89
87
90
define_u8_enum ! (
@@ -122,7 +125,9 @@ SignerMessageTypePrefix {
122
125
/// Mock block signature message from Epoch 2.5 signers
123
126
MockSignature = 4 ,
124
127
/// Mock block message from Epoch 2.5 miners
125
- MockBlock = 5
128
+ MockBlock = 5 ,
129
+ /// State machine update
130
+ StateMachineUpdate = 6
126
131
} ) ;
127
132
128
133
#[ cfg_attr( test, mutants:: skip) ]
@@ -168,6 +173,7 @@ impl From<&SignerMessage> for SignerMessageTypePrefix {
168
173
SignerMessage :: MockProposal ( _) => SignerMessageTypePrefix :: MockProposal ,
169
174
SignerMessage :: MockSignature ( _) => SignerMessageTypePrefix :: MockSignature ,
170
175
SignerMessage :: MockBlock ( _) => SignerMessageTypePrefix :: MockBlock ,
176
+ SignerMessage :: StateMachineUpdate ( _) => SignerMessageTypePrefix :: StateMachineUpdate ,
171
177
}
172
178
}
173
179
}
@@ -187,6 +193,8 @@ pub enum SignerMessage {
187
193
MockProposal ( MockProposal ) ,
188
194
/// A mock block from the epoch 2.5 miners
189
195
MockBlock ( MockBlock ) ,
196
+ /// A state machine update
197
+ StateMachineUpdate ( StateMachineUpdate ) ,
190
198
}
191
199
192
200
impl SignerMessage {
@@ -201,6 +209,7 @@ impl SignerMessage {
201
209
| Self :: MockProposal ( _)
202
210
| Self :: MockBlock ( _) => None ,
203
211
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 ) ,
204
213
}
205
214
}
206
215
}
@@ -217,6 +226,9 @@ impl StacksMessageCodec for SignerMessage {
217
226
SignerMessage :: MockSignature ( signature) => signature. consensus_serialize ( fd) ,
218
227
SignerMessage :: MockProposal ( message) => message. consensus_serialize ( fd) ,
219
228
SignerMessage :: MockBlock ( block) => block. consensus_serialize ( fd) ,
229
+ SignerMessage :: StateMachineUpdate ( state_machine_update) => {
230
+ state_machine_update. consensus_serialize ( fd)
231
+ }
220
232
} ?;
221
233
Ok ( ( ) )
222
234
}
@@ -250,6 +262,10 @@ impl StacksMessageCodec for SignerMessage {
250
262
let block = StacksMessageCodec :: consensus_deserialize ( fd) ?;
251
263
SignerMessage :: MockBlock ( block)
252
264
}
265
+ SignerMessageTypePrefix :: StateMachineUpdate => {
266
+ let state_machine_update = StacksMessageCodec :: consensus_deserialize ( fd) ?;
267
+ SignerMessage :: StateMachineUpdate ( state_machine_update)
268
+ }
253
269
} ;
254
270
Ok ( message)
255
271
}
@@ -525,6 +541,54 @@ impl StacksMessageCodec for MockBlock {
525
541
}
526
542
}
527
543
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
+
528
592
define_u8_enum ! (
529
593
/// Enum representing the reject code type prefix
530
594
RejectCodeTypePrefix {
@@ -1981,4 +2045,78 @@ mod test {
1981
2045
RejectReason :: Unknown ( RejectReasonPrefix :: Unknown as u8 )
1982
2046
) ;
1983
2047
}
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
+ }
1984
2122
}
0 commit comments