Skip to content

Commit

Permalink
Refactor handling Offer requests and update should_store db method.
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed May 12, 2022
1 parent 26deb62 commit 6b8888f
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 27 deletions.
43 changes: 39 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ethportal-peertest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ futures = "0.3.21"
hyper = { version = "0.14", features = ["full"] }
hex = "0.4.3"
log = "0.4.14"
rocksdb = "0.16.0"
rocksdb = "0.18.0"
serde_json = "1.0.59"
structopt = "0.3"
tokio = {version = "1.14.0", features = ["full"]}
Expand Down
1 change: 1 addition & 0 deletions newsfragments/324.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove the dummy should_store method when handling OFFER requests and accept only content we are interested in.
2 changes: 1 addition & 1 deletion trin-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ parking_lot = "0.11.2"
prometheus_exporter = "0.8.4"
rand = "0.8.4"
rlp = "0.5.0"
rocksdb = "0.16.0"
rocksdb = "0.18.0"
rstest = "0.11.0"
r2d2 = "0.8.9"
r2d2_sqlite = "0.19.0"
Expand Down
40 changes: 24 additions & 16 deletions trin-core/src/portalnet/overlay_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
metrics::OverlayMetrics,
storage::PortalStorage,
types::{
content_key::OverlayContentKey,
content_key::{IdentityContentKey, OverlayContentKey},
messages::{
Accept, ByteList, Content, CustomPayload, FindContent, FindNodes, Message, Nodes,
Offer, Ping, Pong, ProtocolId, Request, Response, SszEnr,
Expand Down Expand Up @@ -82,7 +82,7 @@ pub enum OverlayRequestError {

/// Error types resulting from building ACCEPT message
#[error("Error while building accept message")]
AcceptError(ssz_types::Error),
AcceptError(String),

/// Error types resulting from building ACCEPT message
#[error("Error while sending offer message")]
Expand Down Expand Up @@ -643,27 +643,39 @@ impl<TContentKey: OverlayContentKey + Send, TMetric: Metric + Send>
}
}

/// Attempts to build a `Accept` response for a `Offer` request.
/// Attempts to build a `Accept` response for an `Offer` request.
fn handle_offer(&self, request: Offer) -> Result<Accept, OverlayRequestError> {
self.metrics
.as_ref()
.and_then(|m| Some(m.report_inbound_offer()));
let mut requested_keys = BitList::with_capacity(request.content_keys.len())
.map_err(|e| OverlayRequestError::AcceptError(e))?;
let conn_id: u16 = crate::utp::stream::rand();
let mut requested_keys =
BitList::with_capacity(request.content_keys.len()).map_err(|_| {
OverlayRequestError::AcceptError(
"Unable to initialize bitlist for requested keys.".to_owned(),
)
})?;

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

for (i, key) in request.content_keys.iter().enumerate() {
// should_store is currently a dummy function
// the actual function will take ContentKey type, so we'll have to decode keys here
for (i, key) in request.content_keys.into_iter().enumerate() {
let key = IdentityContentKey::try_from(key)
.map_err(|err| OverlayRequestError::AcceptError(err.to_string()))?;
requested_keys
.set(i, should_store(key))
.map_err(|e| OverlayRequestError::AcceptError(e))?;
.set(
i,
self.storage.read().should_store(&key).map_err(|_| {
OverlayRequestError::AcceptError(
"Portal storage error: unable to check content availability".to_owned(),
)
})?,
)
.map_err(|_| {
OverlayRequestError::AcceptError("Unable to set requested keys bits".to_owned())
})?;
}

// listen for incoming connection request on conn_id, as part of utp handshake
let conn_id: u16 = crate::utp::stream::rand();
let utp_request = UtpListenerRequest::OfferStream(conn_id);
if let Err(err) = self.utp_listener_tx.send(utp_request) {
return Err(OverlayRequestError::UtpError(format!(
Expand Down Expand Up @@ -1809,7 +1821,3 @@ mod tests {
assert_eq!(target_bucket_idx, expected_index as u8);
}
}

fn should_store(_key: &Vec<u8>) -> bool {
return true;
}
2 changes: 1 addition & 1 deletion trin-core/src/portalnet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl PortalStorage {
pub fn should_store(&self, key: &impl OverlayContentKey) -> Result<bool, PortalStorageError> {
let content_id = key.content_id();
// Don't store if we already have the data
match self.db.get(&content_id) {
match self.db.get_pinned(&content_id) {
Ok(Some(_)) => return Ok(false),
Err(e) => return Err(PortalStorageError::RocksDB(e)),
_ => (),
Expand Down
5 changes: 3 additions & 2 deletions trin-core/src/portalnet/types/content_key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use ethereum_types::{U256, U512};
use sha2::{Digest as Sha2Digest, Sha256};
use sha3::{Digest, Keccak256};
Expand Down Expand Up @@ -28,12 +29,12 @@ impl IdentityContentKey {
}

impl TryFrom<Vec<u8>> for IdentityContentKey {
type Error = String;
type Error = anyhow::Error;

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

// The following will not panic because of the length check above.
Expand Down
2 changes: 1 addition & 1 deletion trin-history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keccak-hash = "0.8.0"
log = "0.4.14"
parking_lot = "0.11.2"
rlp = "0.5.0"
rocksdb = "0.16.0"
rocksdb = "0.18.0"
serde_json = "1.0.59"
serial_test = "0.5.1"
tempdir = "0.3.7"
Expand Down
2 changes: 1 addition & 1 deletion trin-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ tracing-subscriber = "0.2.18"
tracing-futures = "0.2.5"
tokio = {version = "1.8.0", features = ["full"]}
trin-core = { path = "../trin-core" }
rocksdb = "0.16.0"
rocksdb = "0.18.0"

0 comments on commit 6b8888f

Please sign in to comment.