Skip to content

Commit

Permalink
add check overflow/underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
linhpn99 committed Aug 30, 2024
1 parent 043ccbc commit d32ed5f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/overflow"
"github.com/pmezard/go-difflib/difflib"
)

Expand Down Expand Up @@ -594,7 +595,7 @@ func (tb *testBanker) SendCoins(from, to crypto.Bech32Address, amt std.Coins) {
tb.coinTable[from] = frest
// Second, add to 'to'.
// NOTE: even works when from==to, due to 2-step isolation.
tcoins, _ := tb.coinTable[to]
tcoins := tb.coinTable[to]

Check warning on line 598 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L598

Added line #L598 was not covered by tests
tsum := tcoins.Add(amt)
tb.coinTable[to] = tsum
}
Expand All @@ -604,15 +605,27 @@ func (tb *testBanker) TotalCoin(denom string) int64 {
}

func (tb *testBanker) IssueCoin(addr crypto.Bech32Address, denom string, amt int64) {
coins, _ := tb.coinTable[addr]
coins := tb.coinTable[addr]

Check warning on line 608 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L608

Added line #L608 was not covered by tests
sum := coins.Add(std.Coins{{denom, amt}})
tb.coinTable[addr] = sum
tb.totalCoin[denom] += amt

totalCoin, ok := overflow.Add64(tb.totalCoin[denom], amt)
if !ok {
panic(fmt.Sprintf("totalCoin overflow/underflow for denom %s while adding %d", denom, amt))

Check warning on line 614 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L612-L614

Added lines #L612 - L614 were not covered by tests
}

tb.totalCoin[denom] = totalCoin

Check warning on line 617 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L617

Added line #L617 was not covered by tests
}

func (tb *testBanker) RemoveCoin(addr crypto.Bech32Address, denom string, amt int64) {
coins, _ := tb.coinTable[addr]
coins := tb.coinTable[addr]

Check warning on line 621 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L621

Added line #L621 was not covered by tests
rest := coins.Sub(std.Coins{{denom, amt}})
tb.coinTable[addr] = rest
tb.totalCoin[denom] -= amt

totalCoin, ok := overflow.Sub64(tb.totalCoin[denom], amt)
if !ok {
panic(fmt.Sprintf("totalCoin overflow/underflow for denom %s while adding %d", denom, amt))

Check warning on line 627 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L625-L627

Added lines #L625 - L627 were not covered by tests
}

tb.totalCoin[denom] = totalCoin

Check warning on line 630 in gnovm/tests/file.go

View check run for this annotation

Codecov / codecov/patch

gnovm/tests/file.go#L630

Added line #L630 was not covered by tests
}

0 comments on commit d32ed5f

Please sign in to comment.