Skip to content

Commit

Permalink
refactor: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Apr 12, 2024
1 parent 4d9dcad commit 3626a59
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 51 deletions.
3 changes: 2 additions & 1 deletion apps/indexer/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
};

Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct ChainConfig {
/// For auto withdraw
pub account_address: Option<String>,
pub account_private_key: Option<String>,
#[serde(default="cooling_down_default")]
#[serde(default = "cooling_down_default")]
pub cooling_down: u64,
}

Expand Down
30 changes: 17 additions & 13 deletions apps/indexer/src/ethereum_indexer/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ impl EthereumClient {
pub async fn new(config: ChainConfig) -> Result<EthereumClient> {
let provider = Provider::<Http>::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::<LocalWallet>()?.with_chain_id(chain_id.as_u32());
Expand Down Expand Up @@ -85,24 +88,25 @@ impl EthereumClient {

///
pub async fn get_block_number(&self) -> Result<u64> {

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.
///
Expand Down
5 changes: 4 additions & 1 deletion apps/indexer/src/ethereum_indexer/events.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
12 changes: 6 additions & 6 deletions apps/indexer/src/ethereum_indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
}

Expand Down
66 changes: 43 additions & 23 deletions apps/indexer/src/starknet_indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +31,12 @@ pub struct StarknetIndexer<

impl<T> StarknetIndexer<T>
where
T: RequestStore + EventStore + BlockStore + CrossChainTxStore + StarknetBridgeRequestStore + PendingWithdrawStore,
T: RequestStore
+ EventStore
+ BlockStore
+ CrossChainTxStore
+ StarknetBridgeRequestStore
+ PendingWithdrawStore,
{
///
pub async fn new(
Expand Down Expand Up @@ -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 {
Expand All @@ -129,22 +138,32 @@ 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);

for (block_number, events) in blocks_events {
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;
}
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions apps/indexer/src/storage/mongo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use super::StarknetBridgeRequest;
use super::PendingWithdraw;
use super::StarknetBridgeRequest;

use crate::storage::{
BlockIndex, BridgeChain, CrossChainTx, CrossChainTxKind, Event, EventLabel, Request,
};
use anyhow::Result;
use mongodb::{bson::Bson, options::ClientOptions, Client, Collection};


mod block_store;
mod event_store;
mod pending_withdraw_store;
Expand Down
14 changes: 10 additions & 4 deletions apps/indexer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();
Expand Down

0 comments on commit 3626a59

Please sign in to comment.