Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(starknet_mempool): add remove old transaction API to TransactionPool #4029

Conversation

dafnamatsry
Copy link
Collaborator

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

@dafnamatsry dafnamatsry marked this pull request as ready for review February 9, 2025 08:32
@dafnamatsry dafnamatsry changed the base branch from mempool-test-refactor to main February 9, 2025 08:36
@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch 2 times, most recently from 39f56b8 to 88db340 Compare February 9, 2025 10:54
@dafnamatsry dafnamatsry changed the base branch from main to mempool-test-refactor February 9, 2025 10:54
@dafnamatsry dafnamatsry force-pushed the mempool-test-refactor branch from d75cd2b to 149efdf Compare February 9, 2025 11:21
@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch from 88db340 to 8560ae0 Compare February 9, 2025 11:21
@dafnamatsry dafnamatsry force-pushed the mempool-test-refactor branch 2 times, most recently from 9f7ed95 to 201905b Compare February 10, 2025 12:40
@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch from 8560ae0 to 60e785d Compare February 10, 2025 12:43
@dafnamatsry dafnamatsry force-pushed the mempool-test-refactor branch 2 times, most recently from dc21402 to 99f04ba Compare February 11, 2025 07:17
@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch from 60e785d to bcd5e6f Compare February 11, 2025 08:54
@dafnamatsry dafnamatsry changed the base branch from mempool-test-refactor to main February 11, 2025 08:54
Copy link
Collaborator

@alonh5 alonh5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 5 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 7 unresolved discussions (waiting on @ayeletstarkware)


crates/starknet_mempool/src/transaction_pool.rs line 27 at r3 (raw file):

    txs_by_account: AccountTransactionIndex,
    // Transactions sorted by their time spent in the pool, i.e., in descending order of submission
    // time.

Took me a minute to understand this. Maybe this is easier to compile?

Suggestion:

    // Transactions sorted by their time spent in the pool (i.e. newest to oldest).

crates/starknet_mempool/src/transaction_pool.rs line 130 at r3 (raw file):

    }

    // Rmoves the given `removed_txs` from all the pool mappings, except for the one specified in

You should add the Code Spell Checker extension to your vscode.

Suggestion:

Removes

crates/starknet_mempool/src/transaction_pool.rs line 135 at r3 (raw file):

        &mut self,
        removed_txs: &Vec<TransactionReference>,
        skip_mapping: PoolMappings,

Maybe have three separate functions, one for each mapping, and call two of them in each place?
I think it's a bit more straight forward and readable.


crates/starknet_mempool/src/transaction_pool.rs line 273 at r3 (raw file):

}

impl Eq for SubmissionID {}

Can't the be derived?

Code quote:

impl PartialEq for SubmissionID {
    fn eq(&self, other: &Self) -> bool {
        self.submission_time == other.submission_time && self.tx_hash == other.tx_hash
    }
}

impl Eq for SubmissionID {}

crates/starknet_mempool/src/transaction_pool.rs line 278 at r3 (raw file):

// that has been in the pool for a longer time will be considered "greater" than a transaction that
// has been in the pool for a shorter time.
impl Ord for SubmissionID {

Why do yo actually need this reverse comparison? Can't you remove the first entries instead of the last? In a BTreeMap it should be the same complexity.


crates/starknet_mempool/src/transaction_pool.rs line 295 at r3 (raw file):

#[derive(Debug, Default, Eq, PartialEq)]
struct TimedTransactionMap {
    txs_by_submission_time: BTreeMap<SubmissionID, TransactionReference>,

Do you ever actually need the tx reference here?


crates/starknet_mempool/src/transaction_pool.rs line 301 at r3 (raw file):

impl TimedTransactionMap {
    /// If the transaction with the same transaction hash already exists in the mapping, the old
    /// submission ID is returned.

I don't think this can happen in the flow, it will error with DuplicateTransaction. Maybe add an assertion?

Code quote:

