Skip to content

Commit

Permalink
refactor: re-organize portal-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Dec 13, 2023
1 parent bd4741a commit 847aa97
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 47 deletions.
12 changes: 6 additions & 6 deletions ethportal-peertest/src/scenarios/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use ethportal_api::jsonrpsee::http_client::HttpClient;
use ethportal_api::{
BeaconContentKey, BeaconContentValue, PossibleBeaconContentValue, PossibleHistoryContentValue,
};
use portal_bridge::beacon_bridge::BeaconBridge;
use portal_bridge::bridge::Bridge;
use portal_bridge::consensus_api::ConsensusApi;
use portal_bridge::execution_api::ExecutionApi;
use portal_bridge::mode::BridgeMode;
use portal_bridge::api::consensus::ConsensusApi;
use portal_bridge::api::execution::ExecutionApi;
use portal_bridge::bridge::beacon::BeaconBridge;
use portal_bridge::bridge::history::HistoryBridge;
use portal_bridge::pandaops::PandaOpsMiddleware;
use portal_bridge::types::mode::BridgeMode;
use serde_json::Value;
use std::sync::Arc;
use tokio::time::{sleep, Duration};
Expand All @@ -27,7 +27,7 @@ pub async fn test_history_bridge(peertest: &Peertest, target: &HttpClient) {
let execution_api = ExecutionApi::new(pandaops_middleware);
// Wait for bootnode to start
sleep(Duration::from_secs(1)).await;
let bridge = Bridge::new(
let bridge = HistoryBridge::new(
mode,
execution_api,
portal_clients,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::pandaops::PandaOpsMiddleware;
use std::fmt::Display;

use crate::pandaops::PandaOpsMiddleware;

/// Implements endpoints from the Beacon API to access data from the consensus layer.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ConsensusApi {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::full_header::{FullHeader, FullHeaderBatch};
use crate::pandaops::PandaOpsMiddleware;
use anyhow::{anyhow, bail};
use ethereum_types::H256;
use serde_json::{json, Value};

use crate::pandaops::PandaOpsMiddleware;
use crate::types::full_header::{FullHeader, FullHeaderBatch};
use ethportal_api::types::jsonrpc::params::Params;
use ethportal_api::types::jsonrpc::request::JsonRequest;
use ethportal_api::utils::bytes::hex_encode;
use ethportal_api::{Header, Receipts};
use serde_json::{json, Value};

/// Implements endpoints from the Execution API to access data from the execution layer.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 2 additions & 0 deletions portal-bridge/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod consensus;
pub mod execution;
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use ssz_types::VariableList;
use tokio::time::{interval, sleep, Duration, MissedTickBehavior};
use tracing::{info, warn};

use crate::consensus_api::ConsensusApi;
use crate::api::consensus::ConsensusApi;
use crate::constants::BEACON_GENESIS_TIME;
use crate::gossip::gossip_beacon_content;
use crate::mode::BridgeMode;
use crate::stats::{BeaconSlotStats, StatsReporter};
use crate::types::mode::BridgeMode;
use crate::utils::{
duration_until_next_update, expected_current_slot, read_test_assets_from_file, TestAssets,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use ssz::Decode;
use tokio::time::{sleep, Duration};
use tracing::{debug, info, warn};

use crate::execution_api::ExecutionApi;
use crate::full_header::FullHeader;
use crate::api::execution::ExecutionApi;
use crate::gossip::gossip_history_content;
use crate::mode::{BridgeMode, ModeType};
use crate::stats::{HistoryBlockStats, StatsReporter};
use crate::types::{
full_header::FullHeader,
mode::{BridgeMode, ModeType},
};
use crate::utils::{read_test_assets_from_file, TestAssets};
use ethportal_api::jsonrpsee::http_client::HttpClient;
use ethportal_api::types::execution::{
Expand Down Expand Up @@ -42,15 +44,15 @@ const LATEST_BLOCK_POLL_RATE: u64 = 5; // seconds
const EPOCH_SIZE: u64 = EPOCH_SIZE_USIZE as u64;
const FUTURES_BUFFER_SIZE: usize = 32;

pub struct Bridge {
pub struct HistoryBridge {
pub mode: BridgeMode,
pub portal_clients: Vec<HttpClient>,
pub execution_api: ExecutionApi,
pub header_oracle: HeaderOracle,
pub epoch_acc_path: PathBuf,
}

impl Bridge {
impl HistoryBridge {
pub fn new(
mode: BridgeMode,
execution_api: ExecutionApi,
Expand All @@ -68,7 +70,7 @@ impl Bridge {
}
}

impl Bridge {
impl HistoryBridge {
pub async fn launch(&self) {
info!("Launching bridge mode: {:?}", self.mode);
match self.mode.clone() {
Expand Down Expand Up @@ -237,7 +239,7 @@ impl Bridge {
let block_stats = Arc::new(Mutex::new(HistoryBlockStats::new(
full_header.header.number,
)));
Bridge::gossip_header(&full_header, &portal_clients, block_stats.clone()).await?;
HistoryBridge::gossip_header(&full_header, &portal_clients, block_stats.clone()).await?;
// Sleep for 10 seconds to allow headers to saturate network,
// since they must be available for body / receipt validation.
sleep(Duration::from_secs(HEADER_SATURATION_DELAY)).await;
Expand Down Expand Up @@ -285,7 +287,7 @@ impl Bridge {
}
// Construct HeaderWithProof
let header_with_proof =
Bridge::construct_proof(full_header.header.clone(), epoch_acc).await?;
HistoryBridge::construct_proof(full_header.header.clone(), epoch_acc).await?;
HistoryContentValue::BlockHeaderWithProof(header_with_proof)
}
None => {
Expand Down
2 changes: 2 additions & 0 deletions portal-bridge/src/bridge/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod beacon;
pub mod history;
6 changes: 3 additions & 3 deletions portal-bridge/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use tokio::process::Child;
use url::Url;

use crate::client_handles::{fluffy_handle, trin_handle};
use crate::mode::BridgeMode;
use crate::types::NetworkKind;
use crate::types::mode::BridgeMode;
use crate::types::network::NetworkKind;
use ethportal_api::types::cli::check_private_key_length;

// max value of 16 b/c...
Expand Down Expand Up @@ -128,7 +128,7 @@ impl ClientType {
#[cfg(test)]
mod test {
use super::*;
use crate::mode::ModeType;
use crate::types::mode::ModeType;

#[test]
fn test_default_bridge_config() {
Expand Down
8 changes: 5 additions & 3 deletions portal-bridge/src/client_handles.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::cli::BridgeConfig;
use anyhow::bail;
use portalnet::socket::stun_for_external;
use std::net::SocketAddr;

use anyhow::bail;
use tokio::process::{Child, Command};

use crate::cli::BridgeConfig;
use portalnet::socket::stun_for_external;

pub fn fluffy_handle(
private_key: String,
rpc_port: u16,
Expand Down
10 changes: 6 additions & 4 deletions portal-bridge/src/gossip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::sync::{Arc, Mutex};

use jsonrpsee::http_client::HttpClient;
use tokio::time::{sleep, Duration};
use tracing::{debug, warn};

use crate::stats::{BeaconSlotStats, HistoryBlockStats, StatsReporter};
use ethportal_api::jsonrpsee::core::Error;
use ethportal_api::types::portal::{ContentInfo, TraceGossipInfo};
Expand All @@ -6,10 +12,6 @@ use ethportal_api::{
HistoryContentValue, HistoryNetworkApiClient, OverlayContentKey, PossibleBeaconContentValue,
PossibleHistoryContentValue,
};
use jsonrpsee::http_client::HttpClient;
use std::sync::{Arc, Mutex};
use tokio::time::{sleep, Duration};
use tracing::{debug, warn};

const GOSSIP_RETRY_COUNT: u64 = 3;
const RETRY_AFTER: Duration = Duration::from_secs(15);
Expand Down
6 changes: 1 addition & 5 deletions portal-bridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#![warn(clippy::unwrap_used)]
#![warn(clippy::uninlined_format_args)]

pub mod beacon_bridge;
pub mod api;
pub mod bridge;
pub mod cli;
pub mod client_handles;
pub mod consensus_api;
pub mod constants;
pub mod execution_api;
pub mod full_header;
pub mod gossip;
pub mod mode;
pub mod pandaops;
pub mod stats;
pub mod types;
Expand Down
16 changes: 8 additions & 8 deletions portal-bridge/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::sync::Arc;

use clap::Parser;
use tokio::time::{sleep, Duration};

use ethportal_api::jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use ethportal_api::types::cli::{DEFAULT_DISCOVERY_PORT, DEFAULT_WEB3_HTTP_PORT};
use portal_bridge::beacon_bridge::BeaconBridge;
use portal_bridge::bridge::Bridge;
use portal_bridge::api::{consensus::ConsensusApi, execution::ExecutionApi};
use portal_bridge::bridge::{beacon::BeaconBridge, history::HistoryBridge};
use portal_bridge::cli::BridgeConfig;
use portal_bridge::consensus_api::ConsensusApi;
use portal_bridge::execution_api::ExecutionApi;
use portal_bridge::pandaops::PandaOpsMiddleware;
use portal_bridge::types::NetworkKind;
use portal_bridge::types::network::NetworkKind;
use portal_bridge::utils::generate_spaced_private_keys;
use std::sync::Arc;
use tokio::time::{sleep, Duration};
use trin_utils::log::init_tracing_logger;
use trin_validation::accumulator::MasterAccumulator;
use trin_validation::oracle::HeaderOracle;
Expand Down Expand Up @@ -78,7 +78,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pandaops_middleware = PandaOpsMiddleware::default();
let execution_api = ExecutionApi::new(pandaops_middleware);

let bridge = Bridge::new(
let bridge = HistoryBridge::new(
bridge_config.mode,
execution_api,
portal_clients.expect("Failed to create history JSON-RPC clients"),
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions portal-bridge/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod full_header;
pub mod mode;
pub mod network;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use std::str::FromStr;

use trin_validation::constants::EPOCH_SIZE;

/// Used to help decode cli args identifying the desired bridge mode.
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions portal-bridge/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::ops::Deref;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::bail;
use chrono::Duration;
use discv5::enr::{CombinedKey, Enr, NodeId};
use ethereum_types::H256;
use serde::{Deserialize, Serialize};

use ethportal_api::utils::bytes::hex_encode;
use ethportal_api::HistoryContentKey;
use ethportal_api::{BeaconContentKey, BeaconContentValue, HistoryContentValue};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

/// Generates a set of N private keys, with node ids that are equally spaced
/// around the 256-bit keys space.
Expand Down

0 comments on commit 847aa97

Please sign in to comment.