@@ -239,57 +239,41 @@ pub trait StacksMessageCodecExtensions: Sized {
239
239
fn inner_consensus_deserialize < R : Read > ( fd : & mut R ) -> Result < Self , CodecError > ;
240
240
}
241
241
242
- /// A snapshot of the signer view of the stacks node to be used for mock signing.
243
- #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
244
- pub struct MockSignData {
245
- /// The stacks tip consensus hash at the time of the mock signature
242
+ /// The signer relevant peer information from the stacks node
243
+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
244
+ pub struct PeerInfo {
245
+ /// The burn block height
246
+ pub burn_block_height : u64 ,
247
+ /// The consensus hash of the stacks tip
246
248
pub stacks_tip_consensus_hash : ConsensusHash ,
247
- /// The stacks tip header hash at the time of the mock signature
249
+ /// The stacks tip
248
250
pub stacks_tip : BlockHeaderHash ,
251
+ /// The stacks tip height
252
+ pub stacks_tip_height : u64 ,
253
+ /// The pox consensus
254
+ pub pox_consensus : ConsensusHash ,
249
255
/// The server version
250
256
pub server_version : String ,
251
- /// The burn block height that triggered the mock signature
252
- pub burn_block_height : u64 ,
253
- /// The burn block height of the peer view at the time of the mock signature. Note
254
- /// that this may be different from the burn_block_height if the peer view is stale.
255
- pub peer_burn_block_height : u64 ,
256
- /// The POX consensus hash at the time of the mock signature
257
- pub pox_consensus : ConsensusHash ,
258
- /// The chain id for the mock signature
259
- pub chain_id : u32 ,
260
- }
261
-
262
- impl MockSignData {
263
- fn new ( peer_view : RPCPeerInfoData , burn_block_height : u64 , chain_id : u32 ) -> Self {
264
- Self {
265
- stacks_tip_consensus_hash : peer_view. stacks_tip_consensus_hash ,
266
- stacks_tip : peer_view. stacks_tip ,
267
- server_version : peer_view. server_version ,
268
- burn_block_height,
269
- peer_burn_block_height : peer_view. burn_block_height ,
270
- pox_consensus : peer_view. pox_consensus ,
271
- chain_id,
272
- }
273
- }
274
257
}
275
258
276
- impl StacksMessageCodec for MockSignData {
259
+ impl StacksMessageCodec for PeerInfo {
277
260
fn consensus_serialize < W : Write > ( & self , fd : & mut W ) -> Result < ( ) , CodecError > {
261
+ write_next ( fd, & self . burn_block_height ) ?;
278
262
write_next ( fd, self . stacks_tip_consensus_hash . as_bytes ( ) ) ?;
279
263
write_next ( fd, & self . stacks_tip ) ?;
264
+ write_next ( fd, & self . stacks_tip_height ) ?;
280
265
write_next ( fd, & ( self . server_version . as_bytes ( ) . len ( ) as u8 ) ) ?;
281
266
fd. write_all ( self . server_version . as_bytes ( ) )
282
267
. map_err ( CodecError :: WriteError ) ?;
283
- write_next ( fd, & self . burn_block_height ) ?;
284
- write_next ( fd, & self . peer_burn_block_height ) ?;
285
268
write_next ( fd, & self . pox_consensus ) ?;
286
- write_next ( fd, & self . chain_id ) ?;
287
269
Ok ( ( ) )
288
270
}
289
271
290
272
fn consensus_deserialize < R : Read > ( fd : & mut R ) -> Result < Self , CodecError > {
273
+ let burn_block_height = read_next :: < u64 , _ > ( fd) ?;
291
274
let stacks_tip_consensus_hash = read_next :: < ConsensusHash , _ > ( fd) ?;
292
275
let stacks_tip = read_next :: < BlockHeaderHash , _ > ( fd) ?;
276
+ let stacks_tip_height = read_next :: < u64 , _ > ( fd) ?;
293
277
let len_byte: u8 = read_next ( fd) ?;
294
278
let mut bytes = vec ! [ 0u8 ; len_byte as usize ] ;
295
279
fd. read_exact ( & mut bytes) . map_err ( CodecError :: ReadError ) ?;
@@ -299,17 +283,44 @@ impl StacksMessageCodec for MockSignData {
299
283
"Failed to parse server version name: could not contruct from utf8" . to_string ( ) ,
300
284
)
301
285
} ) ?;
302
- let burn_block_height = read_next :: < u64 , _ > ( fd) ?;
303
- let peer_burn_block_height = read_next :: < u64 , _ > ( fd) ?;
304
286
let pox_consensus = read_next :: < ConsensusHash , _ > ( fd) ?;
305
- let chain_id = read_next :: < u32 , _ > ( fd) ?;
306
287
Ok ( Self {
288
+ burn_block_height,
307
289
stacks_tip_consensus_hash,
308
290
stacks_tip,
291
+ stacks_tip_height,
309
292
server_version,
310
- burn_block_height,
311
- peer_burn_block_height,
312
293
pox_consensus,
294
+ } )
295
+ }
296
+ }
297
+
298
+ /// A snapshot of the signer view of the stacks node to be used for mock signing.
299
+ #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
300
+ pub struct MockSignData {
301
+ /// The view of the stacks node peer information at the time of the mock signature
302
+ pub peer_info : PeerInfo ,
303
+ /// The burn block height of the event that triggered the mock signature
304
+ pub event_burn_block_height : u64 ,
305
+ /// The chain id for the mock signature
306
+ pub chain_id : u32 ,
307
+ }
308
+
309
+ impl StacksMessageCodec for MockSignData {
310
+ fn consensus_serialize < W : Write > ( & self , fd : & mut W ) -> Result < ( ) , CodecError > {
311
+ self . peer_info . consensus_serialize ( fd) ?;
312
+ write_next ( fd, & self . event_burn_block_height ) ?;
313
+ write_next ( fd, & self . chain_id ) ?;
314
+ Ok ( ( ) )
315
+ }
316
+
317
+ fn consensus_deserialize < R : Read > ( fd : & mut R ) -> Result < Self , CodecError > {
318
+ let peer_info = PeerInfo :: consensus_deserialize ( fd) ?;
319
+ let event_burn_block_height = read_next :: < u64 , _ > ( fd) ?;
320
+ let chain_id = read_next :: < u32 , _ > ( fd) ?;
321
+ Ok ( Self {
322
+ peer_info,
323
+ event_burn_block_height,
313
324
chain_id,
314
325
} )
315
326
}
@@ -326,16 +337,21 @@ pub struct MockSignature {
326
337
}
327
338
328
339
impl MockSignature {
329
- /// Create a new mock sign data struct from the provided peer info, burn block height, chain id, and private key.
340
+ /// Create a new mock sign data struct from the provided event burn block height, peer info, chain id, and private key.
341
+ /// Note that peer burn block height and event burn block height may not be the same if the peer view is stale.
330
342
pub fn new (
331
- peer_view : RPCPeerInfoData ,
332
- burn_block_height : u64 ,
343
+ event_burn_block_height : u64 ,
344
+ peer_info : PeerInfo ,
333
345
chain_id : u32 ,
334
346
stacks_private_key : & StacksPrivateKey ,
335
347
) -> Self {
336
348
let mut sig = Self {
337
349
signature : MessageSignature :: empty ( ) ,
338
- sign_data : MockSignData :: new ( peer_view, burn_block_height, chain_id) ,
350
+ sign_data : MockSignData {
351
+ peer_info,
352
+ event_burn_block_height,
353
+ chain_id,
354
+ } ,
339
355
} ;
340
356
sig. sign ( stacks_private_key)
341
357
. expect ( "Failed to sign MockSignature" ) ;
@@ -350,25 +366,39 @@ impl MockSignature {
350
366
TupleData :: from_data ( vec ! [
351
367
(
352
368
"stacks-tip-consensus-hash" . into( ) ,
353
- Value :: buff_from( self . sign_data. stacks_tip_consensus_hash. as_bytes( ) . into( ) )
354
- . unwrap( ) ,
369
+ Value :: buff_from(
370
+ self . sign_data
371
+ . peer_info
372
+ . stacks_tip_consensus_hash
373
+ . as_bytes( )
374
+ . into( ) ,
375
+ )
376
+ . unwrap( ) ,
355
377
) ,
356
378
(
357
379
"stacks-tip" . into( ) ,
358
- Value :: buff_from( self . sign_data. stacks_tip. as_bytes( ) . into( ) ) . unwrap( ) ,
380
+ Value :: buff_from( self . sign_data. peer_info. stacks_tip. as_bytes( ) . into( ) )
381
+ . unwrap( ) ,
382
+ ) ,
383
+ (
384
+ "stacks-tip-height" . into( ) ,
385
+ Value :: UInt ( self . sign_data. peer_info. stacks_tip_height. into( ) ) ,
359
386
) ,
360
387
(
361
388
"server-version" . into( ) ,
362
- Value :: string_ascii_from_bytes( self . sign_data. server_version. clone( ) . into( ) )
363
- . unwrap( ) ,
389
+ Value :: string_ascii_from_bytes(
390
+ self . sign_data. peer_info. server_version. clone( ) . into( ) ,
391
+ )
392
+ . unwrap( ) ,
364
393
) ,
365
394
(
366
- "burn-block-height" . into( ) ,
367
- Value :: UInt ( self . sign_data. burn_block_height . into( ) ) ,
395
+ "event- burn-block-height" . into( ) ,
396
+ Value :: UInt ( self . sign_data. event_burn_block_height . into( ) ) ,
368
397
) ,
369
398
(
370
399
"pox-consensus" . into( ) ,
371
- Value :: buff_from( self . sign_data. pox_consensus. as_bytes( ) . into( ) ) . unwrap( ) ,
400
+ Value :: buff_from( self . sign_data. peer_info. pox_consensus. as_bytes( ) . into( ) )
401
+ . unwrap( ) ,
372
402
) ,
373
403
] )
374
404
. expect ( "Error creating signature hash" ) ,
@@ -822,23 +852,33 @@ mod test {
822
852
assert_eq ! ( signer_message, deserialized_signer_message) ;
823
853
}
824
854
825
- fn random_mock_sign_data ( ) -> MockSignData {
855
+ fn random_peer_data ( ) -> PeerInfo {
856
+ let burn_block_height = thread_rng ( ) . next_u64 ( ) ;
826
857
let stacks_tip_consensus_byte: u8 = thread_rng ( ) . gen ( ) ;
827
858
let stacks_tip_byte: u8 = thread_rng ( ) . gen ( ) ;
859
+ let stacks_tip_height = thread_rng ( ) . next_u64 ( ) ;
860
+ let server_version = "0.0.0" . to_string ( ) ;
828
861
let pox_consensus_byte: u8 = thread_rng ( ) . gen ( ) ;
862
+ PeerInfo {
863
+ burn_block_height,
864
+ stacks_tip_consensus_hash : ConsensusHash ( [ stacks_tip_consensus_byte; 20 ] ) ,
865
+ stacks_tip : BlockHeaderHash ( [ stacks_tip_byte; 32 ] ) ,
866
+ stacks_tip_height,
867
+ server_version,
868
+ pox_consensus : ConsensusHash ( [ pox_consensus_byte; 20 ] ) ,
869
+ }
870
+ }
871
+ fn random_mock_sign_data ( ) -> MockSignData {
829
872
let chain_byte: u8 = thread_rng ( ) . gen_range ( 0 ..=1 ) ;
830
873
let chain_id = if chain_byte == 1 {
831
874
CHAIN_ID_TESTNET
832
875
} else {
833
876
CHAIN_ID_MAINNET
834
877
} ;
878
+ let peer_info = random_peer_data ( ) ;
835
879
MockSignData {
836
- stacks_tip_consensus_hash : ConsensusHash ( [ stacks_tip_consensus_byte; 20 ] ) ,
837
- stacks_tip : BlockHeaderHash ( [ stacks_tip_byte; 32 ] ) ,
838
- server_version : "0.0.0" . to_string ( ) ,
839
- burn_block_height : thread_rng ( ) . next_u64 ( ) ,
840
- peer_burn_block_height : thread_rng ( ) . next_u64 ( ) ,
841
- pox_consensus : ConsensusHash ( [ pox_consensus_byte; 20 ] ) ,
880
+ peer_info,
881
+ event_burn_block_height : thread_rng ( ) . next_u64 ( ) ,
842
882
chain_id,
843
883
}
844
884
}
@@ -871,6 +911,15 @@ mod test {
871
911
. expect( "Failed to verify MockSignature" ) ) ;
872
912
}
873
913
914
+ #[ test]
915
+ fn serde_peer_data ( ) {
916
+ let peer_data = random_peer_data ( ) ;
917
+ let serialized_data = peer_data. serialize_to_vec ( ) ;
918
+ let deserialized_data = read_next :: < PeerInfo , _ > ( & mut & serialized_data[ ..] )
919
+ . expect ( "Failed to deserialize PeerInfo" ) ;
920
+ assert_eq ! ( peer_data, deserialized_data) ;
921
+ }
922
+
874
923
#[ test]
875
924
fn serde_mock_signature ( ) {
876
925
let mock_signature = MockSignature {
0 commit comments