Skip to content

Commit

Permalink
Revert "refactor(starknet_gateway): gateway business logic towards be…
Browse files Browse the repository at this point in the history
…nch test (#2948)"

This reverts commit e3dbfa4.
  • Loading branch information
ArniStarkware committed Jan 1, 2025
1 parent 4633b92 commit 1eb67dd
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions crates/starknet_gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ use crate::utils::compile_contract_and_build_executable_tx;
#[path = "gateway_test.rs"]
pub mod gateway_test;

struct GatewayBusinessLogic {
pub struct Gateway {
pub config: GatewayConfig,
pub stateless_tx_validator: Arc<StatelessTransactionValidator>,
pub stateful_tx_validator: Arc<StatefulTransactionValidator>,
pub state_reader_factory: Arc<dyn StateReaderFactory>,
pub gateway_compiler: Arc<GatewayCompiler>,
pub mempool_client: SharedMempoolClient,
pub chain_info: ChainInfo,
}

impl GatewayBusinessLogic {
impl Gateway {
pub fn new(
config: GatewayConfig,
state_reader_factory: Arc<dyn StateReaderFactory>,
gateway_compiler: GatewayCompiler,
mempool_client: SharedMempoolClient,
) -> Self {
Self {
config: config.clone(),
stateless_tx_validator: Arc::new(StatelessTransactionValidator {
config: config.stateless_tx_validator_config.clone(),
}),
Expand All @@ -49,49 +53,31 @@ impl GatewayBusinessLogic {
}),
state_reader_factory,
gateway_compiler: Arc::new(gateway_compiler),
mempool_client,
chain_info: config.chain_info.clone(),
}
}

pub async fn add_tx(&self, tx: RpcTransaction) -> GatewayResult<AddTransactionArgs> {
info!("Processing tx");
let blocking_task = ProcessTxBlockingTask::new(self, tx);
// Run the blocking task in the current span.
let curr_span = Span::current();
tokio::task::spawn_blocking(move || curr_span.in_scope(|| blocking_task.process_tx()))
.await
.map_err(|join_err| {
error!("Failed to process tx: {}", join_err);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})?
}
}

pub struct Gateway {
pub mempool_client: SharedMempoolClient,
business_logic: GatewayBusinessLogic,
}

impl Gateway {
pub fn new(
config: GatewayConfig,
state_reader_factory: Arc<dyn StateReaderFactory>,
gateway_compiler: GatewayCompiler,
mempool_client: SharedMempoolClient,
) -> Self {
let business_logic =
GatewayBusinessLogic::new(config, state_reader_factory, gateway_compiler);
Self { business_logic, mempool_client }
}

#[instrument(skip(self), ret)]
pub async fn add_tx(
&self,
tx: RpcTransaction,
p2p_message_metadata: Option<BroadcastedMessageMetadata>,
) -> GatewayResult<TransactionHash> {
let add_tx_args = self.business_logic.add_tx(tx).await?;
info!("Processing tx");
let blocking_task = ProcessTxBlockingTask::new(self, tx);
// Run the blocking task in the current span.
let curr_span = Span::current();
let add_tx_args =
tokio::task::spawn_blocking(move || curr_span.in_scope(|| blocking_task.process_tx()))
.await
.map_err(|join_err| {
error!("Failed to process tx: {}", join_err);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})??;

let tx_hash = add_tx_args.tx.tx_hash();

let add_tx_args = AddTransactionArgsWrapper { args: add_tx_args, p2p_message_metadata };
self.mempool_client.add_tx(add_tx_args).await.map_err(|e| {
error!("Failed to send tx to mempool: {}", e);
Expand All @@ -114,7 +100,7 @@ struct ProcessTxBlockingTask {
}

impl ProcessTxBlockingTask {
pub fn new(gateway: &GatewayBusinessLogic, tx: RpcTransaction) -> Self {
pub fn new(gateway: &Gateway, tx: RpcTransaction) -> Self {
Self {
stateless_tx_validator: gateway.stateless_tx_validator.clone(),
stateful_tx_validator: gateway.stateful_tx_validator.clone(),
Expand Down

0 comments on commit 1eb67dd

Please sign in to comment.