-
-
Notifications
You must be signed in to change notification settings - Fork 802
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
VIP: Dynamic Arrays w/ Sizing #1440
Comments
Note from meeting: |
Something like @public
def foo(a: Array(uint256, max_size=5)) -> uint256:
# reverts if len(a) > 5
sum: uint256 = 0
for i in a:
sum += i
return sum |
|
@fubuloubu et al., veteran Pythonista new to Vyper here. I'd happy to put up a PR with this implementation of Dynamic Arrays w/size boundaries if one of you can clue me in as to where exactly in the codebase it should go, and possibly point me to an existing type syntax example in the code for another datatype. Thanks! |
@rayrrr really appreciate that! We've been waiting on the type checker refactor as that will significantly change how we define new syntax (and hopefully make it easier and more scalable), which make this much easier to drop and go. We already have some dynamic types (bytes and strings), that I was hoping to refactor to this new paradigm ( |
Okay, sounds like a bit of yak shaving to do here...how can I and/or others help with the type checker refactor to help move this forward? |
Note: reverted Accepted because of significant changes to proposal |
Note: for v0.2.0, ensure that |
Breaking updates made in #2080, removing from 0.2.0 milestone |
added in #2556 |
Motivation
For inter-operation with Solidity and other languages that do not take Vyper's conservative approach to arrays, it is important to have a syntax that retains our principle of protecting against gas exhaustion attacks (and gas estimation) while encoding as a dynamic array type via the ABI.
Specification
This looks like defining a new "meta" type called
DynArray
:In general, using this syntax, one can describe the minimum and the maximum of what a method will accept for a dynamic array. Min and max size have to be specified via
min_size:max_size
after the type that a dynamic array takes. If only a number is specified,min_size
is assumed to be 0. If both are specified, the restrictionmin_size < max_size
is enforced. Both can be a named constant.When the calldata is parsed by the method, it will do an array bounds check to ensure the array's
len
is within the maximum and/or minimum specified. It will then operate on that array as a dynamic array, for example using hefor item in list
syntax for iteration.This type of list will have limited abilities versus a static array. For example, it might not be allowed to be passed as an argument to an inner private function or to be referenced by index directly (these are possibilities, whatever the compiler needs to do to ensure safe usage of these types is TBD). This type of list may also not be allowed to be a state variable declaration (out-of-gas lock bugs possible), and potentially be disallowed from memory declaration usage as well.
Also, the built-in function
len(...)
should be expanded to allow the ability to query the current length variable of anArray
type (currently it only queries the size ofbytes
objects)Backwards Compatibility
The current syntax for
bytes[N]
/string[N]
uses this same behavior, so to make both syntax consistent we are changing toBytes[N]
/Bytes[M:N]
andString[N]
/String[M:N]
.Dependencies
No dependencies
Copyright
Copyright and related rights waived via CC0
The text was updated successfully, but these errors were encountered: