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

fix(mempool): in add_tx, delete queue transaction with lower nonce t… #452

Conversation

ayeletstarkware
Copy link
Contributor

@ayeletstarkware ayeletstarkware commented Aug 14, 2024

…hat account nonce


This change is Reviewable

Copy link
Contributor

@giladchase giladchase left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @ayeletstarkware, @elintul, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 139 at r1 (raw file):

        // Remove transactions with lower nonce than the account nonce.
        self.tx_pool.remove_up_to_nonce(sender_address, nonce);
        if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)

i think, because it can't be larger? Can it be larger?

If it can, then the comment here should be fixed to reflect that.

Suggestion:

        // Remove transactions with lower nonce than the account nonce.
        self.tx_pool.remove_up_to_nonce(sender_address, nonce);
        if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce < nonce)

crates/mempool/src/mempool.rs line 147 at r1 (raw file):

        // Maybe close nonce gap.
        if self.tx_queue.get_nonce(sender_address).is_none() {

The answer to this check can be inferred from the previous lines, this adds complexity unnecessarily.

BTW I think this happened in a previous PR as well, I don't remember which, but in general when you add code to a function you should consider the whole function cohesively, sometimes the whole flow if it warrents it.

BTW, In this situation, can we forgo the removal/check entirely and just insert into the queue (if we want to know if something was there or not we can return the old value from the insert)

Code quote:

        if self.tx_queue.get_nonce(sender_address).is_none() {

@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from 299153f to b6af979 Compare August 15, 2024 09:24
Copy link
Contributor Author

@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.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 139 at r1 (raw file):

Previously, giladchase wrote…

i think, because it can't be larger? Can it be larger?

If it can, then the comment here should be fixed to reflect that.

I want to align with the commit block logic, so I'll keep the != comparison, which fits here as well, and remove the comment.

@ayeletstarkware ayeletstarkware changed the base branch from ayelet/mempool/add_tx/delete-txs-with-lower-nonces-from-pool to main August 15, 2024 09:24
Copy link
Collaborator

@elintul elintul left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)

a discussion (no related file):
Not sure I understand the motivation for this PR.



crates/mempool/src/mempool.rs line 136 at r2 (raw file):

        let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } =
            input;

I think it's better to remove from queue (which depends on the account state and is "less stable" than the pool) first, then from the pool.

@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from b6af979 to 5fe6253 Compare August 18, 2024 07:17
Copy link
Contributor Author

@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.

Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)

a discussion (no related file):

Previously, elintul (Elin) wrote…

Not sure I understand the motivation for this PR.

The add_tx function passes the current account nonce to insert_tx. The purpose of this PR is to remove the queued transaction if its nonce differs from the current account nonce, ensuring that a valid transaction remains in the queue.



crates/mempool/src/mempool.rs line 147 at r1 (raw file):

Previously, giladchase wrote…

The answer to this check can be inferred from the previous lines, this adds complexity unnecessarily.

BTW I think this happened in a previous PR as well, I don't remember which, but in general when you add code to a function you should consider the whole function cohesively, sometimes the whole flow if it warrents it.

BTW, In this situation, can we forgo the removal/check entirely and just insert into the queue (if we want to know if something was there or not we can return the old value from the insert)

how can the answer to this check be inferred from the previous lines? the queue could be empty before the check/removal above.


crates/mempool/src/mempool.rs line 136 at r2 (raw file):

Previously, elintul (Elin) wrote…

I think it's better to remove from queue (which depends on the account state and is "less stable" than the pool) first, then from the pool.

Done.

Copy link
Collaborator

@elintul elintul 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: 1 of 2 files reviewed, 6 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)

a discussion (no related file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

The add_tx function passes the current account nonce to insert_tx. The purpose of this PR is to remove the queued transaction if its nonce differs from the current account nonce, ensuring that a valid transaction remains in the queue.

Sounds like a bug, right? Please add a test that fails without this corretion.



crates/mempool/src/mempool.rs line 139 at r1 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

I want to align with the commit block logic, so I'll keep the != comparison, which fits here as well, and remove the comment.

I think it can't be larger thanks to validate_input; Ayelet, can you doublecheck?


crates/mempool/src/mempool.rs line 137 at r3 (raw file):

            input;

        if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)

Suggestion:

// Align mempool to given account state.

// First, maybe remove old nonces.
if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)

crates/mempool/src/mempool.rs line 143 at r3 (raw file):

        self.tx_pool.remove_up_to_nonce(sender_address, nonce);

        self.tx_pool.insert(tx)?;

Suggestion:

// Then, insert new transaction.
self.tx_pool.insert(tx)?;

Copy link
Collaborator

@elintul elintul left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 147 at r1 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

how can the answer to this check be inferred from the previous lines? the queue could be empty before the check/removal above.

I think I also got confused here, maybe a sign for low code readability.
There might be accounts that weren't in the queue to begin with.

@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from 5fe6253 to 95898cb Compare August 18, 2024 10:22
Copy link
Contributor Author

@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.

Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)

a discussion (no related file):

Previously, elintul (Elin) wrote…

Sounds like a bug, right? Please add a test that fails without this corretion.

added a test. It fails without this PR code.



crates/mempool/src/mempool.rs line 139 at r1 (raw file):

Previously, elintul (Elin) wrote…

I think it can't be larger thanks to validate_input; Ayelet, can you doublecheck?

I will discuss this with @MohammadNassar1
added a todo.


crates/mempool/src/mempool.rs line 137 at r3 (raw file):

            input;

        if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)

I'll add the comments in the next PR since the order is being changed.

@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from 95898cb to 8ef6446 Compare August 18, 2024 10:24
@ayeletstarkware ayeletstarkware changed the title feat(mempool): in add_tx, delete queue transaction with lower nonce t… fix(mempool): in add_tx, delete queue transaction with lower nonce t… Aug 18, 2024
@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from 8ef6446 to 1f32454 Compare August 18, 2024 11:07
Copy link
Contributor Author

@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.

Reviewable status: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 139 at r1 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

I will discuss this with @MohammadNassar1
added a todo.

changed to a comment.

Copy link
Contributor Author

@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.

Reviewable status: 0 of 2 files reviewed, 6 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 138 at r5 (raw file):

            input;

        // Note: != is actually equivalent to > here, as lower nonces are rejected in validation.

insert_tx is only used in add_tx now, as the new function is removed in #475. I suggest moving this function’s logic directly into add_tx to make the code and comment easier to understand. wdyt? @elintul

Copy link
Contributor Author

@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.

Reviewable status: 0 of 2 files reviewed, 6 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 138 at r5 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

insert_tx is only used in add_tx now, as the new function is removed in #475. I suggest moving this function’s logic directly into add_tx to make the code and comment easier to understand. wdyt? @elintul

will be easier to change after the code share

Copy link
Collaborator

@elintul elintul left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r4, 2 of 2 files at r5, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 138 at r5 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

will be easier to change after the code share

Let's first merge the PRs we have open, and then revisit.


crates/mempool/src/mempool_test.rs line 456 at r5 (raw file):

    expected_mempool_content.assert_eq_queue_content(&mempool);
}

Do we check the case where the account was not in the queue to begin with?

Copy link
Contributor Author

@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.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 147 at r1 (raw file):

Previously, elintul (Elin) wrote…

I think I also got confused here, maybe a sign for low code readability.
There might be accounts that weren't in the queue to begin with.

Is there anything you would suggest I do?


crates/mempool/src/mempool_test.rs line 456 at r5 (raw file):

Previously, elintul (Elin) wrote…

Do we check the case where the account was not in the queue to begin with?

yes, in test_add_tx_account_state_fills_hole

Copy link
Collaborator

@elintul elintul 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, 3 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 147 at r1 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Is there anything you would suggest I do?

Please revisit a possible refactor after these PRs are merged.


crates/mempool/src/mempool_test.rs line 456 at r5 (raw file):

Previously, ayeletstarkware (Ayelet Zilber) wrote…

yes, in test_add_tx_account_state_fills_hole

Thanks. Better to add a line to the code.

Copy link
Collaborator

@elintul elintul 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: all files reviewed, 3 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)

@ayeletstarkware ayeletstarkware force-pushed the ayelet/mempool/add_tx/delete-tx-with-lower-nonce-from-queue branch from 1f32454 to 369e892 Compare August 19, 2024 07:04
Copy link
Contributor Author

@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.

Reviewable status: 0 of 2 files reviewed, 2 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 147 at r1 (raw file):

Previously, elintul (Elin) wrote…

Please revisit a possible refactor after these PRs are merged.

Done.

Copy link
Collaborator

@elintul elintul left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r6, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @giladchase and @MohammadNassar1)

Copy link
Contributor Author

@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.

Dismissed @giladchase from 2 discussions.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @MohammadNassar1)

@ayeletstarkware ayeletstarkware merged commit 9343539 into main Aug 20, 2024
10 checks passed
guy-starkware pushed a commit that referenced this pull request Aug 20, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants