From b120850f86c781a5ad7b675b16c2af0822bb1b45 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Mon, 17 Feb 2025 13:20:57 +0100 Subject: [PATCH] review: spawn_handle + BoundedVec --- node/src/endpoint_gossip/handler.rs | 4 ++-- node/src/endpoint_gossip/mod.rs | 14 +++++--------- node/src/endpoint_gossip/rpc/state.rs | 4 ++-- node/src/endpoint_gossip/rpc/tests.rs | 10 +++++----- node/src/endpoint_gossip/tests.rs | 8 ++++---- node/src/service.rs | 8 +++++++- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/node/src/endpoint_gossip/handler.rs b/node/src/endpoint_gossip/handler.rs index e7c6a6dc..2491298c 100644 --- a/node/src/endpoint_gossip/handler.rs +++ b/node/src/endpoint_gossip/handler.rs @@ -1,5 +1,5 @@ use crate::endpoint_gossip::{ - types::validation_result::DuniterStreamValidationResult, DuniterEndpoint, Peer, Peering, + types::validation_result::DuniterStreamValidationResult, DuniterEndpoints, Peer, Peering, PROPAGATE_TIMEOUT, }; use codec::{Decode, Encode}; @@ -22,7 +22,7 @@ pub fn build< network: N, rpc_sink: TracingUnboundedSender<DuniterPeeringEvent>, command_rx: Option<TracingUnboundedReceiver<DuniterPeeringCommand>>, - endpoints: Vec<DuniterEndpoint>, + endpoints: DuniterEndpoints, ) -> GossipsHandler<B, N> { let local_peer_id = network.local_peer_id(); diff --git a/node/src/endpoint_gossip/mod.rs b/node/src/endpoint_gossip/mod.rs index 593ecc0e..3e6513e5 100644 --- a/node/src/endpoint_gossip/mod.rs +++ b/node/src/endpoint_gossip/mod.rs @@ -6,6 +6,7 @@ mod types; use crate::endpoint_gossip::duniter_peering_protocol_name::NAME; use codec::{Decode, Encode}; +use frame_benchmarking::__private::traits::ConstU32; use sc_network::{ config::{PeerStoreProvider, SetConfig}, types::ProtocolName, @@ -13,6 +14,7 @@ use sc_network::{ }; use serde::{Deserialize, Serialize}; use sp_api::__private::BlockT; +use sp_core::bounded_vec::BoundedVec; use std::{sync::Arc, time}; pub mod well_known_endpoint_types { @@ -20,14 +22,6 @@ pub mod well_known_endpoint_types { pub const SQUID: &str = "squid"; } -#[derive(Debug, Encode, Decode)] -enum EndpointMsg { - /// RPC endpoint of Duniter - RpcEndpoint(String), - /// GraphQL endpoint of Squid - SquidEndpoint(String), -} - pub struct DuniterPeeringParams { /// Handle that is used to communicate with `sc_network::Notifications`. pub notification_service: Box<dyn NotificationService>, @@ -104,7 +98,9 @@ pub struct DuniterEndpoint { pub address: String, } +pub type DuniterEndpoints = BoundedVec<DuniterEndpoint, ConstU32<10>>; + #[derive(Encode, Decode, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct Peering { - endpoints: Vec<DuniterEndpoint>, + endpoints: DuniterEndpoints, } diff --git a/node/src/endpoint_gossip/rpc/state.rs b/node/src/endpoint_gossip/rpc/state.rs index f86bf03e..75d8a252 100644 --- a/node/src/endpoint_gossip/rpc/state.rs +++ b/node/src/endpoint_gossip/rpc/state.rs @@ -1,5 +1,5 @@ use crate::endpoint_gossip::{ - handler::DuniterPeeringEvent, rpc::data::DuniterPeeringsData, DuniterEndpoint, + handler::DuniterPeeringEvent, rpc::data::DuniterPeeringsData, DuniterEndpoints, }; use codec::{Decode, Encode}; use futures::StreamExt; @@ -13,7 +13,7 @@ use std::sync::Arc; #[derive(Encode, Decode, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct PeeringWithId { pub peer_id: String, - pub endpoints: Vec<DuniterEndpoint>, + pub endpoints: DuniterEndpoints, } #[derive(Clone)] diff --git a/node/src/endpoint_gossip/rpc/tests.rs b/node/src/endpoint_gossip/rpc/tests.rs index 38c86f7a..258c5882 100644 --- a/node/src/endpoint_gossip/rpc/tests.rs +++ b/node/src/endpoint_gossip/rpc/tests.rs @@ -4,7 +4,7 @@ use crate::endpoint_gossip::{ state::{DuniterPeeringsState, PeeringWithId}, }, well_known_endpoint_types::{RPC, SQUID}, - DuniterEndpoint, + DuniterEndpoint, DuniterEndpoints, }; use jsonrpsee::RpcModule; @@ -23,7 +23,7 @@ async fn expose_known_peers() { let rpc = setup_new_rpc_with_initial_peerings(vec![ PeeringWithId { peer_id: "12D3KooWRkDXunbB64VegYPCQaitcgtdtEtbsbd7f19nsS7aMjDp".into(), - endpoints: vec![ + endpoints: DuniterEndpoints::truncate_from(vec![ DuniterEndpoint { protocol: RPC.into(), address: "/rpc/wss://gdev.example.com".into(), @@ -32,14 +32,14 @@ async fn expose_known_peers() { protocol: SQUID.into(), address: "/squid/https://squid.gdev.gyroi.de/v1/graphql".into(), }, - ], + ]), }, PeeringWithId { peer_id: "12D3KooWFiUBo3Kjiryvrpz8b3kfNVk7baezhab7SHdfafgY7nmN".into(), - endpoints: vec![DuniterEndpoint { + endpoints: DuniterEndpoints::truncate_from(vec![DuniterEndpoint { protocol: RPC.into(), address: "/rpc/ws://gdev.example.com:9944".into(), - }], + }]), }, ]); let expected_response = r#"{"jsonrpc":"2.0","id":0,"result":{"peerings":[{"peer_id":"12D3KooWRkDXunbB64VegYPCQaitcgtdtEtbsbd7f19nsS7aMjDp","endpoints":[{"protocol":"rpc","address":"/rpc/wss://gdev.example.com"},{"protocol":"squid","address":"/squid/https://squid.gdev.gyroi.de/v1/graphql"}]},{"peer_id":"12D3KooWFiUBo3Kjiryvrpz8b3kfNVk7baezhab7SHdfafgY7nmN","endpoints":[{"protocol":"rpc","address":"/rpc/ws://gdev.example.com:9944"}]}]}}"#.to_string(); diff --git a/node/src/endpoint_gossip/tests.rs b/node/src/endpoint_gossip/tests.rs index 5e437eb7..454c0bc6 100644 --- a/node/src/endpoint_gossip/tests.rs +++ b/node/src/endpoint_gossip/tests.rs @@ -4,7 +4,7 @@ use crate::{ duniter_peering_protocol_name, handler::{DuniterPeeringCommand, DuniterPeeringEvent}, well_known_endpoint_types::RPC, - DuniterEndpoint, Peering, + DuniterEndpoint, DuniterEndpoints, Peering, }, }; use async_channel::Receiver; @@ -76,10 +76,10 @@ fn ensure_only_one_peering_is_accepted( .unbounded_send(DuniterPeeringCommand::SendPeering( peer_id_1, Peering { - endpoints: vec![DuniterEndpoint { + endpoints: DuniterEndpoints::truncate_from(vec![DuniterEndpoint { protocol: RPC.into(), address: "gdev.example.com:9944".into(), - }], + }]), }, )) .unwrap(); @@ -188,7 +188,7 @@ fn start_network(net: &mut DuniterPeeringTestNet, peers: usize) -> impl Future<O net_service, rpc_sink, Some(command_rx), - vec![], + DuniterEndpoints::new(), ); // To send external commands to the handler (for tests or RPC commands). net.peer_streams.push(stream); diff --git a/node/src/service.rs b/node/src/service.rs index 7290d8bd..79867b0d 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -743,7 +743,13 @@ where }); } - task_manager.spawn_essential_handle().spawn_blocking( + let duniter_endpoints = match duniter_endpoints.try_into() { + Ok(endpoints) => endpoints, + Err(_) => { + return Err("Too much Duniter endpoints".into()); + } + }; + task_manager.spawn_handle().spawn_blocking( "duniter-endpoint-gossip-handler", Some("networking"), crate::endpoint_gossip::handler::build::<Block, _>( -- GitLab