-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[bridge] add sui watch dog and a few observables #19612
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "sui-bridge-watchdog" | ||
version = "0.1.0" | ||
authors = ["Mysten Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sui-bridge.workspace = true | ||
mysten-metrics.workspace = true | ||
prometheus.workspace = true | ||
anyhow.workspace = true | ||
futures.workspace = true | ||
async-trait.workspace = true | ||
ethers = { version = "2.0" } | ||
tracing.workspace = true | ||
tokio = { workspace = true, features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! The EthBridgeStatus observable monitors whether the Eth Bridge is paused. | ||
|
||
use crate::Observable; | ||
use async_trait::async_trait; | ||
use ethers::providers::Provider; | ||
use ethers::types::Address as EthAddress; | ||
use prometheus::IntGauge; | ||
use std::sync::Arc; | ||
use sui_bridge::abi::EthSuiBridge; | ||
use sui_bridge::metered_eth_provider::MeteredEthHttpProvier; | ||
use tokio::time::Duration; | ||
use tracing::{error, info}; | ||
|
||
pub struct EthBridgeStatus { | ||
bridge_contract: EthSuiBridge<Provider<MeteredEthHttpProvier>>, | ||
metric: IntGauge, | ||
} | ||
|
||
impl EthBridgeStatus { | ||
pub fn new( | ||
provider: Arc<Provider<MeteredEthHttpProvier>>, | ||
bridge_address: EthAddress, | ||
metric: IntGauge, | ||
) -> Self { | ||
let bridge_contract = EthSuiBridge::new(bridge_address, provider.clone()); | ||
Self { | ||
bridge_contract, | ||
metric, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Observable for EthBridgeStatus { | ||
fn name(&self) -> &str { | ||
"EthBridgeStatus" | ||
} | ||
|
||
async fn observe_and_report(&self) { | ||
let status = self.bridge_contract.paused().call().await; | ||
match status { | ||
Ok(status) => { | ||
self.metric.set(status as i64); | ||
info!("Eth Bridge Status: {:?}", status); | ||
} | ||
Err(e) => { | ||
error!("Error getting eth bridge status: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
fn interval(&self) -> Duration { | ||
Duration::from_secs(10) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::Observable; | ||
use async_trait::async_trait; | ||
use ethers::providers::Provider; | ||
use ethers::types::{Address as EthAddress, U256}; | ||
use prometheus::IntGauge; | ||
use std::sync::Arc; | ||
use sui_bridge::abi::EthERC20; | ||
use sui_bridge::metered_eth_provider::MeteredEthHttpProvier; | ||
use tokio::time::Duration; | ||
use tracing::{error, info}; | ||
|
||
pub struct EthVaultBalance { | ||
coin_contract: EthERC20<Provider<MeteredEthHttpProvier>>, | ||
vault_address: EthAddress, | ||
ten_zeros: U256, | ||
longbowlu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metric: IntGauge, | ||
} | ||
|
||
impl EthVaultBalance { | ||
pub fn new( | ||
provider: Arc<Provider<MeteredEthHttpProvier>>, | ||
vault_address: EthAddress, | ||
coin_address: EthAddress, // for now this only support one coin which is WETH | ||
metric: IntGauge, | ||
) -> Self { | ||
let ten_zeros = U256::from(10_u64.pow(10)); | ||
let coin_contract = EthERC20::new(coin_address, provider); | ||
Self { | ||
coin_contract, | ||
vault_address, | ||
ten_zeros, | ||
metric, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Observable for EthVaultBalance { | ||
fn name(&self) -> &str { | ||
"EthVaultBalance" | ||
} | ||
|
||
async fn observe_and_report(&self) { | ||
match self | ||
.coin_contract | ||
.balance_of(self.vault_address) | ||
.call() | ||
.await | ||
{ | ||
Ok(balance) => { | ||
// Why downcasting is safe: | ||
// 1. On Ethereum we only take the first 8 decimals into account, | ||
// meaning the trailing 10 digits can be ignored | ||
// 2. i64::MAX is 9_223_372_036_854_775_807, with 8 decimal places is | ||
// 92_233_720_368. We likely won't see any balance higher than this | ||
// in the next 12 months. | ||
let balance = (balance / self.ten_zeros).as_u64() as i64; | ||
self.metric.set(balance); | ||
info!("Eth Vault Balance: {:?}", balance); | ||
} | ||
Err(e) => { | ||
error!("Error getting balance from vault: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
fn interval(&self) -> Duration { | ||
Duration::from_secs(10) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! The BridgeWatchDog module is responsible for monitoring the health | ||
//! of the bridge by periodically running various observables and | ||
//! reporting the results. | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use mysten_metrics::spawn_logged_monitored_task; | ||
use std::sync::Arc; | ||
use tokio::time::Duration; | ||
use tokio::time::MissedTickBehavior; | ||
use tracing::{error_span, info, Instrument}; | ||
|
||
pub mod eth_bridge_status; | ||
pub mod eth_vault_balance; | ||
pub mod metrics; | ||
pub mod sui_bridge_status; | ||
|
||
pub struct BridgeWatchDog { | ||
observables: Vec<Arc<dyn Observable + Send + Sync>>, | ||
} | ||
|
||
impl BridgeWatchDog { | ||
pub fn new(observables: Vec<Arc<dyn Observable + Send + Sync>>) -> Self { | ||
Self { observables } | ||
} | ||
|
||
pub async fn run(self) { | ||
let mut handles = vec![]; | ||
for observable in self.observables.into_iter() { | ||
let handle = spawn_logged_monitored_task!(Self::run_observable(observable)); | ||
handles.push(handle); | ||
} | ||
// Return when any task returns an error or all tasks exit. | ||
futures::future::try_join_all(handles).await.unwrap(); | ||
unreachable!("watch dog tasks should not exit"); | ||
} | ||
|
||
async fn run_observable(observable: Arc<dyn Observable + Send + Sync>) -> Result<()> { | ||
let mut interval = tokio::time::interval(observable.interval()); | ||
interval.set_missed_tick_behavior(MissedTickBehavior::Skip); | ||
let name = observable.name(); | ||
let span = error_span!("observable", name); | ||
loop { | ||
info!("Running observable {}", name); | ||
observable | ||
.observe_and_report() | ||
.instrument(span.clone()) | ||
.await; | ||
interval.tick().await; | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait Observable { | ||
fn name(&self) -> &str; | ||
async fn observe_and_report(&self); | ||
fn interval(&self) -> Duration; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently not introduced in this PR, but small typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will fix in a separate PR