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(delayedack): paginate rollapp packets when deleting them #972

Merged
merged 5 commits into from
Jul 9, 2024

Conversation

zale144
Copy link
Contributor

@zale144 zale144 commented Jul 2, 2024

Description

  • When deleting rollapp packets each epoch end, instead of fetching all of them at once, we apply batching now, to prevent unbounded memory usage.
  • The upgrade handlers are refactored so that they use an interface for the upgrades themselves, keepers are extracted to be in a separate package.

Closes #320

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow-up issues.

PR review checkboxes:

I have...

  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Targeted PR against the correct branch
  • included the correct type prefix in the PR title
  • Linked to the GitHub issue with discussion and accepted design
  • Targets only one GitHub issue
  • Wrote unit and integration tests
  • Wrote relevant migration scripts if necessary
  • All CI checks have passed
  • Added relevant godoc comments
  • Updated the scripts for local run, e.g genesis_config_commands.sh if the PR changes parameters
  • Add an issue in the e2e-tests repo if necessary

SDK Checklist

  • Import/Export Genesis
  • Registered Invariants
  • Registered Events
  • Updated openapi.yaml
  • No usage of go map
  • No usage of time.Now()
  • Used fixed point arithmetic and not float arithmetic
  • Avoid panicking in Begin/End block as much as possible
  • No unexpected math Overflow
  • Used sendCoin and not SendCoins
  • Out-of-block compute is bounded
  • No serialized ID at the end of store keys
  • UInt to byte conversion should use BigEndian

Full security checklist here

----;

For Reviewer:

  • Confirmed the correct type prefix in the PR title
  • Reviewers assigned
  • Confirmed all author checklist items have been addressed

---;

After reviewer approval:

  • In case the PR targets the main branch, PR should not be squash merge in order to keep meaningful git history.
  • In case the PR targets a release branch, PR must be rebased.

@zale144 zale144 self-assigned this Jul 2, 2024
@zale144 zale144 requested a review from a team as a code owner July 2, 2024 12:22
@danwt
Copy link
Contributor

danwt commented Jul 4, 2024

Closes what issue?

Copy link
Contributor

@danwt danwt left a comment

Choose a reason for hiding this comment

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

Biggest open question I have is, there are several calls to ListRollappPackets in our code and after this PR not all of them are batched with a limit

So why can't memory problems occur at one of those places?

app/keepers/keepers.go Show resolved Hide resolved
app/upgrades/types.go Show resolved Hide resolved
Comment on lines +20 to +23
// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal
// must have written, in order for the state migration to go smoothly.
// An upgrade must implement this struct, and then set it in the app.go.
// The app.go will then define the handler.
Copy link
Contributor

Choose a reason for hiding this comment

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

obviously it defines a struct, dont need to say that

app/upgrades/types.go Show resolved Hide resolved
expPass bool
}{
{
"Test that upgrade does not panic and sets correct parameters",
Copy link
Contributor

Choose a reason for hiding this comment

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

dont need 'test that'

In general you dont need to repeat contextually obvious info all the time

x/delayedack/keeper/rollapp_packet.go Outdated Show resolved Hide resolved
x/delayedack/keeper/rollapp_packet.go Show resolved Hide resolved
x/delayedack/keeper/rollapp_packet.go Show resolved Hide resolved
@@ -135,6 +135,10 @@ func (suite *DelayedAckTestSuite) TestListRollappPackets() {
revertedPackets := keeper.ListRollappPackets(ctx, types.ByStatus(commontypes.Status_REVERTED))
suite.Require().Equal(expectRevertedLength, len(revertedPackets))

expectRevertedLengthLimit := 4
revertedPacketsLimit := keeper.ListRollappPackets(ctx, types.ByStatus(commontypes.Status_REVERTED).Take(4))
Copy link
Contributor

Choose a reason for hiding this comment

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

4 hardcoded in Take()

Comment on lines +86 to +89
func (f RollappPacketListFilter) Take(limit int) RollappPacketListFilter {
f.Limit = limit
return f
}
Copy link
Contributor

Choose a reason for hiding this comment

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

beaut ❤️

@zale144
Copy link
Contributor Author

zale144 commented Jul 4, 2024

Biggest open question I have is, there are several calls to ListRollappPackets in our code and after this PR not all of them are batched with a limit

So why can't memory problems occur at one of those places?

The scope of this PR is to address the potential DoS when deleting the finalized and reverted packets

@zale144 zale144 force-pushed the zale144/paginate-delete-rollapp-packets branch from afe1b39 to d565784 Compare July 4, 2024 14:14
@zale144 zale144 requested a review from danwt July 4, 2024 14:14
@danwt
Copy link
Contributor

danwt commented Jul 5, 2024

The task is really to fix the DOS, the one in after epoch is just an example. If you think it should be broken down into multiple PRs then create issues to fix the other places. I don't see why adding a limit to after epoch solves the problem when the grpc queries and FinalizeRollappPackets still do everything unlimited?

Also, some of the naming is still sloppy IMO and the keepers/ module is unwarranted

@zale144
Copy link
Contributor Author

zale144 commented Jul 5, 2024

The task is really to fix the DOS, the one in after epoch is just an example. If you think it should be broken down into multiple PRs then create issues to fix the other places. I don't see why adding a limit to after epoch solves the problem when the grpc queries and FinalizeRollappPackets still do everything unlimited?

Also, some of the naming is still sloppy IMO and the keepers/ module is unwarranted

The FinalizeRollappPackets issue is tracked separately and will be addressed in another PR. For this one, it's just preventing out of bonds memory use.


for _, packet := range toDeletePackets {
// copy 'packet' not needed since Go 1.22
_ = osmoutils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's log any case of deletion error

)

const (
defaultEpochIdentifier = "hour"
defaultEpochIdentifier = "hour"
defaultDeletePacketBatchSize = 1000
Copy link
Contributor

Choose a reason for hiding this comment

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

The default valuie should be much higher IMO
the goal of this value is to be PROTECTION against memory exhasution, not optimization tuning

e.Logger(ctx).Debug("Deleting rollapp packets", "num_packets", len(toDeletePackets))

for _, packet := range toDeletePackets {
// copy 'packet' not needed since Go 1.22
Copy link
Contributor

Choose a reason for hiding this comment

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

comment is irrelevant

@mtsitrin
Copy link
Contributor

mtsitrin commented Jul 8, 2024

@zale144 as discussed

  1. pls split the deletion across multiple iterations
  2. document the assumptions we discussed

@danwt u right regarding the finalization and queries

  1. finalization will be handled in different issue (https://github.com/dymensionxyz/research/issues/327)
  2. grpc queries are expected to be blocked and not publicly facing (@omritoptix )

app/app.go Outdated

v3upgrade "github.com/dymensionxyz/dymension/v3/app/upgrades/v3"
)
/* ---------------------------- upgrade handlers ---------------------------- */)
Copy link
Contributor

Choose a reason for hiding this comment

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

this seperator is misplaced

@mtsitrin mtsitrin merged commit 1b11625 into main Jul 9, 2024
73 of 89 checks passed
@mtsitrin mtsitrin deleted the zale144/paginate-delete-rollapp-packets branch July 9, 2024 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants