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(mempool): impl mempool state #497

Closed
wants to merge 1 commit into from

Conversation

MohammadNassar1
Copy link
Contributor

@MohammadNassar1 MohammadNassar1 commented Jul 18, 2024

This change is Reviewable

@codecov-commenter
Copy link

codecov-commenter commented Jul 18, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 12 lines in your changes missing coverage. Please review.

Project coverage is 80.81%. Comparing base (93de0bd) to head (d43df34).

Files Patch % Lines
crates/mempool/src/mempool.rs 50.00% 10 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #497      +/-   ##
==========================================
- Coverage   81.21%   80.81%   -0.41%     
==========================================
  Files          42       42              
  Lines        1826     1850      +24     
  Branches     1826     1850      +24     
==========================================
+ Hits         1483     1495      +12     
- Misses        269      279      +10     
- Partials       74       76       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from 402e775 to 9774833 Compare July 18, 2024 07:08
Copy link
Contributor Author

@MohammadNassar1 MohammadNassar1 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, 1 unresolved discussion

a discussion (no related file):
This PR implements the Mempool state, representing the state that the mempool is aware of during batch processing.
The Mempool state is updated in get_txs, used in add_tx and commit_block, and reset after commit_block.


@MohammadNassar1 MohammadNassar1 requested review from giladchase, ayeletstarkware and elintul and removed request for giladchase July 18, 2024 07:08
@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from 9774833 to 3dac360 Compare July 18, 2024 11:17
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 1 of 2 files at r1, 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @elintul, @giladchase, and @MohammadNassar1)


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

        for tx in &eligible_txs {
            self.mempool_state.entry(tx.sender_address).or_default().nonce = tx.nonce;
        }

Can you explain these loops?

