Skip to content

Commit

Permalink
refactor(chain): expunge rocksdb from near-jsonrpc-primitives dep…
Browse files Browse the repository at this point in the history
…endency tree (#4651)

Under the initiative to build `near-api-rs`, we need to ensure its dependencies from within `nearcore` are as lightweight as possible. On initial inspection, `near-jsonrpc-primitives`, which should be lightweight enough to be used across the ecosystem, actually isn't, as it's dependency tree currently includes `rocksdb`. To remedy this, we extract useful structures from `near-chunks` and `near-network` into their own `-primitives` counterpart crates and depend on those instead.

### New Crates

- `near-chunks-primitives` from `near-chunks`
- `near-network-primitives` from `near-network`

### Analysis

Dump of `cargo tree -p near-jsonrpc-primitives | wc -l`:

```console
   624 master (before this refactor)
   610 after introducing `near-chunks-primitives` (`rocksdb` still present)
   494 after introducing `near-network-primitives` (bye, bye, rocksdb)
   459 after refining `near-network{,-primitives}`
   457 after moving metrics back to `near-network` (thanks, @bowenwang1996)
   417 after removing `metric_recorder` and moving it's dependent back to `near-network` 
```

#### Test Plan

- [X] Ensure unit tests pass

<sub> <i> An unfortunate consequence of this kind of code movearound is that git looses track of blame, but for reference, modules in either of the new crates that git thinks of as a "new file", was extracted from their original counterparts. </i> </sub>
  • Loading branch information
miraclx authored Aug 20, 2021
1 parent e5847dd commit e242f71
Show file tree
Hide file tree
Showing 27 changed files with 1,213 additions and 1,466 deletions.
30 changes: 26 additions & 4 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions chain/chunks-primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "near-chunks-primitives"
version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2018"

[dependencies]
near-chain-primitives = { path = "../chain-primitives" }
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

#[derive(Debug)]
pub enum Error {
InvalidPartMessage,
Expand All @@ -10,12 +12,12 @@ pub enum Error {
DuplicateChunkHeight,
UnknownChunk,
KnownPart,
ChainError(near_chain::Error),
ChainError(near_chain_primitives::Error),
IOError(std::io::Error),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{:?}", self)
}
}
Expand All @@ -26,8 +28,8 @@ impl From<std::io::Error> for Error {
}
}

impl From<near_chain::Error> for Error {
fn from(err: near_chain::Error) -> Self {
impl From<near_chain_primitives::Error> for Error {
fn from(err: near_chain_primitives::Error) -> Self {
Error::ChainError(err)
}
}
3 changes: 3 additions & 0 deletions chain/chunks-primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod error;

pub use error::Error;
1 change: 1 addition & 0 deletions chain/chunks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ reed-solomon-erasure = "4"

near-crypto = { path = "../../core/crypto" }
near-primitives = { path = "../../core/primitives" }
near-chunks-primitives = { path = "../chunks-primitives" }
near-store = { path = "../../core/store" }
near-network = { path = "../network" }
near-chain = { path = "../chain" }
Expand Down
3 changes: 1 addition & 2 deletions chain/chunks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ use near_primitives::version::ProtocolVersion;
use near_primitives::{checked_feature, unwrap_or_return};

use crate::chunk_cache::{EncodedChunksCache, EncodedChunksCacheEntry};
pub use crate::types::Error;
pub use near_chunks_primitives::Error;
use rand::Rng;

mod chunk_cache;
pub mod test_utils;
mod types;

const CHUNK_PRODUCER_BLACKLIST_SIZE: usize = 100;
pub const CHUNK_REQUEST_RETRY_MS: u64 = 100;
Expand Down
7 changes: 2 additions & 5 deletions chain/client-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ thiserror = "1.0"
near-chain-primitives = { path = "../chain-primitives" }
near-chain-configs = { path = "../../core/chain-configs" }

near-chunks = { path = "../chunks" }
near-chunks-primitives = { path = "../chunks-primitives" }
near-crypto = { path = "../../core/crypto" }
near-network = { path = "../network" }
near-network-primitives = { path = "../network-primitives" }
near-primitives = { path = "../../core/primitives" }

[features]
metric_recorder = []
13 changes: 4 additions & 9 deletions chain/client-primitives/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "metric_recorder")]
use near_network::recorder::MetricRecorder;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand All @@ -9,8 +7,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use near_chain_configs::ProtocolConfigView;
use near_network::types::{AccountOrPeerIdOrHash, KnownProducer};
use near_network::PeerInfo;
use near_network_primitives::types::{AccountOrPeerIdOrHash, KnownProducer, PeerInfo};
use near_primitives::errors::InvalidTxError;
use near_primitives::hash::CryptoHash;
use near_primitives::merkle::{MerklePath, PartialMerkleTree};
Expand All @@ -33,7 +30,7 @@ pub use near_primitives::views::{StatusResponse, StatusSyncInfo};
#[derive(Debug)]
pub enum Error {
Chain(near_chain_primitives::Error),
Chunk(near_chunks::Error),
Chunk(near_chunks_primitives::Error),
BlockProducer(String),
ChunkProducer(String),
Other(String),
Expand Down Expand Up @@ -66,8 +63,8 @@ impl From<near_chain_primitives::ErrorKind> for Error {
}
}

