1- use crate :: rpc:: methods:: * ;
21use crate :: rpc:: {
32 codec:: base:: OutboundCodec ,
43 protocol:: { Encoding , Protocol , ProtocolId , RPCError , Version , ERROR_TYPE_MAX , ERROR_TYPE_MIN } ,
54} ;
65use crate :: rpc:: { RPCCodedResponse , RPCRequest , RPCResponse } ;
6+ use crate :: { rpc:: methods:: * , EnrSyncCommitteeBitfield } ;
77use libp2p:: bytes:: BytesMut ;
88use snap:: read:: FrameDecoder ;
99use snap:: write:: FrameEncoder ;
@@ -78,7 +78,21 @@ impl<TSpec: EthSpec> Encoder<RPCCodedResponse<TSpec>> for SSZSnappyInboundCodec<
7878 attnets : res. attnets ( ) . clone ( ) ,
7979 } )
8080 . as_ssz_bytes ( ) ,
81- Version :: V2 => res. as_ssz_bytes ( ) ,
81+ Version :: V2 => {
82+ // `res` is of type MetaDataV2, return the ssz bytes
83+ if res. syncnets ( ) . is_ok ( ) {
84+ res. as_ssz_bytes ( )
85+ } else {
86+ // `res` is of type MetaDataV1, create a MetaDataV2 by adding a default syncnets field
87+ // Note: This code path is redundant as `res` would be always of type MetaDataV2
88+ MetaData :: < TSpec > :: V2 ( MetaDataV2 {
89+ seq_number : * res. seq_number ( ) ,
90+ attnets : res. attnets ( ) . clone ( ) ,
91+ syncnets : EnrSyncCommitteeBitfield :: < TSpec > :: default ( ) ,
92+ } )
93+ . as_ssz_bytes ( )
94+ }
95+ }
8296 }
8397 }
8498 } ,
@@ -175,6 +189,8 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
175189 let mut reader = FrameDecoder :: new ( limit_reader) ;
176190 let mut decoded_buffer = vec ! [ 0 ; length] ;
177191
192+ dbg ! ( & self . protocol) ;
193+
178194 match reader. read_exact ( & mut decoded_buffer) {
179195 Ok ( ( ) ) => {
180196 // `n` is how many bytes the reader read in the compressed stream
@@ -203,6 +219,9 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
203219 Protocol :: Ping => Ok ( Some ( RPCRequest :: Ping ( Ping {
204220 data : u64:: from_ssz_bytes ( & decoded_buffer) ?,
205221 } ) ) ) ,
222+
223+ // MetaData requests return early from InboundUpgrade and do not reach the decoder.
224+ // Handle this case just for completeness.
206225 Protocol :: MetaData => {
207226 if !decoded_buffer. is_empty ( ) {
208227 Err ( RPCError :: InvalidData )
@@ -212,26 +231,30 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
212231 }
213232 } ,
214233 // Receiving a Rpc request for protocol version 2 for range and root requests
215- Version :: V2 => {
216- match self . protocol . message_name {
217- // Request type doesn't change, only response type
218- Protocol :: BlocksByRange => Ok ( Some ( RPCRequest :: BlocksByRange (
219- BlocksByRangeRequest :: from_ssz_bytes ( & decoded_buffer) ?,
220- ) ) ) ,
221- Protocol :: BlocksByRoot => {
222- Ok ( Some ( RPCRequest :: BlocksByRoot ( BlocksByRootRequest {
223- block_roots : VariableList :: from_ssz_bytes ( & decoded_buffer) ?,
224- } ) ) )
234+ Version :: V2 => match self . protocol . message_name {
235+ // Request type doesn't change, only response type
236+ Protocol :: BlocksByRange => Ok ( Some ( RPCRequest :: BlocksByRange (
237+ BlocksByRangeRequest :: from_ssz_bytes ( & decoded_buffer) ?,
238+ ) ) ) ,
239+ Protocol :: BlocksByRoot => {
240+ Ok ( Some ( RPCRequest :: BlocksByRoot ( BlocksByRootRequest {
241+ block_roots : VariableList :: from_ssz_bytes ( & decoded_buffer) ?,
242+ } ) ) )
243+ }
244+ // MetaData requests return early from InboundUpgrade and do not reach the decoder.
245+ // Handle this case just for completeness.
246+ Protocol :: MetaData => {
247+ if !decoded_buffer. is_empty ( ) {
248+ Err ( RPCError :: InvalidData )
249+ } else {
250+ Ok ( Some ( RPCRequest :: MetaData ( PhantomData ) ) )
225251 }
226- _ => Err ( RPCError :: ErrorResponse (
227- RPCResponseErrorCode :: InvalidRequest ,
228- format ! (
229- "{} does not support version 2" ,
230- self . protocol. message_name
231- ) ,
232- ) ) ,
233252 }
234- }
253+ _ => Err ( RPCError :: ErrorResponse (
254+ RPCResponseErrorCode :: InvalidRequest ,
255+ format ! ( "{} does not support version 2" , self . protocol. message_name) ,
256+ ) ) ,
257+ } ,
235258 }
236259 }
237260 Err ( e) => handle_error ( e, reader. get_ref ( ) . get_ref ( ) . position ( ) , max_compressed_len) ,
@@ -547,7 +570,7 @@ mod tests {
547570 use crate :: rpc:: { protocol:: * , MetaData } ;
548571 use crate :: {
549572 rpc:: { methods:: StatusMessage , Ping , RPCResponseErrorCode } ,
550- types:: EnrAttestationBitfield ,
573+ types:: { EnrAttestationBitfield , EnrSyncCommitteeBitfield } ,
551574 } ;
552575 use std:: sync:: Arc ;
553576 use types:: {
@@ -597,6 +620,14 @@ mod tests {
597620 } )
598621 }
599622
623+ fn metadata_v2 ( ) -> MetaData < Spec > {
624+ MetaData :: V2 ( MetaDataV2 {
625+ seq_number : 1 ,
626+ attnets : EnrAttestationBitfield :: < Spec > :: default ( ) ,
627+ syncnets : EnrSyncCommitteeBitfield :: < Spec > :: default ( ) ,
628+ } )
629+ }
630+
600631 /// Encodes the given protocol response as bytes.
601632 fn encode (
602633 protocol : Protocol ,
@@ -714,9 +745,27 @@ mod tests {
714745 Ok ( Some ( RPCResponse :: MetaData ( metadata( ) ) ) ) ,
715746 ) ;
716747
717- // TODO: add metadataV2 response failure case
748+ assert_eq ! (
749+ encode_then_decode(
750+ Protocol :: MetaData ,
751+ Version :: V1 ,
752+ RPCCodedResponse :: Success ( RPCResponse :: MetaData ( metadata( ) ) ) ,
753+ ) ,
754+ Ok ( Some ( RPCResponse :: MetaData ( metadata( ) ) ) ) ,
755+ ) ;
756+
757+ // A MetaDataV2 still encodes as a MetaDataV1 since version is Version::V1
758+ assert_eq ! (
759+ encode_then_decode(
760+ Protocol :: MetaData ,
761+ Version :: V1 ,
762+ RPCCodedResponse :: Success ( RPCResponse :: MetaData ( metadata_v2( ) ) ) ,
763+ ) ,
764+ Ok ( Some ( RPCResponse :: MetaData ( metadata( ) ) ) ) ,
765+ ) ;
718766 }
719767
768+ // Test RPCResponse encoding/decoding for V1 messages
720769 #[ test]
721770 fn test_encode_then_decode_v2 ( ) {
722771 assert ! (
@@ -780,6 +829,25 @@ mod tests {
780829 ) ,
781830 Ok ( Some ( RPCResponse :: BlocksByRoot ( Box :: new( altair_block( ) ) ) ) )
782831 ) ;
832+
833+ // A MetaDataV1 still encodes as a MetaDataV2 since version is Version::V2
834+ assert_eq ! (
835+ encode_then_decode(
836+ Protocol :: MetaData ,
837+ Version :: V2 ,
838+ RPCCodedResponse :: Success ( RPCResponse :: MetaData ( metadata( ) ) )
839+ ) ,
840+ Ok ( Some ( RPCResponse :: MetaData ( metadata_v2( ) ) ) )
841+ ) ;
842+
843+ assert_eq ! (
844+ encode_then_decode(
845+ Protocol :: MetaData ,
846+ Version :: V2 ,
847+ RPCCodedResponse :: Success ( RPCResponse :: MetaData ( metadata_v2( ) ) )
848+ ) ,
849+ Ok ( Some ( RPCResponse :: MetaData ( metadata_v2( ) ) ) )
850+ ) ;
783851 }
784852
785853 #[ test]
0 commit comments