@@ -14,7 +14,7 @@ use types::light_client_update::CURRENT_SYNC_COMMITTEE_PROOF_LEN;
1414use types:: {
1515 BeaconBlock , BeaconBlockAltair , BeaconBlockBase , BeaconBlockHeader , BeaconBlockMerge ,
1616 EmptyBlock , Epoch , EthSpec , ForkContext , ForkName , Hash256 , MinimalEthSpec , Signature ,
17- SignedBeaconBlock , Slot , SyncCommittee ,
17+ SignedBeaconBlock , Slot , SyncCommittee , LightClientOptimisticUpdate , SyncAggregate ,
1818} ;
1919
2020mod common;
@@ -145,7 +145,7 @@ fn test_status_rpc() {
145145fn test_light_client_bootstrap_rpc ( ) {
146146 // set up the logging. The level and enabled logging or not
147147 let log_level = Level :: Debug ;
148- let enable_logging = false ;
148+ let enable_logging = true ;
149149
150150 let rt = Arc :: new ( Runtime :: new ( ) . unwrap ( ) ) ;
151151
@@ -167,9 +167,7 @@ fn test_light_client_bootstrap_rpc() {
167167 let rpc_request = Request :: LightClientBootstrap ( LightClientBootstrapRequest {
168168 root : Hash256 :: from_low_u64_be ( 0 ) ,
169169 } ) ;
170-
171- // Dummy LightClientBootstrap RPC response
172- let nl_rpc_response = RPCResponse :: LightClientBootstrap ( LightClientBootstrap {
170+ let light_client_bootstrap = LightClientBootstrap :: < E > {
173171 header : BeaconBlockHeader {
174172 slot : Slot :: new ( 0 ) ,
175173 proposer_index : 0 ,
@@ -183,30 +181,109 @@ fn test_light_client_bootstrap_rpc() {
183181 CURRENT_SYNC_COMMITTEE_PROOF_LEN
184182 ] )
185183 . unwrap ( ) ,
186- } ) ;
184+ } ;
185+ let rpc_response = RPCResponse :: LightClientBootstrap ( light_client_bootstrap. clone ( ) ) ;
187186
188- let rpc_response = Response :: LightClientBootstrap ( LightClientBootstrap {
189- header : BeaconBlockHeader {
187+ // build the sender future
188+ let sender_future = async {
189+ loop {
190+ match sender. 0 . next_event ( ) . await {
191+ Some ( SwarmEvent :: ConnectionEstablished { peer_id, .. } ) => {
192+ // Send a LightClientBootstrap message
193+ debug ! ( log, "Sending RPC" ) ;
194+ sender. 0 . send_request ( peer_id, 10 , rpc_request. clone ( ) ) ;
195+ }
196+ Some ( SwarmEvent :: Behaviour ( RPCMessage {
197+ peer_id : _,
198+ conn_id : _,
199+ event : Ok ( RPCReceived :: Response ( _, response) ) ,
200+ } ) ) => {
201+ // Should receive the RPC response
202+ debug ! ( log, "Sender Received" ) ;
203+ assert_eq ! ( response, rpc_response. clone( ) ) ;
204+ debug ! ( log, "Sender Completed" ) ;
205+ return ;
206+ }
207+ _ => { }
208+ }
209+ }
210+ } ;
211+
212+ // build the receiver future
213+ let receiver_future = async {
214+ loop {
215+ match receiver. next_event ( ) . await {
216+ NetworkEvent :: RequestReceived {
217+ peer_id,
218+ id,
219+ request,
220+ } => {
221+ if request == rpc_request {
222+ // send the response
223+ debug ! ( log, "Receiver Received" ) ;
224+ receiver. send_response ( peer_id, id, Response :: LightClientBootstrap ( light_client_bootstrap. clone ( ) ) ) ;
225+ }
226+ }
227+ _ => { } // Ignore other events
228+ }
229+ }
230+ } ;
231+
232+ tokio:: select! {
233+ _ = sender_future => { }
234+ _ = receiver_future => { }
235+ _ = sleep( Duration :: from_secs( 30 ) ) => {
236+ panic!( "Future timed out" ) ;
237+ }
238+ }
239+ } )
240+ }
241+
242+ // Tests the LightClientOptimisticUpdate RPC message
243+ #[ test]
244+ #[ allow( clippy:: single_match) ]
245+ fn test_light_client_optimistic_update_rpc ( ) {
246+ // set up the logging. The level and enabled logging or not
247+ let log_level = Level :: Debug ;
248+ let enable_logging = true ;
249+
250+ let rt = Arc :: new ( Runtime :: new ( ) . unwrap ( ) ) ;
251+
252+ let log = common:: build_log ( log_level, enable_logging) ;
253+
254+ rt. block_on ( async {
255+ // get sender/receiver
256+ let ( mut sender, mut receiver) : (
257+ common:: MockLibp2pLightClientInstance ,
258+ common:: Libp2pInstance ,
259+ ) = common:: build_node_and_light_client (
260+ Arc :: downgrade ( & rt) ,
261+ & log,
262+ ForkName :: Altair ,
263+ )
264+ . await ;
265+
266+ // Dummy LightClientOptimisticUpdate RPC request
267+ let rpc_request = Request :: LightClientOptimisticUpdate ;
268+ let light_client_optimistic_update = Arc :: new ( LightClientOptimisticUpdate :: < E > {
269+ attested_header : BeaconBlockHeader {
190270 slot : Slot :: new ( 0 ) ,
191271 proposer_index : 0 ,
192272 parent_root : Hash256 :: zero ( ) ,
193273 state_root : Hash256 :: zero ( ) ,
194274 body_root : Hash256 :: zero ( ) ,
195275 } ,
196- current_sync_committee : Arc :: new ( SyncCommittee :: temporary ( ) . unwrap ( ) ) ,
197- current_sync_committee_branch : FixedVector :: new ( vec ! [
198- Hash256 :: zero( ) ;
199- CURRENT_SYNC_COMMITTEE_PROOF_LEN
200- ] )
201- . unwrap ( ) ,
276+ sync_aggregate : SyncAggregate :: empty ( ) ,
277+ signature_slot : Slot :: new ( 0 ) ,
202278 } ) ;
279+ let rpc_response = RPCResponse :: LightClientOptimisticUpdate ( light_client_optimistic_update. clone ( ) ) ;
203280
204281 // build the sender future
205282 let sender_future = async {
206283 loop {
207284 match sender. 0 . next_event ( ) . await {
208285 Some ( SwarmEvent :: ConnectionEstablished { peer_id, .. } ) => {
209- // Send a LightClientBootstrap message
286+ // Send a LightClientOptimisticUpdate message
210287 debug ! ( log, "Sending RPC" ) ;
211288 sender. 0 . send_request ( peer_id, 10 , rpc_request. clone ( ) ) ;
212289 }
@@ -217,7 +294,7 @@ fn test_light_client_bootstrap_rpc() {
217294 } ) ) => {
218295 // Should receive the RPC response
219296 debug ! ( log, "Sender Received" ) ;
220- assert_eq ! ( response, nl_rpc_response . clone ( ) ) ;
297+ assert_eq ! ( response, rpc_response ) ;
221298 debug ! ( log, "Sender Completed" ) ;
222299 return ;
223300 }
@@ -238,7 +315,11 @@ fn test_light_client_bootstrap_rpc() {
238315 if request == rpc_request {
239316 // send the response
240317 debug ! ( log, "Receiver Received" ) ;
241- receiver. send_response ( peer_id, id, rpc_response. clone ( ) ) ;
318+ receiver. send_response (
319+ peer_id,
320+ id,
321+ Response :: LightClientOptimisticUpdate ( light_client_optimistic_update. clone ( ) ) ,
322+ ) ;
242323 }
243324 }
244325 _ => { } // Ignore other events
@@ -256,6 +337,7 @@ fn test_light_client_bootstrap_rpc() {
256337 } )
257338}
258339
340+
259341// Tests a streamed BlocksByRange RPC Message
260342#[ test]
261343#[ allow( clippy:: single_match) ]
@@ -304,7 +386,7 @@ fn test_blocks_by_range_chunked_rpc() {
304386 loop {
305387 match sender. next_event ( ) . await {
306388 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
307- // Send a STATUS message
389+ // Send a BlocksByRange message
308390 debug ! ( log, "Sending RPC" ) ;
309391 sender. send_request ( peer_id, request_id, rpc_request. clone ( ) ) ;
310392 }
@@ -419,7 +501,7 @@ fn test_blocks_by_range_over_limit() {
419501 loop {
420502 match sender. next_event ( ) . await {
421503 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
422- // Send a STATUS message
504+ // Send a BlocksByRange message
423505 debug ! ( log, "Sending RPC" ) ;
424506 sender. send_request ( peer_id, request_id, rpc_request. clone ( ) ) ;
425507 }
@@ -507,7 +589,7 @@ fn test_blocks_by_range_chunked_rpc_terminates_correctly() {
507589 loop {
508590 match sender. next_event ( ) . await {
509591 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
510- // Send a STATUS message
592+ // Send a BlocksByRange message
511593 debug ! ( log, "Sending RPC" ) ;
512594 sender. send_request ( peer_id, request_id, rpc_request. clone ( ) ) ;
513595 }
@@ -631,7 +713,7 @@ fn test_blocks_by_range_single_empty_rpc() {
631713 loop {
632714 match sender. next_event ( ) . await {
633715 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
634- // Send a STATUS message
716+ // Send a BlocksByRange message
635717 debug ! ( log, "Sending RPC" ) ;
636718 sender. send_request ( peer_id, 10 , rpc_request. clone ( ) ) ;
637719 }
@@ -746,7 +828,7 @@ fn test_blocks_by_root_chunked_rpc() {
746828 loop {
747829 match sender. next_event ( ) . await {
748830 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
749- // Send a STATUS message
831+ // Send a BlocksByRoot message
750832 debug ! ( log, "Sending RPC" ) ;
751833 sender. send_request ( peer_id, 6 , rpc_request. clone ( ) ) ;
752834 }
@@ -870,7 +952,7 @@ fn test_blocks_by_root_chunked_rpc_terminates_correctly() {
870952 loop {
871953 match sender. next_event ( ) . await {
872954 NetworkEvent :: PeerConnectedOutgoing ( peer_id) => {
873- // Send a STATUS message
955+ // Send a BlocksByRoot message
874956 debug ! ( log, "Sending RPC" ) ;
875957 sender. send_request ( peer_id, 10 , rpc_request. clone ( ) ) ;
876958 }
0 commit comments