Code quote:

        for tx in &eligible_txs {
            self.mempool_state.entry(tx.sender_address).and_modify(|account| {
                account.nonce = tx.nonce;
            });
        }

        for tx in &eligible_txs {
            self.mempool_state.entry(tx.sender_address).or_default().nonce = tx.nonce;
        }

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

    fn is_duplicated_tx(&self, tx: &ThinTransaction) -> MempoolResult<()> {
        if let Some(AccountState { nonce }) = self.mempool_state.get(&tx.sender_address) {
            if *nonce >= tx.nonce {

Will this work?
if nonce >= &tx.nonce {

Code quote:

*nonce >= tx.nonce

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch 2 times, most recently from 380e27f to 071bd79 Compare July 18, 2024 15:50
Copy link
Contributor Author

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


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

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Can you explain these loops?

My bad. I deleted the first loop.

The second one that I kept goes over the popped transactions and updates the mempool state for these addresses. I used 'or_default' so if it didn't have the address, it would generate the default 'AccountState'.


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

Previously, ayeletstarkware (Ayelet Zilber) wrote…

Will this work?
if nonce >= &tx.nonce {

Both are okay,
I used your suggestion, as using references is preferred.

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 r4, all commit messages.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 25 at r4 (raw file):

    // Transactions eligible for sequencing.
    tx_queue: TransactionQueue,
    // Represents the current state of the mempool during batch processing.

Suggestion:

block creation.

crates/mempool/src/mempool.rs line 62 at r4 (raw file):

        // Update the mempool state with the new nonces.
        for tx in eligible_txs.iter() {

Isn't for x in iterable sugar for this?

Code quote:

.iter()

crates/mempool/src/mempool.rs line 120 at r4 (raw file):

    }

    fn is_duplicated_tx(&self, tx: &ThinTransaction) -> MempoolResult<()> {

Is this related to this PR?

Code quote:

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

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

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from 071bd79 to c67cc0a Compare July 23, 2024 07:39
Copy link
Contributor Author

@MohammadNassar1 MohammadNassar1 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 @ayeletstarkware, @elintul, and @giladchase)


crates/mempool/src/mempool.rs line 25 at r4 (raw file):

    // Transactions eligible for sequencing.
    tx_queue: TransactionQueue,
    // Represents the current state of the mempool during batch processing.

Done


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, elintul (Elin) wrote…

Is this related to this PR?

Yes, this PR adds the mempool_state and in is_duplicate_tx function it verifies that the transaction is valid using the mempool_state.

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 r5, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, MohammadNassar1 (mohammad-starkware) wrote…

Yes, this PR adds the mempool_state and in is_duplicate_tx function it verifies that the transaction is valid using the mempool_state.

Doesn't this fall into the case we discussed, where add_tx input goes back?
Should we check the Q as well?

Copy link
Contributor Author

@MohammadNassar1 MohammadNassar1 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 @ayeletstarkware, @elintul, and @giladchase)


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, elintul (Elin) wrote…

Doesn't this fall into the case we discussed, where add_tx input goes back?
Should we check the Q as well?

This was added to prevent adding a transaction that was sent to the batcher using get_txs but not yet committed.

Check with whom?

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


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, MohammadNassar1 (mohammad-starkware) wrote…

This was added to prevent adding a transaction that was sent to the batcher using get_txs but not yet committed.

Check with whom?

Check the queue. Okay, I understand it's not necessarily the same case.
Let's add the other checks to this method as well.


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

        }
        Ok(())
    }

Would that work?

Suggestion:

fn should_insert(&self, tx: &ThinTransaction) -> bool {
    self.mempool_state
        .get(&tx.sender_address)
        .map_or(true, |AccountState { nonce }| nonce < &tx.nonce)
}

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch 2 times, most recently from 2200458 to 49bfc23 Compare July 23, 2024 13:10
Copy link
Contributor Author

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


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, elintul (Elin) wrote…

Check the queue. Okay, I understand it's not necessarily the same case.
Let's add the other checks to this method as well.

Sorry, I didn't get it :(
Which another check?


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

Previously, elintul (Elin) wrote…

Would that work?

Following our discussion, we decided to add another Error DuplicateNonce.
The code was updated.

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


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, MohammadNassar1 (mohammad-starkware) wrote…

Sorry, I didn't get it :(
Which another check?

There's #510, maybe we should add this check to should_insert.


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

Previously, MohammadNassar1 (mohammad-starkware) wrote…

Following our discussion, we decided to add another Error DuplicateNonce.
The code was updated.

Given that we'll add more checks to this method, perhaps: validate_input?

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from 49bfc23 to a104502 Compare July 23, 2024 14:00
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:

Reviewed 1 of 1 files at r7, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @ayeletstarkware and @giladchase)

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from a104502 to cf24cda Compare July 23, 2024 14:10
Copy link
Contributor Author

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


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, elintul (Elin) wrote…

There's #510, maybe we should add this check to should_insert.

This is the same check :).
I added a TODO in that PR to use this method.


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

Previously, elintul (Elin) wrote…

Given that we'll add more checks to this method, perhaps: validate_input?

Done.

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from cf24cda to 44e4e35 Compare July 23, 2024 14:14
@MohammadNassar1 MohammadNassar1 dismissed ayeletstarkware’s stale review July 23, 2024 14:32

Her comments were implemented

Copy link
Contributor Author

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


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

Previously, MohammadNassar1 (mohammad-starkware) wrote…

Both are okay,
I used your suggestion, as using references is preferred.

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

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 r9.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @ayeletstarkware, @giladchase, and @MohammadNassar1)


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, MohammadNassar1 (mohammad-starkware) wrote…

This is the same check :).
I added a TODO in that PR to use this method.

Why do we need both PRs?


crates/mempool/src/mempool.rs line 122 at r9 (raw file):

    }

    fn validate_input(&self, tx: &ThinTransaction, account: Account) -> MempoolResult<()> {

Pass MempoolInput.

Code quote:

validate_input

@MohammadNassar1 MohammadNassar1 force-pushed the mohammad/mempool/add-mempool-state branch from 44e4e35 to d43df34 Compare July 24, 2024 12:47
Copy link
Contributor Author

@MohammadNassar1 MohammadNassar1 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 @ayeletstarkware and @giladchase)


crates/mempool/src/mempool.rs line 120 at r4 (raw file):

Previously, elintul (Elin) wrote…

Why do we need both PRs?

Talked F2F.

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 r10, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ayeletstarkware and @giladchase)

@elintul elintul closed this Jul 25, 2024
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