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

[R4R] Cache-wrap context during ante handler exec #2781

Merged
merged 21 commits into from
Nov 16, 2018

Conversation

alexanderbez
Copy link
Contributor

@alexanderbez alexanderbez commented Nov 12, 2018

  • Cache-wrap the context (multi-store) during the ante handler execution in runTx. State will only be persisted if the ante handler does fail (abort signaled).
  • Made getState a method of BaseApp -- not sure why it wasn't before as we were passing the reference in directly as a parameter.
  • Added initial BaseApp documentation.

I believe the existing base app unit tests cover the cases that need to be covered.

closes: #2772


  • Targeted PR against correct branch (see CONTRIBUTING.md)

  • Linked to github-issue with discussion and accepted design OR link to spec that describes this work.

  • Wrote tests

  • Updated relevant documentation (docs/)

  • Added entries in PENDING.md with issue #

  • rereviewed Files changed in the github PR explorer


For Admin Use:

  • Added appropriate labels to PR (ex. wip, ready-for-review, docs)
  • Reviewers Assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

@codecov
Copy link

codecov bot commented Nov 12, 2018

Codecov Report

Merging #2781 into develop will increase coverage by 0.02%.
The diff coverage is 61.9%.

@@             Coverage Diff             @@
##           develop    #2781      +/-   ##
===========================================
+ Coverage    56.74%   56.77%   +0.02%     
===========================================
  Files          131      131              
  Lines         8564     8545      -19     
===========================================
- Hits          4860     4851       -9     
+ Misses        3371     3363       -8     
+ Partials       333      331       -2

@alexanderbez alexanderbez changed the title [WIP] Cache-wrap context during ante handler exec [R4R] Cache-wrap context during ante handler exec Nov 12, 2018
Copy link
Contributor

@alessio alessio left a comment

Choose a reason for hiding this comment

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

Looks good

Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

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

See comment. Can we add a section to the SDK docs covering baseapp and the expected sequence of transactions through CheckTx / DeliverTx, explaining when ante handler state will be persisted, and explaining that a proposer can bypass CheckTx? All of these are non-obvious to developers (and quite possibly also to us).

(and then link to parts of that spec directly in this code)

baseapp/baseapp.go Outdated Show resolved Hide resolved
@alexanderbez
Copy link
Contributor Author

@cwgoes I can absolutely do that, I think that is a fantastic idea!

@alexanderbez
Copy link
Contributor Author

@cwgoes addressed your concerns about state transitions + added BaseApp docs

Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

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

Minor comments, mostly looks good.

baseapp/baseapp.go Show resolved Hide resolved
baseapp/baseapp.go Outdated Show resolved Hide resolved
baseapp/baseapp.go Outdated Show resolved Hide resolved
docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
Copy link
Member

@ebuchman ebuchman left a comment

Choose a reason for hiding this comment

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

Some questions and minor comments

docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
### CheckTx

During the execution of `CheckTx`, first the AnteHandler is executed. If the
AnteHandler fails or panics then the transaction fails. After the successful
Copy link
Member

Choose a reason for hiding this comment

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

When should it panic?

Copy link
Contributor Author

@alexanderbez alexanderbez Nov 15, 2018

Choose a reason for hiding this comment

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

During any state transition that doesn't gracefully return an sdk.Result that's also not out of gas.

docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved

anteCtx := ctx
if mode == runTxModeDeliver {
anteCtx, msCache = app.cacheTxContext(ctx, txBytes, mode)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we want to do this for all modes? Otherwise eg invalid txs will change the checktx state

Copy link
Contributor Author

@alexanderbez alexanderbez Nov 15, 2018

Choose a reason for hiding this comment

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

This is the current behavior so I left as-is and I don't see any direct reason to change it, unless you do?

baseapp/baseapp.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Show resolved Hide resolved
baseapp/baseapp_test.go Show resolved Hide resolved
baseapp/baseapp.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Outdated Show resolved Hide resolved
baseapp/baseapp_test.go Outdated Show resolved Hide resolved

// execute a successful ante handler and message execution where state is
// implicitly checked by previous tx executions
tx = newTxCounter(1, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok so I guess the first arg, 1, means that the ante handler for one of the two above succeeded? But it's not clear which one.

The second 0 means that none of the msgs succeeded, but that wasn't clear either...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added explicit store checks via getting the app's state. Lmk if this still isn't clear. This logic is baked into the testing ante handler and message handler, so I'm trying to work with what I already have without making further drastic changes.

docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved
docs/sdk/core/baseapp.md Outdated Show resolved Hide resolved

The transaction execution during `DeliverTx` operates in a similar fashion to
`CheckTx`. However, state transitions caused by the AnteHandler are persisted
unless the transaction fails.
Copy link
Contributor

@jaekwon jaekwon Nov 15, 2018

Choose a reason for hiding this comment

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

state transitions that occur during the AnteHandler are persisted even when the following Handler processing logic fails.

It is possible that a malicious proposer may include a transaction in a block that fails CheckTx. In this case (i.e. the AnteHandler processing itself fails during DeliverTx), all state transitions for the offending transaction are discarded.

Copy link
Contributor Author

@alexanderbez alexanderbez Nov 15, 2018

Choose a reason for hiding this comment

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

I'm not sure what you're suggesting here @jaekwon. I'm simply stating ante handler state updates are discarded/reverted if the ante handler fails during deliverTx.

If you want rewording, can you please suggest something?

Copy link
Contributor

@jaekwon jaekwon left a comment

Choose a reason for hiding this comment

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

Please address the comments and ping me back!

@jaekwon
Copy link
Contributor

jaekwon commented Nov 15, 2018

Lol, Ethan and I left more or less the same comments.

@jaekwon jaekwon merged commit 15b6fa0 into develop Nov 16, 2018
@cwgoes cwgoes deleted the bez/2772-ante-persistence branch November 16, 2018 18:38
@alexanderbez
Copy link
Contributor Author

alexanderbez commented Nov 16, 2018

@jaekwon your change is failing on TestGaiaCLISendGenerateSignAndBroadcast due to the same reason my attempt failed previously:

ERROR: {"codespace":"sdk","code":3,"message":"Invalid sequence. Got 1, expected 2"}

I think this was the reason why I only did context caching on DeliverTx. Because before this PR we did not persist CheckTx state, only state from running messages.

@cwgoes
Copy link
Contributor

cwgoes commented Nov 16, 2018

Perhaps we need to change the testcase.

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.

Attacker validator can issue steak at AnteHandler
6 participants