-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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!: limit the length of key/value #14645
Changes from 6 commits
7652876
9296c61
13edf2b
5e6bec2
ccd0f78
cf765ec
447c365
35dce19
fd4d0ac
4c30239
ecf2f83
e3c2d75
2fa8200
a4b3e97
b97cbfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
package types | ||
|
||
// AssertValidKey checks if the key is valid(key is not nil) | ||
import math "math" | ||
|
||
const ( | ||
// 32K | ||
MaxKeyLength = math.MaxUint16 | ||
// 4G | ||
MaxValueLength = math.MaxUint32 | ||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should cover all cases even in CW? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alpe do you happen to know? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is wrong, the limit is 4G, since it's uint32, I think there was no limit before? we are collecting opinions on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
deterministic state machine, as I explained in PR description. the goal is set a deterministic limit which are supported by all supported db backends, and can cover hopefully all existing use cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we need to be a little bit smaller than hard limit, because iavl leaf node encode both key and value and a few other bytes together as value in low level db, I'd prefer 2G(aka. MaxInt32) if it's enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got feedback from Mauro on CosmWasm limits for contracts. They are much lower than the suggested values: https://github.com/CosmWasm/cosmwasm/blob/main/packages/vm/src/imports.rs#L32-L35 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note there is no good reason for having different values in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to know, thanks @peterbourgon. In this case my only concern is that the current key limit is smaller than the limit that CosmWasm set before (64 KiB + wasmd prefix + iavl wrapper?) and we have no way to know who is using values close to this limit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Congratulations to this clever analysis. It's not helping though. You asked for empirical data, worried about breaking and other people have been asking for our insights. And yes, in a smart contract scenario we cannot control what people are using right now within the currently enforced range. |
||
) | ||
|
||
// AssertValidKey checks if the key is valid(key is not nil and within length limit) | ||
func AssertValidKey(key []byte) { | ||
if len(key) == 0 { | ||
panic("key is nil") | ||
webmaster128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if len(key) > MaxKeyLength { | ||
panic("key is too large") | ||
} | ||
} | ||
|
||
// AssertValidValue checks if the value is valid(value is not nil) | ||
// AssertValidValue checks if the value is valid(value is not nil and within length limit) | ||
func AssertValidValue(value []byte) { | ||
if value == nil { | ||
panic("value is nil") | ||
} | ||
if len(value) > MaxValueLength { | ||
panic("value is too large") | ||
} | ||
tac0turtle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MaxUint16
is picked so the length can be encoded in 2bytes with fixed length, should be more than enough for all use cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2^16 bytes (64 KiB) is definitely not enough for CosmWasm values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value is
2^32
,2^16
is for keys.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any length limits for keys on DBs used that are shorter than 2^32? Personally, I don't see any reasons why we should have a shorter limit for keys than for values. The original motivation was deterministic behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👆 Data structures like sets (i.e. a map from key to nothing) store all its data in keys. So treating keys and value differently should be avoided if possible IMO.