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

Add guidelines for ValidateBasic #10680

Closed
4 tasks
robert-zaremba opened this issue Dec 6, 2021 · 16 comments · Fixed by #10983
Closed
4 tasks

Add guidelines for ValidateBasic #10680

robert-zaremba opened this issue Dec 6, 2021 · 16 comments · Fixed by #10983
Assignees
Labels
T:Docs Changes and features related to documentation.

Comments

@robert-zaremba
Copy link
Collaborator

robert-zaremba commented Dec 6, 2021

Summary

We don't have a clear guidelines about the ValidateBasic. We should have a good recommendation how to use it.

Problem Definition

ValidateBasic is happening during the CheckTx phase and it doesn't have access to the state. So it can verify only the current object. For complex objects, it can be an intensive operation.

Proposal

In my opinion the ValidateBasic should be strictly minimum to allow the chain charge a gas. So we shouldn't verify all fields there, but only minimum to allow running a transaction and charge user if he is not creating a correct transaction. The reason is that it costs a CPU to do ValidateBasic and in an extreme case, an attacker could DDOS the chain.


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@robert-zaremba robert-zaremba added the T:Docs Changes and features related to documentation. label Dec 6, 2021
@robert-zaremba robert-zaremba self-assigned this Dec 6, 2021
@amaury1093
Copy link
Contributor

In my opinion, ValidateBasic should be as strict as possible.

Maybe we can charge a small constant fee in ValidateBasicMiddleware, so that it's not free computation.

@alpe
Copy link
Contributor

alpe commented Dec 20, 2021

I think it would make sense to view this with regards to the different processes to come up with a good plan.
A) mempool checkTX
B) mempool re-checkTX
C) DeliverTX
D) Simulation queries

A)+B)+D) are free of charge for the caller. C) fees depends on the min gas fee set on the node and can be 0.

@robert-zaremba
Copy link
Collaborator Author

For module design I have the following pattern:

  • msg.ValidateBasic() -- minimalistic, because it's called in CheckTx and user doesn't pay any gas if it fails.
  • msg.Validate(...) -- possible stateful, all strict validation, called at the beginning of Msg RPC method. Moreover we can charge extra fee here.

@aaronc
Copy link
Member

aaronc commented Jan 14, 2022

  • msg.Validate(...) -- possible stateful, all strict validation, called at the beginning of Msg RPC method. Moreover we can charge extra fee here.

We can't really make an interface method stateful because it has no access to state. Also I'd like to move away from interface methods to handlers (#10368) and I think this should be included in documentated guidelines. I do think we should allow modules to have stateful CheckTx handlers for messages, however, which I think accomplishes what you intend here.

Also, msg.Validate() could be another stateless method checked in CheckTx and generated by https://github.com/envoyproxy/protoc-gen-validate

@robert-zaremba
Copy link
Collaborator Author

We can't really make an interface method stateful because it has no access to state.

Here, msg.Validate(...) is not part of any interface. It is called manually and it's argument may vary (depending on the msg type), eg: msgA.Validate(ctx.BlockTime(), msgB.Validate(otherObject), where otherObject` is a relate object from the state.

@sunnya97
Copy link
Member

The nice thing about msg.ValidateBasic is that it can be run by a client with no access to the state itself, such as a standalone CLI.

@aaronc
Copy link
Member

aaronc commented Jan 14, 2022

We can't really make an interface method stateful because it has no access to state.

Here, msg.Validate(...) is not part of any interface. It is called manually and it's argument may vary (depending on the msg type), eg: msgA.Validate(ctx.BlockTime(), msgB.Validate(otherObject), where otherObject` is a relate object from the state.

Okay makes sense. It still would be better to do that with a handler than an instance method IMHO because of the interface/api module issues

@robert-zaremba
Copy link
Collaborator Author

It still would be better to do that with a handler than an instance method

OK, this will follow the new app wiring directions. The main thing to agree though is the ValidateBasic guideline.

@alexanderbez
Copy link
Contributor

What is the exact proposal on ValidateBasic guidelines? Applications are able to implement these checks however they wish.

@robert-zaremba
Copy link
Collaborator Author

@alexanderbez this is an interface method, so we should document the best practice. Like I wrote above, doing too much in ValidateBasic is bad from economic perspective.

@robert-zaremba
Copy link
Collaborator Author

I like @AmauryM idea of adding / updating middleware for charging a small fee for ValidateBasic -> let's move it to other issue: #10984

@alexanderbez
Copy link
Contributor

I'm not sure I agree with that perspective. I agree with @AmauryM's original post. ValidateBasic should check every possible (stateless) thing, i.e. it should be strict.

@robert-zaremba
Copy link
Collaborator Author

Then it should be called ValidateAllStateless ;)
@alexanderbez Why do you think we should do non trivial operations which are not needed in the to pass the minimum middleware set?

In Ethereum, only a strict minimum is checked on the mempool level and as much as possible is passed to the user - which is happening during tx execution.

@alexanderbez
Copy link
Contributor

alexanderbez commented Jan 20, 2022

I don't think it matters really what you call it, the point is the function does as many checks as possible to ensure the message is "valid". And I would say they are needed. Anything that could make a message invalid (which doesn't depend on state), should be checked IMO.

@aaronc
Copy link
Member

aaronc commented Jan 20, 2022

So generally checking fields and anything in memory is going to be cheaper than any sort of stateful processing. The Basic here refers to stateless as I understand it. Maybe it's a misnomer, but sounds like the guidance should be as strict as possible but fully stateless.

@robert-zaremba
Copy link
Collaborator Author

My problem with "as strict as possible" is that even some stateless operations may have significant cost (attribute decompression, some math, proof verification) and user should pay for it to avoid network spamming. In the PR I created, I can update the language in the guideline to:

Hence, we should only do the most necessary stateless sanity checks to enable middleware operations (eg: parsing the required signer accounts to validate a signature by a middleware) or stateless cheap operation not impacting the CheckTx phase performance.

How about that?

@mergify mergify bot closed this as completed in #10983 Jan 21, 2022
mergify bot pushed a commit that referenced this issue Jan 21, 2022
## Description

Closes: #10680

---

### Author Checklist

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

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
mergify bot pushed a commit that referenced this issue Jan 21, 2022
## Description

Closes: #10680

---

### Author Checklist

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

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 87bb06c)
tac0turtle pushed a commit that referenced this issue Jan 23, 2022
## Description

Closes: #10680

---

### Author Checklist

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

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 87bb06c)

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
JimLarson pushed a commit to agoric-labs/cosmos-sdk that referenced this issue Jul 7, 2022
## Description

Closes: cosmos#10680

---

### Author Checklist

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

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 87bb06c)

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T:Docs Changes and features related to documentation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants