-
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.
- Loading branch information
Showing
21 changed files
with
290 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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>> { | ||
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(), | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
core/lib/zksync_core/src/api_server/tx_sender/deny_list_pool_sink.rs
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,83 @@ | ||
use std::collections::{ | ||
hash_map::{Entry, HashMap}, | ||
HashSet, | ||
}; | ||
|
||
use tokio::sync::Mutex; | ||
use zksync_dal::{transactions_dal::L2TxSubmissionResult, ConnectionPool, Core, CoreDal}; | ||
use zksync_shared_metrics::{TxStage, APP_METRICS}; | ||
use zksync_types::{fee::TransactionExecutionMetrics, l2::L2Tx, Address, Nonce, H256}; | ||
|
||
use super::{tx_sink::TxSink, SubmitTxError}; | ||
use crate::api_server::web3::metrics::API_METRICS; | ||
|
||
/// Wrapper for the master DB pool that allows to submit transactions to the mempool. | ||
#[derive(Debug)] | ||
pub struct DenyListPoolSink { | ||
deny_list_pool: ConnectionPool<Core>, | ||
deny_list: HashSet<Address>, | ||
inflight_requests: Mutex<HashMap<(Address, Nonce), H256>>, | ||
} | ||
|
||
impl DenyListPoolSink { | ||
pub fn new(deny_list_pool: ConnectionPool<Core>, deny_list: HashSet<Address>) -> Self { | ||
Self { | ||
deny_list_pool, | ||
deny_list, | ||
inflight_requests: Mutex::new(HashMap::new()), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl TxSink for DenyListPoolSink { | ||
async fn submit_tx( | ||
&self, | ||
tx: &L2Tx, | ||
execution_metrics: TransactionExecutionMetrics, | ||
) -> 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); | ||
} | ||
|
||
let mut lock = self.inflight_requests.lock().await; | ||
match lock.entry(address_and_nonce) { | ||
Entry::Occupied(entry) => { | ||
let submission_res_handle = if entry.get() == &tx.hash() { | ||
L2TxSubmissionResult::Duplicate | ||
} else { | ||
L2TxSubmissionResult::InsertionInProgress | ||
}; | ||
APP_METRICS.processed_txs[&TxStage::Mempool(submission_res_handle)].inc(); | ||
return Ok(submission_res_handle); | ||
} | ||
Entry::Vacant(entry) => { | ||
entry.insert(tx.hash()); | ||
API_METRICS.inflight_tx_submissions.inc_by(1); | ||
} | ||
}; | ||
drop(lock); | ||
|
||
let result = match self.deny_list_pool.connection_tagged("api").await { | ||
Ok(mut connection) => connection | ||
.transactions_dal() | ||
.insert_transaction_l2(tx, execution_metrics) | ||
.await | ||
.map(|submission_res_handle| { | ||
APP_METRICS.processed_txs[&TxStage::Mempool(submission_res_handle)].inc(); | ||
submission_res_handle | ||
}) | ||
.map_err(|err| err.generalize().into()), | ||
Err(err) => Err(err.generalize().into()), | ||
}; | ||
|
||
self.inflight_requests | ||
.lock() | ||
.await | ||
.remove(&address_and_nonce); | ||
API_METRICS.inflight_tx_submissions.dec_by(1); | ||
|
||
result | ||
} | ||
} |
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
Oops, something went wrong.