@@ -67,7 +67,13 @@ impl<E: EthSpec> SSZSnappyInboundCodec<E> {
6767 ) -> Result < ( ) , RPCError > {
6868 let bytes = match & item {
6969 RpcResponse :: Success ( resp) => match & resp {
70- RpcSuccessResponse :: Status ( res) => res. as_ssz_bytes ( ) ,
70+ RpcSuccessResponse :: Status ( res) => match self . protocol . versioned_protocol {
71+ SupportedProtocol :: StatusV1 => res. status_v1 ( ) . as_ssz_bytes ( ) ,
72+ SupportedProtocol :: StatusV2 => res. status_v2 ( ) . as_ssz_bytes ( ) ,
73+ _ => {
74+ unreachable ! ( "We only send status responses on negotiating status protocol" )
75+ }
76+ } ,
7177 RpcSuccessResponse :: BlocksByRange ( res) => res. as_ssz_bytes ( ) ,
7278 RpcSuccessResponse :: BlocksByRoot ( res) => res. as_ssz_bytes ( ) ,
7379 RpcSuccessResponse :: BlobsByRange ( res) => res. as_ssz_bytes ( ) ,
@@ -329,7 +335,16 @@ impl<E: EthSpec> Encoder<RequestType<E>> for SSZSnappyOutboundCodec<E> {
329335
330336 fn encode ( & mut self , item : RequestType < E > , dst : & mut BytesMut ) -> Result < ( ) , Self :: Error > {
331337 let bytes = match item {
332- RequestType :: Status ( req) => req. as_ssz_bytes ( ) ,
338+ RequestType :: Status ( req) => {
339+ // Send the status message based on the negotiated protocol
340+ match self . protocol . versioned_protocol {
341+ SupportedProtocol :: StatusV1 => req. status_v1 ( ) . as_ssz_bytes ( ) ,
342+ SupportedProtocol :: StatusV2 => req. status_v2 ( ) . as_ssz_bytes ( ) ,
343+ _ => {
344+ unreachable ! ( "We only send status requests on negotiating status protocol" )
345+ }
346+ }
347+ }
333348 RequestType :: Goodbye ( req) => req. as_ssz_bytes ( ) ,
334349 RequestType :: BlocksByRange ( r) => match r {
335350 OldBlocksByRangeRequest :: V1 ( req) => req. as_ssz_bytes ( ) ,
@@ -553,9 +568,12 @@ fn handle_rpc_request<E: EthSpec>(
553568 spec : & ChainSpec ,
554569) -> Result < Option < RequestType < E > > , RPCError > {
555570 match versioned_protocol {
556- SupportedProtocol :: StatusV1 => Ok ( Some ( RequestType :: Status (
557- StatusMessage :: from_ssz_bytes ( decoded_buffer) ?,
558- ) ) ) ,
571+ SupportedProtocol :: StatusV1 => Ok ( Some ( RequestType :: Status ( StatusMessage :: V1 (
572+ StatusMessageV1 :: from_ssz_bytes ( decoded_buffer) ?,
573+ ) ) ) ) ,
574+ SupportedProtocol :: StatusV2 => Ok ( Some ( RequestType :: Status ( StatusMessage :: V2 (
575+ StatusMessageV2 :: from_ssz_bytes ( decoded_buffer) ?,
576+ ) ) ) ) ,
559577 SupportedProtocol :: GoodbyeV1 => Ok ( Some ( RequestType :: Goodbye (
560578 GoodbyeReason :: from_ssz_bytes ( decoded_buffer) ?,
561579 ) ) ) ,
@@ -666,9 +684,12 @@ fn handle_rpc_response<E: EthSpec>(
666684 fork_name : Option < ForkName > ,
667685) -> Result < Option < RpcSuccessResponse < E > > , RPCError > {
668686 match versioned_protocol {
669- SupportedProtocol :: StatusV1 => Ok ( Some ( RpcSuccessResponse :: Status (
670- StatusMessage :: from_ssz_bytes ( decoded_buffer) ?,
671- ) ) ) ,
687+ SupportedProtocol :: StatusV1 => Ok ( Some ( RpcSuccessResponse :: Status ( StatusMessage :: V1 (
688+ StatusMessageV1 :: from_ssz_bytes ( decoded_buffer) ?,
689+ ) ) ) ) ,
690+ SupportedProtocol :: StatusV2 => Ok ( Some ( RpcSuccessResponse :: Status ( StatusMessage :: V2 (
691+ StatusMessageV2 :: from_ssz_bytes ( decoded_buffer) ?,
692+ ) ) ) ) ,
672693 // This case should be unreachable as `Goodbye` has no response.
673694 SupportedProtocol :: GoodbyeV1 => Err ( RPCError :: InvalidData (
674695 "Goodbye RPC message has no valid response" . to_string ( ) ,
@@ -1036,14 +1057,25 @@ mod tests {
10361057 SignedBeaconBlock :: from_block ( block, Signature :: empty ( ) )
10371058 }
10381059
1039- fn status_message ( ) -> StatusMessage {
1040- StatusMessage {
1060+ fn status_message_v1 ( ) -> StatusMessage {
1061+ StatusMessage :: V1 ( StatusMessageV1 {
10411062 fork_digest : [ 0 ; 4 ] ,
10421063 finalized_root : Hash256 :: zero ( ) ,
10431064 finalized_epoch : Epoch :: new ( 1 ) ,
10441065 head_root : Hash256 :: zero ( ) ,
10451066 head_slot : Slot :: new ( 1 ) ,
1046- }
1067+ } )
1068+ }
1069+
1070+ fn status_message_v2 ( ) -> StatusMessage {
1071+ StatusMessage :: V2 ( StatusMessageV2 {
1072+ fork_digest : [ 0 ; 4 ] ,
1073+ finalized_root : Hash256 :: zero ( ) ,
1074+ finalized_epoch : Epoch :: new ( 1 ) ,
1075+ head_root : Hash256 :: zero ( ) ,
1076+ head_slot : Slot :: new ( 1 ) ,
1077+ earliest_available_slot : Slot :: new ( 0 ) ,
1078+ } )
10471079 }
10481080
10491081 fn bbrange_request_v1 ( ) -> OldBlocksByRangeRequest {
@@ -1284,11 +1316,22 @@ mod tests {
12841316 assert_eq ! (
12851317 encode_then_decode_response(
12861318 SupportedProtocol :: StatusV1 ,
1287- RpcResponse :: Success ( RpcSuccessResponse :: Status ( status_message ( ) ) ) ,
1319+ RpcResponse :: Success ( RpcSuccessResponse :: Status ( status_message_v1 ( ) ) ) ,
12881320 ForkName :: Base ,
12891321 & chain_spec,
12901322 ) ,
1291- Ok ( Some ( RpcSuccessResponse :: Status ( status_message( ) ) ) )
1323+ Ok ( Some ( RpcSuccessResponse :: Status ( status_message_v1( ) ) ) )
1324+ ) ;
1325+
1326+ // A StatusV2 still encodes as a StatusV1 since version is Version::V1
1327+ assert_eq ! (
1328+ encode_then_decode_response(
1329+ SupportedProtocol :: StatusV1 ,
1330+ RpcResponse :: Success ( RpcSuccessResponse :: Status ( status_message_v2( ) ) ) ,
1331+ ForkName :: Fulu ,
1332+ & chain_spec,
1333+ ) ,
1334+ Ok ( Some ( RpcSuccessResponse :: Status ( status_message_v1( ) ) ) )
12921335 ) ;
12931336
12941337 assert_eq ! (
@@ -1716,6 +1759,27 @@ mod tests {
17161759 ) ,
17171760 Ok ( Some ( RpcSuccessResponse :: MetaData ( metadata_v2( ) ) ) )
17181761 ) ;
1762+
1763+ // A StatusV1 still encodes as a StatusV2 since version is Version::V2
1764+ assert_eq ! (
1765+ encode_then_decode_response(
1766+ SupportedProtocol :: StatusV2 ,
1767+ RpcResponse :: Success ( RpcSuccessResponse :: Status ( status_message_v1( ) ) ) ,
1768+ ForkName :: Fulu ,
1769+ & chain_spec,
1770+ ) ,
1771+ Ok ( Some ( RpcSuccessResponse :: Status ( status_message_v2( ) ) ) )
1772+ ) ;
1773+
1774+ assert_eq ! (
1775+ encode_then_decode_response(
1776+ SupportedProtocol :: StatusV2 ,
1777+ RpcResponse :: Success ( RpcSuccessResponse :: Status ( status_message_v2( ) ) ) ,
1778+ ForkName :: Fulu ,
1779+ & chain_spec,
1780+ ) ,
1781+ Ok ( Some ( RpcSuccessResponse :: Status ( status_message_v2( ) ) ) )
1782+ ) ;
17191783 }
17201784
17211785 // Test RPCResponse encoding/decoding for V2 messages
@@ -1901,7 +1965,8 @@ mod tests {
19011965
19021966 let requests: & [ RequestType < Spec > ] = & [
19031967 RequestType :: Ping ( ping_message ( ) ) ,
1904- RequestType :: Status ( status_message ( ) ) ,
1968+ RequestType :: Status ( status_message_v1 ( ) ) ,
1969+ RequestType :: Status ( status_message_v2 ( ) ) ,
19051970 RequestType :: Goodbye ( GoodbyeReason :: Fault ) ,
19061971 RequestType :: BlocksByRange ( bbrange_request_v1 ( ) ) ,
19071972 RequestType :: BlocksByRange ( bbrange_request_v2 ( ) ) ,
@@ -1948,7 +2013,7 @@ mod tests {
19482013 let malicious_padding: & ' static [ u8 ] = b"\xFE \x00 \x00 \x00 " ;
19492014
19502015 // Status message is 84 bytes uncompressed. `max_compressed_len` is 32 + 84 + 84/6 = 130.
1951- let status_message_bytes = StatusMessage {
2016+ let status_message_bytes = StatusMessageV1 {
19522017 fork_digest : [ 0 ; 4 ] ,
19532018 finalized_root : Hash256 :: zero ( ) ,
19542019 finalized_epoch : Epoch :: new ( 1 ) ,
@@ -2071,7 +2136,7 @@ mod tests {
20712136 assert_eq ! ( stream_identifier. len( ) , 10 ) ;
20722137
20732138 // Status message is 84 bytes uncompressed. `max_compressed_len` is 32 + 84 + 84/6 = 130.
2074- let status_message_bytes = StatusMessage {
2139+ let status_message_bytes = StatusMessageV1 {
20752140 fork_digest : [ 0 ; 4 ] ,
20762141 finalized_root : Hash256 :: zero ( ) ,
20772142 finalized_epoch : Epoch :: new ( 1 ) ,
0 commit comments