Skip to content

Commit 376ff7d

Browse files
committed
Refactor handling Offer req and update should_store db method.
1 parent dee38a1 commit 376ff7d

File tree

8 files changed

+71
-27
lines changed

8 files changed

+71
-27
lines changed

Cargo.lock

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethportal-peertest/Cargo.toml

Lines changed: 1 addition & 1 deletion
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"]}

trin-core/Cargo.toml

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

Lines changed: 24 additions & 16 deletions
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::{IdentityContentKey, OverlayContentKey},
1010
messages::{
1111
Accept, ByteList, Content, CustomPayload, FindContent, FindNodes, Message, Nodes,
1212
Offer, Ping, Pong, ProtocolId, Request, Response, SszEnr,
@@ -82,7 +82,7 @@ pub enum OverlayRequestError {
8282

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

8787
/// Error types resulting from building ACCEPT message
8888
#[error("Error while sending offer message")]
@@ -643,27 +643,39 @@ 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();
651+
let mut requested_keys =
652+
BitList::with_capacity(request.content_keys.len()).map_err(|_| {
653+
OverlayRequestError::AcceptError(
654+
"Unable to initialize bitlist for requested keys.".to_owned(),
655+
)
656+
})?;
654657

655-
// TODO: Pipe this with overlay DB and request only not available keys.
656658
let accept_keys = request.content_keys.clone();
657659

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
660+
for (i, key) in request.content_keys.into_iter().enumerate() {
661+
let key = IdentityContentKey::try_from(key)
662+
.map_err(|err| OverlayRequestError::AcceptError(err.to_string()))?;
661663
requested_keys
662-
.set(i, should_store(key))
663-
.map_err(|e| OverlayRequestError::AcceptError(e))?;
664+
.set(
665+
i,
666+
self.storage.read().should_store(&key).map_err(|_| {
667+
OverlayRequestError::AcceptError(
668+
"Portal storage error: unable to check content availability".to_owned(),
669+
)
670+
})?,
671+
)
672+
.map_err(|_| {
673+
OverlayRequestError::AcceptError("Unable to set requested keys bits".to_owned())
674+
})?;
664675
}
665676

666677
// listen for incoming connection request on conn_id, as part of utp handshake
678+
let conn_id: u16 = crate::utp::stream::rand();
667679
let utp_request = UtpListenerRequest::OfferStream(conn_id);
668680
if let Err(err) = self.utp_listener_tx.send(utp_request) {
669681
return Err(OverlayRequestError::UtpError(format!(
@@ -1809,7 +1821,3 @@ mod tests {
18091821
assert_eq!(target_bucket_idx, expected_index as u8);
18101822
}
18111823
}
1812-
1813-
fn should_store(_key: &Vec<u8>) -> bool {
1814-
return true;
1815-
}

trin-core/src/portalnet/storage.rs

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

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

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

Lines changed: 1 addition & 1 deletion
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)