Skip to content

Commit

Permalink
feat: Adds token whitelists and blacklists (#229)
Browse files Browse the repository at this point in the history
* feat: Adds token whitelists and blacklists

* renabe variable
  • Loading branch information
montekki committed Oct 24, 2023
1 parent 6883ba6 commit 8080708
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/withdrawal-finalizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
color-eyre = { workspace = true }
ethers = { workspace = true, default-features = false, features = ["ws", "rustls"] }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }
url = { workspace = true, features = ["serde"] }
eyre = { workspace = true }
Expand Down
20 changes: 20 additions & 0 deletions bin/withdrawal-finalizer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
use std::str::FromStr;

use envconfig::Envconfig;
use ethers::types::Address;
use serde::Deserialize;
use url::Url;

#[derive(Deserialize, Debug)]
pub struct TokenList(pub Vec<Address>);

impl FromStr for TokenList {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = serde_json::from_str(s)?;
Ok(TokenList(res))
}
}

/// Withdrawal finalizer configuration.
///
/// Can be read from
Expand Down Expand Up @@ -62,4 +76,10 @@ pub struct Config {

#[envconfig(from = "TX_RETRY_TIMEOUT_SECS")]
pub tx_retry_timeout: usize,

#[envconfig(from = "TOKEN_WHITELIST")]
pub token_whitelist: Option<TokenList>,

#[envconfig(from = "TOKEN_BLACKLIST")]
pub token_blacklist: Option<TokenList>,
}
4 changes: 4 additions & 0 deletions bin/withdrawal-finalizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ async fn main() -> Result<()> {
l1_bridge,
config.tx_retry_timeout,
finalizer_account_address,
finalizer::TokensRestrictions::new(
config.token_whitelist.map(|t| t.0),
config.token_blacklist.map(|t| t.0),
),
);
let finalizer_handle = tokio::spawn(finalizer.run(client_l2));

Expand Down
54 changes: 52 additions & 2 deletions finalizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ mod accumulator;
mod error;
mod metrics;

/// A configuration of a blacklist or whitelist policy for finalizing tokens
pub enum TokensRestrictions {
/// Only finalize the whitelisted tokens
WhiteList(Vec<Address>),

/// Finalize all tokens except for this blacklist
BlackList(Vec<Address>),
}

impl TokensRestrictions {
/// Create new token restrictions set.
pub fn new(
token_whitelist: Option<Vec<Address>>,
token_blacklist: Option<Vec<Address>>,
) -> Option<Self> {
if let Some(whitelist) = token_whitelist {
return Some(Self::WhiteList(whitelist.to_vec()));
}

if let Some(blacklist) = token_blacklist {
return Some(Self::BlackList(blacklist.to_vec()));
}

None
}
}

/// A limit to cap a transaction fee (in ether) for safety reasons.
const TX_FEE_LIMIT: f64 = 0.8;

Expand All @@ -60,6 +87,7 @@ pub struct Finalizer<M1, M2> {
tx_retry_timeout: Duration,
account_address: Address,
withdrawals_meterer: WithdrawalsMeter,
token_restrictions: Option<TokensRestrictions>,
}

const NO_NEW_WITHDRAWALS_BACKOFF: Duration = Duration::from_secs(5);
Expand Down Expand Up @@ -87,6 +115,7 @@ where
l1_bridge: IL1Bridge<M>,
tx_retry_timeout: usize,
account_address: Address,
token_restrictions: Option<TokensRestrictions>,
) -> Self {
let withdrawals_meterer =
WithdrawalsMeter::new(pgpool.clone(), MeteringComponent::FinalizedWithdrawals);
Expand All @@ -107,6 +136,7 @@ where
tx_retry_timeout: Duration::from_secs(tx_retry_timeout as u64),
account_address,
withdrawals_meterer,
token_restrictions,
}
}

Expand Down Expand Up @@ -288,8 +318,28 @@ where
async fn loop_iteration(&mut self) -> Result<()> {
tracing::debug!("begin iteration of the finalizer loop");

let try_finalize_these =
storage::withdrwals_to_finalize(&self.pgpool, self.query_db_pagination_limit).await?;
let try_finalize_these = match &self.token_restrictions {
None => {
storage::withdrawals_to_finalize(&self.pgpool, self.query_db_pagination_limit)
.await?
}
Some(TokensRestrictions::WhiteList(w)) => {
storage::withdrawals_to_finalize_with_whitelist(
&self.pgpool,
self.query_db_pagination_limit,
w,
)
.await?
}
Some(TokensRestrictions::BlackList(b)) => {
storage::withdrawals_to_finalize_with_blacklist(
&self.pgpool,
self.query_db_pagination_limit,
b,
)
.await?
}
};

tracing::debug!("trying to finalize these {try_finalize_these:?}");

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8080708

Please sign in to comment.