@@ -603,7 +603,7 @@ impl From<&BlockResponse> for BlockResponseTypePrefix {
603
603
#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
604
604
pub enum BlockResponse {
605
605
/// The Nakamoto block was accepted and therefore signed
606
- Accepted ( ( Sha512Trunc256Sum , MessageSignature ) ) ,
606
+ Accepted ( BlockAccepted ) ,
607
607
/// The Nakamoto block was rejected and therefore not signed
608
608
Rejected ( BlockRejection ) ,
609
609
}
@@ -616,7 +616,7 @@ impl std::fmt::Display for BlockResponse {
616
616
write ! (
617
617
f,
618
618
"BlockAccepted: signer_sighash = {}, signature = {}" ,
619
- a. 0 , a. 1
619
+ a. signer_signature_hash , a. signature
620
620
)
621
621
}
622
622
BlockResponse :: Rejected ( r) => {
@@ -633,7 +633,10 @@ impl std::fmt::Display for BlockResponse {
633
633
impl BlockResponse {
634
634
/// Create a new accepted BlockResponse for the provided block signer signature hash and signature
635
635
pub fn accepted ( hash : Sha512Trunc256Sum , sig : MessageSignature ) -> Self {
636
- Self :: Accepted ( ( hash, sig) )
636
+ Self :: Accepted ( BlockAccepted {
637
+ signer_signature_hash : hash,
638
+ signature : sig,
639
+ } )
637
640
}
638
641
639
642
/// Create a new rejected BlockResponse for the provided block signer signature hash and rejection code and sign it with the provided private key
@@ -651,9 +654,8 @@ impl StacksMessageCodec for BlockResponse {
651
654
fn consensus_serialize < W : Write > ( & self , fd : & mut W ) -> Result < ( ) , CodecError > {
652
655
write_next ( fd, & ( BlockResponseTypePrefix :: from ( self ) as u8 ) ) ?;
653
656
match self {
654
- BlockResponse :: Accepted ( ( hash, sig) ) => {
655
- write_next ( fd, hash) ?;
656
- write_next ( fd, sig) ?;
657
+ BlockResponse :: Accepted ( accepted) => {
658
+ write_next ( fd, accepted) ?;
657
659
}
658
660
BlockResponse :: Rejected ( rejection) => {
659
661
write_next ( fd, rejection) ?;
@@ -667,9 +669,8 @@ impl StacksMessageCodec for BlockResponse {
667
669
let type_prefix = BlockResponseTypePrefix :: try_from ( type_prefix_byte) ?;
668
670
let response = match type_prefix {
669
671
BlockResponseTypePrefix :: Accepted => {
670
- let hash = read_next :: < Sha512Trunc256Sum , _ > ( fd) ?;
671
- let sig = read_next :: < MessageSignature , _ > ( fd) ?;
672
- BlockResponse :: Accepted ( ( hash, sig) )
672
+ let accepted = read_next :: < BlockAccepted , _ > ( fd) ?;
673
+ BlockResponse :: Accepted ( accepted)
673
674
}
674
675
BlockResponseTypePrefix :: Rejected => {
675
676
let rejection = read_next :: < BlockRejection , _ > ( fd) ?;
@@ -680,6 +681,32 @@ impl StacksMessageCodec for BlockResponse {
680
681
}
681
682
}
682
683
684
+ /// A rejection response from a signer for a proposed block
685
+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
686
+ pub struct BlockAccepted {
687
+ /// The signer signature hash of the block that was accepted
688
+ pub signer_signature_hash : Sha512Trunc256Sum ,
689
+ /// The signer's signature across the acceptance
690
+ pub signature : MessageSignature ,
691
+ }
692
+
693
+ impl StacksMessageCodec for BlockAccepted {
694
+ fn consensus_serialize < W : Write > ( & self , fd : & mut W ) -> Result < ( ) , CodecError > {
695
+ write_next ( fd, & self . signer_signature_hash ) ?;
696
+ write_next ( fd, & self . signature ) ?;
697
+ Ok ( ( ) )
698
+ }
699
+
700
+ fn consensus_deserialize < R : Read > ( fd : & mut R ) -> Result < Self , CodecError > {
701
+ let signer_signature_hash = read_next :: < Sha512Trunc256Sum , _ > ( fd) ?;
702
+ let signature = read_next :: < MessageSignature , _ > ( fd) ?;
703
+ Ok ( Self {
704
+ signer_signature_hash,
705
+ signature,
706
+ } )
707
+ }
708
+ }
709
+
683
710
/// A rejection response from a signer for a proposed block
684
711
#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
685
712
pub struct BlockRejection {
@@ -894,7 +921,7 @@ mod test {
894
921
use clarity:: consts:: CHAIN_ID_MAINNET ;
895
922
use clarity:: types:: chainstate:: { ConsensusHash , StacksBlockId , TrieHash } ;
896
923
use clarity:: types:: PrivateKey ;
897
- use clarity:: util:: hash:: MerkleTree ;
924
+ use clarity:: util:: hash:: { hex_bytes , MerkleTree } ;
898
925
use clarity:: util:: secp256k1:: MessageSignature ;
899
926
use rand:: rngs:: mock;
900
927
use rand:: { thread_rng, Rng , RngCore } ;
@@ -958,8 +985,11 @@ mod test {
958
985
959
986
#[ test]
960
987
fn serde_block_response ( ) {
961
- let response =
962
- BlockResponse :: Accepted ( ( Sha512Trunc256Sum ( [ 0u8 ; 32 ] ) , MessageSignature :: empty ( ) ) ) ;
988
+ let accepted = BlockAccepted {
989
+ signer_signature_hash : Sha512Trunc256Sum ( [ 0u8 ; 32 ] ) ,
990
+ signature : MessageSignature :: empty ( ) ,
991
+ } ;
992
+ let response = BlockResponse :: Accepted ( accepted) ;
963
993
let serialized_response = response. serialize_to_vec ( ) ;
964
994
let deserialized_response = read_next :: < BlockResponse , _ > ( & mut & serialized_response[ ..] )
965
995
. expect ( "Failed to deserialize BlockResponse" ) ;
@@ -979,10 +1009,11 @@ mod test {
979
1009
980
1010
#[ test]
981
1011
fn serde_signer_message ( ) {
982
- let signer_message = SignerMessage :: BlockResponse ( BlockResponse :: Accepted ( (
983
- Sha512Trunc256Sum ( [ 2u8 ; 32 ] ) ,
984
- MessageSignature :: empty ( ) ,
985
- ) ) ) ;
1012
+ let accepted = BlockAccepted {
1013
+ signer_signature_hash : Sha512Trunc256Sum ( [ 2u8 ; 32 ] ) ,
1014
+ signature : MessageSignature :: empty ( ) ,
1015
+ } ;
1016
+ let signer_message = SignerMessage :: BlockResponse ( BlockResponse :: Accepted ( accepted) ) ;
986
1017
let serialized_signer_message = signer_message. serialize_to_vec ( ) ;
987
1018
let deserialized_signer_message =
988
1019
read_next :: < SignerMessage , _ > ( & mut & serialized_signer_message[ ..] )
@@ -1122,4 +1153,48 @@ mod test {
1122
1153
. expect ( "Failed to deserialize MockSignData" ) ;
1123
1154
assert_eq ! ( mock_block, deserialized_data) ;
1124
1155
}
1156
+
1157
+ #[ test]
1158
+ fn test_backwards_compatibility ( ) {
1159
+ let block_rejected_hex = "010100000050426c6f636b206973206e6f7420612074656e7572652d737461727420626c6f636b2c20616e642068617320616e20756e7265636f676e697a65642074656e75726520636f6e73656e7375732068617368000691f95f84b7045f7dce7757052caa986ef042cb58f7df5031a3b5b5d0e3dda63e80000000006fb349212e1a1af1a3c712878d5159b5ec14636adb6f70be00a6da4ad4f88a9934d8a9abb229620dd8e0f225d63401e36c64817fb29e6c05591dcbe95c512df3" ;
1160
+ let block_rejected_bytes = hex_bytes ( & block_rejected_hex) . unwrap ( ) ;
1161
+ let block_accepted_hex = "010011717149677c2ac97d15ae5954f7a716f10100b9cb81a2bf27551b2f2e54ef19001c694f8134c5c90f2f2bcd330e9f423204884f001b5df0050f36a2c4ff79dd93522bb2ae395ea87de4964886447507c18374b7a46ee2e371e9bf332f0706a3e8" ;
1162
+ let block_accepted_bytes = hex_bytes ( & block_accepted_hex) . unwrap ( ) ;
1163
+ let block_rejected = read_next :: < SignerMessage , _ > ( & mut & block_rejected_bytes[ ..] )
1164
+ . expect ( "Failed to deserialize BlockRejection" ) ;
1165
+ let block_accepted = read_next :: < SignerMessage , _ > ( & mut & block_accepted_bytes[ ..] )
1166
+ . expect ( "Failed to deserialize BlockRejection" ) ;
1167
+
1168
+ assert_eq ! (
1169
+ block_rejected,
1170
+ SignerMessage :: BlockResponse ( BlockResponse :: Rejected ( BlockRejection {
1171
+ reason_code: RejectCode :: ValidationFailed ( ValidateRejectCode :: NoSuchTenure ) ,
1172
+ reason: "Block is not a tenure-start block, and has an unrecognized tenure consensus hash" . to_string( ) ,
1173
+ signer_signature_hash: Sha512Trunc256Sum ( [ 145 , 249 , 95 , 132 , 183 , 4 , 95 , 125 , 206 , 119 , 87 , 5 , 44 , 170 , 152 , 110 , 240 , 66 , 203 , 88 , 247 , 223 , 80 , 49 , 163 , 181 , 181 , 208 , 227 , 221 , 166 , 62 ] ) ,
1174
+ chain_id: CHAIN_ID_TESTNET ,
1175
+ signature: MessageSignature ( [
1176
+ 0 , 111 , 179 , 73 , 33 , 46 , 26 , 26 , 241 , 163 , 199 , 18 , 135 , 141 , 81 , 89 , 181 , 236 ,
1177
+ 20 , 99 , 106 , 219 , 111 , 112 , 190 , 0 , 166 , 218 , 74 , 212 , 248 , 138 , 153 , 52 , 216 ,
1178
+ 169 , 171 , 178 , 41 , 98 , 13 , 216 , 224 , 242 , 37 , 214 , 52 , 1 , 227 , 108 , 100 , 129 ,
1179
+ 127 , 178 , 158 , 108 , 5 , 89 , 29 , 203 , 233 , 92 , 81 , 45 , 243 ,
1180
+ ] ) ,
1181
+ } ) )
1182
+ ) ;
1183
+
1184
+ assert_eq ! (
1185
+ block_accepted,
1186
+ SignerMessage :: BlockResponse ( BlockResponse :: Accepted ( BlockAccepted {
1187
+ signer_signature_hash: Sha512Trunc256Sum ( [
1188
+ 17 , 113 , 113 , 73 , 103 , 124 , 42 , 201 , 125 , 21 , 174 , 89 , 84 , 247 , 167 , 22 , 241 ,
1189
+ 1 , 0 , 185 , 203 , 129 , 162 , 191 , 39 , 85 , 27 , 47 , 46 , 84 , 239 , 25
1190
+ ] ) ,
1191
+ signature: MessageSignature ( [
1192
+ 0 , 28 , 105 , 79 , 129 , 52 , 197 , 201 , 15 , 47 , 43 , 205 , 51 , 14 , 159 , 66 , 50 , 4 ,
1193
+ 136 , 79 , 0 , 27 , 93 , 240 , 5 , 15 , 54 , 162 , 196 , 255 , 121 , 221 , 147 , 82 , 43 , 178 ,
1194
+ 174 , 57 , 94 , 168 , 125 , 228 , 150 , 72 , 134 , 68 , 117 , 7 , 193 , 131 , 116 , 183 , 164 ,
1195
+ 110 , 226 , 227 , 113 , 233 , 191 , 51 , 47 , 7 , 6 , 163 , 232
1196
+ ] ) ,
1197
+ } ) )
1198
+ ) ;
1199
+ }
1125
1200
}
0 commit comments