Skip to content

Commit

Permalink
refactor(starknet_gateway): gateway business logic towards bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 26, 2024
1 parent 7e3a704 commit 416c277
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
44 changes: 32 additions & 12 deletions crates/starknet_gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,21 @@ use crate::utils::compile_contract_and_build_executable_tx;
#[path = "gateway_test.rs"]
pub mod gateway_test;

pub struct Gateway {
pub config: GatewayConfig,
struct GatewayBusinessLogic {
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 Gateway {
impl GatewayBusinessLogic {
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 @@ -56,17 +52,15 @@ impl Gateway {
}),
state_reader_factory,
gateway_compiler: Arc::new(gateway_compiler),
mempool_client,
chain_info: config.chain_info.clone(),
}
}

#[instrument(skip(self), ret)]
pub async fn add_tx(
&self,
tx: RpcTransaction,
p2p_message_metadata: Option<BroadcastedMessageMetadata>,
) -> GatewayResult<TransactionHash> {
) -> GatewayResult<AddTransactionArgsWrapper> {
info!("Processing tx");
let blocking_task = ProcessTxBlockingTask::new(self, tx);
// Run the blocking task in the current span.
Expand All @@ -79,9 +73,35 @@ impl Gateway {
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})??;

let tx_hash = add_tx_args.tx.tx_hash();
Ok(AddTransactionArgsWrapper { args: add_tx_args, p2p_message_metadata })
}
}

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

let add_tx_args = AddTransactionArgsWrapper { args: add_tx_args, p2p_message_metadata };
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, p2p_message_metadata).await?;
let tx_hash = add_tx_args.args.tx.tx_hash();
self.mempool_client.add_tx(add_tx_args).await.map_err(|e| {
error!("Failed to send tx to mempool: {}", e);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
Expand All @@ -103,7 +123,7 @@ struct ProcessTxBlockingTask {
}

impl ProcessTxBlockingTask {
pub fn new(gateway: &Gateway, tx: RpcTransaction) -> Self {
pub fn new(gateway: &GatewayBusinessLogic, tx: RpcTransaction) -> Self {
Self {
stateless_tx_validator: gateway.stateless_tx_validator.clone(),
stateful_tx_validator: gateway.stateful_tx_validator.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn test_instantiate_validator(stateful_validator: StatefulTransactionValidator)

let mut mock_state_reader_factory = MockStateReaderFactory::new();

// Make sure stateful_validator uses the latest block in the initiall call.
// Make sure stateful_validator uses the latest block in the initial call.
let latest_state_reader = state_reader_factory.get_state_reader_from_latest_block();
mock_state_reader_factory
.expect_get_state_reader_from_latest_block()
Expand Down

0 comments on commit 416c277

Please sign in to comment.