fix: use updated consensus layer light client api (#68)

* update consensus api

* update mainnet checkpoint
This commit is contained in:
Noah Citron 2022-10-13 13:59:37 -04:00 committed by GitHub
parent 98420e5d63
commit 4757fa06ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 34 deletions

View File

@ -15,13 +15,17 @@ impl BlockNotFoundError {
}
#[derive(Debug, Error)]
#[error("rpc error: {message}")]
#[error("rpc error on method: {method}, message: {message}")]
pub struct RpcError {
method: String,
message: String,
}
impl RpcError {
pub fn new(message: String) -> Self {
Self { message }
pub fn new<E: ToString>(method: &str, err: E) -> Self {
Self {
method: method.to_string(),
message: err.to_string(),
}
}
}

View File

@ -18,7 +18,7 @@ pub struct BaseConfig {
pub fn mainnet() -> BaseConfig {
BaseConfig {
checkpoint: hex_str_to_bytes(
"0x5ca31c7c795d8f2de2e844718cdb08835639c644365427b9f20f82083e7dac9a",
"0xb0fe0394e2ed1a96b758b67fad1942371db5f7070502d10937fa65dacfc2ea4b",
)
.unwrap(),
rpc_port: 8545,

View File

@ -20,56 +20,56 @@ impl Rpc for NimbusRpc {
async fn get_bootstrap(&self, block_root: &Vec<u8>) -> Result<Bootstrap> {
let root_hex = hex::encode(block_root);
let req = format!(
"{}/eth/v0/beacon/light_client/bootstrap/0x{}",
"{}/eth/v1/beacon/light_client/bootstrap/0x{}",
self.rpc, root_hex
);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new(e.to_string()))?
.map_err(|e| RpcError::new("bootstrap", e))?
.json::<BootstrapResponse>()
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("bootstrap", e))?;
Ok(res.data.v)
Ok(res.data)
}
async fn get_updates(&self, period: u64) -> Result<Vec<Update>> {
let req = format!(
"{}/eth/v0/beacon/light_client/updates?start_period={}&count=1000",
"{}/eth/v1/beacon/light_client/updates?start_period={}&count=1000",
self.rpc, period
);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new(e.to_string()))?
.map_err(|e| RpcError::new("updates", e))?
.json::<UpdateResponse>()
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("updates", e))?;
Ok(res.data)
Ok(res.iter().map(|d| d.data.clone()).collect())
}
async fn get_finality_update(&self) -> Result<FinalityUpdate> {
let req = format!("{}/eth/v0/beacon/light_client/finality_update", self.rpc);
let req = format!("{}/eth/v1/beacon/light_client/finality_update", self.rpc);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new(e.to_string()))?
.map_err(|e| RpcError::new("finality_update", e))?
.json::<FinalityUpdateResponse>()
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("finality_update", e))?;
Ok(res.data)
}
async fn get_optimistic_update(&self) -> Result<OptimisticUpdate> {
let req = format!("{}/eth/v0/beacon/light_client/optimistic_update", self.rpc);
let req = format!("{}/eth/v1/beacon/light_client/optimistic_update", self.rpc);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new(e.to_string()))?
.map_err(|e| RpcError::new("optimistic_update", e))?
.json::<OptimisticUpdateResponse>()
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("optimistic_update", e))?;
Ok(res.data)
}
@ -78,10 +78,10 @@ impl Rpc for NimbusRpc {
let req = format!("{}/eth/v2/beacon/blocks/{}", self.rpc, slot);
let res = reqwest::get(req)
.await
.map_err(|e| RpcError::new(e.to_string()))?
.map_err(|e| RpcError::new("blocks", e))?
.json::<BeaconBlockResponse>()
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("blocks", e))?;
Ok(res.data.message)
}
@ -97,9 +97,11 @@ struct BeaconBlockData {
message: BeaconBlock,
}
type UpdateResponse = Vec<UpdateData>;
#[derive(serde::Deserialize, Debug)]
struct UpdateResponse {
data: Vec<Update>,
struct UpdateData {
data: Update,
}
#[derive(serde::Deserialize, Debug)]
@ -114,10 +116,5 @@ struct OptimisticUpdateResponse {
#[derive(serde::Deserialize, Debug)]
struct BootstrapResponse {
data: BootstrapData,
}
#[derive(serde::Deserialize, Debug)]
struct BootstrapData {
v: Bootstrap,
data: Bootstrap,
}

View File

@ -53,7 +53,7 @@ impl Rpc for HttpRpc {
.provider
.get_proof(*address, slots.to_vec(), block)
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("get_proof", e))?;
Ok(proof_response)
}
@ -78,7 +78,7 @@ impl Rpc for HttpRpc {
.provider
.create_access_list(&tx, block)
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("create_access_list", e))?;
Ok(list.access_list)
}
@ -89,7 +89,7 @@ impl Rpc for HttpRpc {
.provider
.get_code(*address, block)
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("get_code", e))?;
Ok(code.to_vec())
}
@ -100,7 +100,7 @@ impl Rpc for HttpRpc {
.provider
.send_raw_transaction(bytes)
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("send_raw_transaction", e))?;
Ok(tx.tx_hash())
}
@ -110,12 +110,16 @@ impl Rpc for HttpRpc {
.provider
.get_transaction_receipt(*tx_hash)
.await
.map_err(|e| RpcError::new(e.to_string()))?;
.map_err(|e| RpcError::new("get_transaction_receipt", e))?;
Ok(receipt)
}
async fn get_transaction(&self, tx_hash: &H256) -> Result<Option<Transaction>> {
Ok(self.provider.get_transaction(*tx_hash).await?)
Ok(self
.provider
.get_transaction(*tx_hash)
.await
.map_err(|e| RpcError::new("get_transaction", e))?)
}
}