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

vms/platformvm: Prune mempool periodically #2566

Merged
merged 31 commits into from
Jan 2, 2024

Conversation

dhrubabasu
Copy link
Contributor

@dhrubabasu dhrubabasu commented Dec 29, 2023

Why this should be merged

The only time the P-Chain mempool gets exhausted is when BuildBlock is called. If a node does not build a block, the mempool can continue to gain in size with no re-verification of the mempool txs against the preferred state. This PR adds a goroutine to periodically iterate through the mempool and remove invalid txs.

How this works

Periodically builds an ephemeral block (verifies all mempool txs and marks them as dropped). After this block is built, the txs are re-added to the mempool.

Periodic re-verification was chosen over on-demand eviction for better maintainability as P-chain verification rules change. UTXO eviction could be improved to allow skipping during the re-verification without too much work. But tracking validator set modifications for eviction would likely be fairly fragile. We can reconsider this as a future improvement.

How this was tested

  • CI + added UT
  • Canary

@dhrubabasu dhrubabasu added the vm This involves virtual machines label Dec 29, 2023
@dhrubabasu dhrubabasu added this to the v1.10.18 milestone Dec 29, 2023
@dhrubabasu dhrubabasu self-assigned this Dec 29, 2023
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/block/builder/builder.go Outdated Show resolved Hide resolved
@dhrubabasu dhrubabasu changed the base branch from dev to standardize-cancel-func December 31, 2023 08:11
@dhrubabasu dhrubabasu marked this pull request as ready for review December 31, 2023 08:15
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/vm.go Outdated Show resolved Hide resolved
vms/platformvm/vm.go Outdated Show resolved Hide resolved
Base automatically changed from standardize-cancel-func to dev December 31, 2023 21:01
Copy link
Contributor

@StephenButtolph StephenButtolph left a comment

Choose a reason for hiding this comment

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

after this I think this lgtm

vms/platformvm/vm.go Outdated Show resolved Hide resolved
pull bot pushed a commit to AKAJOKER2/avalanchego that referenced this pull request Jan 1, 2024
@aaronbuchwald
Copy link
Collaborator

It seems that with this change there are now two different verification paths for transactions in the P-Chain mempool.

When transactions are issued, they are issued into the mempool from the networking code:

if err := g.txVerifier.VerifyTx(tx); err != nil {

which uses the mempool tx verifier:

type MempoolTxVerifier struct {

but the new code uses the block builder, which enforces slightly different logic by packing transactions into a block. This seems like it could get a bit error prone and make it more difficult to reason about the behavior of the P-Chain mempool. Would it make sense to better align this behavior in the future?

Copy link
Collaborator

@aaronbuchwald aaronbuchwald left a comment

Choose a reason for hiding this comment

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

LGTM - left one comment about two different verification paths for transactions in the P-Chain mempool with this change, but this seems like a good change regardless.

@StephenButtolph
Copy link
Contributor

Would it make sense to better align this behavior in the future?

Yeah - we should probably remove the MempoolTxVerifier. That was really more of a relic of the AdvanceTimeTx era... Whereas now we can just AdvanceTimeTo like we do during block building.

@StephenButtolph StephenButtolph added this pull request to the merge queue Jan 2, 2024
vm.ctx.Lock.Lock()
defer vm.ctx.Lock.Unlock()

blockTxs, err := vm.Builder.PackBlockTxs(math.MaxInt)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this approach could lead to some transactions invalidating others (based on their position in the "fake block"). I assume this is fine but we may want to comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is actually a benefit for pruning this way. Happy for a comment though. Seems related to aaron's comment about different verification paths

}

for _, tx := range blockTxs {
if err := vm.Builder.Add(tx); err != nil {
Copy link
Contributor

@patrick-ogrady patrick-ogrady Jan 2, 2024

Choose a reason for hiding this comment

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

Would it make sense to make vm.Builder.Add accept (...tx)?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we'd then need to return []error which seems like a pain

Merged via the queue into dev with commit 021f4f7 Jan 2, 2024
17 checks passed
@StephenButtolph StephenButtolph deleted the prune-p-mempool-on-a-timer branch January 2, 2024 16:59
@dhrubabasu
Copy link
Contributor Author

Would it make sense to better align this behavior in the future?

Yeah - we should probably remove the MempoolTxVerifier. That was really more of a relic of the AdvanceTimeTx era... Whereas now we can just AdvanceTimeTo like we do during block building.

I've got a PR that removes it here: #2362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vm This involves virtual machines
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants