helios/consensus/src/rpc/nimbus_rpc.rs

151 lines
4.0 KiB
Rust
Raw Normal View History

use async_trait::async_trait;
use common::errors::RpcError;
2022-08-21 15:21:50 +00:00
use eyre::Result;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use std::cmp;
2022-08-21 15:21:50 +00:00
use super::ConsensusRpc;
use crate::constants::MAX_REQUEST_LIGHT_CLIENT_UPDATES;
use crate::types::*;
2022-08-21 15:21:50 +00:00
pub struct NimbusRpc {
2022-08-21 15:21:50 +00:00
rpc: String,
client: ClientWithMiddleware,
2022-08-21 15:21:50 +00:00
}
#[async_trait]
impl ConsensusRpc for NimbusRpc {
fn new(rpc: &str) -> Self {
let retry_policy = ExponentialBackoff::builder()
.backoff_exponent(1)
.build_with_max_retries(3);
let client = ClientBuilder::new(reqwest::Client::new())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
NimbusRpc {
2022-08-21 16:59:47 +00:00
rpc: rpc.to_string(),
client,
2022-08-21 16:59:47 +00:00
}
2022-08-21 15:21:50 +00:00
}
async fn get_bootstrap(&self, block_root: &Vec<u8>) -> Result<Bootstrap> {
2022-08-27 00:05:12 +00:00
let root_hex = hex::encode(block_root);
2022-08-21 15:21:50 +00:00
let req = format!(
"{}/eth/v1/beacon/light_client/bootstrap/0x{}",
2022-08-27 00:05:12 +00:00
self.rpc, root_hex
2022-08-21 15:21:50 +00:00
);
let res = self
.client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("bootstrap", e))?
.json::<BootstrapResponse>()
.await
.map_err(|e| RpcError::new("bootstrap", e))?;
Ok(res.data)
2022-08-21 15:21:50 +00:00
}
async fn get_updates(&self, period: u64, count: u8) -> Result<Vec<Update>> {
let count = cmp::min(count, MAX_REQUEST_LIGHT_CLIENT_UPDATES);
2022-08-21 15:21:50 +00:00
let req = format!(
"{}/eth/v1/beacon/light_client/updates?start_period={}&count={}",
self.rpc, period, count
2022-08-21 15:21:50 +00:00
);
let res = self
.client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("updates", e))?
.json::<UpdateResponse>()
.await
.map_err(|e| RpcError::new("updates", e))?;
Ok(res.iter().map(|d| d.data.clone()).collect())
2022-08-21 15:21:50 +00:00
}
async fn get_finality_update(&self) -> Result<FinalityUpdate> {
let req = format!("{}/eth/v1/beacon/light_client/finality_update", self.rpc);
let res = self
.client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("finality_update", e))?
2022-08-21 15:21:50 +00:00
.json::<FinalityUpdateResponse>()
.await
.map_err(|e| RpcError::new("finality_update", e))?;
2022-08-21 15:21:50 +00:00
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 = self
.client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("optimistic_update", e))?
2022-08-31 00:31:58 +00:00
.json::<OptimisticUpdateResponse>()
.await
.map_err(|e| RpcError::new("optimistic_update", e))?;
2022-08-31 00:31:58 +00:00
Ok(res.data)
}
async fn get_block(&self, slot: u64) -> Result<BeaconBlock> {
2022-08-21 15:21:50 +00:00
let req = format!("{}/eth/v2/beacon/blocks/{}", self.rpc, slot);
let res = self
.client
.get(req)
.send()
.await
.map_err(|e| RpcError::new("blocks", e))?
2022-08-21 15:21:50 +00:00
.json::<BeaconBlockResponse>()
.await
.map_err(|e| RpcError::new("blocks", e))?;
2022-08-21 15:21:50 +00:00
Ok(res.data.message)
}
}
#[derive(serde::Deserialize, Debug)]
struct BeaconBlockResponse {
data: BeaconBlockData,
}
#[derive(serde::Deserialize, Debug)]
struct BeaconBlockData {
message: BeaconBlock,
}
type UpdateResponse = Vec<UpdateData>;
2022-08-21 15:21:50 +00:00
#[derive(serde::Deserialize, Debug)]
struct UpdateData {
data: Update,
2022-08-21 15:21:50 +00:00
}
#[derive(serde::Deserialize, Debug)]
struct FinalityUpdateResponse {
data: FinalityUpdate,
}
2022-08-31 00:31:58 +00:00
#[derive(serde::Deserialize, Debug)]
struct OptimisticUpdateResponse {
data: OptimisticUpdate,
}
2022-08-21 15:21:50 +00:00
#[derive(serde::Deserialize, Debug)]
struct BootstrapResponse {
data: Bootstrap,
2022-08-21 15:21:50 +00:00
}