Skip to content

Commit

Permalink
CCCP-259, cleanup: improve code quality (#96)
Browse files Browse the repository at this point in the history
* CCCP-259, cleanup service.rs

* CCCP-259, refactor: cleanup bridge relay handler

* CCCP-259, refactor: cleanup roundup relay handler

* CCCP-259, refactor: move constants

* CCCP-259, refactor: move bootstrap shared data

* CCCP-259, refactor: remove unnecessary pubs

* CCCP-259, refactor: split contract instances

* CCCP-259, chore: bump dependencies

* CCCP-259, chore: remove unusing methods and pubs

* CCCP-259, fix: resolve wrong operator
  • Loading branch information
dnjscksdn98 authored Oct 19, 2023
1 parent c587e39 commit 9b9411c
Show file tree
Hide file tree
Showing 24 changed files with 1,125 additions and 944 deletions.
32 changes: 16 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,41 @@ repository = "https://github.com/bifrost-platform/bifrost-relayer.rs"

[workspace.dependencies]
# General
log = "0.4.17"
log = "0.4.20"
env_logger = "0.10.0"
sentry = { version = "0.31.5", default-features = false, features = [
sentry = { version = "0.31.7", default-features = false, features = [
"reqwest",
"rustls",
"backtrace",
"contexts",
"panic",
] }

async-trait = "0.1.57"
async-recursion = "1.0.4"
clap = "4.2.1"
async-trait = "0.1.73"
async-recursion = "1.0.5"
clap = "4.4.6"
cron = "0.12.0"
chrono = "0.4.24"
thiserror = "1.0.39"
futures = "0.3.26"
reqwest = { version = "0.11.18", default-features = false, features = [
chrono = "0.4.31"
thiserror = "1.0.49"
futures = "0.3.28"
reqwest = { version = "0.11.22", default-features = false, features = [
"rustls-tls",
"json",
] }
rand = "0.8.5"
lazy_static = "1.4.0"

serde_yaml = "0.9.19"
serde = "1.0.153"
serde_json = "1.0.94"
serde_yaml = "0.9.25"
serde = "1.0.188"
serde_json = "1.0.107"

hex = "0.4.3"
ethers = "2.0.8"
ethers = "2.0.10"
k256 = "0.13.1"
sha3 = "0.10.7"
sha3 = "0.10.8"

tokio = "1.22.0"
tokio-stream = "0.1"
tokio = "1.33.0"
tokio-stream = "0.1.14"

# Substrate Client
sc-cli = { git = "https://github.com/bifrost-platform/bifrost-substrate", default-features = false, branch = "bifrost-polkadot-v0.9.43" }
Expand Down
31 changes: 17 additions & 14 deletions client/src/eth/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ use ethers::{
types::{BlockNumber, Filter, Log, SyncingStatus, U64},
};
use tokio::{
sync::{
broadcast::{self, Receiver, Sender},
RwLock,
},
sync::broadcast::{self, Receiver, Sender},
time::{sleep, Duration},
};

use br_primitives::{
bootstrap::{BootstrapHandler, BootstrapSharedData},
eth::{BootstrapState, ChainID},
sub_display_format,
};

use super::{BootstrapHandler, EthClient};
use super::EthClient;

#[derive(Clone, Debug)]
/// The message format passed through the block channel.
Expand Down Expand Up @@ -57,9 +55,9 @@ pub struct BlockManager<T> {
/// The channel sending block messages.
pub sender: Sender<BlockMessage>,
/// The block waiting for enough confirmations.
pub waiting_block: U64,
/// State of bootstrapping
pub bootstrap_states: Arc<RwLock<Vec<BootstrapState>>>,
waiting_block: U64,
/// The bootstrap shared data.
bootstrap_shared_data: Arc<BootstrapSharedData>,
/// The flag whether the relayer has enabled self balance synchronization. This field will be
/// enabled when prometheus exporter is enabled.
is_balance_sync_enabled: bool,
Expand All @@ -69,7 +67,7 @@ impl<T: JsonRpcClient> BlockManager<T> {
/// Instantiates a new `BlockManager` instance.
pub fn new(
client: Arc<EthClient<T>>,
bootstrap_states: Arc<RwLock<Vec<BootstrapState>>>,
bootstrap_shared_data: Arc<BootstrapSharedData>,
is_balance_sync_enabled: bool,
) -> Self {
let (sender, _receiver) = broadcast::channel(512);
Expand All @@ -78,7 +76,7 @@ impl<T: JsonRpcClient> BlockManager<T> {
client,
sender,
waiting_block: U64::default(),
bootstrap_states,
bootstrap_shared_data,
is_balance_sync_enabled,
}
}
Expand Down Expand Up @@ -133,7 +131,7 @@ impl<T: JsonRpcClient> BlockManager<T> {
let filter = Filter::new()
.from_block(BlockNumber::from(from))
.to_block(BlockNumber::from(to))
.address(self.client.contracts.socket.address());
.address(self.client.protocol_contracts.socket.address());

let target_logs = self.client.get_logs(&filter).await;
if !target_logs.is_empty() {
Expand Down Expand Up @@ -183,12 +181,12 @@ impl<T: JsonRpcClient> BlockManager<T> {
status.highest_block,
);
} else {
for state in self.bootstrap_states.write().await.iter_mut() {
for state in self.bootstrap_shared_data.bootstrap_states.write().await.iter_mut() {
if *state == BootstrapState::NodeSyncing {
*state = BootstrapState::BootstrapRoundUpPhase1;
}
}
return
return;
}

sleep(Duration::from_millis(self.client.metadata.call_interval)).await;
Expand All @@ -205,6 +203,11 @@ impl<T: JsonRpcClient> BootstrapHandler for BlockManager<T> {
}

async fn is_bootstrap_state_synced_as(&self, state: BootstrapState) -> bool {
self.bootstrap_states.read().await.iter().all(|s| *s == state)
self.bootstrap_shared_data
.bootstrap_states
.read()
.await
.iter()
.all(|s| *s == state)
}
}
28 changes: 14 additions & 14 deletions client/src/eth/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub const MAX_PRIORITY_FEE_COEFFICIENT: u64 = 2;

#[derive(Clone, Debug)]
pub struct BridgeRelayMetadata {
/// The bridge direction.
pub direction: String,
/// The bridge direction flag.
pub is_inbound: bool,
/// The bridge request status.
pub status: SocketEventStatus,
/// The bridge request sequence ID.
Expand All @@ -57,13 +57,7 @@ impl BridgeRelayMetadata {
src_chain_id: ChainID,
dst_chain_id: ChainID,
) -> Self {
Self {
direction: if is_inbound { "Inbound".to_string() } else { "Outbound".to_string() },
status,
sequence,
src_chain_id,
dst_chain_id,
}
Self { is_inbound, status, sequence, src_chain_id, dst_chain_id }
}
}

Expand All @@ -72,7 +66,11 @@ impl Display for BridgeRelayMetadata {
write!(
f,
"Relay({}-{:?}-{:?}, {:?} -> {:?})",
self.direction, self.status, self.sequence, self.src_chain_id, self.dst_chain_id
if self.is_inbound { "Inbound".to_string() } else { "Outbound".to_string() },
self.status,
self.sequence,
self.src_chain_id,
self.dst_chain_id
)
}
}
Expand Down Expand Up @@ -256,10 +254,12 @@ impl TxRequest {
/// Sets the `gas` field in the transaction to the provided.
pub fn gas(&self, estimated_gas: U256) -> Self {
match self {
TxRequest::Legacy(tx_request) =>
TxRequest::Legacy(tx_request.clone().gas(estimated_gas)),
TxRequest::Eip1559(tx_request) =>
TxRequest::Eip1559(tx_request.clone().gas(estimated_gas)),
TxRequest::Legacy(tx_request) => {
TxRequest::Legacy(tx_request.clone().gas(estimated_gas))
},
TxRequest::Eip1559(tx_request) => {
TxRequest::Eip1559(tx_request.clone().gas(estimated_gas))
},
}
}

Expand Down
Loading

0 comments on commit 9b9411c

Please sign in to comment.