Skip to content

Commit e7ece03

Browse files
committed
Refactor handling Offer requests and update should_store db method.
1 parent 26deb62 commit e7ece03

File tree

9 files changed

+78
-30
lines changed

9 files changed

+78
-30
lines changed

Cargo.lock

+39-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethportal-peertest/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ futures = "0.3.21"
1414
hyper = { version = "0.14", features = ["full"] }
1515
hex = "0.4.3"
1616
log = "0.4.14"
17-
rocksdb = "0.16.0"
17+
rocksdb = "0.18.0"
1818
serde_json = "1.0.59"
1919
structopt = "0.3"
2020
tokio = {version = "1.14.0", features = ["full"]}

newsfragments/324.fixed.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove the dummy should_store method when handling OFFER requests and accept only content we are interested in.

trin-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ parking_lot = "0.11.2"
3232
prometheus_exporter = "0.8.4"
3333
rand = "0.8.4"
3434
rlp = "0.5.0"
35-
rocksdb = "0.16.0"
35+
rocksdb = "0.18.0"
3636
rstest = "0.11.0"
3737
r2d2 = "0.8.9"
3838
r2d2_sqlite = "0.19.0"

trin-core/src/portalnet/overlay_service.rs

+30-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
metrics::OverlayMetrics,
77
storage::PortalStorage,
88
types::{
9-
content_key::OverlayContentKey,
9+
content_key::{HistoryContentKey, OverlayContentKey},
1010
messages::{
1111
Accept, ByteList, Content, CustomPayload, FindContent, FindNodes, Message, Nodes,
1212
Offer, Ping, Pong, ProtocolId, Request, Response, SszEnr,
@@ -81,15 +81,15 @@ pub enum OverlayRequestError {
8181
Discv5Error(discv5::RequestError),
8282

8383
/// Error types resulting from building ACCEPT message
84-
#[error("Error while building accept message")]
85-
AcceptError(ssz_types::Error),
84+
#[error("Error while building accept message: {0}")]
85+
AcceptError(String),
8686

8787
/// Error types resulting from building ACCEPT message
88-
#[error("Error while sending offer message")]
88+
#[error("Error while sending offer message: {0}")]
8989
OfferError(String),
9090

9191
/// uTP request error
92-
#[error("uTP request error")]
92+
#[error("uTP request error: {0}")]
9393
UtpError(String),
9494
}
9595

@@ -643,27 +643,42 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
643643
}
644644
}
645645

646-
/// Attempts to build a `Accept` response for a `Offer` request.
646+
/// Attempts to build a `Accept` response for an `Offer` request.
647647
fn handle_offer(&self, request: Offer) -> Result<Accept, OverlayRequestError> {
648648
self.metrics
649649
.as_ref()
650650
.and_then(|m| Some(m.report_inbound_offer()));
651-
let mut requested_keys = BitList::with_capacity(request.content_keys.len())
652-
.map_err(|e| OverlayRequestError::AcceptError(e))?;
653-
let conn_id: u16 = crate::utp::stream::rand();
654651

655-
// TODO: Pipe this with overlay DB and request only not available keys.
652+
let mut requested_keys =
653+
BitList::with_capacity(request.content_keys.len()).map_err(|_| {
654+
OverlayRequestError::AcceptError(
655+
"Unable to initialize bitlist for requested keys.".to_owned(),
656+
)
657+
})?;
658+
656659
let accept_keys = request.content_keys.clone();
657660

658-
for (i, key) in request.content_keys.iter().enumerate() {
659-
// should_store is currently a dummy function
660-
// the actual function will take ContentKey type, so we'll have to decode keys here
661+
for (i, key) in request.content_keys.into_iter().enumerate() {
662+
// TODO: This will not work when we receive state Offer message with state content keys
663+
// we need to refactor it in a way to get access to the protocol id in this method
664+
let key = HistoryContentKey::try_from(key)
665+
.map_err(|err| OverlayRequestError::AcceptError(err.to_string()))?;
661666
requested_keys
662-
.set(i, should_store(key))
663-
.map_err(|e| OverlayRequestError::AcceptError(e))?;
667+
.set(
668+
i,
669+
self.storage.read().should_store(&key).map_err(|_| {
670+
OverlayRequestError::AcceptError(
671+
"Portal storage error: unable to check content availability".to_owned(),
672+
)
673+
})?,
674+
)
675+
.map_err(|_| {
676+
OverlayRequestError::AcceptError("Unable to set requested keys bits".to_owned())
677+
})?;
664678
}
665679

666680
// listen for incoming connection request on conn_id, as part of utp handshake
681+
let conn_id: u16 = crate::utp::stream::rand();
667682
let utp_request = UtpListenerRequest::OfferStream(conn_id);
668683
if let Err(err) = self.utp_listener_tx.send(utp_request) {
669684
return Err(OverlayRequestError::UtpError(format!(
@@ -1809,7 +1824,3 @@ mod tests {
18091824
assert_eq!(target_bucket_idx, expected_index as u8);
18101825
}
18111826
}
1812-
1813-
fn should_store(_key: &Vec<u8>) -> bool {
1814-
return true;
1815-
}

trin-core/src/portalnet/storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl PortalStorage {
122122
pub fn should_store(&self, key: &impl OverlayContentKey) -> Result<bool, PortalStorageError> {
123123
let content_id = key.content_id();
124124
// Don't store if we already have the data
125-
match self.db.get(&content_id) {
125+
match self.db.get_pinned(&content_id) {
126126
Ok(Some(_)) => return Ok(false),
127127
Err(e) => return Err(PortalStorageError::RocksDB(e)),
128128
_ => (),

trin-core/src/portalnet/types/content_key.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::anyhow;
12
use ethereum_types::{U256, U512};
23
use sha2::{Digest as Sha2Digest, Sha256};
34
use sha3::{Digest, Keccak256};
@@ -28,12 +29,12 @@ impl IdentityContentKey {
2829
}
2930

3031
impl TryFrom<Vec<u8>> for IdentityContentKey {
31-
type Error = String;
32+
type Error = anyhow::Error;
3233

3334
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
3435
// Require that length of input is equal to 32.
3536
if value.len() != 32 {
36-
return Err(String::from("Input Vec has invalid length"));
37+
return Err(anyhow!("Input Vec has invalid length"));
3738
}
3839

3940
// The following will not panic because of the length check above.

trin-history/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ keccak-hash = "0.8.0"
1616
log = "0.4.14"
1717
parking_lot = "0.11.2"
1818
rlp = "0.5.0"
19-
rocksdb = "0.16.0"
19+
rocksdb = "0.18.0"
2020
serde_json = "1.0.59"
2121
serial_test = "0.5.1"
2222
tempdir = "0.3.7"

trin-state/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ tracing-subscriber = "0.2.18"
2222
tracing-futures = "0.2.5"
2323
tokio = {version = "1.8.0", features = ["full"]}
2424
trin-core = { path = "../trin-core" }
25-
rocksdb = "0.16.0"
25+
rocksdb = "0.18.0"

0 commit comments

Comments
 (0)