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)] #[derive(Debug, Error)]
#[error("rpc error: {message}")] #[error("rpc error on method: {method}, message: {message}")]
pub struct RpcError { pub struct RpcError {
method: String,
message: String, message: String,
} }
impl RpcError { impl RpcError {
pub fn new(message: String) -> Self { pub fn new<E: ToString>(method: &str, err: E) -> Self {
Self { message } Self {
method: method.to_string(),
message: err.to_string(),
}
} }
} }

View File

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

View File

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

View File

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