Skip to content

Commit 391e5f5

Browse files
committed
PR feedback + import ordering
1 parent 42f1916 commit 391e5f5

File tree

10 files changed

+60
-44
lines changed

10 files changed

+60
-44
lines changed

src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use trin_core::cli::TrinConfig;
2-
use trin_core::utils::infura::build_infura_project_url_from_env;
1+
use trin_core::{cli::TrinConfig, utils::infura::build_infura_project_url_from_env};
32

43
use trin::run_trin;
54

trin-core/src/portalnet/overlay.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::anyhow;
22
use std::{
33
collections::HashSet,
4+
fmt::Debug,
45
marker::{PhantomData, Sync},
56
sync::Arc,
67
time::Duration,
@@ -20,10 +21,12 @@ use crate::portalnet::{
2021
},
2122
};
2223

23-
use crate::types::validation::Validator;
24-
use crate::utp::{
25-
stream::{UtpListenerRequest, UtpSocket, BUF_SIZE},
26-
trin_helpers::{UtpAccept, UtpMessage},
24+
use crate::{
25+
types::validation::Validator,
26+
utp::{
27+
stream::{UtpListenerRequest, UtpSocket, BUF_SIZE},
28+
trin_helpers::{UtpAccept, UtpMessage},
29+
},
2730
};
2831
use discv5::{
2932
enr::NodeId,
@@ -101,6 +104,8 @@ impl<
101104
TMetric: Metric + Send,
102105
TValidator: 'static + Validator<TContentKey> + Send,
103106
> OverlayProtocol<TContentKey, TMetric, TValidator>
107+
where
108+
<TContentKey as TryFrom<Vec<u8>>>::Error: Debug,
104109
{
105110
pub async fn new(
106111
config: OverlayConfig,

trin-core/src/portalnet/overlay_service.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::HashMap,
33
fmt,
4+
fmt::Debug,
45
marker::{PhantomData, Sync},
56
sync::Arc,
67
task::Poll,
@@ -277,6 +278,8 @@ impl<
277278
TMetric: Metric + Send,
278279
TValidator: 'static + Validator<TContentKey> + Send,
279280
> OverlayService<TContentKey, TMetric, TValidator>
281+
where
282+
<TContentKey as TryFrom<Vec<u8>>>::Error: Debug,
280283
{
281284
/// Spawns the overlay network service.
282285
///
@@ -971,32 +974,31 @@ impl<
971974
async fn process_received_content(&mut self, content: ByteList, request: FindContent) {
972975
let content_key = match TContentKey::try_from(request.content_key) {
973976
Ok(val) => val,
974-
Err(_) => {
975-
error!("Unable to process received content: Invalid content key.");
977+
Err(msg) => {
978+
error!("Unable to process received content: Invalid content key: {msg:?}");
976979
return;
977980
}
978981
};
979-
match self
982+
if let Err(err) = self
980983
.validator
981984
.validate_content(&content_key, &content)
982985
.await
983986
{
984-
Ok(_) => (),
985-
Err(msg) => {
986-
error!("Unable to validate received content: {msg:?}");
987-
return;
988-
}
989-
}
987+
error!("Unable to validate received content: {err:?}");
988+
return;
989+
};
990990
match self.storage.read().should_store(&content_key) {
991-
Ok(should_store) => match should_store {
992-
true => match self.storage.write().store(&content_key, &content.into()) {
993-
Ok(_) => (),
994-
Err(msg) => error!("Content received, but not stored: {msg}"),
995-
},
996-
false => debug!(
997-
"Content received, but not stored: Content is already stored or its distance falls outside current radius."
998-
),
999-
},
991+
Ok(should_store) => {
992+
if should_store {
993+
if let Err(err) = self.storage.write().store(&content_key, &content.into()) {
994+
error!("Content received, but not stored: {err}")
995+
}
996+
} else {
997+
debug!(
998+
"Content received, but not stored: Content is already stored or its distance falls outside current radius."
999+
)
1000+
}
1001+
}
10001002
Err(_) => {
10011003
error!("Content received, but not stored: Error communicating with db.");
10021004
}

trin-core/src/types/validation.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ use anyhow::anyhow;
22
use async_trait::async_trait;
33
use serde_json::{json, Value};
44

5-
use crate::jsonrpc::service::dispatch_infura_request;
6-
use crate::jsonrpc::types::{HistoryJsonRpcRequest, JsonRequest, Params};
7-
use crate::portalnet::types::content_key::IdentityContentKey;
8-
use crate::portalnet::types::messages::ByteList;
5+
use crate::{
6+
jsonrpc::{
7+
service::dispatch_infura_request,
8+
types::{HistoryJsonRpcRequest, JsonRequest, Params},
9+
},
10+
portalnet::types::{content_key::IdentityContentKey, messages::ByteList},
11+
utils::infura::INFURA_BASE_URL,
12+
};
913

1014
/// Responsible for dispatching cross-overlay-network requests
1115
/// for data to perform validation. Currently, it just proxies these requests
1216
/// on to infura.
13-
#[derive(Clone)]
17+
#[derive(Debug, Clone)]
1418
pub struct HeaderOracle {
1519
pub infura_url: String,
1620
// We could simply store the main portal jsonrpc tx channel here, rather than each
@@ -25,7 +29,7 @@ pub struct HeaderOracle {
2529
impl Default for HeaderOracle {
2630
fn default() -> Self {
2731
Self {
28-
infura_url: "https://mainnet.infura.io:443/v3/".to_string(),
32+
infura_url: INFURA_BASE_URL.to_string(),
2933
history_jsonrpc_tx: None,
3034
header_gossip_jsonrpc_tx: None,
3135
block_indices_jsonrpc_tx: None,

trin-core/src/utils/infura.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::env;
22

3+
pub const INFURA_BASE_URL: &str = "https://mainnet.infura.io:443/v3/";
4+
35
pub fn build_infura_project_url_from_env() -> String {
46
let infura_project_id = match env::var("TRIN_INFURA_PROJECT_ID") {
57
Ok(val) => val,
@@ -9,5 +11,5 @@ pub fn build_infura_project_url_from_env() -> String {
911
),
1012
};
1113

12-
format!("https://mainnet.infura.io:443/v3/{}", infura_project_id)
14+
format!("{}{}", INFURA_BASE_URL, infura_project_id)
1315
}

trin-history/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use std::sync::{Arc, RwLock};
88
use discv5::TalkRequest;
99
use log::info;
1010
use network::HistoryNetwork;
11-
use tokio::sync::mpsc::UnboundedSender;
12-
use tokio::{sync::mpsc, task::JoinHandle};
11+
use tokio::{
12+
sync::{mpsc, mpsc::UnboundedSender},
13+
task::JoinHandle,
14+
};
1315

1416
use crate::{events::HistoryEvents, jsonrpc::HistoryRequestHandler};
1517
use trin_core::{

trin-history/src/network.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::anyhow;
22
use log::debug;
3-
use std::sync::Arc;
4-
use std::sync::RwLock as StdRwLock;
3+
use std::sync::{Arc, RwLock as StdRwLock};
54

65
use parking_lot::RwLock;
76
use tokio::sync::mpsc::UnboundedSender;

trin-history/src/validation.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ use anyhow::anyhow;
44
use async_trait::async_trait;
55
use rlp::Rlp;
66

7-
use trin_core::portalnet::types::content_key::HistoryContentKey;
8-
use trin_core::portalnet::types::messages::ByteList;
9-
use trin_core::types::header::Header;
10-
use trin_core::types::validation::{HeaderOracle, Validator};
7+
use trin_core::{
8+
portalnet::types::{content_key::HistoryContentKey, messages::ByteList},
9+
types::{
10+
header::Header,
11+
validation::{HeaderOracle, Validator},
12+
},
13+
};
1114

1215
pub struct ChainHistoryValidator {
1316
pub header_oracle: Arc<RwLock<HeaderOracle>>,

trin-state/src/network.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use trin_core::{
2121
utp::stream::UtpListenerRequest,
2222
};
2323

24-
use crate::trie::TrieDB;
25-
use crate::validation::StateValidator;
24+
use crate::{trie::TrieDB, validation::StateValidator};
2625

2726
/// State network layer on top of the overlay protocol. Encapsulates state network specific data and logic.
2827
#[derive(Clone)]

trin-state/src/validation.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use async_trait::async_trait;
22

3-
use trin_core::portalnet::types::content_key::StateContentKey;
4-
use trin_core::portalnet::types::messages::ByteList;
5-
use trin_core::types::validation::{HeaderOracle, Validator};
3+
use trin_core::{
4+
portalnet::types::{content_key::StateContentKey, messages::ByteList},
5+
types::validation::{HeaderOracle, Validator},
6+
};
67

78
pub struct StateValidator {
89
pub header_oracle: HeaderOracle,

0 commit comments

Comments
 (0)