Skip to content

Commit

Permalink
Make sure no external tx may come from rpc to mempool with system caller
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Mar 22, 2024
1 parent 64d0946 commit 6310ed5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 31 deletions.
89 changes: 64 additions & 25 deletions full-node/chainway-sequencer/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,80 @@
use std::sync::Arc;

use reth_primitives::{BaseFeeParamsKind, Chain, ChainSpec};
use reth_primitives::{BaseFeeParamsKind, Chain, ChainSpec, TxHash};
use reth_tasks::TokioTaskExecutor;
use reth_transaction_pool::blobstore::NoopBlobStore;
use reth_transaction_pool::error::PoolError;
use reth_transaction_pool::{
CoinbaseTipOrdering, EthPooledTransaction, EthTransactionValidator, Pool,
TransactionValidationTaskExecutor,
BestTransactions, BestTransactionsAttributes, CoinbaseTipOrdering, EthPooledTransaction,
EthTransactionValidator, Pool, PoolResult, TransactionPool, TransactionValidationTaskExecutor,
ValidPoolTransaction,
};
use sov_evm::SYSTEM_SIGNER;

pub use crate::db_provider::DbProvider;

pub(crate) type CitreaMempool<C> = Pool<
type CitreaMempoolImpl<C> = Pool<
TransactionValidationTaskExecutor<EthTransactionValidator<DbProvider<C>, EthPooledTransaction>>,
CoinbaseTipOrdering<EthPooledTransaction>,
NoopBlobStore,
>;

pub(crate) fn create_mempool<C: sov_modules_api::Context>(
client: DbProvider<C>,
) -> CitreaMempool<C> {
let blob_store = NoopBlobStore::default();
let genesis_hash = client.genesis_block().unwrap().unwrap().header.hash;
let evm_config = client.cfg();
let chain_spec = ChainSpec {
chain: Chain::from_id(evm_config.chain_id),
genesis_hash,
base_fee_params: BaseFeeParamsKind::Constant(evm_config.base_fee_params),
..Default::default()
};
Pool::eth_pool(
TransactionValidationTaskExecutor::eth(
client,
Arc::new(chain_spec),
type Transaction<C> = <CitreaMempoolImpl<C> as TransactionPool>::Transaction;

pub(crate) struct CitreaMempool<C: sov_modules_api::Context>(CitreaMempoolImpl<C>);

impl<C: sov_modules_api::Context> CitreaMempool<C> {
pub(crate) fn new(client: DbProvider<C>) -> Self {
let blob_store = NoopBlobStore::default();
let genesis_hash = client.genesis_block().unwrap().unwrap().header.hash;
let evm_config = client.cfg();
let chain_spec = ChainSpec {
chain: Chain::from_id(evm_config.chain_id),
genesis_hash,
base_fee_params: BaseFeeParamsKind::Constant(evm_config.base_fee_params),
..Default::default()
};
Self(Pool::eth_pool(
TransactionValidationTaskExecutor::eth(
client,
Arc::new(chain_spec),
blob_store,
TokioTaskExecutor::default(),
),
blob_store,
TokioTaskExecutor::default(),
),
blob_store,
Default::default(),
)
Default::default(),
))
}

pub(crate) async fn add_external_transaction(
&self,
transaction: EthPooledTransaction,
) -> PoolResult<TxHash> {
if transaction.transaction().signer() == SYSTEM_SIGNER {
return Err(PoolError::other(
transaction.transaction().hash(),
"system transactions from rpc are not allowed",
));
}
self.0.add_external_transaction(transaction).await
}

pub(crate) fn get(&self, hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Transaction<C>>>> {
self.0.get(hash)
}

pub(crate) fn remove_transactions(
&self,
tx_hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Transaction<C>>>> {
self.0.remove_transactions(tx_hashes)
}

pub(crate) fn best_transactions_with_attributes(
&self,
best_transactions_attributes: BestTransactionsAttributes,
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Transaction<C>>>>> {
self.0
.best_transactions_with_attributes(best_transactions_attributes)
}
}
4 changes: 2 additions & 2 deletions full-node/chainway-sequencer/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::RpcModule;
use reth_primitives::{Bytes, FromRecoveredPooledTransaction, IntoRecoveredTransaction, B256};
use reth_rpc_types_compat::transaction::from_recovered;
use reth_transaction_pool::{EthPooledTransaction, TransactionOrigin, TransactionPool};
use reth_transaction_pool::EthPooledTransaction;
use sov_evm::Evm;
use sov_mock_da::{MockAddress, MockDaService};
use sov_modules_api::utils::to_jsonrpsee_error_object;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub(crate) fn create_rpc_module<C: sov_modules_api::Context>(
// submit the transaction to the pool with a `Local` origin
let hash: B256 = ctx
.mempool
.add_transaction(TransactionOrigin::External, pool_transaction)
.add_external_transaction(pool_transaction)
.await
.map_err(|e| to_jsonrpsee_error_object(e, ETH_RPC_ERROR))?;
Ok::<B256, ErrorObjectOwned>(hash)
Expand Down
6 changes: 3 additions & 3 deletions full-node/chainway-sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::StreamExt;
use jsonrpsee::RpcModule;
use reth_primitives::IntoRecoveredTransaction;
use reth_provider::BlockReaderIdExt;
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use reth_transaction_pool::BestTransactionsAttributes;
use sov_accounts::Accounts;
use sov_accounts::Response::{AccountEmpty, AccountExists};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
Expand All @@ -35,7 +35,7 @@ use tracing::{debug, info, warn};
use crate::commitment_controller;
use crate::config::SequencerConfig;
use crate::db_provider::DbProvider;
use crate::mempool::{create_mempool, CitreaMempool};
use crate::mempool::CitreaMempool;
use crate::rpc::{create_rpc_module, RpcContext};

type StateRoot<ST, Vm, Da> = <ST as StateTransitionFunction<Vm, Da>>::StateRoot;
Expand Down Expand Up @@ -122,7 +122,7 @@ where
// used as client of reth's mempool
let db_provider = DbProvider::new(storage.clone());

let pool = create_mempool(db_provider.clone());
let pool = CitreaMempool::new(db_provider.clone());

let rpc_config = runner_config.rpc_config;

Expand Down
2 changes: 1 addition & 1 deletion module-system/module-implementations/sov-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod query;
#[cfg(feature = "native")]
pub use query::*;
mod signer;
pub use signer::DevSigner;
pub use signer::{DevSigner, SYSTEM_SIGNER};
#[cfg(feature = "smart_contracts")]
pub mod smart_contracts;

Expand Down

0 comments on commit 6310ed5

Please sign in to comment.