Skip to content

Commit

Permalink
re: Remove usage of serde from near-network-primitives (#5546)
Browse files Browse the repository at this point in the history
We are planning to separate `jsonrpc` types from rest of the code base. We can start by separating types from `near-network-primitives` from `jsonrpc` by removing direct usage of `serde` from that crate.

See #5516

TODO: (next PR)
- Fix database memory leak.
  • Loading branch information
pmnoxx authored Dec 10, 2021
1 parent 7f10dbd commit 23615a0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 45 additions & 4 deletions chain/jsonrpc-primitives/src/types/network_info.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
use near_network_primitives::types::{KnownProducer, PeerInfo};
use near_primitives::network::PeerId;
use near_primitives::types::AccountId;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[derive(Serialize, Deserialize, Debug)]
pub struct RpcPeerInfo {
pub id: PeerId,
pub addr: Option<SocketAddr>,
pub account_id: Option<AccountId>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RpcKnownProducer {
pub account_id: AccountId,
pub addr: Option<SocketAddr>,
pub peer_id: PeerId,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct RpcNetworkInfoResponse {
pub active_peers: Vec<PeerInfo>,
pub active_peers: Vec<RpcPeerInfo>,
pub num_active_peers: usize,
pub peer_max_count: u32,
pub sent_bytes_per_sec: u64,
pub received_bytes_per_sec: u64,
/// Accounts of known block and chunk producers from routing table.
pub known_producers: Vec<KnownProducer>,
pub known_producers: Vec<RpcKnownProducer>,
}

#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
Expand All @@ -19,15 +36,39 @@ pub enum RpcNetworkInfoError {
InternalError { error_message: String },
}

impl From<PeerInfo> for RpcPeerInfo {
fn from(peer_info: PeerInfo) -> Self {
Self { id: peer_info.id, addr: peer_info.addr, account_id: peer_info.account_id }
}
}

impl From<KnownProducer> for RpcKnownProducer {
fn from(known_producer: KnownProducer) -> Self {
Self {
account_id: known_producer.account_id,
addr: known_producer.addr,
peer_id: known_producer.peer_id,
}
}
}

impl From<near_client_primitives::types::NetworkInfoResponse> for RpcNetworkInfoResponse {
fn from(network_info_response: near_client_primitives::types::NetworkInfoResponse) -> Self {
Self {
active_peers: network_info_response.connected_peers,
active_peers: network_info_response
.connected_peers
.iter()
.map(|pi| pi.clone().into())
.collect(),
num_active_peers: network_info_response.num_connected_peers,
peer_max_count: network_info_response.peer_max_count,
sent_bytes_per_sec: network_info_response.sent_bytes_per_sec,
received_bytes_per_sec: network_info_response.received_bytes_per_sec,
known_producers: network_info_response.known_producers,
known_producers: network_info_response
.known_producers
.iter()
.map(|kp| kp.clone().into())
.collect(),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion chain/network-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ actix = "=0.11.0-beta.2"
tokio = { version = "1.1", features = ["full"] }
chrono = { version = "0.4.4", features = ["serde"] }
borsh = "0.9"
serde = { version = "1", features = ["derive", "rc", "alloc"] }
strum = { version = "0.20", features = ["derive"] }
tracing = "0.1.13"
deepsize = { version = "0.2.0", optional = true }
Expand Down
11 changes: 5 additions & 6 deletions chain/network-primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use near_primitives::transaction::{ExecutionOutcomeWithIdAndProof, SignedTransac
use near_primitives::types::{AccountId, BlockHeight, BlockReference, EpochId, ShardId};
use near_primitives::utils::{from_timestamp, to_timestamp};
use near_primitives::views::{FinalExecutionOutcomeView, QueryRequest, QueryResponse};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::fmt::{Debug, Error, Formatter};
Expand All @@ -44,7 +43,7 @@ pub const ROUTED_MESSAGE_TTL: u8 = 100;
pub const UPDATE_INTERVAL_LAST_TIME_RECEIVED_MESSAGE: Duration = Duration::from_secs(60);

/// Peer information.
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
pub struct PeerInfo {
pub id: PeerId,
pub addr: Option<SocketAddr>,
Expand Down Expand Up @@ -167,7 +166,7 @@ impl From<PeerChainInfo> for PeerChainInfoV2 {

/// Peer type.
#[cfg_attr(feature = "deepsize_feature", derive(DeepSizeOf))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PeerType {
/// Inbound session
Inbound,
Expand Down Expand Up @@ -650,7 +649,7 @@ impl FromStr for PatternAddr {
}

/// Status of the known peers.
#[derive(BorshSerialize, BorshDeserialize, Serialize, Eq, PartialEq, Debug, Clone)]
#[derive(BorshSerialize, BorshDeserialize, Eq, PartialEq, Debug, Clone)]
pub enum KnownPeerStatus {
Unknown,
NotConnected,
Expand Down Expand Up @@ -776,7 +775,7 @@ where

/// Ban reason.
#[cfg_attr(feature = "deepsize_feature", derive(DeepSizeOf))]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Debug, Clone, PartialEq, Eq, Copy)]
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, PartialEq, Eq, Copy)]
pub enum ReasonForBan {
None = 0,
BadBlock = 1,
Expand Down Expand Up @@ -817,7 +816,7 @@ pub enum PeerManagerRequest {
#[rtype(result = "()")]
pub enum PeerRequest {}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Debug, Clone)]
pub struct KnownProducer {
pub account_id: AccountId,
pub addr: Option<SocketAddr>,
Expand Down
1 change: 0 additions & 1 deletion chain/network/src/routing/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub const DELETE_PEERS_AFTER_TIME: Duration = Duration::from_secs(3_600);
pub const MAX_NUM_PEERS: usize = 128;

#[derive(Debug)]
#[cfg_attr(feature = "test_features", derive(serde::Serialize))]
pub struct PeerRequestResult {
pub peers: Vec<PeerInfo>,
}
Expand Down

0 comments on commit 23615a0

Please sign in to comment.