    /// If the transaction with the same transaction hash already exists in the mapping, the old
    /// submission ID is returned.

@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch from bcd5e6f to 5c329b7 Compare February 11, 2025 13:35
Copy link
Collaborator Author

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 1 of 5 files reviewed, 7 unresolved discussions (waiting on @alonh5 and @ayeletstarkware)


crates/starknet_mempool/src/transaction_pool.rs line 27 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Took me a minute to understand this. Maybe this is easier to compile?

Done.


crates/starknet_mempool/src/transaction_pool.rs line 130 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

You should add the Code Spell Checker extension to your vscode.

Done. (And installed)


crates/starknet_mempool/src/transaction_pool.rs line 135 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Maybe have three separate functions, one for each mapping, and call two of them in each place?
I think it's a bit more straight forward and readable.

Done.
I agree it's more strait forward. The downside is that there is no one place that ensures all the mappings are aligned.


crates/starknet_mempool/src/transaction_pool.rs line 273 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Can't the be derived?

Done.


crates/starknet_mempool/src/transaction_pool.rs line 278 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Why do yo actually need this reverse comparison? Can't you remove the first entries instead of the last? In a BTreeMap it should be the same complexity.

The split_off function I use removes the greatest entries (and I did't find an equivalent for the smallest).

I could have removed elements one by one, but I think the split_off is more efficient.


crates/starknet_mempool/src/transaction_pool.rs line 295 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Do you ever actually need the tx reference here?

It is used as the return value of remove_txs_older_than, which is later used to remove from the account index. And will also be used to remove from the queue in the next PR.


crates/starknet_mempool/src/transaction_pool.rs line 301 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

I don't think this can happen in the flow, it will error with DuplicateTransaction. Maybe add an assertion?

I did the same as in AccountTransactionIndex to be consistent.
There is an assertion above, when calling this function.

Copy link
Collaborator

@alonh5 alonh5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4 of 4 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ayeletstarkware and @dafnamatsry)


crates/starknet_mempool/src/transaction_pool.rs line 295 at r3 (raw file):

Previously, dafnamatsry wrote…

It is used as the return value of remove_txs_older_than, which is later used to remove from the account index. And will also be used to remove from the queue in the next PR.

Got it, thanks.


crates/starknet_mempool/src/transaction_pool.rs line 301 at r3 (raw file):

Previously, dafnamatsry wrote…

I did the same as in AccountTransactionIndex to be consistent.
There is an assertion above, when calling this function.

Oh right. Don't you think it's better to assert inside the function and not return anything? For both

Copy link
Collaborator Author

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @alonh5 and @ayeletstarkware)


crates/starknet_mempool/src/transaction_pool.rs line 301 at r3 (raw file):

Previously, alonh5 (Alon Haramati) wrote…

Oh right. Don't you think it's better to assert inside the function and not return anything? For both

I personally like it this way, it makes these maps more generic (the behaviour is just like HashMap).
But either way is fine I think...

Copy link
Collaborator

@alonh5 alonh5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ayeletstarkware)


crates/starknet_mempool/src/transaction_pool.rs line 301 at r3 (raw file):

Previously, dafnamatsry wrote…

I personally like it this way, it makes these maps more generic (the behaviour is just like HashMap).
But either way is fine I think...

Okay, not crucial for me.

Copy link
Contributor

@ayeletstarkware ayeletstarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @dafnamatsry)


crates/starknet_mempool/src/transaction_pool.rs line 90 at r4 (raw file):

        self.remove_from_timed_mapping(&removed_txs);

        self.capacity.remove_n(removed_txs.len());

Since those removals are related, should we merge them into a single remove_transactions function? and use it in remove_txs_older_than as well

Code quote:

        self.remove_from_main_mapping(&removed_txs);
        self.remove_from_timed_mapping(&removed_txs);

        self.capacity.remove_n(removed_txs.len());

crates/starknet_mempool/src/transaction_pool.rs line 294 at r4 (raw file):

impl TimedTransactionMap {
    /// If the transaction with the same transaction hash already exists in the mapping, the old
    /// submission ID is returned.

Does this scenario occur when a transaction is submitted with a higher fee?

Suggestion:

/// If a transaction with the same transaction hash already exists in the mapping, the previous
/// submission ID is returned.

crates/starknet_mempool/src/transaction_pool.rs line 311 at r4 (raw file):

    /// Removes all transactions that were submitted to the pool before the given duration.
    #[allow(dead_code)]
    pub fn remove_txs_older_than(&mut self, duration: Duration) -> Vec<TransactionReference> {

Is this function tested in the next PRs?

@dafnamatsry dafnamatsry force-pushed the 02-09-feat_starknet_mempool_add_remove_old_transaction_api_to_transactionpool branch from 5c329b7 to e445ce1 Compare February 12, 2025 10:53
Copy link
Collaborator Author

@dafnamatsry dafnamatsry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 5 files reviewed, 3 unresolved discussions (waiting on @alonh5 and @ayeletstarkware)


crates/starknet_mempool/src/transaction_pool.rs line 90 at r4 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Since those removals are related, should we merge them into a single remove_transactions function? and use it in remove_txs_older_than as well

That's what I did in the beginning, and Alon asked to split it (you can see the previous revisions).
Note that every public remove calls 2 different remove_from_.. functions.


crates/starknet_mempool/src/transaction_pool.rs line 294 at r4 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Does this scenario occur when a transaction is submitted with a higher fee?

Yes, the tx is inserted to all the mappings like any other transaction.
A new submission time will be associated with the new tx.


crates/starknet_mempool/src/transaction_pool.rs line 311 at r4 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Is this function tested in the next PRs?

Yes!

Copy link
Contributor

@ayeletstarkware ayeletstarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @dafnamatsry)

@dafnamatsry dafnamatsry added this pull request to the merge queue Feb 12, 2025
Merged via the queue into main with commit a1f86a4 Feb 12, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants