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

Allow Counter increment by more than just 1 #3296

Closed
tab00 opened this issue Mar 25, 2022 · 12 comments
Closed

Allow Counter increment by more than just 1 #3296

tab00 opened this issue Mar 25, 2022 · 12 comments

Comments

@tab00
Copy link

tab00 commented Mar 25, 2022

After a batch mint of ERC1155 NFTs I want to be able to increment the total supply count like this:

_mintBatch(_receiver, ids, amounts, "");
supply.increment(ids.length);

However currently increment() can only increase the counter by 1:

function increment(Counter storage counter) internal {

So I have to resort to using a loop:

_mintBatch(_receiver, ids, amounts, "");
for (uint256 i = 0; i < ids.length; i++) supply.increment();

or not use Counters and do supply += ids.length;

Please allow increment() to increase the counter by more than just 1.

@Amxx Amxx added the good first issue Low hanging fruit for new contributors to get involved! label Mar 25, 2022
@mw2000
Copy link
Contributor

mw2000 commented Mar 26, 2022

I propose adding overloaded increment and decrement functionswith the following signatures:

function increment(Counter storage counter, uint256 amount)
function decrement(Counter storage counter, uint256 amount)

@tab00
Copy link
Author

tab00 commented Mar 26, 2022

function increment(Counter storage counter, uint256 amount)
function decrement(Counter storage counter, uint256 amount)

I was thinking the same.

Here are examples of simple counters that take an amount value to increment by that I've used before:
https://github.com/awwx/meteor-mongo-counter
https://www.npmjs.com/package/mongodb-counter

@philipSKYBIT
Copy link

I need this too. I might try a pull request.

@Amxx
Copy link
Collaborator

Amxx commented Mar 26, 2022

Hello @tab00

I understand your request, but I'm not super comfortable with where this is heading.

We already have:

  • increment(Counter storage counter)
  • decrement(Counter storage counter)
  • reset(Counter storage counter)

You'd like to add

  • increment(Counter storage counter, uint256)
  • decrement(Counter storage counter, uint256)

Then someone will ask for set(Counter storage counter, uint256) ... and we'll just have a simple uint256 wrapper ... which doesn't sound like it makes much sense (why not just use uint256?)

@frangio What do you think? Should we expand the Counters library further? Should we use UDVT instead of structs?

Note: it's not "clean" but if you really want to do that now, you can always do counter._value += 17; or counter._value -= 42;

@tab00
Copy link
Author

tab00 commented Mar 26, 2022

why not just use uint256

You could ask the same with how Counters is now - why not just use count += 1, count -= 1, count = 0 instead of increment(), decrement(), reset()?

I was thinking a value of Counters is its safety checks. e.g. currently there is a check that prevents underflow.

reset(Counter storage counter, uint256 newValue) would actually be a good idea too. These proposed functions would be more generalized versions of what currently exists, and if implemented then the original functions could call them by passing 1 or 0 for the amounts.

Note: it's not "clean" but if you really want to do that now, you can always do counter._value += 17; or counter._value -= 42;

That's a possible workaround in the meantime and definitely more gas-efficient, though there is a comment that explicitly says _value should never be directly accessed:

// This variable should never be directly accessed by users of the library: interactions must be restricted to

@mw2000
Copy link
Contributor

mw2000 commented Mar 26, 2022

I agree with @tab00 on this, I think the proposed new functions to the Counters library would be good in terms of the added functionality mixed in with the safety checks.

@dhxmo
Copy link

dhxmo commented Mar 27, 2022

hi @Amxx I'd like to contribute to this issue. Are we thinking of changing increment, decrement and reset function to include a newValue or only the reset function?

also, is there any resource I can study to understand the necessary safety checks for this op? thank you.

@hack3r-0m
Copy link

A better solution would be to create a new library CounterExtended.sol which will not break existing apis.

@Amxx
Copy link
Collaborator

Amxx commented Mar 28, 2022

A better solution would be to create a new library CounterExtended.sol which will not break existing apis.

What would this new library provide over just using uint256? Adding code creates discoverability and maintainability issues.

@frangio frangio removed the good first issue Low hanging fruit for new contributors to get involved! label Mar 28, 2022
@Amxx
Copy link
Collaborator

Amxx commented Mar 28, 2022

One important thing with the Counter is that by increasing by 1, we can very safely assume that increment will not overflow, and we can "uncheck" it. As soon as we increment by arbitrary values, we can no longer make this assumption, so we should re-add the overflow checks, which IMO defeats the whole point of having Counter.

@frangio
Copy link
Contributor

frangio commented Mar 31, 2022

Yes, the intent of the Counter abstraction is to represent a value that is known to never overflow. Incrementing by arbitrary uint256 removes that benefit entirely. We could consider incrementing by small integer types like uint8 if that's seen as valuable, but I don't think it's really necessary.

@frangio
Copy link
Contributor

frangio commented Sep 5, 2023

Counters are being removed in 5.0.

@frangio frangio closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2023
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

No branches or pull requests

7 participants