trying to integrate Discovery

This commit is contained in:
geemo 2023-02-28 00:10:35 -06:00
parent e73aa9bc05
commit 12cb00496d
9 changed files with 88 additions and 26 deletions

View File

@ -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<DB: Database>(self) -> Result<Client<DB>> {
pub fn build<DB: Database, N: ConsensusNetworkInterface>(self) -> Result<Client<DB, N>> {
let base_config = if let Some(network) = self.network {
network.to_base_config()
} else {
@ -211,16 +212,16 @@ impl ClientBuilder {
}
}
pub struct Client<DB: Database> {
node: Arc<RwLock<Node>>,
pub struct Client<DB: Database, N: ConsensusNetworkInterface> {
node: Arc<RwLock<Node<N>>>,
#[cfg(not(target_arch = "wasm32"))]
rpc: Option<Rpc>,
rpc: Option<Rpc<N>>,
db: DB,
fallback: Option<String>,
load_external_fallback: bool,
}
impl<DB: Database> Client<DB> {
impl<DB: Database, N: ConsensusNetworkInterface> Client<DB, N> {
fn new(mut config: Config) -> Result<Self> {
let db = DB::new(&config)?;
if config.checkpoint.is_none() {
@ -229,7 +230,7 @@ impl<DB: Database> Client<DB> {
}
let config = Arc::new(config);
let node = Node::new(config.clone())?;
let node = Node::<N>::new(config.clone())?;
let node = Arc::new(RwLock::new(node));
#[cfg(not(target_arch = "wasm32"))]

View File

@ -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<NimbusRpc>,
pub struct Node<N: ConsensusNetworkInterface> {
pub consensus: ConsensusClient<N>,
pub execution: Arc<ExecutionClient<HttpRpc>>,
pub config: Arc<Config>,
payloads: BTreeMap<u64, ExecutionPayload>,
@ -32,7 +32,7 @@ pub struct Node {
pub history_size: usize,
}
impl Node {
impl<N: ConsensusNetworkInterface> Node<N> {
pub fn new(config: Arc<Config>) -> Result<Self, NodeError> {
let consensus_rpc = &config.consensus_rpc;
let checkpoint_hash = &config.checkpoint.as_ref().unwrap();

View File

@ -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<RwLock<Node>>,
pub struct Rpc<N: ConsensusNetworkInterface> {
node: Arc<RwLock<Node<N>>>,
handle: Option<HttpServerHandle>,
port: u16,
}
impl Rpc {
pub fn new(node: Arc<RwLock<Node>>, port: u16) -> Self {
impl<N: ConsensusNetworkInterface> Rpc<N> {
pub fn new(node: Arc<RwLock<Node<N>>>, port: u16) -> Self {
Rpc {
node,
handle: None,
@ -124,14 +125,22 @@ trait NetRpc {
async fn version(&self) -> Result<String, Error>;
}
#[derive(Clone)]
struct RpcInner {
node: Arc<RwLock<Node>>,
struct RpcInner<N: ConsensusNetworkInterface> {
node: Arc<RwLock<Node<N>>>,
port: u16,
}
impl<N: ConsensusNetworkInterface> std::clone::Clone for RpcInner<N> {
fn clone(&self) -> Self {
RpcInner {
node: self.node.clone(),
port: self.port,
}
}
}
#[async_trait]
impl EthRpcServer for RpcInner {
impl<N: ConsensusNetworkInterface> EthRpcServer for RpcInner<N> {
async fn get_balance(&self, address: &str, block: BlockTag) -> Result<String, Error> {
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<N: ConsensusNetworkInterface> NetRpcServer for RpcInner<N> {
async fn version(&self) -> Result<String, Error> {
let node = self.node.read().await;
Ok(node.chain_id().to_string())
}
}
async fn start(rpc: RpcInner) -> Result<(HttpServerHandle, SocketAddr)> {
async fn start<N: ConsensusNetworkInterface>(rpc: RpcInner<N>) -> Result<(HttpServerHandle, SocketAddr)> {
let addr = format!("127.0.0.1:{}", rpc.port);
let server = HttpServerBuilder::default().build(addr).await?;

View File

@ -35,8 +35,8 @@ use wasm_timer::UNIX_EPOCH;
// does not implement force updates
#[derive(Debug)]
pub struct ConsensusClient<R: ConsensusNetworkInterface> {
network_interface: R,
pub struct ConsensusClient<N: ConsensusNetworkInterface> {
network_interface: N,
store: LightClientStore,
initial_checkpoint: Vec<u8>,
pub last_checkpoint: Option<Vec<u8>>,
@ -53,13 +53,13 @@ struct LightClientStore {
current_max_active_participants: u64,
}
impl<R: ConsensusNetworkInterface> ConsensusClient<R> {
impl<N: ConsensusNetworkInterface> ConsensusClient<N> {
pub fn new(
rpc: &str,
checkpoint_block_root: &[u8],
config: Arc<Config>,
) -> Result<ConsensusClient<R>> {
let network_interface = R::new(rpc);
) -> Result<ConsensusClient<N>> {
let network_interface = N::new(rpc);
Ok(ConsensusClient {
network_interface,

View File

@ -1,5 +1,7 @@
mod discovery;
mod config;
mod p2p_network_interface;
pub use discovery::*;
pub use ::config::*;
pub use p2p_network_interface::*;

View File

@ -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<Bootstrap> {
unimplemented!()
}
async fn get_updates(&self, _period: u64, _count: u8) -> Result<Vec<Update>> {
unimplemented!()
}
async fn get_finality_update(&self) -> Result<FinalityUpdate> {
unimplemented!()
}
async fn get_optimistic_update(&self) -> Result<OptimisticUpdate> {
unimplemented!()
}
async fn get_block(&self, _slot: u64) -> Result<BeaconBlock> {
unimplemented!()
}
async fn chain_id(&self) -> Result<u64> {
unimplemented!()
}
}
#[derive(NetworkBehaviour)]
pub struct Behaviour {
discovery: Discovery,
}

View File

@ -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,
}

View File

@ -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};

View File

@ -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/<YOUR_API_KEY>";
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";