Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Compile helios in a canister #1

Merged
merged 10 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,215 changes: 436 additions & 779 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dotenv = "0.15.0"
tokio = { version = "1", features = ["full"] }
eyre = "0.6.8"
dirs = "4.0.0"
ethers = { version = "2.0.2", features = [ "abigen" ] }
ethers = { version = "2.0.8", default-features = false }
env_logger = "0.9.0"
log = "0.4.17"
tracing-test = "0.2.4"
Expand Down
3 changes: 0 additions & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
cargo-features = ["different-binary-name"]

[package]
name = "cli"
version = "0.4.1"
edition = "2021"

[[bin]]
name = "cli"
filename = "helios"
path = "src/main.rs"

[dependencies]
Expand Down
7 changes: 3 additions & 4 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
ethers = "2.0.2"
ethers-core = { version = "2.0.8", default-features = false }
futures = "0.3.23"
log = "0.4.17"
thiserror = "1.0.37"
Expand All @@ -23,7 +23,6 @@ jsonrpsee = { version = "0.15.1", features = ["full"] }
tokio = { version = "1", features = ["full"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
gloo-timers = "0.2.6"
wasm-bindgen-futures = "0.4.33"
ic-cdk-timers = "0.4.0"
ic-cdk = "0.10.0"
tokio = { version = "1", features = ["sync"] }
zvolin marked this conversation as resolved.
Show resolved Hide resolved

21 changes: 12 additions & 9 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::sync::Arc;

use config::networks::Network;
use consensus::errors::ConsensusError;
use ethers::prelude::{Address, U256};
use ethers::types::{
FeeHistory, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256,
use ethers_core::types::{
Address, FeeHistory, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256, U256,
};
use eyre::{eyre, Result};

Expand All @@ -24,9 +23,9 @@ use tokio::spawn;
use tokio::time::sleep;

#[cfg(target_arch = "wasm32")]
use gloo_timers::callback::Interval;
use ic_cdk::spawn;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local;
use ic_cdk_timers::set_timer_interval;

use crate::database::Database;
use crate::errors::NodeError;
Expand Down Expand Up @@ -249,6 +248,8 @@ impl<DB: Database> Client<DB> {
let config = Arc::new(config);
let node = Node::new(config.clone())?;
let node = Arc::new(RwLock::new(node));

#[cfg(not(target_arch = "wasm32"))]
let mut rpc: Option<Rpc> = None;

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -331,17 +332,19 @@ impl<DB: Database> Client<DB> {

#[cfg(target_arch = "wasm32")]
fn start_advance_thread(&self) {
use std::time::Duration;

let node = self.node.clone();
Interval::new(12000, move || {

set_timer_interval(Duration::from_secs(12), move || {
let node = node.clone();
spawn_local(async move {
spawn(async move {
let res = node.write().await.advance().await;
if let Err(err) = res {
warn!("consensus error: {}", err);
}
});
})
.forget();
});
}

async fn boot_from_fallback(&self) -> eyre::Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions client/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::Duration;

use ethers::prelude::{Address, U256};
use ethers::types::{
FeeHistory, Filter, Log, SyncProgress, SyncingStatus, Transaction, TransactionReceipt, H256,
use ethers_core::types::{
Address, FeeHistory, Filter, Log, SyncProgress, SyncingStatus, Transaction, TransactionReceipt,
H256, U256,
};
use eyre::{eyre, Result};

Expand Down
2 changes: 1 addition & 1 deletion client/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethers::{
use ethers_core::{
abi::AbiEncode,
types::{Address, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256, U256},
};
Expand Down
4 changes: 3 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
ethers = "2.0.2"
ethers-core = "2.0.8"
thiserror = "1.0.37"
ic-cdk = "0.10.0"
ic-http = { path = "../ic-http" }
15 changes: 14 additions & 1 deletion common/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ethers::types::H256;
use ethers_core::types::H256;
use ic_cdk::api::call::RejectionCode;
use thiserror::Error;

use crate::types::BlockTag;
Expand Down Expand Up @@ -42,3 +43,15 @@ impl<E: ToString> RpcError<E> {
}
}
}

#[derive(Debug, Error)]
pub enum HttpError {
#[error("canister call error: rejection code: {0:?}, message: {1}")]
CanisterCall(RejectionCode, String),
}

impl From<(RejectionCode, String)> for HttpError {
fn from(value: (RejectionCode, String)) -> Self {
HttpError::CanisterCall(value.0, value.1)
}
}
9 changes: 9 additions & 0 deletions common/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::errors::HttpError;
use ic_cdk::api::management_canister::http_request::HttpResponse;

pub async fn http_get(url: &str) -> Result<HttpResponse, HttpError> {
let req = ic_http::create_request().get(url).build();
// TODO: should we pass cycles in http_get method or we should have a default one?
zvolin marked this conversation as resolved.
Show resolved Hide resolved
let resp = ic_http::http_request(req, 2_603_101_200).await?;
Ok(resp.0)
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod errors;
pub mod http;
pub mod types;
pub mod utils;
2 changes: 1 addition & 1 deletion common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethers::prelude::Address;
use ethers_core::types::Address;
use eyre::Result;
use ssz_rs::{Node, Vector};

Expand Down
7 changes: 3 additions & 4 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[package]
name = "config"
version = "0.4.1"
Expand All @@ -9,13 +8,13 @@ eyre = "0.6.8"
serde = { version = "1.0.143", features = ["derive"] }
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
ethers = "2.0.2"
ethers-core = { version = "2.0.8", default-features = false }
figment = { version = "0.10.7", features = ["toml", "env"] }
thiserror = "1.0.37"
log = "0.4.17"
reqwest = "0.11.13"
serde_yaml = "0.9.14"
strum = "0.24.1"
serde_json = "1.0.103"
strum = { version = "0.24.1", features = ["derive"] }
futures = "0.3.23"

common = { path = "../common" }
Expand Down
18 changes: 8 additions & 10 deletions config/src/checkpoints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use ethers::types::H256;
use common::http::http_get;
use ethers_core::types::H256;
use serde::{Deserialize, Serialize};

use crate::networks;
Expand Down Expand Up @@ -87,9 +88,8 @@ impl CheckpointFallback {
/// The list is defined in [ethPandaOps/checkpoint-fallback-service](https://github.com/ethpandaops/checkpoint-sync-health-checks/blob/master/_data/endpoints.yaml).
pub async fn build(mut self) -> eyre::Result<Self> {
// Fetch the services
let client = reqwest::Client::new();
let res = client.get(CHECKPOINT_SYNC_SERVICES_LIST).send().await?;
let yaml = res.text().await?;
let resp = http_get(CHECKPOINT_SYNC_SERVICES_LIST).await?;
let yaml = String::from_utf8(resp.body)?;

// Parse the yaml content results.
let list: serde_yaml::Value = serde_yaml::from_str(&yaml)?;
Expand Down Expand Up @@ -122,10 +122,9 @@ impl CheckpointFallback {
}

async fn query_service(endpoint: &str) -> Option<RawSlotResponse> {
let client = reqwest::Client::new();
let constructed_url = Self::construct_url(endpoint);
let res = client.get(&constructed_url).send().await.ok()?;
let raw: RawSlotResponse = res.json().await.ok()?;
let resp = http_get(&constructed_url).await.ok()?;
let raw: RawSlotResponse = serde_json::from_slice(&resp.body).ok()?;
Some(raw)
}

Expand Down Expand Up @@ -199,10 +198,9 @@ impl CheckpointFallback {
/// service api url.
pub async fn fetch_checkpoint_from_api(url: &str) -> eyre::Result<H256> {
// Fetch the url
let client = reqwest::Client::new();
let constructed_url = Self::construct_url(url);
let res = client.get(constructed_url).send().await?;
let raw: RawSlotResponse = res.json().await?;
let resp = http_get(&constructed_url).await?;
let raw: RawSlotResponse = serde_json::from_slice(&resp.body)?;
let slot = raw.data.slots[0].clone();
slot.block_root
.ok_or_else(|| eyre::eyre!("Checkpoint not in returned slot"))
Expand Down
2 changes: 1 addition & 1 deletion config/tests/checkpoints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use config::networks;
use ethers::types::H256;
use ethers_core::types::H256;

#[tokio::test]
async fn test_checkpoint_fallback() {
Expand Down
4 changes: 1 addition & 3 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ serde_json = "1.0.85"
hex = "0.4.3"
ssz-rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "d09f55b4f8554491e3431e01af1c32347a8781cd" }
milagro_bls = { git = "https://github.com/Snowfork/milagro_bls" }
ethers = "2.0.2"
ethers-core = { version = "2.0.8", default-features = false }
bytes = "1.2.1"
toml = "0.5.9"
async-trait = "0.1.57"
log = "0.4.17"
chrono = "0.4.23"
thiserror = "1.0.37"
reqwest = { version = "0.11.13", features = ["json"] }
superstruct = "0.7.0"

common = { path = "../common" }
Expand All @@ -30,4 +29,3 @@ tokio = { version = "1", features = ["full"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-timer = "0.2.5"
oblique marked this conversation as resolved.
Show resolved Hide resolved

71 changes: 25 additions & 46 deletions consensus/src/rpc/nimbus_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::ConsensusRpc;
use crate::constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES;
use crate::types::*;
use common::errors::RpcError;
use common::http::http_get;

#[derive(Debug)]
pub struct NimbusRpc {
Expand All @@ -28,15 +29,7 @@ impl ConsensusRpc for NimbusRpc {
self.rpc, root_hex
);

let client = reqwest::Client::new();
let res = client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("bootstrap", e))?
.json::<BootstrapResponse>()
.await
.map_err(|e| RpcError::new("bootstrap", e))?;
let res: BootstrapResponse = rpc_request("bootstrap", req).await?;

Ok(res.data)
}
Expand All @@ -48,64 +41,32 @@ impl ConsensusRpc for NimbusRpc {
self.rpc, period, count
);

let client = reqwest::Client::new();
let res = client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("updates", e))?
.json::<UpdateResponse>()
.await
.map_err(|e| RpcError::new("updates", e))?;
let res: UpdateResponse = rpc_request("updates", req).await?;

Ok(res.into_iter().map(|d| d.data).collect())
}

async fn get_finality_update(&self) -> Result<FinalityUpdate> {
let req = format!("{}/eth/v1/beacon/light_client/finality_update", self.rpc);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new("finality_update", e))?
.json::<FinalityUpdateResponse>()
.await
.map_err(|e| RpcError::new("finality_update", e))?;

let res: FinalityUpdateResponse = rpc_request("finality_update", req).await?;
Ok(res.data)
}

async fn get_optimistic_update(&self) -> Result<OptimisticUpdate> {
let req = format!("{}/eth/v1/beacon/light_client/optimistic_update", self.rpc);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new("optimistic_update", e))?
.json::<OptimisticUpdateResponse>()
.await
.map_err(|e| RpcError::new("optimistic_update", e))?;

let res: OptimisticUpdateResponse = rpc_request("optimistic_update", req).await?;
Ok(res.data)
}

async fn get_block(&self, slot: u64) -> Result<BeaconBlock> {
let req = format!("{}/eth/v2/beacon/blocks/{}", self.rpc, slot);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new("blocks", e))?
.json::<BeaconBlockResponse>()
.await
.map_err(|e| RpcError::new("blocks", e))?;

let res: BeaconBlockResponse = rpc_request("blocks", req).await?;
Ok(res.data.message)
}

async fn chain_id(&self) -> Result<u64> {
let req = format!("{}/eth/v1/config/spec", self.rpc);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new("spec", e))?
.json::<SpecResponse>()
.await
.map_err(|e| RpcError::new("spec", e))?;

let res: SpecResponse = rpc_request("spec", req).await?;
Ok(res.data.chain_id)
}
}
Expand Down Expand Up @@ -152,3 +113,21 @@ struct Spec {
#[serde(rename = "DEPOSIT_NETWORK_ID", deserialize_with = "u64_deserialize")]
chain_id: u64,
}

async fn rpc_request<T>(name: impl AsRef<str>, url: impl AsRef<str>) -> Result<T>
where
T: serde::de::DeserializeOwned,
{
let name = name.as_ref();
let url = url.as_ref();
let resp = http_get(url).await.map_err(|e| RpcError::new(name, e))?;

if resp.status != 200 {
let e = format!("http response with status {}", resp.status);
Err(RpcError::new(name, e))?;
}

let value = serde_json::from_slice(&resp.body).map_err(|e| RpcError::new(name, e))?;

Ok(value)
}
2 changes: 1 addition & 1 deletion consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ where
D: serde::Deserializer<'de>,
{
let val: String = serde::Deserialize::deserialize(deserializer)?;
let x = ethers::types::U256::from_dec_str(&val).map_err(D::Error::custom)?;
let x = ethers_core::types::U256::from_dec_str(&val).map_err(D::Error::custom)?;
let mut x_bytes = [0; 32];
x.to_little_endian(&mut x_bytes);
Ok(U256::from_bytes_le(x_bytes))
Expand Down
Loading