-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: missing DenyList feature (#100)
- Loading branch information
Showing
23 changed files
with
301 additions
and
31 deletions.
There are no files selected for viewing
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
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,27 @@ | ||
use std::{collections::HashSet, str::FromStr}; | ||
|
||
use serde::Deserialize; | ||
use zksync_basic_types::Address; | ||
|
||
#[derive(Debug, Deserialize, Clone, PartialEq)] | ||
pub struct TxSinkConfig { | ||
pub deny_list: Option<String>, | ||
} | ||
|
||
impl TxSinkConfig { | ||
pub fn deny_list(&self) -> Option<HashSet<Address>> { | ||
// Return deny list is not set or empty | ||
if self.deny_list.is_none() || self.deny_list.as_ref().unwrap().is_empty() { | ||
return None; | ||
} | ||
|
||
// Parse deny list from a string and return it as a set of addresses | ||
// Example: "0x1,0x2,0x3" -> { Address::from_str("0x1").unwrap(), Address::from_str("0x2").unwrap(), Address::from_str("0x3").unwrap() } | ||
// Note: This assumes that the addresses are separated by commas and are in valid format. | ||
self.deny_list.as_ref().map(|list| { | ||
list.split(',') | ||
.map(|element| Address::from_str(element).unwrap()) | ||
.collect() | ||
}) | ||
} | ||
} |
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,35 @@ | ||
use zksync_config::configs::TxSinkConfig; | ||
|
||
use crate::{envy_load, FromEnv}; | ||
|
||
impl FromEnv for TxSinkConfig { | ||
fn from_env() -> anyhow::Result<Self> { | ||
envy_load("tx_sink", "TX_SINK_") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_utils::EnvMutex; | ||
|
||
static MUTEX: EnvMutex = EnvMutex::new(); | ||
|
||
fn expected_config() -> TxSinkConfig { | ||
TxSinkConfig { | ||
deny_list: Some("0x1234567890abcdef".to_string()), | ||
} | ||
} | ||
|
||
#[test] | ||
fn from_env() { | ||
let mut lock = MUTEX.lock(); | ||
let config = r#" | ||
TX_SINK_DENY_LIST="0x1234567890abcdef" | ||
"#; | ||
lock.set_env(config); | ||
|
||
let actual = TxSinkConfig::from_env().unwrap(); | ||
assert_eq!(actual, expected_config()); | ||
} | ||
} |
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,7 @@ | ||
syntax = "proto3"; | ||
|
||
package zksync.config.tx_sink; | ||
|
||
message TxSink { | ||
optional string deny_list = 1; // optional | ||
} |
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,19 @@ | ||
use zksync_config::configs; | ||
use zksync_protobuf::repr::ProtoRepr; | ||
|
||
use crate::proto::tx_sink as proto; | ||
|
||
impl ProtoRepr for proto::TxSink { | ||
type Type = configs::tx_sink::TxSinkConfig; | ||
fn read(&self) -> anyhow::Result<Self::Type> { | ||
Ok(Self::Type { | ||
deny_list: self.deny_list.clone(), | ||
}) | ||
} | ||
|
||
fn build(this: &Self::Type) -> Self { | ||
Self { | ||
deny_list: this.deny_list.clone(), | ||
} | ||
} | ||
} |
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,43 @@ | ||
use std::collections::HashSet; | ||
|
||
use zksync_dal::transactions_dal::L2TxSubmissionResult; | ||
use zksync_multivm::interface::{tracer::ValidationTraces, TransactionExecutionMetrics}; | ||
use zksync_types::{l2::L2Tx, Address}; | ||
|
||
use super::{master_pool_sink::MasterPoolSink, tx_sink::TxSink, SubmitTxError}; | ||
//use crate::api_server::tx_sender::master_pool_sink::MasterPoolSink; | ||
|
||
/// Wrapper for the master DB pool that allows to submit transactions to the mempool. | ||
#[derive(Debug)] | ||
pub struct DenyListPoolSink { | ||
deny_list: HashSet<Address>, | ||
master_pool_sync: MasterPoolSink, | ||
} | ||
|
||
impl DenyListPoolSink { | ||
pub fn new(master_pool_sync: MasterPoolSink, deny_list: HashSet<Address>) -> Self { | ||
Self { | ||
master_pool_sync, | ||
deny_list, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl TxSink for DenyListPoolSink { | ||
async fn submit_tx( | ||
&self, | ||
tx: &L2Tx, | ||
execution_metrics: TransactionExecutionMetrics, | ||
validation_traces: ValidationTraces, | ||
) -> Result<L2TxSubmissionResult, SubmitTxError> { | ||
let address_and_nonce = (tx.initiator_account(), tx.nonce()); | ||
if self.deny_list.contains(&address_and_nonce.0) { | ||
return Err(SubmitTxError::SenderInDenyList(tx.initiator_account())); | ||
} | ||
|
||
self.master_pool_sync | ||
.submit_tx(tx, execution_metrics, validation_traces) | ||
.await | ||
} | ||
} |
Oops, something went wrong.