Skip to content

Commit

Permalink
refactor! clean up ContentInfo type, remove unexpected paths (#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML authored Oct 19, 2024
1 parent 9261c11 commit 4b212d4
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 108 deletions.
13 changes: 8 additions & 5 deletions ethportal-api/src/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
content_key::beacon::BeaconContentKey,
enr::Enr,
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
AcceptInfo, DataRadius, FindContentInfo, FindNodesInfo, GetContentInfo,
PaginateLocalContentInfo, PongInfo, TraceContentInfo, TraceGossipInfo,
},
portal_wire::OfferTrace,
},
Expand Down Expand Up @@ -74,13 +74,16 @@ pub trait BeaconNetworkApi {

/// Send FINDCONTENT message to get the content with a content key.
#[method(name = "beaconFindContent")]
async fn find_content(&self, enr: Enr, content_key: BeaconContentKey)
-> RpcResult<ContentInfo>;
async fn find_content(
&self,
enr: Enr,
content_key: BeaconContentKey,
) -> RpcResult<FindContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network
#[method(name = "beaconGetContent")]
async fn get_content(&self, content_key: BeaconContentKey) -> RpcResult<ContentInfo>;
async fn get_content(&self, content_key: BeaconContentKey) -> RpcResult<GetContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network. Return tracing info.
Expand Down
8 changes: 4 additions & 4 deletions ethportal-api/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
content_key::history::HistoryContentKey,
enr::Enr,
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
AcceptInfo, DataRadius, FindContentInfo, FindNodesInfo, GetContentInfo,
PaginateLocalContentInfo, PongInfo, TraceContentInfo, TraceGossipInfo,
},
portal_wire::OfferTrace,
},
Expand Down Expand Up @@ -59,12 +59,12 @@ pub trait HistoryNetworkApi {
&self,
enr: Enr,
content_key: HistoryContentKey,
) -> RpcResult<ContentInfo>;
) -> RpcResult<FindContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network
#[method(name = "historyGetContent")]
async fn get_content(&self, content_key: HistoryContentKey) -> RpcResult<ContentInfo>;
async fn get_content(&self, content_key: HistoryContentKey) -> RpcResult<GetContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network. Return tracing info.
Expand Down
12 changes: 8 additions & 4 deletions ethportal-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
content_key::state::StateContentKey,
enr::Enr,
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
AcceptInfo, DataRadius, FindContentInfo, FindNodesInfo, GetContentInfo,
PaginateLocalContentInfo, PongInfo, TraceContentInfo, TraceGossipInfo,
},
portal_wire::OfferTrace,
},
Expand Down Expand Up @@ -55,12 +55,16 @@ pub trait StateNetworkApi {

/// Send FINDCONTENT message to get the content with a content key.
#[method(name = "stateFindContent")]
async fn find_content(&self, enr: Enr, content_key: StateContentKey) -> RpcResult<ContentInfo>;
async fn find_content(
&self,
enr: Enr,
content_key: StateContentKey,
) -> RpcResult<FindContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network
#[method(name = "stateGetContent")]
async fn get_content(&self, content_key: StateContentKey) -> RpcResult<ContentInfo>;
async fn get_content(&self, content_key: StateContentKey) -> RpcResult<GetContentInfo>;

/// First checks local storage if content is not found lookup a target content key in the
/// network. Return tracing info.
Expand Down
16 changes: 10 additions & 6 deletions ethportal-api/src/types/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ pub struct TraceGossipInfo {
pub transferred: Vec<String>,
}

/// Response for FindContent & GetContent endpoints
/// Response for the FindContent endpoint
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum ContentInfo {
#[serde(rename_all = "camelCase")]
ConnectionId { connection_id: u16 },
pub enum FindContentInfo {
#[serde(rename_all = "camelCase")]
Content {
content: RawContentValue,
Expand All @@ -61,8 +58,15 @@ pub enum ContentInfo {
Enrs { enrs: Vec<Enr> },
}

/// Response for the GetContent endpoint
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetContentInfo {
pub content: RawContentValue,
pub utp_transfer: bool,
}

/// Parsed response for TraceGetContent endpoint
///
/// This struct represents the content info, and is only used
/// when the content is found locally or on the network.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions ethportal-peertest/src/scenarios/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::info;

use crate::{utils::fixture_header_by_hash, Peertest};
use ethportal_api::{
types::{network::Subnetwork, portal::ContentInfo},
types::{network::Subnetwork, portal::FindContentInfo},
utils::bytes::hex_decode,
BeaconNetworkApiClient, ContentValue, Enr, HistoryNetworkApiClient, OverlayContentKey,
StateNetworkApiClient,
Expand Down Expand Up @@ -69,7 +69,7 @@ pub async fn test_find_content_return_enr(target: &Client, peertest: &Peertest)
)
.await;

let enrs = if let Ok(ContentInfo::Enrs { enrs }) = result {
let enrs = if let Ok(FindContentInfo::Enrs { enrs }) = result {
enrs
} else {
panic!("Error: Invalid response from FINDCONTENT request, expected ENRs got: {result:?}");
Expand Down
19 changes: 7 additions & 12 deletions ethportal-peertest/src/scenarios/utp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use discv5::enr::NodeId;
use ethportal_api::{
types::portal::{ContentInfo, TraceContentInfo},
types::portal::{GetContentInfo, TraceContentInfo},
ContentValue, HistoryNetworkApiClient,
};
use tracing::info;
Expand Down Expand Up @@ -32,22 +32,17 @@ pub async fn test_recursive_utp(peertest: &Peertest) {

assert!(store_result);

let content_info = peertest.nodes[0]
let GetContentInfo {
content,
utp_transfer,
} = peertest.nodes[0]
.ipc_client
.get_content(content_key)
.await
.unwrap();

if let ContentInfo::Content {
content,
utp_transfer,
} = content_info
{
assert_eq!(content, content_value.encode());
assert!(utp_transfer);
} else {
panic!("Error: Unexpected content info response");
}
assert_eq!(content, content_value.encode());
assert!(utp_transfer);
}

pub async fn test_trace_recursive_utp(peertest: &Peertest) {
Expand Down
10 changes: 5 additions & 5 deletions ethportal-peertest/src/scenarios/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use alloy::primitives::B256;
use ethportal_api::{
jsonrpsee::async_client::Client,
types::{enr::Enr, portal::ContentInfo},
types::{enr::Enr, portal::FindContentInfo},
ContentValue, HistoryContentKey, HistoryNetworkApiClient,
};
use std::str::FromStr;
Expand Down Expand Up @@ -38,7 +38,7 @@ pub async fn test_validate_pre_merge_header_by_hash(peertest: &Peertest, target:
.unwrap();

match result {
ContentInfo::Content {
FindContentInfo::Content {
content,
utp_transfer,
} => {
Expand Down Expand Up @@ -74,7 +74,7 @@ pub async fn test_validate_pre_merge_header_by_number(peertest: &Peertest, targe
.unwrap();

match result {
ContentInfo::Content {
FindContentInfo::Content {
content,
utp_transfer,
} => {
Expand Down Expand Up @@ -146,7 +146,7 @@ pub async fn test_validate_pre_merge_block_body(peertest: &Peertest, target: &Cl
.unwrap();

match result {
ContentInfo::Content {
FindContentInfo::Content {
content,
utp_transfer,
} => {
Expand Down Expand Up @@ -189,7 +189,7 @@ pub async fn test_validate_pre_merge_receipts(peertest: &Peertest, target: &Clie
.unwrap();

match result {
ContentInfo::Content {
FindContentInfo::Content {
content,
utp_transfer,
} => {
Expand Down
13 changes: 6 additions & 7 deletions portal-bridge/src/bridge/era1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ use crate::{
types::mode::{BridgeMode, FourFoursMode},
};
use ethportal_api::{
jsonrpsee::http_client::HttpClient,
types::{execution::accumulator::EpochAccumulator, portal::ContentInfo},
jsonrpsee::http_client::HttpClient, types::execution::accumulator::EpochAccumulator,
HistoryContentKey, HistoryContentValue, HistoryNetworkApiClient,
};
use trin_validation::{
Expand Down Expand Up @@ -176,7 +175,7 @@ impl Era1Bridge {
let hunter_threshold = (content_keys_to_sample.len() as u64 * threshold / 100) as usize;
for content_key in content_keys_to_sample {
let result = self.portal_client.get_content(content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = result {
if result.is_ok() {
found += 1;
if found == hunter_threshold {
info!("Hunter found enough content ({hunter_threshold}) to stop hunting in epoch {epoch}");
Expand Down Expand Up @@ -368,7 +367,7 @@ impl Era1Bridge {
let header_hash = block_tuple.header.header.hash();
let header_content_key = HistoryContentKey::new_block_header_by_hash(header_hash);
let header_content_info = portal_client.get_content(header_content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = header_content_info {
if header_content_info.is_ok() {
info!(
"Skipping header by hash at height: {} as header already found",
block_tuple.header.header.number
Expand Down Expand Up @@ -419,7 +418,7 @@ impl Era1Bridge {
let header_content_key =
HistoryContentKey::new_block_header_by_number(block_tuple.header.header.number);
let header_content_info = portal_client.get_content(header_content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = header_content_info {
if header_content_info.is_ok() {
info!(
"Skipping header by number at height: {} as header already found",
block_tuple.header.header.number
Expand Down Expand Up @@ -460,7 +459,7 @@ impl Era1Bridge {
let body_hash = block_tuple.header.header.hash();
let body_content_key = HistoryContentKey::new_block_body(body_hash);
let body_content_info = portal_client.get_content(body_content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = body_content_info {
if body_content_info.is_ok() {
info!(
"Skipping body at height: {} as body already found",
block_tuple.header.header.number
Expand Down Expand Up @@ -501,7 +500,7 @@ impl Era1Bridge {
let receipts_content_info = portal_client
.get_content(receipts_content_key.clone())
.await;
if let Ok(ContentInfo::Content { .. }) = receipts_content_info {
if receipts_content_info.is_ok() {
info!(
"Skipping receipts at height: {} as receipts already found",
block_tuple.header.header.number
Expand Down
10 changes: 5 additions & 5 deletions portal-bridge/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use tracing::{debug, warn, Instrument};

use crate::stats::{BeaconSlotStats, HistoryBlockStats, StatsReporter};
use ethportal_api::{
types::portal::{ContentInfo, TraceGossipInfo},
BeaconContentKey, BeaconContentValue, BeaconNetworkApiClient, ContentValue, HistoryContentKey,
HistoryContentValue, HistoryNetworkApiClient, OverlayContentKey,
types::portal::TraceGossipInfo, BeaconContentKey, BeaconContentValue, BeaconNetworkApiClient,
ContentValue, HistoryContentKey, HistoryContentValue, HistoryNetworkApiClient,
OverlayContentKey,
};

const GOSSIP_RETRY_COUNT: u64 = 3;
Expand Down Expand Up @@ -61,7 +61,7 @@ async fn beacon_trace_gossip(
}
// if not, make rfc request to see if data is available on network
let result = BeaconNetworkApiClient::get_content(&client, content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = result {
if result.is_ok() {
debug!("Found content on network, after failing to gossip, aborting gossip. content key={:?}", content_key.to_hex());
found = true;
return GossipReport {
Expand Down Expand Up @@ -133,7 +133,7 @@ async fn history_trace_gossip(
}
// if not, make rfc request to see if data is available on network
let result = HistoryNetworkApiClient::get_content(&client, content_key.clone()).await;
if let Ok(ContentInfo::Content { .. }) = result {
if result.is_ok() {
debug!("Found content on network, after failing to gossip, aborting gossip. content key={:?}", content_key.to_hex());
found = true;
return GossipReport {
Expand Down
9 changes: 5 additions & 4 deletions rpc/src/beacon_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use ethportal_api::{
enr::Enr,
jsonrpc::{endpoints::BeaconEndpoint, request::BeaconJsonRpcRequest},
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo, MAX_CONTENT_KEYS_PER_OFFER,
AcceptInfo, DataRadius, FindContentInfo, FindNodesInfo, GetContentInfo,
PaginateLocalContentInfo, PongInfo, TraceContentInfo, TraceGossipInfo,
MAX_CONTENT_KEYS_PER_OFFER,
},
portal_wire::OfferTrace,
},
Expand Down Expand Up @@ -120,14 +121,14 @@ impl BeaconNetworkApiServer for BeaconNetworkApi {
&self,
enr: Enr,
content_key: BeaconContentKey,
) -> RpcResult<ContentInfo> {
) -> RpcResult<FindContentInfo> {
let endpoint = BeaconEndpoint::FindContent(enr, content_key);
Ok(proxy_to_subnet(&self.network, endpoint).await?)
}

/// First checks local storage if content is not found lookup a target content key in the
/// network
async fn get_content(&self, content_key: BeaconContentKey) -> RpcResult<ContentInfo> {
async fn get_content(&self, content_key: BeaconContentKey) -> RpcResult<GetContentInfo> {
let endpoint = BeaconEndpoint::GetContent(content_key);
Ok(proxy_to_subnet(&self.network, endpoint).await?)
}
Expand Down
11 changes: 3 additions & 8 deletions rpc/src/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ethportal_api::{
endpoints::HistoryEndpoint,
request::{HistoryJsonRpcRequest, StateJsonRpcRequest},
},
portal::ContentInfo,
portal::GetContentInfo,
},
ContentValue, EthApiServer, Header, HistoryContentKey, HistoryContentValue,
};
Expand Down Expand Up @@ -155,13 +155,8 @@ impl EthApi {
content_key: HistoryContentKey,
) -> Result<HistoryContentValue, RpcServeError> {
let endpoint = HistoryEndpoint::GetContent(content_key.clone());
let response: ContentInfo = proxy_to_subnet(&self.history_network, endpoint).await?;
let ContentInfo::Content { content, .. } = response else {
return Err(RpcServeError::Message(format!(
"Invalid response variant: History GetContent should contain content value; got {response:?}"
)));
};

let GetContentInfo { content, .. } =
proxy_to_subnet(&self.history_network, endpoint).await?;
let content_value = HistoryContentValue::decode(&content_key, &content)?;
Ok(content_value)
}
Expand Down
15 changes: 5 additions & 10 deletions rpc/src/evm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethportal_api::{
types::{
content_key::state::{AccountTrieNodeKey, ContractBytecodeKey, ContractStorageTrieNodeKey},
jsonrpc::{endpoints::StateEndpoint, request::StateJsonRpcRequest},
portal::ContentInfo,
portal::GetContentInfo,
state_trie::{
account_state::AccountState,
nibbles::Nibbles,
Expand Down Expand Up @@ -162,15 +162,10 @@ impl EvmBlockState {
}

let endpoint = StateEndpoint::GetContent(content_key.clone());
let response: ContentInfo = proxy_to_subnet(&self.state_network, endpoint)
.await
.map_err(|err| EvmStateError::InternalError(err.to_string()))?;
let ContentInfo::Content { content, .. } = response else {
return Err(EvmStateError::InternalError(format!(
"Invalid response variant: State GetContent should contain content value; got {response:?}"
)));
};

let GetContentInfo { content, .. } =
proxy_to_subnet(&self.state_network, endpoint)
.await
.map_err(|err| EvmStateError::InternalError(err.to_string()))?;
let content_value = StateContentValue::decode(&content_key, &content)?;
self.cache.insert(content_key, content_value.clone());
Ok(content_value)
Expand Down
Loading

0 comments on commit 4b212d4

Please sign in to comment.