Skip to content

Commit

Permalink
chore(starknet_mempool): rename has_tx_from_address to contains_tx_fr…
Browse files Browse the repository at this point in the history
…om (#3298)
  • Loading branch information
ArniStarkware authored Jan 14, 2025
1 parent 0d7fc03 commit 0d45204
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
11 changes: 4 additions & 7 deletions crates/starknet_mempool/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,8 @@ impl MempoolCommunicationWrapper {
self.mempool.get_txs(n_txs)
}

pub(crate) fn has_tx_from_address(
&self,
account_address: ContractAddress,
) -> MempoolResult<bool> {
Ok(self.mempool.has_tx_from_address(account_address))
pub(crate) fn contains_tx_from(&self, account_address: ContractAddress) -> MempoolResult<bool> {
Ok(self.mempool.contains_tx_from(account_address))
}
}

Expand All @@ -121,8 +118,8 @@ impl ComponentRequestHandler<MempoolRequest, MempoolResponse> for MempoolCommuni
MempoolRequest::GetTransactions(n_txs) => {
MempoolResponse::GetTransactions(self.get_txs(n_txs))
}
MempoolRequest::DeployAccountExists(account_address) => {
MempoolResponse::DeployAccountExists(self.has_tx_from_address(account_address))
MempoolRequest::ContainsTransactionFrom(account_address) => {
MempoolResponse::ContainsTransactionFrom(self.contains_tx_from(account_address))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl Mempool {
Ok(())
}

pub fn has_tx_from_address(&self, account_address: ContractAddress) -> bool {
pub fn contains_tx_from(&self, account_address: ContractAddress) -> bool {
self.state.get(account_address).is_some()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn mempool() -> Mempool {
MempoolContentBuilder::new().build_into_mempool()
}

/// Used for the has_tx_from_address tests.
/// Used for the contains_tx_from tests.
fn deployer_address() -> ContractAddress {
ContractAddress::from(100_u32)
}
Expand Down Expand Up @@ -927,5 +927,5 @@ fn test_rejected_tx_deleted_from_mempool(mut mempool: Mempool) {
fn tx_from_address_exists(#[case] state: MempoolState, #[case] expected_result: bool) {
let mempool = MempoolContentBuilder::new().with_state(state).build_into_mempool();

assert_eq!(mempool.has_tx_from_address(deployer_address()), expected_result);
assert_eq!(mempool.contains_tx_from(deployer_address()), expected_result);
}
9 changes: 5 additions & 4 deletions crates/starknet_mempool/tests/flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,29 +307,30 @@ fn test_update_gas_price_threshold(mut mempool: Mempool) {
get_txs_and_assert_expected(&mut mempool, 2, &[input_gas_price_20.tx]);
}

/// Test that the API function [Mempool::has_tx_from_address] behaves as expected under various
/// Test that the API function [Mempool::contains_tx_from] behaves as expected under various
/// conditions.
#[rstest]
fn mempool_state_retains_address_across_api_calls(mut mempool: Mempool) {
// Setup.
let address = "0x1";
let input_address_1 = add_tx_input!(address: address);
let account_address = contract_address!(address);

// Test.
add_tx(&mut mempool, &input_address_1);
// Assert: Mempool state includes the address of the added transaction.
assert!(mempool.has_tx_from_address(contract_address!(address)));
assert!(mempool.contains_tx_from(account_address));

// Test.
mempool.get_txs(1).unwrap();
// Assert: The Mempool state still contains the address, even after it was sent to the batcher.
assert!(mempool.has_tx_from_address(contract_address!(address)));
assert!(mempool.contains_tx_from(account_address));

// Test.
let nonces = [(address, 1)];
commit_block(&mut mempool, nonces, []);
// Assert: Mempool state still contains the address, even though the transaction was committed.
// Note that in the future, the Mempool's state may be periodically cleared from records of old
// committed transactions. Mirroring this behavior may require a modification of this test.
assert!(mempool.has_tx_from_address(contract_address!(address)));
assert!(mempool.contains_tx_from(account_address));
}
14 changes: 7 additions & 7 deletions crates/starknet_mempool_types/src/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub trait MempoolClient: Send + Sync {
async fn add_tx(&self, args: AddTransactionArgsWrapper) -> MempoolClientResult<()>;
async fn commit_block(&self, args: CommitBlockArgs) -> MempoolClientResult<()>;
async fn get_txs(&self, n_txs: usize) -> MempoolClientResult<Vec<AccountTransaction>>;
async fn has_tx_from_address(
async fn contains_tx_from(
&self,
contract_address: ContractAddress,
) -> MempoolClientResult<bool>;
Expand All @@ -57,15 +57,15 @@ pub enum MempoolRequest {
AddTransaction(AddTransactionArgsWrapper),
CommitBlock(CommitBlockArgs),
GetTransactions(usize),
DeployAccountExists(ContractAddress),
ContainsTransactionFrom(ContractAddress),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum MempoolResponse {
AddTransaction(MempoolResult<()>),
CommitBlock(MempoolResult<()>),
GetTransactions(MempoolResult<Vec<AccountTransaction>>),
DeployAccountExists(MempoolResult<bool>),
ContainsTransactionFrom(MempoolResult<bool>),
}

#[derive(Clone, Debug, Error)]
Expand Down Expand Up @@ -114,14 +114,14 @@ where
)
}

async fn has_tx_from_address(
async fn contains_tx_from(
&self,
contract_address: ContractAddress,
account_address: ContractAddress,
) -> MempoolClientResult<bool> {
let request = MempoolRequest::DeployAccountExists(contract_address);
let request = MempoolRequest::ContainsTransactionFrom(account_address);
handle_all_response_variants!(
MempoolResponse,
DeployAccountExists,
ContainsTransactionFrom,
MempoolClientError,
MempoolError,
Direct
Expand Down

0 comments on commit 0d45204

Please sign in to comment.