Skip to content

Commit

Permalink
chore: fixed all clippy errors
Browse files Browse the repository at this point in the history
Refactoring code to resolve `too_many_lines` clippy error will take too much time, so I decided to silence the error for now. All other issues were fixed
  • Loading branch information
frolvanya committed Jan 15, 2025
1 parent b51fb3b commit 6eb9c79
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 155 deletions.
1 change: 1 addition & 0 deletions omni-relayer/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-many-lines-threshold = 200
15 changes: 8 additions & 7 deletions omni-relayer/src/startup/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,19 @@ pub async fn start_indexer(
arb.expected_finalization_time,
)
}
_ => anyhow::bail!("Unsupported chain kind: {:?}", chain_kind),
_ => anyhow::bail!("Unsupported chain kind: {chain_kind:?}"),
};

let http_provider = ProviderBuilder::new().on_http(rpc_http_url.parse().context(format!(
"Failed to parse {:?} rpc provider as url",
chain_kind
"Failed to parse {chain_kind:?} rpc provider as url",
))?);

let ws_provider = ProviderBuilder::new()
.on_ws(WsConnect::new(rpc_ws_url))
.await
.context(format!("Failed to initialize {:?} WS provider", chain_kind))?;
.context(format!("Failed to initialize {chain_kind:?} WS provider"))?;

