Comments (5)
This would more than double gas costs. It would require the map to keep track of an array of all keys and keep it updated, so that you can go through the keys when needed. It would also substantially increase code complexity. In that specific case, you can always just manually make a funders_list
array and push to it. IMO a better approach for these kinds of problems is the "poke-based" paradigm, where a user needs to poke the contract to refund themselves; this more clearly aligns gas incentives and keeps per-transaction gas requirements down.
from vyper.
I agree with the poke-based paradigm, that is mostly what I was using when I writing examples as it is the cheapeast and puts the gas costs in the right place. In some examples it is more useful (e.g. refunding in the crowdunding example) or unavoidable (e.g. calculating any sort of global statistics over members in the map).
Does gas get consumed to read data? My understanding was that gas only actually got consumed when writing out data (hence the constant
decorator). I agree it would be a substantial enough increase to gas cost for that datatype that it shouldn't be the default for a map, I don't see how overall gas costs would increase that substantially though. I think this suggestion has enough utility that it should be considered as a future feature implemented in some easier-to-use way than the current method of the range
method for iterating an array.
If you think this idea has limited utility (i.e. the need to call transactions for all the members of a map in a clear way or compute global statistics is not common), then I will close this issue.
from vyper.
Another thought, can additional code used to track membership in the mapping (e.g. funders_list
) be added as a compiler extension such that when the proposed feature is requested, a standard method to track the information is employed behind the scenes, which prevents easy mistakes from having to manually track entries (e.g. forgetting to add to the array when multiple methods update the mapping)?
This way, it is optional but standardized and only increases gas costs when necessary.
Updated example:
funders: public(wei_value[address])
# Set public getter on mapping to return list of (k, v) pairs
@constant
def is_funder(_addr: address) -> bool:
return _addr in self.funders.keys() # Evaluates to list of tracked keys
# Note: self.balance should be the same number, but ignoring that for our example
@constant
def amt_raised() -> wei_value:
sum: wei_value
# self.funders.values() evaluates to [self.funders[k] for k in self.funders.keys()]
for value in self.funders.values():
sum += value
return sum # or return sum(self.funders.values()) if sum() exists
# Not enough money was raised! Refund a person
def refund(_funder: address):
assert block.timestamp >= self.deadline and self.balance < self.goal
assert is_funder(_funder)
send(funder, self.funders[_funder])
del self.funders[_funder] # Resets value in self.funders AND removes from tracking list
Call above using [c.refund(f) for (f, _) in c.get_funders()]
which avoids gas estimate problems and refunds everyone all at once since the list of addresses is accessed as @constant (no gas) and the transactions are processed sequentially by the caller externally (e.g. all N transactions can be created at once externally instead of trying to put an upper bound on the estimate of the range()
function in the contract, putting control in the user's hands to decide how much gas s/he can spend at once)
from vyper.
@fubuloubu can this be closed?
from vyper.
Yeah, doesn't make sense to think about mappings this way, plus we have other better features/proposals (a la iterators, etc.) that will take care of the proposed here.
from vyper.
Related Issues (20)
- Kessak algorithm implementation may be vulnerable HOT 2
- Support for arm64 architecture HOT 1
- VIP: move builtin interfaces to `ethereum.ercs` HOT 2
- allow `range(x, y, bound=N)` HOT 1
- VIP [DRAFT]: allow overloaded functions
- Is the `unsafe_div` divide by zero result correct? HOT 2
- Internal Compiler Error When Compiling Oracle HOT 1
- improve error messages wrt module namespacing
- method_id Compiler Error
- VIP [DRAFT]: Safely expose `DELEGATECALL` HOT 7
- VIP: disallow the use of `self` as an address HOT 7
- VIP: add new built-in function `raw_create` HOT 1
- VIP: `__script__()` function
- VIP: native asset types HOT 14
- setval/getval dunder methods HOT 1
- VIP: stateful singleton modules with ownership hierarchy HOT 8
- VIP: instantiable stateful modules HOT 2
- update sphinx version HOT 1
- concat builtin can overrun memory
- VIP: new `@deploy` visibility
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vyper.