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

refactor(relayer, ethereum-client): A bunch of small refactorings to relayer and ethereum-client #103

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions ethereum/client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use alloy::transports::{RpcError, TransportErrorKind};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Not implemented")]
NotImplemented,
#[error("Error in HTTP transport")]
ErrorInHTTPTransport,
#[error("Error in HTTP transport: {0}")]
ErrorInHTTPTransport(RpcError<TransportErrorKind>),
#[error("Wrong address")]
WrongAddress,
#[error("Wrong node URL")]
Expand All @@ -16,6 +15,8 @@ pub enum Error {
ErrorDuringContractExecution(alloy::contract::Error),
#[error("Error sending transaction: {0}")]
ErrorSendingTransaction(alloy::contract::Error),
#[error("Error querying event: {0}")]
ErrorQueryingEvent(alloy::contract::Error),
#[error("Error waiting transaction receipt")]
ErrorWaitingTransactionReceipt,
#[error("Error fetching transaction")]
Expand All @@ -24,10 +25,10 @@ pub enum Error {
ErrorFetchingTransactionReceipt,
#[error("Error fetching block")]
ErrorFetchingBlock,
#[error("Wrong path to file")]
WrongPathToFile,
#[error("Wrong JSON format")]
WrongJsonFormation,
#[error("Cannot convert to U256")]
UnableToConvertToU256,
}

impl From<RpcError<TransportErrorKind>> for Error {
fn from(value: RpcError<TransportErrorKind>) -> Self {
Self::ErrorInHTTPTransport(value)
}
}
39 changes: 13 additions & 26 deletions ethereum/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,9 @@ where
}

pub async fn get_approx_balance(&self, address: Address) -> Result<f64, Error> {
let balance = self.provider.get_balance(address).latest().await;

if let Ok(balance) = balance {
let balance: f64 = balance.into();
Ok(balance / 1_000_000_000_000_000_000.0)
} else {
Err(Error::ErrorInHTTPTransport)
}
let balance = self.provider.get_balance(address).latest().await?;
let balance: f64 = balance.into();
Ok(balance / 1_000_000_000_000_000_000.0)
}

pub async fn provide_merkle_root(
Expand Down Expand Up @@ -254,18 +249,11 @@ where
}

pub async fn block_number(&self) -> Result<u64, Error> {
self.provider
.get_block_number()
.await
.map_err(|_| Error::ErrorInHTTPTransport)
self.provider.get_block_number().await.map_err(|e| e.into())
}

pub async fn fetch_merkle_roots(&self, depth: u64) -> Result<Vec<MerkleRootEntry>, Error> {
let current_block: u64 = self
.provider
.get_block_number()
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let current_block: u64 = self.provider.get_block_number().await?;

self.fetch_merkle_roots_in_range(
current_block.checked_sub(depth).unwrap_or_default(),
Expand All @@ -287,15 +275,14 @@ where

let event: Event<T, P, MerkleRoot, Ethereum> = Event::new(self.provider.clone(), filter);

match event.query().await {
Ok(logs) => Ok(logs
.iter()
.map(|(event, _)| MerkleRootEntry {
block_number: event.blockNumber.to(),
})
.collect()),
Err(_) => Err(Error::ErrorInHTTPTransport),
}
let logs = event.query().await.map_err(Error::ErrorQueryingEvent)?;

Ok(logs
.iter()
.map(|(event, _)| MerkleRootEntry {
block_number: event.blockNumber.to(),
})
.collect())
}

#[allow(clippy::too_many_arguments)]
Expand Down
15 changes: 3 additions & 12 deletions ethereum/client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ where
erc20_treasury_proxy: *erc20_treasury_proxy.address(),
};

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let relayer_proxy = ProxyContract::new(deployment.relayer_proxy, provider.clone());

Expand All @@ -158,10 +155,7 @@ where
.await
.map_err(|_| Error::ErrorWaitingTransactionReceipt)?;

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let pending_tx = message_queue_proxy
.upgradeToAndCall(deployment.message_queue, Bytes::new())
Expand All @@ -177,10 +171,7 @@ where
.await
.map_err(|_| Error::ErrorWaitingTransactionReceipt)?;

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let pending_tx = erc20_treasury_proxy
.upgradeToAndCall(deployment.erc20_treasury, Bytes::new())
Expand Down
2 changes: 1 addition & 1 deletion gear-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ impl GearApi {
.api
.rpc()
.request(
"gearBridge_merkleProof",
"gearEthBridge_merkleProof",
rpc_params![message_hash, Some(block)],
)
.await?;
Expand Down
12 changes: 3 additions & 9 deletions relayer/src/proof_storage/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,9 @@ impl GearProofStorage {
) -> anyhow::Result<GearProofStorage> {
let wrapped_gear_api = WrappedGearApi::new(endpoint).await?;

assert_eq!(
&endpoint[..5],
"ws://",
"Invalid endpoint format: expected ws://..."
);

let endpoint: Vec<_> = endpoint[5..].split(':').collect();
let domain = ["ws://", endpoint[0]].concat();
let port = endpoint[1].parse::<u16>()?;
let endpoint: Vec<_> = endpoint.split(':').collect();
let domain = [endpoint[0], ":", endpoint[1]].concat();
let port = endpoint[2].parse::<u16>()?;
let address = WSAddress::try_new(domain, port)?;

let gear_api = GearApi::init_with(address, fee_payer).await?;
Expand Down
8 changes: 7 additions & 1 deletion relayer/src/relay_merkle_roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ impl MerkleRootRelayer {
async fn prove_message_sent(&self) -> anyhow::Result<FinalProof> {
let finalized_head = self.gear_api.latest_finalized_block().await?;

log::info!("Proving merkle root presense in block #{}", finalized_head);
let finalized_block_number = self.gear_api.block_hash_to_number(finalized_head).await?;

log::info!(
"Proving merkle root presense in block #{} with hash {}",
finalized_block_number,
finalized_head
);

let authority_set_id = self
.gear_api
Expand Down
Loading