let last_processed_block_key = utils::redis::get_last_processed_key(chain_kind).await;
let last_processed_block_key = utils::redis::get_last_processed_key(chain_kind);
let latest_block = http_provider.get_block_number().await?;
let from_block = match start_block {
Some(block) => block,
Expand All @@ -100,7 +99,9 @@ pub async fn start_indexer(
.to_vec(),
);

for current_block in (from_block..latest_block).step_by(block_processing_batch_size as usize) {
for current_block in
(from_block..latest_block).step_by(usize::try_from(block_processing_batch_size)?)
{
let logs = http_provider
.get_logs(
&filter
Expand Down Expand Up @@ -201,7 +202,7 @@ async fn process_log(

utils::redis::update_last_processed(
redis_connection,
&utils::redis::get_last_processed_key(chain_kind).await,
&utils::redis::get_last_processed_key(chain_kind),
block_number,
)
.await;
Expand Down
5 changes: 1 addition & 4 deletions omni-relayer/src/startup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ fn build_evm_bridge_client(
.private_key(Some(crate::config::get_private_key(chain_kind)))
.bridge_token_factory_address(Some(evm.bridge_token_factory_address.to_string()))
.build()
.context(format!(
"Failed to build EvmBridgeClient ({:?})",
chain_kind
))
.context(format!("Failed to build EvmBridgeClient ({chain_kind:?})"))
})
.transpose()
}
Expand Down
10 changes: 6 additions & 4 deletions omni-relayer/src/startup/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ async fn create_lake_config(
Some(block) => block,
None => utils::redis::get_last_processed::<&str, u64>(
redis_connection,
&utils::redis::get_last_processed_key(ChainKind::Near).await,
&utils::redis::get_last_processed_key(ChainKind::Near),
)
.await
.map(|block_height| block_height + 1)
.unwrap_or(utils::near::get_final_block(jsonrpc_client).await?),
.map_or(
utils::near::get_final_block(jsonrpc_client).await?,
|block_height| block_height + 1,
),
};

info!("NEAR Lake will start from block: {}", start_block_height);
Expand Down Expand Up @@ -127,7 +129,7 @@ pub async fn start_indexer(

utils::redis::update_last_processed(
&mut redis_connection,
&utils::redis::get_last_processed_key(ChainKind::Near).await,
&utils::redis::get_last_processed_key(ChainKind::Near),
streamer_message.block.header.height,
)
.await;
Expand Down
47 changes: 22 additions & 25 deletions omni-relayer/src/startup/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,28 @@ async fn process_recent_signatures(
program_id: &Pubkey,
start_signature: Option<String>,
) -> Result<()> {
let from_signature = match start_signature {
Some(signature) => {
utils::redis::add_event(
redis_connection,
utils::redis::SOLANA_EVENTS,
signature.clone(),
// TODO: It's better to come up with a solution that wouldn't require storing `Null` value
serde_json::Value::Null,
)
.await;

Signature::from_str(&signature)?
}
None => {
let Some(signature) = utils::redis::get_last_processed::<&str, String>(
redis_connection,
&utils::redis::get_last_processed_key(ChainKind::Sol).await,
)
.await
.and_then(|s| Signature::from_str(&s).ok()) else {
return Ok(());
};

signature
}
let from_signature = if let Some(signature) = start_signature {
utils::redis::add_event(
redis_connection,
utils::redis::SOLANA_EVENTS,
signature.clone(),
// TODO: It's better to come up with a solution that wouldn't require storing `Null` value
serde_json::Value::Null,
)
.await;

Signature::from_str(&signature)?
} else {
let Some(signature) = utils::redis::get_last_processed::<&str, String>(
redis_connection,
&utils::redis::get_last_processed_key(ChainKind::Sol),
)
.await
.and_then(|s| Signature::from_str(&s).ok()) else {
return Ok(());
};

signature
};

let signatures: Vec<RpcConfirmedTransactionStatusWithSignature> = http_client
Expand Down
7 changes: 2 additions & 5 deletions omni-relayer/src/utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,10 @@ pub async fn construct_prover_args(
borsh::to_vec(&evm_proof_args).ok()
}

pub async fn string_to_evm_omniaddress(
chain_kind: ChainKind,
address: String,
) -> Result<OmniAddress> {
pub fn string_to_evm_omniaddress(chain_kind: ChainKind, address: &str) -> Result<OmniAddress> {
OmniAddress::new_from_evm_address(
chain_kind,
H160::from_str(&address)
H160::from_str(address)
.map_err(|err| anyhow::anyhow!("Failed to parse as H160 address: {:?}", err))?,
)
.map_err(|err| anyhow::anyhow!("Failed to parse as EvmOmniAddress address: {:?}", err))
Expand Down
4 changes: 2 additions & 2 deletions omni-relayer/src/utils/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub const SLEEP_TIME_AFTER_EVENTS_PROCESS_SECS: u64 = 10;
const QUERY_RETRY_ATTEMPTS: u64 = 10;
const QUERY_RETRY_SLEEP_SECS: u64 = 1;

pub async fn get_last_processed_key(chain_kind: ChainKind) -> String {
pub fn get_last_processed_key(chain_kind: ChainKind) -> String {
match chain_kind {
ChainKind::Sol => "SOLANA_LAST_PROCESSED_SIGNATURE".to_string(),
_ => format!("{:?}_LAST_PROCESSED_BLOCK", chain_kind),
_ => format!("{chain_kind:?}_LAST_PROCESSED_BLOCK"),
}
}

Expand Down
6 changes: 3 additions & 3 deletions omni-relayer/src/utils/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn process_message(
let account_keys = instruction
.accounts
.into_iter()
.map(|i| message.account_keys.get(i as usize).cloned())
.map(|i| message.account_keys.get(usize::from(i)).cloned())
.collect::<Vec<_>>();

if let Err(err) = decode_instruction(
Expand Down Expand Up @@ -120,7 +120,7 @@ async fn decode_instruction(
let Some(sequence) = log
.split_ascii_whitespace()
.last()
.map(|sequence| sequence.to_string())
.map(std::string::ToString::to_string)
else {
warn!("Failed to parse sequence number from log: {:?}", log);
continue;
Expand Down Expand Up @@ -184,7 +184,7 @@ async fn decode_instruction(
let Some(sequence) = log
.split_ascii_whitespace()
.last()
.map(|sequence| sequence.to_string())
.map(std::string::ToString::to_string)
else {
warn!("Failed to parse sequence number from log: {:?}", log);
continue;
Expand Down
37 changes: 7 additions & 30 deletions omni-relayer/src/utils/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,28 @@ async fn get_token_id(
let omni_token_address = match chain_kind {
ChainKind::Near => {
let token = AccountId::from_str(token_address).map_err(|_| {
format!(
"Failed to parse token address as AccountId: {:?}",
token_address
)
format!("Failed to parse token address as AccountId: {token_address:?}",)
})?;
Ok(OmniAddress::Near(token))
}
ChainKind::Eth | ChainKind::Base | ChainKind::Arb => {
utils::evm::string_to_evm_omniaddress(chain_kind, token_address.to_string())
.await
utils::evm::string_to_evm_omniaddress(chain_kind, token_address)
.map_err(|err| err.to_string())
}
ChainKind::Sol => {
let token = Pubkey::from_str(token_address).map_err(|_| {
format!(
"Failed to parse token address as Pubkey: {:?}",
token_address
)
format!("Failed to parse token address as Pubkey: {token_address:?}",)
})?;
OmniAddress::new_from_slice(ChainKind::Sol, &token.to_bytes())
}
}
.map_err(|_| {
format!(
"Failed to convert token address to OmniAddress: {:?}",
token_address
)
})?;
.map_err(|_| format!("Failed to convert token address to OmniAddress: {token_address:?}",))?;

let token_id = connector
.near_get_token_id(omni_token_address.clone())
.await
.map_err(|_| {
format!(
"Failed to get token id by omni token address: {:?}",
omni_token_address
)
format!("Failed to get token id by omni token address: {omni_token_address:?}",)
})?;

Ok(token_id)
Expand All @@ -69,10 +54,7 @@ async fn add_storage_deposit_action(
.near_get_required_storage_deposit(token_id.clone(), account_id.clone())
.await
.map_err(|_| {
format!(
"Failed to get required storage deposit for account: {:?}",
account_id
)
format!("Failed to get required storage deposit for account: {account_id:?}",)
})? {
amount if amount > 0 => Some(amount),
_ => None,
Expand Down Expand Up @@ -124,12 +106,7 @@ pub async fn get_storage_deposit_actions(
let token_id = connector
.near_get_native_token_id(chain_kind)
.await
.map_err(|_| {
format!(
"Failed to get native token id by chain kind: {:?}",
chain_kind
)
})?;
.map_err(|_| format!("Failed to get native token id by chain kind: {chain_kind:?}",))?;

let relayer = connector
.near_bridge_client()
Expand Down
65 changes: 24 additions & 41 deletions omni-relayer/src/workers/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ pub async fn finalize_transfer(
loop {
let mut redis_connection = redis_connection.clone();

let events = match utils::redis::get_events(
let Some(events) = utils::redis::get_events(
&mut redis_connection,
utils::redis::EVM_INIT_TRANSFER_EVENTS.to_string(),
)
.await
{
Some(events) => events,
None => {
tokio::time::sleep(tokio::time::Duration::from_secs(
utils::redis::SLEEP_TIME_AFTER_EVENTS_PROCESS_SECS,
))
.await;
continue;
}
else {
tokio::time::sleep(tokio::time::Duration::from_secs(
utils::redis::SLEEP_TIME_AFTER_EVENTS_PROCESS_SECS,
))
.await;
continue;
};

let mut handlers = Vec::new();
Expand Down Expand Up @@ -137,10 +134,8 @@ async fn handle_init_transfer_event(
{
let sender = match utils::evm::string_to_evm_omniaddress(
init_transfer_with_timestamp.chain_kind,
init_log.inner.sender.to_string(),
)
.await
{
&init_log.inner.sender.to_string(),
) {
Ok(sender) => sender,
Err(err) => {
warn!("{}", err);
Expand All @@ -150,10 +145,8 @@ async fn handle_init_transfer_event(

let token = match utils::evm::string_to_evm_omniaddress(
init_transfer_with_timestamp.chain_kind,
init_log.inner.tokenAddress.to_string(),
)
.await
{
&init_log.inner.tokenAddress.to_string(),
) {
Ok(token) => token,
Err(err) => {
warn!("{}", err);
Expand Down Expand Up @@ -198,16 +191,12 @@ async fn handle_init_transfer_event(

if vaa.is_none() {
if init_transfer_with_timestamp.chain_kind == ChainKind::Eth {
let light_client_latest_block_number =
match utils::near::get_eth_light_client_last_block_number(&config, &jsonrpc_client)
.await
{
Ok(block_number) => block_number,
Err(_) => {
warn!("Failed to get eth light client last block number");
return;
}
};
let Ok(light_client_latest_block_number) =
utils::near::get_eth_light_client_last_block_number(&config, &jsonrpc_client).await
else {
warn!("Failed to get eth light client last block number");
return;
};

if init_transfer_with_timestamp.block_number > light_client_latest_block_number {
warn!("ETH light client is not synced yet");
Expand All @@ -227,30 +216,24 @@ async fn handle_init_transfer_event(
}
}

let topic = match init_transfer_with_timestamp.log.topic0() {
Some(topic) => topic,
None => {
warn!("No topic0 in log: {:?}", init_transfer_with_timestamp.log);
return;
}
let Some(topic) = init_transfer_with_timestamp.log.topic0() else {
warn!("No topic0 in log: {:?}", init_transfer_with_timestamp.log);
return;
};

let tx_hash = H256::from_slice(tx_hash.as_slice());

let prover_args = match utils::evm::construct_prover_args(
let Some(prover_args) = utils::evm::construct_prover_args(
&config,
vaa,
tx_hash,
H256::from_slice(topic.as_slice()),
ProofKind::InitTransfer,
)
.await
{
Some(prover_args) => prover_args,
None => {
warn!("Failed to construct prover args");
return;
}
else {
warn!("Failed to construct prover args");
return;
};

let storage_deposit_actions = match utils::storage::get_storage_deposit_actions(
Expand Down
Loading

0 comments on commit 6eb9c79

Please sign in to comment.