Skip to content

Commit

Permalink
chore: uin64 overflow fix (#3610)
Browse files Browse the repository at this point in the history
* chore: uin64 overflow fix

* chore: uin64 overflow fix

* chore: fix memory nil point problem

* chore: migrate with sdkmath

* chore: replae sdk.Int to sdkmath.Int

* chore: add change log

* chore: remove deprecated sdk.Int

---------

Co-authored-by: Free Bird <[email protected]>
Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent e9e6ff4 commit 0891ffe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
6 changes: 4 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
### Changes

- [#3581](https://github.com/ignite/cli/pull/3581) Bump cometbft and cometbft-db in the template
- [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1`
- [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1`
- [#3522](https://github.com/ignite/cli/pull/3522) Remove indentation from `chain serve` output

### Fixes

- [#3592](https://github.com/ignite/cli/pull/3592) Fix `pkg/protoanalysis` to support HTTP rule parameter arguments
- [#3598](https://github.com/ignite/cli/pull/3598) Fix consensus param keeper constructor key in `app.go`
- [#3610](https://github.com/ignite/cli/pull/3610) Fix overflow issue of cosmos faucet in `pkg/cosmosfaucet/transfer.go` and `pkg/cosmosfaucet/cosmosfaucet.go`
- [#3618](https://github.com/ignite/cli/pull/3618) Fix TS client generation import path issue

## [`v0.27.0`](https://github.com/ignite/cli/releases/tag/v0.27.0)

### Features
-

-
- [#3505](https://github.com/ignite/cli/pull/3505) Auto migrate dependency tools
- [#3538](https://github.com/ignite/cli/pull/3538) bump sdk to `v0.47.3` and ibc to `v7.1.0`
- [#2736](https://github.com/ignite/cli/issues/2736) Add `--skip-git` flag to skip git repository initialization.
Expand Down
10 changes: 5 additions & 5 deletions ignite/pkg/cosmosfaucet/cosmosfaucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Faucet struct {

// coinsMax is a denom-max pair.
// it holds the maximum amounts of coins that can be sent to a single account.
coinsMax map[string]uint64
coinsMax map[string]sdkmath.Int

limitRefreshWindow time.Duration

Expand All @@ -81,9 +81,9 @@ func Account(name, mnemonic string, coinType string) Option {
// amount is the amount of the coin can be distributed per request.
// maxAmount is the maximum amount of the coin that can be sent to a single account.
// denom is denomination of the coin to be distributed by the faucet.
func Coin(amount, maxAmount uint64, denom string) Option {
func Coin(amount, maxAmount sdkmath.Int, denom string) Option {
return func(f *Faucet) {
f.coins = append(f.coins, sdk.NewCoin(denom, sdkmath.NewIntFromUint64(amount)))
f.coins = append(f.coins, sdk.NewCoin(denom, amount))
f.coinsMax[denom] = maxAmount
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func New(ctx context.Context, ccr chaincmdrunner.Runner, options ...Option) (Fau
f := Faucet{
runner: ccr,
accountName: DefaultAccountName,
coinsMax: make(map[string]uint64),
coinsMax: make(map[string]sdkmath.Int),
openAPIData: openAPIData{"Blockchain", "http://localhost:1317"},
}

Expand All @@ -123,7 +123,7 @@ func New(ctx context.Context, ccr chaincmdrunner.Runner, options ...Option) (Fau
}

if len(f.coins) == 0 {
Coin(DefaultAmount, DefaultMaxAmount, DefaultDenom)(&f)
Coin(sdkmath.NewInt(DefaultAmount), sdkmath.NewInt(DefaultMaxAmount), DefaultDenom)(&f)
}

if f.limitRefreshWindow == 0 {
Expand Down
30 changes: 15 additions & 15 deletions ignite/pkg/cosmosfaucet/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,41 @@ import (
"sync"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
)

// transferMutex is a mutex used for keeping transfer requests in a queue so checking account balance and sending tokens is atomic.
var transferMutex = &sync.Mutex{}

// TotalTransferredAmount returns the total transferred amount from faucet account to toAccountAddress.
func (f Faucet) TotalTransferredAmount(ctx context.Context, toAccountAddress, denom string) (totalAmount uint64, err error) {
func (f Faucet) TotalTransferredAmount(ctx context.Context, toAccountAddress, denom string) (totalAmount sdkmath.Int, err error) {
fromAccount, err := f.runner.ShowAccount(ctx, f.accountName)
if err != nil {
return 0, err
return sdkmath.NewInt(0), err
}

events, err := f.runner.QueryTxEvents(ctx,
chaincmdrunner.NewEventSelector("message", "sender", fromAccount.Address),
chaincmdrunner.NewEventSelector("transfer", "recipient", toAccountAddress))
if err != nil {
return 0, err
return sdkmath.NewInt(0), err
}

totalAmount = sdkmath.NewInt(0)
for _, event := range events {
if event.Type == "transfer" {
for _, attr := range event.Attributes {
if attr.Key == "amount" {
coins, err := sdk.ParseCoinsNormalized(attr.Value)
if err != nil {
return 0, err
return sdkmath.NewInt(0), err
}

amount := coins.AmountOf(denom).Uint64()

if amount > 0 && time.Since(event.Time) < f.limitRefreshWindow {
totalAmount += amount
amount := coins.AmountOf(denom)
if amount.GT(sdkmath.NewInt(0)) && time.Since(event.Time) < f.limitRefreshWindow {
totalAmount = totalAmount.Add(amount)
}
}
}
Expand All @@ -64,21 +64,21 @@ func (f *Faucet) Transfer(ctx context.Context, toAccountAddress string, coins sd
if err != nil {
return err
}

if f.coinsMax[c.Denom] != 0 {
if totalSent >= f.coinsMax[c.Denom] {
coinMax, found := f.coinsMax[c.Denom]
if found && !coinMax.IsNil() && !coinMax.Equal(sdkmath.NewInt(0)) {
if totalSent.GTE(coinMax) {
return fmt.Errorf(
"account has reached to the max. allowed amount (%d) for %q denom",
f.coinsMax[c.Denom],
coinMax,
c.Denom,
)
}

if (totalSent + c.Amount.Uint64()) > f.coinsMax[c.Denom] {
if (totalSent.Add(c.Amount)).GT(coinMax) {
return fmt.Errorf(
`ask less amount for %q denom. account is reaching to the limit (%d) that faucet can tolerate`,
c.Denom,
f.coinsMax[c.Denom],
coinMax,
)
}
}
Expand Down
7 changes: 4 additions & 3 deletions ignite/services/chain/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

sdkmath "cosmossdk.io/math"
chainconfig "github.com/ignite/cli/ignite/config/chain"
chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner"
"github.com/ignite/cli/ignite/pkg/cosmosfaucet"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *Chain) Faucet(ctx context.Context) (cosmosfaucet.Faucet, error) {
return cosmosfaucet.Faucet{}, fmt.Errorf("%w: %s", err, coin)
}

var amountMax uint64
var amountMax sdkmath.Int

// find out the max amount for this coin.
for _, coinMax := range conf.Faucet.CoinsMax {
Expand All @@ -98,12 +99,12 @@ func (c *Chain) Faucet(ctx context.Context) (cosmosfaucet.Faucet, error) {
return cosmosfaucet.Faucet{}, fmt.Errorf("%w: %s", err, coin)
}
if parsedMax.Denom == parsedCoin.Denom {
amountMax = parsedMax.Amount.Uint64()
amountMax = parsedMax.Amount
break
}
}

faucetOptions = append(faucetOptions, cosmosfaucet.Coin(parsedCoin.Amount.Uint64(), amountMax, parsedCoin.Denom))
faucetOptions = append(faucetOptions, cosmosfaucet.Coin(parsedCoin.Amount, amountMax, parsedCoin.Denom))
}

if conf.Faucet.RateLimitWindow != "" {
Expand Down

0 comments on commit 0891ffe

Please sign in to comment.