add cli app

This commit is contained in:
Noah Citron 2022-08-29 16:54:58 -04:00
parent 1d810caa92
commit ad43cf7668
15 changed files with 232 additions and 51 deletions

154
Cargo.lock generated
View File

@ -83,6 +83,17 @@ dependencies = [
"rustc_version",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "auto_impl"
version = "1.0.1"
@ -333,6 +344,45 @@ dependencies = [
"generic-array 0.14.6",
]
[[package]]
name = "clap"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15f2ea93df33549dbe2e8eecd1ca55269d63ae0b3ba1f55db030817d1c2867f"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "client"
version = "0.1.0"
@ -340,6 +390,7 @@ dependencies = [
"blst",
"bytes",
"common",
"config",
"consensus",
"ethers",
"execution",
@ -416,18 +467,24 @@ dependencies = [
name = "common"
version = "0.1.0"
dependencies = [
"blst",
"bytes",
"ethers",
"eyre",
"futures",
"hex",
"jsonrpsee",
"reqwest",
"revm",
"serde",
"ssz-rs",
"tokio",
"toml",
]
[[package]]
name = "config"
version = "0.1.0"
dependencies = [
"common",
"ethers",
"eyre",
"hex",
"serde",
"ssz-rs",
"toml",
]
@ -438,6 +495,7 @@ dependencies = [
"blst",
"bytes",
"common",
"config",
"ethers",
"eyre",
"futures",
@ -564,6 +622,26 @@ dependencies = [
"subtle",
]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "ecdsa"
version = "0.14.4"
@ -1679,21 +1757,12 @@ checksum = "64de3cc433455c14174d42e554d4027ee631c4d046d43e3ecc6efc4636cdc7a7"
name = "lightclient"
version = "0.1.0"
dependencies = [
"blst",
"bytes",
"clap",
"client",
"common",
"ethers",
"config",
"dirs",
"eyre",
"futures",
"hex",
"jsonrpsee",
"reqwest",
"revm",
"serde",
"ssz-rs",
"tokio",
"toml",
]
[[package]]
@ -1933,6 +2002,12 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "os_str_bytes"
version = "6.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
[[package]]
name = "parity-scale-codec"
version = "3.1.5"
@ -2237,6 +2312,17 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"redox_syscall",
"thiserror",
]
[[package]]
name = "regex"
version = "1.6.0"
@ -2810,6 +2896,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strum"
version = "0.24.1"
@ -2894,6 +2986,21 @@ dependencies = [
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "thiserror"
version = "1.0.32"
@ -3338,6 +3445,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -4,6 +4,7 @@ members = [
"cli",
"client",
"common",
"config",
"consensus",
"execution",
]

View File

@ -4,19 +4,11 @@ version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
clap = { version = "3.2.18", features = ["derive"] }
eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs" }
blst = "0.3.10"
ethers = "0.17.0"
jsonrpsee = { version = "0.15.1", features = ["full"] }
revm = "1.9.0"
bytes = "1.2.1"
futures = "0.3.23"
toml = "0.5.9"
dirs = "4.0.0"
client = { path = "../client" }
common = { path = "../common" }
config = { path = "../config" }

View File

@ -1,23 +1,41 @@
use std::{path::Path, sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration};
use clap::Parser;
use dirs::home_dir;
use eyre::Result;
use tokio::time::sleep;
use client::{rpc::Rpc, Client};
use common::config::Config;
use config::{networks, Config};
#[tokio::main]
async fn main() -> Result<()> {
let config = Config::from_file(Path::new("./configs/goerli.toml"))?;
let cli = Cli::parse();
let config = match cli.network.as_str() {
"goerli" => networks::goerli(),
_ => {
let home = home_dir().unwrap();
let config_path = home.join(format!(".lightclient/configs/{}.toml", cli.network));
Config::from_file(&config_path).unwrap()
}
};
let mut client = Client::new(Arc::new(config)).await?;
client.sync().await?;
let mut rpc = Rpc::new(Arc::new(client));
let mut rpc = Rpc::new(Arc::new(client), cli.port.unwrap_or(8545));
let addr = rpc.start().await?;
println!("{}", addr);
println!("started rpc at: {}", addr);
sleep(Duration::from_secs(300)).await;
Ok(())
}
#[derive(Parser)]
struct Cli {
#[clap(long)]
network: String,
#[clap(long)]
port: Option<u16>,
}

View File

@ -23,3 +23,4 @@ toml = "0.5.9"
common = { path = "../common" }
consensus = { path = "../consensus" }
execution = { path = "../execution" }
config = { path = "../config" }

View File

@ -3,7 +3,7 @@ use std::sync::Arc;
use ethers::prelude::{Address, U256};
use eyre::Result;
use common::config::Config;
use config::Config;
use consensus::types::Header;
use consensus::ConsensusClient;
use execution::evm::Evm;

View File

@ -19,19 +19,22 @@ use super::Client;
pub struct Rpc {
client: Arc<Client>,
handle: Option<HttpServerHandle>,
port: u16,
}
impl Rpc {
pub fn new(client: Arc<Client>) -> Self {
pub fn new(client: Arc<Client>, port: u16) -> Self {
Rpc {
client,
handle: None,
port,
}
}
pub async fn start(&mut self) -> Result<SocketAddr> {
let rpc_inner = RpcInner {
client: self.client.clone(),
port: self.port,
};
let (handle, addr) = start(rpc_inner).await?;
self.handle = Some(handle);
@ -62,7 +65,8 @@ trait EthRpc {
}
struct RpcInner {
pub client: Arc<Client>,
client: Arc<Client>,
port: u16,
}
#[async_trait]
@ -154,7 +158,8 @@ impl EthRpcServer for RpcInner {
}
async fn start(rpc: RpcInner) -> Result<(HttpServerHandle, SocketAddr)> {
let server = HttpServerBuilder::default().build("127.0.0.1:8545").await?;
let addr = format!("127.0.0.1:{}", rpc.port);
let server = HttpServerBuilder::default().build(addr).await?;
let addr = server.local_addr()?;
let handle = server.start(rpc.into_rpc())?;

View File

@ -6,16 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs" }
blst = "0.3.10"
ethers = "0.17.0"
jsonrpsee = { version = "0.15.1", features = ["full"] }
revm = "1.9.0"
bytes = "1.2.1"
futures = "0.3.23"
toml = "0.5.9"

View File

@ -1,3 +1,2 @@
pub mod config;
pub mod types;
pub mod utils;

17
config/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "config"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs" }
ethers = "0.17.0"
toml = "0.5.9"
common = { path = "../common" }

View File

@ -1,9 +1,11 @@
pub mod networks;
use std::{fs, path::Path};
use eyre::Result;
use serde::Deserialize;
use super::utils::hex_str_to_bytes;
use common::utils::hex_str_to_bytes;
#[derive(Deserialize, Debug)]
pub struct Config {

37
config/src/networks.rs Normal file
View File

@ -0,0 +1,37 @@
use common::utils::hex_str_to_bytes;
use crate::{Config, Fork, Forks, General};
pub fn goerli() -> Config {
Config {
general: General {
chain_id: 5,
genesis_root: hex_str_to_bytes(
"0x043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb",
)
.unwrap(),
checkpoint: hex_str_to_bytes(
"0x172128eadf1da46467f4d6a822206698e2d3f957af117dd650954780d680dc99",
)
.unwrap(),
consensus_rpc: "http://testing.prater.beacon-api.nimbus.team".to_string(),
execution_rpc:
"https://eth-goerli.g.alchemy.com:443/v2/o_8Qa9kgwDPf9G8sroyQ-uQtyhyWa3ao"
.to_string(),
},
forks: Forks {
genesis: Fork {
epoch: 0,
fork_version: hex_str_to_bytes("0x00001020").unwrap(),
},
altair: Fork {
epoch: 36660,
fork_version: hex_str_to_bytes("0x01001020").unwrap(),
},
bellatrix: Fork {
epoch: 112260,
fork_version: hex_str_to_bytes("0x02001020").unwrap(),
},
},
}
}

View File

@ -21,4 +21,4 @@ futures = "0.3.23"
toml = "0.5.9"
common = { path = "../common" }
config = { path = "../config" }

View File

@ -5,9 +5,9 @@ use blst::BLST_ERROR;
use eyre::Result;
use ssz_rs::prelude::*;
use common::config::Config;
use common::types::*;
use common::utils::*;
use config::Config;
use super::rpc::Rpc;
use super::types::*;