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

feat(examples): added check for overflow/underflow #2874

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

MalekLahbib
Copy link
Contributor

@MalekLahbib MalekLahbib commented Sep 30, 2024

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@MalekLahbib MalekLahbib requested review from a team as code owners September 30, 2024 16:43
@MalekLahbib MalekLahbib requested review from sw360cab and piux2 and removed request for a team September 30, 2024 16:43
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Sep 30, 2024
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 60.95%. Comparing base (14acb90) to head (1d6f6e0).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2874   +/-   ##
=======================================
  Coverage   60.95%   60.95%           
=======================================
  Files         564      564           
  Lines       75273    75273           
=======================================
+ Hits        45884    45886    +2     
  Misses      26017    26017           
+ Partials     3372     3370    -2     
Flag Coverage Δ
contribs/gnodev 61.46% <ø> (ø)
contribs/gnofaucet 14.46% <ø> (ø)
gno.land 67.92% <ø> (ø)
gnovm 65.77% <ø> (ø)
misc/genstd 80.54% <ø> (ø)
misc/logos 20.23% <ø> (ø)
tm2 62.03% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

examples/gno.land/p/demo/grc/grc20/banker.gno Outdated Show resolved Hide resolved
examples/gno.land/p/demo/grc/grc20/banker.gno Outdated Show resolved Hide resolved
@@ -12,6 +12,7 @@ var (
ErrInsufficientAllowance = errors.New("insufficient allowance")
ErrInvalidAddress = errors.New("invalid address")
ErrCannotTransferToSelf = errors.New("cannot send transfer to self")
ErrUnderflow = errors.New("the amount is higher than the total supply")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be used somewhere? It should be deleted if not.

Copy link
Contributor

@leohhhn leohhhn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the error(s) you defined, and also please write tests for these cases

@MalekLahbib
Copy link
Contributor Author

Use the error(s) you defined, and also please write tests for these cases

@moul @deelawn @leohhhn I'm trying to figure out how is it possible to have underflow in Burn function, but for me it seems to be impossible as every function is protected with conditions. I'm doing some research to learn about erc20 known vulnerabilities/issues and figure out how to protect grc20 functions from them. I'm thinking about using "math/overflow' functions (as Add64, Sub64...) to add a protection/check layer.

@Kouteki Kouteki added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 3, 2024
Comment on lines +1 to +21
package main

import (
banker "gno.land/p/demo/grc/grc20"
"gno.land/p/demo/testutils"
)

const uint64_max = 1<<64 - 1

func main() {
b := banker.NewBanker("Dummy", "DUMMY", 6)
var owner = testutils.TestAddress("owner")
// Mint uint64_max - 1000 tokens for owner
b.Mint(owner, uint64_max-1000)
// Try to mint 1001 tokens for owner, should fail because it exceeds the limit
println(b.Mint(owner, 1001).Error())

}

// Output:
// you can't mint more than 1000 tokens
Copy link
Contributor

@leohhhn leohhhn Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, filetests should be discouraged. We should strive to write proper tests, that are more Go idiomatic.
They are more readable & more understandable by the broader community, and provide actual type checking on the errors. On the other hand, filetests are supereasy to write, but in the end just provide a println output.

Please write a unit test for this.

@Kouteki Kouteki removed the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Oct 4, 2024
Comment on lines +62 to +63
err := ufmt.Sprintf("you can't mint more than %d tokens", diff)
return errors.New(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lines can be shortened using ufmt.Errorf.

Suggested change
err := ufmt.Sprintf("you can't mint more than %d tokens", diff)
return errors.New(err)
return ufmt.Errorf("you can't mint more than %d tokens", diff)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: No status
Status: In Review
Development

Successfully merging this pull request may close these issues.

5 participants