Skip to content

Commit 0766ae6

Browse files
committed
Move findcontent message processing up pipeline
1 parent 17dd835 commit 0766ae6

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

trin-core/src/portalnet/overlay_service.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,17 @@ impl<
925925
match response {
926926
Response::Pong(pong) => self.process_pong(pong, source),
927927
Response::Nodes(nodes) => self.process_nodes(nodes, source),
928-
Response::Content(content) => self.process_content(content, source, request).await,
928+
Response::Content(content) => {
929+
let find_content_request = match request {
930+
Request::FindContent(find_content) => find_content,
931+
_ => {
932+
error!("Unable to process received content: Invalid request message.");
933+
return;
934+
}
935+
};
936+
self.process_content(content, source, find_content_request)
937+
.await
938+
}
929939
_ => {}
930940
}
931941
}
@@ -973,7 +983,7 @@ impl<
973983
}
974984

975985
/// Processes a Content response.
976-
async fn process_content(&mut self, content: Content, source: Enr, request: Request) {
986+
async fn process_content(&mut self, content: Content, source: Enr, request: FindContent) {
977987
debug!(
978988
"[{:?}] Processing Content response from node. Node: {}",
979989
self.protocol,
@@ -990,15 +1000,8 @@ impl<
9901000
}
9911001
}
9921002

993-
async fn process_received_content(&mut self, content: ByteList, request: Request) {
994-
let find_content = match request {
995-
Request::FindContent(find_content) => find_content,
996-
_ => {
997-
error!("Unable to process received content: Invalid request message.");
998-
return;
999-
}
1000-
};
1001-
let content_key = match TContentKey::try_from(find_content.content_key) {
1003+
async fn process_received_content(&mut self, content: ByteList, request: FindContent) {
1004+
let content_key = match TContentKey::try_from(request.content_key) {
10021005
Ok(val) => val,
10031006
Err(_) => {
10041007
error!("Unable to process received content: Invalid content key.");
@@ -1011,16 +1014,16 @@ impl<
10111014
.await
10121015
{
10131016
Ok(_) => (),
1014-
Err(_) => {
1015-
error!("Unable to validate received content.");
1017+
Err(msg) => {
1018+
error!("Unable to validate received content: {msg:?}");
10161019
return;
10171020
}
10181021
}
10191022
match self.storage.read().should_store(&content_key) {
10201023
Ok(should_store) => match should_store {
10211024
true => match self.storage.write().store(&content_key, &content.into()) {
10221025
Ok(_) => (),
1023-
Err(msg) => error!("{}", format!("Content received, but not stored: {msg}")),
1026+
Err(msg) => error!("Content received, but not stored: {msg}"),
10241027
},
10251028
false => debug!(
10261029
"Content received, but not stored: Content is already stored or its distance falls outside current radius."
@@ -1329,7 +1332,7 @@ mod tests {
13291332
content_key::IdentityContentKey, messages::PortalnetConfig, metric::XorMetric,
13301333
},
13311334
},
1332-
types::validation::IdentityValidator,
1335+
types::validation::MockValidator,
13331336
utils::node_id::generate_random_remote_enr,
13341337
};
13351338

@@ -1344,7 +1347,7 @@ mod tests {
13441347
};
13451348
}
13461349

1347-
fn build_service() -> OverlayService<IdentityContentKey, XorMetric, IdentityValidator> {
1350+
fn build_service() -> OverlayService<IdentityContentKey, XorMetric, MockValidator> {
13481351
let portal_config = PortalnetConfig {
13491352
no_stun: true,
13501353
..Default::default()
@@ -1375,7 +1378,7 @@ mod tests {
13751378
let (request_tx, request_rx) = mpsc::unbounded_channel();
13761379
let (response_tx, response_rx) = mpsc::unbounded_channel();
13771380
let metrics = None;
1378-
let validator = IdentityValidator {};
1381+
let validator = MockValidator {};
13791382

13801383
OverlayService {
13811384
discovery,

trin-core/src/types/validation.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use crate::jsonrpc::types::{HistoryJsonRpcRequest, JsonRequest, Params};
77
use crate::portalnet::types::content_key::IdentityContentKey;
88
use crate::portalnet::types::messages::ByteList;
99

10-
// This struct is responsible for dispatching cross-overlay-network requests
11-
// for data to perform validation. Currently, it just proxies these requests
12-
// on to infura.
10+
/// Responsible for dispatching cross-overlay-network requests
11+
/// for data to perform validation. Currently, it just proxies these requests
12+
/// on to infura.
1313
#[derive(Clone)]
1414
pub struct HeaderOracle {
1515
pub infura_url: String,
@@ -69,7 +69,7 @@ impl HeaderOracle {
6969
}
7070
}
7171

72-
// This trait is used by all overlay-network Validators to validate content in the overlay service.
72+
/// Used by all overlay-network Validators to validate content in the overlay service.
7373
#[async_trait]
7474
pub trait Validator<TContentKey> {
7575
async fn validate_content(
@@ -81,11 +81,11 @@ pub trait Validator<TContentKey> {
8181
TContentKey: 'async_trait;
8282
}
8383

84-
// This is a mock Validator for use in tests where no validation is required.
85-
pub struct IdentityValidator {}
84+
/// For use in tests where no validation needs to be performed.
85+
pub struct MockValidator {}
8686

8787
#[async_trait]
88-
impl Validator<IdentityContentKey> for IdentityValidator {
88+
impl Validator<IdentityContentKey> for MockValidator {
8989
async fn validate_content(
9090
&mut self,
9191
_content_key: &IdentityContentKey,

trin-core/tests/overlay.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use trin_core::{
1414
Enr,
1515
},
1616
socket,
17-
types::validation::IdentityValidator,
17+
types::validation::MockValidator,
1818
};
1919

2020
use discv5::Discv5Event;
@@ -30,7 +30,7 @@ use trin_core::utp::stream::UtpListenerRequest;
3030
async fn init_overlay(
3131
discovery: Arc<Discovery>,
3232
protocol: ProtocolId,
33-
) -> OverlayProtocol<IdentityContentKey, XorMetric, IdentityValidator> {
33+
) -> OverlayProtocol<IdentityContentKey, XorMetric, MockValidator> {
3434
let storage_config = PortalStorage::setup_config(
3535
discovery.local_enr().node_id(),
3636
DEFAULT_STORAGE_CAPACITY.parse().unwrap(),
@@ -40,7 +40,7 @@ async fn init_overlay(
4040
let overlay_config = OverlayConfig::default();
4141
// Ignore all uTP events
4242
let (utp_listener_tx, _) = unbounded_channel::<UtpListenerRequest>();
43-
let validator = IdentityValidator {};
43+
let validator = MockValidator {};
4444

4545
OverlayProtocol::new(
4646
overlay_config,
@@ -56,7 +56,7 @@ async fn init_overlay(
5656

5757
async fn spawn_overlay(
5858
discovery: Arc<Discovery>,
59-
overlay: Arc<OverlayProtocol<IdentityContentKey, XorMetric, IdentityValidator>>,
59+
overlay: Arc<OverlayProtocol<IdentityContentKey, XorMetric, MockValidator>>,
6060
) {
6161
let (overlay_tx, mut overlay_rx) = mpsc::unbounded_channel();
6262
let mut discovery_rx = discovery

trin-history/src/validation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ impl Validator<HistoryContentKey> for ChainHistoryValidator {
2626
match content_key {
2727
HistoryContentKey::BlockHeader(key) => {
2828
let rlp = Rlp::new(content);
29-
let header = Header::decode_rlp(&rlp).expect("invalid header");
29+
let header = Header::decode_rlp(&rlp)?;
3030
let number = format!("0x{:02X}", header.number);
3131
let expected_hash = &self
3232
.header_oracle
3333
.write()
3434
.unwrap()
35-
.get_hash_at_height(number)
36-
.unwrap();
35+
.get_hash_at_height(number)?;
3736
let actual_hash = &hex::encode(key.block_hash);
3837
if actual_hash == expected_hash {
3938
Ok(())

0 commit comments

Comments
 (0)