From 0891ffec63fb9ced4abde75e6824057e88c9df41 Mon Sep 17 00:00:00 2001 From: codelover <62229912+DedicatedDev@users.noreply.github.com> Date: Thu, 10 Aug 2023 13:55:44 +0300 Subject: [PATCH] chore: uin64 overflow fix (#3610) * 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 Co-authored-by: Danilo Pantani --- changelog.md | 6 +++-- ignite/pkg/cosmosfaucet/cosmosfaucet.go | 10 ++++----- ignite/pkg/cosmosfaucet/transfer.go | 30 ++++++++++++------------- ignite/services/chain/faucet.go | 7 +++--- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/changelog.md b/changelog.md index d3da581fbb..4b682d0de0 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/ignite/pkg/cosmosfaucet/cosmosfaucet.go b/ignite/pkg/cosmosfaucet/cosmosfaucet.go index ec627c40c8..e64ba81f10 100644 --- a/ignite/pkg/cosmosfaucet/cosmosfaucet.go +++ b/ignite/pkg/cosmosfaucet/cosmosfaucet.go @@ -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 @@ -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 } } @@ -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"}, } @@ -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 { diff --git a/ignite/pkg/cosmosfaucet/transfer.go b/ignite/pkg/cosmosfaucet/transfer.go index 57332f0f45..37a437e648 100644 --- a/ignite/pkg/cosmosfaucet/transfer.go +++ b/ignite/pkg/cosmosfaucet/transfer.go @@ -7,8 +7,8 @@ import ( "sync" "time" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - chaincmdrunner "github.com/ignite/cli/ignite/pkg/chaincmd/runner" ) @@ -16,32 +16,32 @@ import ( 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) } } } @@ -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, ) } } diff --git a/ignite/services/chain/faucet.go b/ignite/services/chain/faucet.go index 02e16cc996..07cc1c7c02 100644 --- a/ignite/services/chain/faucet.go +++ b/ignite/services/chain/faucet.go @@ -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" @@ -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 { @@ -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 != "" {