Skip to content

Commit

Permalink
refactor: decode ContentValue only with known ContentKey (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev authored Aug 28, 2024
1 parent 5ee5e92 commit 1aa4c2d
Show file tree
Hide file tree
Showing 49 changed files with 659 additions and 622 deletions.
22 changes: 12 additions & 10 deletions ethportal-api/src/beacon.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{
types::{
beacon::{ContentInfo, PaginateLocalContentInfo, TraceContentInfo},
content_key::beacon::BeaconContentKey,
enr::Enr,
portal::{AcceptInfo, DataRadius, FindNodesInfo, PongInfo, TraceGossipInfo},
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
},
},
BeaconContentValue, RoutingTableInfo,
RawContentValue, RoutingTableInfo,
};
use alloy_primitives::B256;
use discv5::enr::NodeId;
Expand Down Expand Up @@ -82,15 +84,15 @@ pub trait BeaconNetworkApi {
&self,
offset: u64,
limit: u64,
) -> RpcResult<PaginateLocalContentInfo>;
) -> RpcResult<PaginateLocalContentInfo<BeaconContentKey>>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
/// all peers. Return the number of peers that the content was gossiped to.
#[method(name = "beaconGossip")]
async fn gossip(
&self,
content_key: BeaconContentKey,
content_value: BeaconContentValue,
content_value: RawContentValue,
) -> RpcResult<u32>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
Expand All @@ -99,7 +101,7 @@ pub trait BeaconNetworkApi {
async fn trace_gossip(
&self,
content_key: BeaconContentKey,
content_value: BeaconContentValue,
content_value: RawContentValue,
) -> RpcResult<TraceGossipInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer and wait for a response.
Expand All @@ -111,7 +113,7 @@ pub trait BeaconNetworkApi {
&self,
enr: Enr,
content_key: BeaconContentKey,
content_value: BeaconContentValue,
content_value: RawContentValue,
) -> RpcResult<AcceptInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer.
Expand All @@ -123,7 +125,7 @@ pub trait BeaconNetworkApi {
&self,
enr: Enr,
content_key: BeaconContentKey,
content_value: BeaconContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Send an OFFER request with given ContentKeys, to the designated peer and wait for a
Expand All @@ -142,10 +144,10 @@ pub trait BeaconNetworkApi {
async fn store(
&self,
content_key: BeaconContentKey,
content_value: BeaconContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Get a content from the local database
#[method(name = "beaconLocalContent")]
async fn local_content(&self, content_key: BeaconContentKey) -> RpcResult<BeaconContentValue>;
async fn local_content(&self, content_key: BeaconContentKey) -> RpcResult<RawContentValue>;
}
23 changes: 12 additions & 11 deletions ethportal-api/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::{
types::{
content_key::history::HistoryContentKey,
enr::Enr,
history::{ContentInfo, PaginateLocalContentInfo, TraceContentInfo},
portal::{AcceptInfo, DataRadius, FindNodesInfo, PongInfo, TraceGossipInfo},
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
},
},
HistoryContentValue, RoutingTableInfo,
RawContentValue, RoutingTableInfo,
};
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
Expand Down Expand Up @@ -78,15 +80,15 @@ pub trait HistoryNetworkApi {
&self,
offset: u64,
limit: u64,
) -> RpcResult<PaginateLocalContentInfo>;
) -> RpcResult<PaginateLocalContentInfo<HistoryContentKey>>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
/// all peers. Return the number of peers that the content was gossiped to.
#[method(name = "historyGossip")]
async fn gossip(
&self,
content_key: HistoryContentKey,
content_value: HistoryContentValue,
content_value: RawContentValue,
) -> RpcResult<u32>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
Expand All @@ -95,7 +97,7 @@ pub trait HistoryNetworkApi {
async fn trace_gossip(
&self,
content_key: HistoryContentKey,
content_value: HistoryContentValue,
content_value: RawContentValue,
) -> RpcResult<TraceGossipInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer and wait for a response.
Expand All @@ -107,7 +109,7 @@ pub trait HistoryNetworkApi {
&self,
enr: Enr,
content_key: HistoryContentKey,
content_value: HistoryContentValue,
content_value: RawContentValue,
) -> RpcResult<AcceptInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer.
Expand All @@ -119,7 +121,7 @@ pub trait HistoryNetworkApi {
&self,
enr: Enr,
content_key: HistoryContentKey,
content_value: HistoryContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Send an OFFER request with given ContentKeys, to the designated peer and wait for a
Expand All @@ -138,11 +140,10 @@ pub trait HistoryNetworkApi {
async fn store(
&self,
content_key: HistoryContentKey,
content_value: HistoryContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Get a content value from the local database
#[method(name = "historyLocalContent")]
async fn local_content(&self, content_key: HistoryContentKey)
-> RpcResult<HistoryContentValue>;
async fn local_content(&self, content_key: HistoryContentKey) -> RpcResult<RawContentValue>;
}
33 changes: 16 additions & 17 deletions ethportal-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,42 @@ mod dashboard;
pub mod discv5;
mod eth;
mod history;
pub mod state;
mod state;
#[cfg(test)]
mod test_utils;
pub mod types;
pub mod utils;
mod web3;

pub use crate::discv5::{Discv5ApiClient, Discv5ApiServer};
pub use beacon::{BeaconNetworkApiClient, BeaconNetworkApiServer};
pub use discv5::{Discv5ApiClient, Discv5ApiServer};
pub use eth::{EthApiClient, EthApiServer};
pub use history::{HistoryNetworkApiClient, HistoryNetworkApiServer};
pub use state::{StateNetworkApiClient, StateNetworkApiServer};
pub use web3::{Web3ApiClient, Web3ApiServer};

pub use types::content_key::{
beacon::{BeaconContentKey, LightClientBootstrapKey, LightClientUpdatesByRangeKey},
error::ContentKeyError,
history::{
BlockBodyKey, BlockHeaderKey, BlockReceiptsKey, EpochAccumulatorKey, HistoryContentKey,
RawContentKey,
},
overlay::{IdentityContentKey, OverlayContentKey},
state::StateContentKey,
};

pub use types::{
consensus,
consensus::light_client,
content_key::{
beacon::{BeaconContentKey, LightClientBootstrapKey, LightClientUpdatesByRangeKey},
error::ContentKeyError,
history::{
BlockBodyKey, BlockHeaderKey, BlockReceiptsKey, EpochAccumulatorKey, HistoryContentKey,
},
overlay::{IdentityContentKey, OverlayContentKey},
state::StateContentKey,
},
content_value::{
beacon::BeaconContentValue, error::ContentValueError, history::HistoryContentValue,
state::StateContentValue,
state::StateContentValue, ContentValue,
},
discv5::*,
enr::*,
execution::{block_body::*, header::*, receipts::*},
node_id::*,
portal::{RawContentKey, RawContentValue},
};

// Re-exports jsonrpsee crate
pub use jsonrpsee;
pub use types::content_value::ContentValue;

pub use types::{discv5::*, enr::*, node_id::*};
22 changes: 12 additions & 10 deletions ethportal-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::{
types::{
content_key::state::StateContentKey,
enr::Enr,
portal::{AcceptInfo, DataRadius, FindNodesInfo, PongInfo, TraceGossipInfo},
state::{ContentInfo, PaginateLocalContentInfo, TraceContentInfo},
portal::{
AcceptInfo, ContentInfo, DataRadius, FindNodesInfo, PaginateLocalContentInfo, PongInfo,
TraceContentInfo, TraceGossipInfo,
},
},
RoutingTableInfo, StateContentValue,
RawContentValue, RoutingTableInfo,
};
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
Expand Down Expand Up @@ -71,15 +73,15 @@ pub trait StateNetworkApi {
&self,
offset: u64,
limit: u64,
) -> RpcResult<PaginateLocalContentInfo>;
) -> RpcResult<PaginateLocalContentInfo<StateContentKey>>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
/// all peers. Return the number of peers that the content was gossiped to.
#[method(name = "stateGossip")]
async fn gossip(
&self,
content_key: StateContentKey,
content_value: StateContentValue,
content_value: RawContentValue,
) -> RpcResult<u32>;

/// Send the provided content value to interested peers. Clients may choose to send to some or
Expand All @@ -88,7 +90,7 @@ pub trait StateNetworkApi {
async fn trace_gossip(
&self,
content_key: StateContentKey,
content_value: StateContentValue,
content_value: RawContentValue,
) -> RpcResult<TraceGossipInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer and wait for a response.
Expand All @@ -100,7 +102,7 @@ pub trait StateNetworkApi {
&self,
enr: Enr,
content_key: StateContentKey,
content_value: StateContentValue,
content_value: RawContentValue,
) -> RpcResult<AcceptInfo>;

/// Send an OFFER request with given ContentKey, to the designated peer.
Expand All @@ -112,18 +114,18 @@ pub trait StateNetworkApi {
&self,
enr: Enr,
content_key: StateContentKey,
content_value: StateContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Store content key with a content data to the local database.
#[method(name = "stateStore")]
async fn store(
&self,
content_key: StateContentKey,
content_value: StateContentValue,
content_value: RawContentValue,
) -> RpcResult<bool>;

/// Get a content from the local database
#[method(name = "stateLocalContent")]
async fn local_content(&self, content_key: StateContentKey) -> RpcResult<StateContentValue>;
async fn local_content(&self, content_key: StateContentKey) -> RpcResult<RawContentValue>;
}
39 changes: 0 additions & 39 deletions ethportal-api/src/types/beacon.rs

This file was deleted.

3 changes: 0 additions & 3 deletions ethportal-api/src/types/content_key/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ pub const HISTORY_BLOCK_BODY_KEY_PREFIX: u8 = 0x01;
pub const HISTORY_BLOCK_RECEIPTS_KEY_PREFIX: u8 = 0x02;
pub const HISTORY_BLOCK_EPOCH_ACCUMULATOR_KEY_PREFIX: u8 = 0x03;

/// SSZ encoded overlay content key as bytes
pub type RawContentKey = Vec<u8>;

/// A content key in the history overlay network.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HistoryContentKey {
Expand Down
Loading

0 comments on commit 1aa4c2d

Please sign in to comment.