Skip to content

Commit

Permalink
Get rid of math/overflow in stdlibs. Fix generate for git worktrees
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes committed Dec 19, 2024
1 parent dc2327a commit 1317314
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 24 deletions.
1 change: 0 additions & 1 deletion gnovm/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 4 additions & 22 deletions gnovm/stdlibs/std/coins.gno
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package std

import (
"math/overflow"
"strconv"
)
import "strconv"

// NOTE: this is selectively copied over from tm2/pkgs/std/coin.go

Expand Down Expand Up @@ -56,13 +53,7 @@ func (c Coin) IsEqual(other Coin) bool {
// An invalid result panics.
func (c Coin) Add(other Coin) Coin {
mustMatchDenominations(c.Denom, other.Denom)

sum, ok := overflow.Add64(c.Amount, other.Amount)
if !ok {
panic("coin add overflow/underflow: " + strconv.Itoa(int(c.Amount)) + " +/- " + strconv.Itoa(int(other.Amount)))
}

c.Amount = sum
c.Amount += other.Amount
return c
}

Expand All @@ -72,13 +63,7 @@ func (c Coin) Add(other Coin) Coin {
// An invalid result panics.
func (c Coin) Sub(other Coin) Coin {
mustMatchDenominations(c.Denom, other.Denom)

dff, ok := overflow.Sub64(c.Amount, other.Amount)
if !ok {
panic("coin sub overflow/underflow: " + strconv.Itoa(int(c.Amount)) + " +/- " + strconv.Itoa(int(other.Amount)))
}
c.Amount = dff

c.Amount -= other.Amount
return c
}

Expand Down Expand Up @@ -113,10 +98,7 @@ func NewCoins(coins ...Coin) Coins {

for _, coin := range coins {
if currentAmount, exists := coinMap[coin.Denom]; exists {
var ok bool
if coinMap[coin.Denom], ok = overflow.Add64(currentAmount, coin.Amount); !ok {
panic("coin sub overflow/underflow: " + strconv.Itoa(int(currentAmount)) + " +/- " + strconv.Itoa(int(coin.Amount)))
}
coinMap[coin.Denom] = currentAmount + coin.Amount
} else {
coinMap[coin.Denom] = coin.Amount
}
Expand Down
3 changes: 2 additions & 1 deletion misc/genstd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func findDirs() (gitRoot string, relPath string, err error) {
}
p := wd
for {
if s, e := os.Stat(filepath.Join(p, ".git")); e == nil && s.IsDir() {
// .git is normally a directory, or a file in case of a git worktree.
if _, e := os.Stat(filepath.Join(p, ".git")); e == nil {
// make relPath relative to the git root
rp := strings.TrimPrefix(wd, p+string(filepath.Separator))
// normalize separator to /
Expand Down

0 comments on commit 1317314

Please sign in to comment.