diff --git a/apps/indexer/src/build.rs b/apps/indexer/src/build.rs index b176f383..2af6aca0 100644 --- a/apps/indexer/src/build.rs +++ b/apps/indexer/src/build.rs @@ -21,7 +21,8 @@ fn main() { } else { match std::fs::read_to_string(path) { Ok(res) => res, - Err(_) => env::var("GIT_HASH").expect("Failed to retrieve git version from file or environnment") + Err(_) => env::var("GIT_HASH") + .expect("Failed to retrieve git version from file or environnment"), } }; diff --git a/apps/indexer/src/config.rs b/apps/indexer/src/config.rs index 27e04b08..06b53bff 100644 --- a/apps/indexer/src/config.rs +++ b/apps/indexer/src/config.rs @@ -16,7 +16,7 @@ pub struct ChainConfig { /// For auto withdraw pub account_address: Option, pub account_private_key: Option, - #[serde(default="cooling_down_default")] + #[serde(default = "cooling_down_default")] pub cooling_down: u64, } diff --git a/apps/indexer/src/ethereum_indexer/client.rs b/apps/indexer/src/ethereum_indexer/client.rs index cad5b61b..fcbb15d6 100644 --- a/apps/indexer/src/ethereum_indexer/client.rs +++ b/apps/indexer/src/ethereum_indexer/client.rs @@ -48,7 +48,10 @@ impl EthereumClient { pub async fn new(config: ChainConfig) -> Result { let provider = Provider::::try_from(&config.rpc_url)?; - let chain_id = provider.get_chainid().await.expect("Failed to retrieve ChainId"); + let chain_id = provider + .get_chainid() + .await + .expect("Failed to retrieve ChainId"); let provider_signer = if let Some(pk) = &config.account_private_key { let wallet: LocalWallet = pk.parse::()?.with_chain_id(chain_id.as_u32()); @@ -85,24 +88,25 @@ impl EthereumClient { /// pub async fn get_block_number(&self) -> Result { - - match self.provider - .get_block_number() - .await { - Ok(v) => Ok(v.try_into().unwrap()), - Err(e) => Err(anyhow!("{:?}", e)), - } + match self.provider.get_block_number().await { + Ok(v) => Ok(v.try_into().unwrap()), + Err(e) => Err(anyhow!("{:?}", e)), + } } pub async fn get_block_timestamp(&self, block_id: u64) -> u64 { - match self.provider + match self + .provider .get_block(block_id) .await - .expect(&format!("Can't fetch block {}", block_id)) { - None => 0, - Some(block) => block.timestamp.try_into().expect("Can't convert block timestamp to u64") + .expect(&format!("Can't fetch block {}", block_id)) + { + None => 0, + Some(block) => block + .timestamp + .try_into() + .expect("Can't convert block timestamp to u64"), } - } /// Fetches logs for the given block options. /// diff --git a/apps/indexer/src/ethereum_indexer/events.rs b/apps/indexer/src/ethereum_indexer/events.rs index 37433046..c3be194e 100644 --- a/apps/indexer/src/ethereum_indexer/events.rs +++ b/apps/indexer/src/ethereum_indexer/events.rs @@ -1,4 +1,7 @@ -use crate::{storage::{BridgeChain, CrossChainTx, CrossChainTxKind, Event, EventLabel, Request}, utils::normalize_hex}; +use crate::{ + storage::{BridgeChain, CrossChainTx, CrossChainTxKind, Event, EventLabel, Request}, + utils::normalize_hex, +}; use anyhow::{anyhow, Result}; use ethers::prelude::*; use serde_json::{json, Value}; diff --git a/apps/indexer/src/ethereum_indexer/indexer.rs b/apps/indexer/src/ethereum_indexer/indexer.rs index 26d812c1..53bdbb96 100644 --- a/apps/indexer/src/ethereum_indexer/indexer.rs +++ b/apps/indexer/src/ethereum_indexer/indexer.rs @@ -93,12 +93,12 @@ where Ok(_) => (), Err(e) => { log::error!( - "Error processing logs for block {:?}\n{:?}", - block_number, - e - ); - need_cool_down = true; - }, + "Error processing logs for block {:?}\n{:?}", + block_number, + e + ); + need_cool_down = true; + } }; } diff --git a/apps/indexer/src/handlers/requests.rs b/apps/indexer/src/handlers/requests.rs index 8b3a06e5..0eda43ba 100644 --- a/apps/indexer/src/handlers/requests.rs +++ b/apps/indexer/src/handlers/requests.rs @@ -3,16 +3,18 @@ use axum::{ http::StatusCode, Json, }; -use serde::{Deserialize, Serialize}; use serde::ser::SerializeStruct; +use serde::{Deserialize, Serialize}; use super::AppState; -use crate::{storage::{ - protocol::ProtocolParser, - store::{EventStore, RequestStore}, - Event, Request, -}, utils::{denormalize_hex, normalize_hex}}; - +use crate::{ + storage::{ + protocol::ProtocolParser, + store::{EventStore, RequestStore}, + Event, Request, + }, + utils::{denormalize_hex, normalize_hex}, +}; #[derive(Debug, Deserialize)] pub struct RequestWrapper(pub Request); @@ -20,22 +22,35 @@ pub struct RequestWrapper(pub Request); impl Serialize for RequestWrapper { fn serialize(&self, serializer: S) -> Result where - S: serde::Serializer { + S: serde::Serializer, + { let RequestWrapper(ref inner) = *self; let mut state = serializer.serialize_struct("Request", 7)?; state.serialize_field("hash", &inner.hash)?; state.serialize_field("chain_src", &inner.chain_src)?; - state.serialize_field("from", &denormalize_hex(&inner.from).expect("Failed to denormalize 'from'"))?; - state.serialize_field("to", &denormalize_hex(&inner.to).expect("Failed to denormalize 'to'"))?; - state.serialize_field("collection_src", &denormalize_hex(&inner.collection_src).expect("Failed to denormalize 'collection_src'"))?; - state.serialize_field("collection_dst", &denormalize_hex(&inner.collection_dst).expect("Failed to denormalize 'collection_dst'"))?; + state.serialize_field( + "from", + &denormalize_hex(&inner.from).expect("Failed to denormalize 'from'"), + )?; + state.serialize_field( + "to", + &denormalize_hex(&inner.to).expect("Failed to denormalize 'to'"), + )?; + state.serialize_field( + "collection_src", + &denormalize_hex(&inner.collection_src) + .expect("Failed to denormalize 'collection_src'"), + )?; + state.serialize_field( + "collection_dst", + &denormalize_hex(&inner.collection_dst) + .expect("Failed to denormalize 'collection_dst'"), + )?; state.serialize_field("content", &inner.content)?; state.end() } } - - #[derive(Debug, Serialize, Deserialize)] pub struct RequestInfo { req: RequestWrapper, diff --git a/apps/indexer/src/starknet_indexer/indexer.rs b/apps/indexer/src/starknet_indexer/indexer.rs index 83e0d0ec..513de0f9 100644 --- a/apps/indexer/src/starknet_indexer/indexer.rs +++ b/apps/indexer/src/starknet_indexer/indexer.rs @@ -3,8 +3,11 @@ use super::events; use crate::config::ChainConfig; use crate::storage::protocol::ProtocolParser; use crate::storage::{ - store::{BlockStore, CrossChainTxStore, EventStore, PendingWithdrawStore, RequestStore, StarknetBridgeRequestStore}, - BlockIndex, BridgeChain, CrossChainTxKind, EventLabel, PendingWithdraw + store::{ + BlockStore, CrossChainTxStore, EventStore, PendingWithdrawStore, RequestStore, + StarknetBridgeRequestStore, + }, + BlockIndex, BridgeChain, CrossChainTxKind, EventLabel, PendingWithdraw, }; use crate::utils; use crate::ChainsBlocks; @@ -28,7 +31,12 @@ pub struct StarknetIndexer< impl StarknetIndexer where - T: RequestStore + EventStore + BlockStore + CrossChainTxStore + StarknetBridgeRequestStore + PendingWithdrawStore, + T: RequestStore + + EventStore + + BlockStore + + CrossChainTxStore + + StarknetBridgeRequestStore + + PendingWithdrawStore, { /// pub async fn new( @@ -111,14 +119,15 @@ where let latest_u64 = match self .client .block_id_to_u64(&BlockId::Tag(BlockTag::Latest)) - .await { - Ok(v) => v, - Err(e) => { - log::error!("Failed to retrieve blockid: {:#}", e); - need_cool_down = true; - continue; - } - }; + .await + { + Ok(v) => v, + Err(e) => { + log::error!("Failed to retrieve blockid: {:#}", e); + need_cool_down = true; + continue; + } + }; // Don't fetch if we already are on the head of the chain. if from_u64 >= latest_u64 { @@ -129,14 +138,20 @@ where let blocks_events = match self .client .fetch_events(BlockId::Number(from_u64), BlockId::Number(latest_u64)) - .await { - Ok(r) => r, - Err(e) => { - log::error!("Failed to fetch events for block ({:#}-{:#}): {:#}", from_u64, latest_u64, e); - need_cool_down = true; - continue; - } - }; + .await + { + Ok(r) => r, + Err(e) => { + log::error!( + "Failed to fetch events for block ({:#}-{:#}): {:#}", + from_u64, + latest_u64, + e + ); + need_cool_down = true; + continue; + } + }; // log::debug!("blocks events: {:?}", blocks_events); @@ -144,7 +159,11 @@ where match self.process_events(block_number, events).await { Ok(_) => (), Err(e) => { - log::error!("Failed to process events for block {:#}: {:#}", block_number, e); + log::error!( + "Failed to process events for block {:#}: {:#}", + block_number, + e + ); need_cool_down = true; continue; } @@ -205,12 +224,13 @@ where } if ev.label == EventLabel::WithdrawCompletedL2 { - self.store.insert_request(ev.tx_hash.clone(), req.clone()).await?; + self.store + .insert_request(ev.tx_hash.clone(), req.clone()) + .await?; } if ev.label == EventLabel::DepositInitiatedL2 { - self - .store + self.store .insert_pending_withdraw(PendingWithdraw { req_hash: req.clone().hash, tx_hash: ev.tx_hash, diff --git a/apps/indexer/src/storage/mongo/mod.rs b/apps/indexer/src/storage/mongo/mod.rs index 55d749c1..f283126b 100644 --- a/apps/indexer/src/storage/mongo/mod.rs +++ b/apps/indexer/src/storage/mongo/mod.rs @@ -1,5 +1,5 @@ -use super::StarknetBridgeRequest; use super::PendingWithdraw; +use super::StarknetBridgeRequest; use crate::storage::{ BlockIndex, BridgeChain, CrossChainTx, CrossChainTxKind, Event, EventLabel, Request, @@ -7,7 +7,6 @@ use crate::storage::{ use anyhow::Result; use mongodb::{bson::Bson, options::ClientOptions, Client, Collection}; - mod block_store; mod event_store; mod pending_withdraw_store; diff --git a/apps/indexer/src/utils.rs b/apps/indexer/src/utils.rs index 710263e8..dbb3ad4e 100644 --- a/apps/indexer/src/utils.rs +++ b/apps/indexer/src/utils.rs @@ -49,16 +49,22 @@ mod tests { pub fn test_normalize_hex() { let s = "0x123456"; let r = normalize_hex(s).unwrap(); - assert_eq!(r, "0x0000000000000000000000000000000000000000000000000000000000123456"); + assert_eq!( + r, + "0x0000000000000000000000000000000000000000000000000000000000123456" + ); let s = "0x1212121212121212121212121212121212121212121212121212121212121212"; let r = normalize_hex(s).unwrap(); assert_eq!(r, s); - + // support ethereum checksum address let s = "0x8c7173Db918EB0f015ba2D319E94e1EaB95c63fb"; let r = normalize_hex(s).unwrap(); - assert_eq!(r, "0x0000000000000000000000008c7173db918eb0f015ba2d319e94e1eab95c63fb"); + assert_eq!( + r, + "0x0000000000000000000000008c7173db918eb0f015ba2d319e94e1eab95c63fb" + ); } #[test] @@ -70,7 +76,7 @@ mod tests { let s = "0x1212121212121212121212121212121212121212121212121212121212121212"; let r = denormalize_hex(s).unwrap(); assert_eq!(r, s); - + // support ethereum checksum address let s = "0x0000000000000000000000008c7173db918eb0f015ba2d319e94e1eab95c63fb"; let r = denormalize_hex(s).unwrap();