impl From<near_chunks::Error> for Error {
fn from(err: near_chunks::Error) -> Self {
impl From<near_chunks_primitives::Error> for Error {
fn from(err: near_chunks_primitives::Error) -> Self {
Error::Chunk(err)
}
}
Expand Down Expand Up @@ -469,8 +466,6 @@ pub struct NetworkInfoResponse {
pub received_bytes_per_sec: u64,
/// Accounts of known block and chunk producers from routing table.
pub known_producers: Vec<KnownProducer>,
#[cfg(feature = "metric_recorder")]
pub metric_recorder: MetricRecorder,
}

/// Status of given transaction including all the subsequent receipts.
Expand Down
1 change: 0 additions & 1 deletion chain/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ near-test-contracts = { path = "../../runtime/near-test-contracts" }
byzantine_asserts = ["near-chain/byzantine_asserts"]
expensive_tests = []
adversarial = ["near-network/adversarial", "near-chain/adversarial"]
metric_recorder = ["near-client-primitives/metric_recorder"]
delay_detector = ["near-chain/delay_detector", "near-network/delay_detector", "delay-detector"]
protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "near-store/protocol_feature_block_header_v3"]
nightly_protocol = []
Expand Down
6 changes: 0 additions & 6 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ use near_chain_configs::ClientConfig;
#[cfg(feature = "adversarial")]
use near_chain_configs::GenesisConfig;
use near_crypto::Signature;
#[cfg(feature = "metric_recorder")]
use near_network::recorder::MetricRecorder;
#[cfg(feature = "adversarial")]
use near_network::types::NetworkAdversarialMessage;
use near_network::types::{NetworkInfo, ReasonForBan};
Expand Down Expand Up @@ -154,8 +152,6 @@ impl ClientActor {
received_bytes_per_sec: 0,
sent_bytes_per_sec: 0,
known_producers: vec![],
#[cfg(feature = "metric_recorder")]
metric_recorder: MetricRecorder::default(),
peer_counter: 0,
},
last_validator_announce_time: None,
Expand Down Expand Up @@ -652,8 +648,6 @@ impl Handler<GetNetworkInfo> for ClientActor {
sent_bytes_per_sec: self.network_info.sent_bytes_per_sec,
received_bytes_per_sec: self.network_info.received_bytes_per_sec,
known_producers: self.network_info.known_producers.clone(),
#[cfg(feature = "metric_recorder")]
metric_recorder: self.network_info.metric_recorder.clone(),
})
}
}
Expand Down
4 changes: 0 additions & 4 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use near_chain::{
};
use near_chain_configs::ClientConfig;
use near_crypto::{InMemorySigner, KeyType, PublicKey};
#[cfg(feature = "metric_recorder")]
use near_network::recorder::MetricRecorder;
use near_network::routing::EdgeInfo;
use near_network::types::{
AccountOrPeerIdOrHash, NetworkInfo, NetworkViewClientMessages, NetworkViewClientResponses,
Expand Down Expand Up @@ -487,8 +485,6 @@ pub fn setup_mock_all_validators(
sent_bytes_per_sec: 0,
received_bytes_per_sec: 0,
known_producers: vec![],
#[cfg(feature = "metric_recorder")]
metric_recorder: MetricRecorder::default(),
peer_counter: 0,
};
client_addr.do_send(NetworkClientMessages::NetworkInfo(info));
Expand Down
21 changes: 21 additions & 0 deletions chain/network-primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "near-network-primitives"
version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
edition = "2018"

[dependencies]
actix = "=0.11.0-beta.2"
tokio = { version = "1.1", features = ["full"] }
chrono = { version = "0.4.4", features = ["serde"] }
borsh = "0.8.1"
serde = { version = "1", features = [ "derive" ] }
strum = { version = "0.20", features = ["derive"] }
tracing = "0.1.13"

near-crypto = { path = "../../core/crypto" }
near-primitives = { path = "../../core/primitives" }

[features]
adversarial = []
sandbox = []
1 change: 1 addition & 0 deletions chain/network-primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
Loading

0 comments on commit e242f71

Please sign in to comment.