From 12cb00496d3d6618557346ac0778b09bf7de6da7 Mon Sep 17 00:00:00 2001 From: geemo Date: Tue, 28 Feb 2023 00:10:35 -0600 Subject: [PATCH] trying to integrate Discovery --- client/src/client.rs | 13 +++--- client/src/node.rs | 8 ++-- client/src/rpc.rs | 29 ++++++++----- consensus/src/consensus.rs | 10 ++--- consensus/src/p2p/mod.rs | 2 + consensus/src/p2p/p2p_network_interface.rs | 48 ++++++++++++++++++++++ consensus/src/rpc/mock_rpc.rs | 1 + consensus/src/rpc/mod.rs | 1 + examples/basic.rs | 2 +- 9 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 consensus/src/p2p/p2p_network_interface.rs diff --git a/client/src/client.rs b/client/src/client.rs index 77abdce..aafecdb 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use config::networks::Network; use consensus::errors::ConsensusError; +use consensus::rpc::ConsensusNetworkInterface; use ethers::prelude::{Address, U256}; use ethers::types::{Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256}; use eyre::{eyre, Result}; @@ -107,7 +108,7 @@ impl ClientBuilder { self } - pub fn build(self) -> Result> { + pub fn build(self) -> Result> { let base_config = if let Some(network) = self.network { network.to_base_config() } else { @@ -211,16 +212,16 @@ impl ClientBuilder { } } -pub struct Client { - node: Arc>, +pub struct Client { + node: Arc>>, #[cfg(not(target_arch = "wasm32"))] - rpc: Option, + rpc: Option>, db: DB, fallback: Option, load_external_fallback: bool, } -impl Client { +impl Client { fn new(mut config: Config) -> Result { let db = DB::new(&config)?; if config.checkpoint.is_none() { @@ -229,7 +230,7 @@ impl Client { } let config = Arc::new(config); - let node = Node::new(config.clone())?; + let node = Node::::new(config.clone())?; let node = Arc::new(RwLock::new(node)); #[cfg(not(target_arch = "wasm32"))] diff --git a/client/src/node.rs b/client/src/node.rs index 6f06cee..b89d21c 100644 --- a/client/src/node.rs +++ b/client/src/node.rs @@ -12,7 +12,7 @@ use common::errors::BlockNotFoundError; use common::types::BlockTag; use config::Config; -use consensus::rpc::nimbus_rpc::NimbusRpc; +use consensus::rpc::ConsensusNetworkInterface; use consensus::types::{ExecutionPayload, Header}; use consensus::ConsensusClient; use execution::evm::Evm; @@ -22,8 +22,8 @@ use execution::ExecutionClient; use crate::errors::NodeError; -pub struct Node { - pub consensus: ConsensusClient, +pub struct Node { + pub consensus: ConsensusClient, pub execution: Arc>, pub config: Arc, payloads: BTreeMap, @@ -32,7 +32,7 @@ pub struct Node { pub history_size: usize, } -impl Node { +impl Node { pub fn new(config: Arc) -> Result { let consensus_rpc = &config.consensus_rpc; let checkpoint_hash = &config.checkpoint.as_ref().unwrap(); diff --git a/client/src/rpc.rs b/client/src/rpc.rs index 2603715..513d7aa 100644 --- a/client/src/rpc.rs +++ b/client/src/rpc.rs @@ -1,3 +1,4 @@ +use consensus::rpc::ConsensusNetworkInterface; use ethers::{ abi::AbiEncode, types::{Address, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256, U256}, @@ -21,14 +22,14 @@ use common::{ }; use execution::types::{CallOpts, ExecutionBlock}; -pub struct Rpc { - node: Arc>, +pub struct Rpc { + node: Arc>>, handle: Option, port: u16, } -impl Rpc { - pub fn new(node: Arc>, port: u16) -> Self { +impl Rpc { + pub fn new(node: Arc>>, port: u16) -> Self { Rpc { node, handle: None, @@ -124,14 +125,22 @@ trait NetRpc { async fn version(&self) -> Result; } -#[derive(Clone)] -struct RpcInner { - node: Arc>, +struct RpcInner { + node: Arc>>, port: u16, } +impl std::clone::Clone for RpcInner { + fn clone(&self) -> Self { + RpcInner { + node: self.node.clone(), + port: self.port, + } + } +} + #[async_trait] -impl EthRpcServer for RpcInner { +impl EthRpcServer for RpcInner { async fn get_balance(&self, address: &str, block: BlockTag) -> Result { let address = convert_err(Address::from_str(address))?; let node = self.node.read().await; @@ -305,14 +314,14 @@ impl EthRpcServer for RpcInner { } #[async_trait] -impl NetRpcServer for RpcInner { +impl NetRpcServer for RpcInner { async fn version(&self) -> Result { let node = self.node.read().await; Ok(node.chain_id().to_string()) } } -async fn start(rpc: RpcInner) -> Result<(HttpServerHandle, SocketAddr)> { +async fn start(rpc: RpcInner) -> Result<(HttpServerHandle, SocketAddr)> { let addr = format!("127.0.0.1:{}", rpc.port); let server = HttpServerBuilder::default().build(addr).await?; diff --git a/consensus/src/consensus.rs b/consensus/src/consensus.rs index cd7ebfc..d5b9949 100644 --- a/consensus/src/consensus.rs +++ b/consensus/src/consensus.rs @@ -35,8 +35,8 @@ use wasm_timer::UNIX_EPOCH; // does not implement force updates #[derive(Debug)] -pub struct ConsensusClient { - network_interface: R, +pub struct ConsensusClient { + network_interface: N, store: LightClientStore, initial_checkpoint: Vec, pub last_checkpoint: Option>, @@ -53,13 +53,13 @@ struct LightClientStore { current_max_active_participants: u64, } -impl ConsensusClient { +impl ConsensusClient { pub fn new( rpc: &str, checkpoint_block_root: &[u8], config: Arc, - ) -> Result> { - let network_interface = R::new(rpc); + ) -> Result> { + let network_interface = N::new(rpc); Ok(ConsensusClient { network_interface, diff --git a/consensus/src/p2p/mod.rs b/consensus/src/p2p/mod.rs index eb38942..4bef511 100644 --- a/consensus/src/p2p/mod.rs +++ b/consensus/src/p2p/mod.rs @@ -1,5 +1,7 @@ mod discovery; mod config; +mod p2p_network_interface; pub use discovery::*; pub use ::config::*; +pub use p2p_network_interface::*; diff --git a/consensus/src/p2p/p2p_network_interface.rs b/consensus/src/p2p/p2p_network_interface.rs new file mode 100644 index 0000000..02acf7f --- /dev/null +++ b/consensus/src/p2p/p2p_network_interface.rs @@ -0,0 +1,48 @@ +use eyre::Result; +use crate::types::{BeaconBlock, Bootstrap, FinalityUpdate, OptimisticUpdate, Update}; +use async_trait::async_trait; +use libp2p::swarm::NetworkBehaviour; + +use crate::p2p::discovery::Discovery; +use crate::rpc::ConsensusNetworkInterface; + +pub struct P2pNetworkInterface { + discovery: Discovery, +} + +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +impl ConsensusNetworkInterface for P2pNetworkInterface { + fn new(_path: &str) -> Self { + P2pNetworkInterface {} + } + + async fn get_bootstrap(&self, _block_root: &'_ [u8]) -> Result { + unimplemented!() + } + + async fn get_updates(&self, _period: u64, _count: u8) -> Result> { + unimplemented!() + } + + async fn get_finality_update(&self) -> Result { + unimplemented!() + } + + async fn get_optimistic_update(&self) -> Result { + unimplemented!() + } + + async fn get_block(&self, _slot: u64) -> Result { + unimplemented!() + } + + async fn chain_id(&self) -> Result { + unimplemented!() + } +} + +#[derive(NetworkBehaviour)] +pub struct Behaviour { + discovery: Discovery, +} diff --git a/consensus/src/rpc/mock_rpc.rs b/consensus/src/rpc/mock_rpc.rs index 5e42e80..782c35d 100644 --- a/consensus/src/rpc/mock_rpc.rs +++ b/consensus/src/rpc/mock_rpc.rs @@ -4,6 +4,7 @@ use super::ConsensusNetworkInterface; use crate::types::{BeaconBlock, Bootstrap, FinalityUpdate, OptimisticUpdate, Update}; use async_trait::async_trait; use eyre::Result; + pub struct MockRpc { testdata: PathBuf, } diff --git a/consensus/src/rpc/mod.rs b/consensus/src/rpc/mod.rs index c478f67..e97eacb 100644 --- a/consensus/src/rpc/mod.rs +++ b/consensus/src/rpc/mod.rs @@ -3,6 +3,7 @@ pub mod nimbus_rpc; use async_trait::async_trait; use eyre::Result; +use std::marker::{Sync, Send}; use crate::types::{BeaconBlock, Bootstrap, FinalityUpdate, OptimisticUpdate, Update}; diff --git a/examples/basic.rs b/examples/basic.rs index 2b076b2..f4d1c39 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -9,7 +9,7 @@ use helios::{config::networks::Network, prelude::*}; async fn main() -> Result<()> { env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); - let untrusted_rpc_url = "https://eth-mainnet.g.alchemy.com/v2/"; + let untrusted_rpc_url = "https://eth-mainnet.g.alchemy.com/v2/nObEU8Wh4FIT-X_UDFpK9oVGiTHzznML"; log::info!("Using untrusted RPC URL [REDACTED]"); let consensus_rpc = "https://www.lightclientdata.org";