Skip to content

Commit 7df2dae

Browse files
committed
PR feedback + import ordering
1 parent 0766ae6 commit 7df2dae

File tree

11 files changed

+55
-40
lines changed

11 files changed

+55
-40
lines changed

newsfragments/331.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Initial HeaderOracle implementation.

src/main.rs

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 0 deletions
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,
@@ -104,6 +105,8 @@ impl<
104105
TMetric: Metric + Send,
105106
TValidator: 'static + Validator<TContentKey> + Send,
106107
> OverlayProtocol<TContentKey, TMetric, TValidator>
108+
where
109+
<TContentKey as TryFrom<Vec<u8>>>::Error: Debug,
107110
{
108111
pub async fn new(
109112
config: OverlayConfig,

trin-core/src/portalnet/overlay_service.rs

Lines changed: 20 additions & 18 deletions
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,
@@ -280,6 +281,8 @@ impl<
280281
TMetric: Metric + Send,
281282
TValidator: 'static + Validator<TContentKey> + Send,
282283
> OverlayService<TContentKey, TMetric, TValidator>
284+
where
285+
<TContentKey as TryFrom<Vec<u8>>>::Error: Debug,
283286
{
284287
/// Spawns the overlay network service.
285288
///
@@ -1003,32 +1006,31 @@ impl<
10031006
async fn process_received_content(&mut self, content: ByteList, request: FindContent) {
10041007
let content_key = match TContentKey::try_from(request.content_key) {
10051008
Ok(val) => val,
1006-
Err(_) => {
1007-
error!("Unable to process received content: Invalid content key.");
1009+
Err(msg) => {
1010+
error!("Unable to process received content: Invalid content key: {msg:?}");
10081011
return;
10091012
}
10101013
};
1011-
match self
1014+
if let Err(err) = self
10121015
.validator
10131016
.validate_content(&content_key, &content)
10141017
.await
10151018
{
1016-
Ok(_) => (),
1017-
Err(msg) => {
1018-
error!("Unable to validate received content: {msg:?}");
1019-
return;
1020-
}
1021-
}
1019+
error!("Unable to validate received content: {err:?}");
1020+
return;
1021+
};
10221022
match self.storage.read().should_store(&content_key) {
1023-
Ok(should_store) => match should_store {
1024-
true => match self.storage.write().store(&content_key, &content.into()) {
1025-
Ok(_) => (),
1026-
Err(msg) => error!("Content received, but not stored: {msg}"),
1027-
},
1028-
false => debug!(
1029-
"Content received, but not stored: Content is already stored or its distance falls outside current radius."
1030-
),
1031-
},
1023+
Ok(should_store) => {
1024+
if should_store {
1025+
if let Err(err) = self.storage.write().store(&content_key, &content.into()) {
1026+
error!("Content received, but not stored: {err}")
1027+
}
1028+
} else {
1029+
debug!(
1030+
"Content received, but not stored: Content is already stored or its distance falls outside current radius."
1031+
)
1032+
}
1033+
}
10321034
Err(_) => {
10331035
error!("Content received, but not stored: Error communicating with db.");
10341036
}

trin-core/src/types/validation.rs

Lines changed: 10 additions & 6 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 4 additions & 3 deletions
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)