From e1920fce124817c0b11fd42b5f5d09bb04af5d9e Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:07:58 +0100 Subject: [PATCH 1/4] fix: truncate dec when converting to cosmos legacy dec --- math/dec.go | 41 ++++++++++++++++++++++++++++++++++++++--- math/dec_test.go | 1 + 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/math/dec.go b/math/dec.go index 955b8e47e..f50900022 100644 --- a/math/dec.go +++ b/math/dec.go @@ -6,10 +6,12 @@ package math import ( "encoding/json" + "errors" "fmt" goMath "math" "math/big" "math/bits" + "strings" errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" @@ -487,6 +489,34 @@ func (x Dec) Floor() (Dec, error) { return z, nil } +// Truncate returns a Dec truncated to the given number of decimal places, without mutating x. +func (x Dec) Truncate(decimalPlaces int64) (Dec, error) { + if decimalPlaces < 0 { + return Dec{}, errors.New("decimal places must be non-negative") + } + + fullStr := x.String() + + dotIndex := strings.Index(fullStr, ".") + if dotIndex == -1 { + return x, nil + } + + endIndex := dotIndex + int(decimalPlaces) + 1 + if endIndex > len(fullStr) { + return x, nil + } + + truncatedStr := fullStr[:endIndex] + + truncatedDec, err := NewDecFromString(truncatedStr) + if err != nil { + return Dec{}, errorsmod.Wrap(err, "failed to parse truncated decimal") + } + + return truncatedDec, nil +} + // Int64 converts x to an int64 or returns an error if x cannot // fit precisely into an int64. func (x Dec) Int64() (int64, error) { @@ -593,14 +623,19 @@ func (x Dec) SdkIntTrim() (sdkmath.Int, error) { return sdkmath.NewIntFromBigInt(&r), nil } -// SdkLegacyDec converts Dec to `sdkmath.LegacyDec` +// SdkLegacyDec converts Dec to `sdkmath.LegacyDec`, truncating it if necessary. // can return nil if the value is not representable in a LegacyDec func (x Dec) SdkLegacyDec() (sdkmath.LegacyDec, error) { if x.IsNaN() { return sdkmath.LegacyDec{}, errorsmod.Wrap(ErrNaN, "cannot convert NaN to sdkmath.LegacyDec") } - stringRep := x.dec.Text('f') - return sdkmath.LegacyNewDecFromStr(stringRep) + + d, err := x.Truncate(18) + if err != nil { + return sdkmath.LegacyDec{}, err + } + + return sdkmath.LegacyNewDecFromStr(d.dec.Text('f')) } func (x Dec) String() string { diff --git a/math/dec_test.go b/math/dec_test.go index 632bc1a2d..9d35d1ed3 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -772,6 +772,7 @@ func TestToSdkLegacyDec(t *testing.T) { }{ {i1, i1}, {"1000000000000000000000000000000000000123456789.321000000000000000", i1}, + {"0.5555555555555555555555555555", "0.555555555555555555"}, {"123.456e6", "123456000.000000000000000000"}, {"123.456e1", "1234.560000000000000000"}, {"123.456", "123.456000000000000000"}, From 32c935d1b8092799523afd1196794cb276ca1633 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:13:40 +0100 Subject: [PATCH 2/4] fix: avoir returning errors from mint begin blocker --- x/mint/module/abci.go | 28 ++++++++++++++-------------- x/mint/module/module.go | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/x/mint/module/abci.go b/x/mint/module/abci.go index 63494d29f..45518ee08 100644 --- a/x/mint/module/abci.go +++ b/x/mint/module/abci.go @@ -3,7 +3,7 @@ package mint import ( "context" - "cosmossdk.io/errors" + errorsmod "cosmossdk.io/errors" "github.com/allora-network/allora-chain/x/mint/keeper" "github.com/allora-network/allora-chain/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,7 +17,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { moduleParams, err := k.Params.Get(ctx) if err != nil { - return err + return errorsmod.Wrap(err, "could not get module params") } // if emissions are not enabled, do nothing if !moduleParams.EmissionEnabled { @@ -26,30 +26,30 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { // Get the balance of the "ecosystem" module account ecosystemBalance, err := k.GetEcosystemBalance(ctx, moduleParams.MintDenom) if err != nil { - return err + return errorsmod.Wrap(err, "could not get ecosystem balance") } blockHeight := uint64(sdkCtx.BlockHeight()) blockEmission, err := k.PreviousBlockEmission.Get(ctx) if err != nil { - return err + return errorsmod.Wrap(err, "could not get previous block emission") } ecosystemMintSupplyRemaining, err := k.GetEcosystemMintSupplyRemaining(sdkCtx, moduleParams) if err != nil { - return err + return errorsmod.Wrap(err, "could not get ecosystem mint supply remaining") } blocksPerMonth, err := k.GetParamsBlocksPerMonth(ctx) if err != nil { - return err + return errorsmod.Wrap(err, "could not get blocks per month") } vPercentADec, err := k.GetValidatorsVsAlloraPercentReward(ctx) if err != nil { - return err + return errorsmod.Wrap(err, "could not get validators vs allora percent reward") } vPercent, err := vPercentADec.SdkLegacyDec() if err != nil { - return err + return errorsmod.Wrap(err, "could not convert validators vs allora percent reward to legacy dec") } // every month on the first block of the month, update the emissions rate if blockHeight%blocksPerMonth == 1 { // easier to test when genesis starts at 1 @@ -69,7 +69,7 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { vPercent, ) if err != nil { - return errors.Wrap(err, "error recalculating target emission") + return errorsmod.Wrap(err, "could not recalculate target emission") } } // if the expected amount of emissions is greater than the balance of the ecosystem module account @@ -82,16 +82,16 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { coins := sdk.NewCoins(sdk.NewCoin(moduleParams.MintDenom, tokensToMint)) err = k.MintCoins(sdkCtx, coins) if err != nil { - return err + return errorsmod.Wrap(err, "could not mint coins") } err = k.MoveCoinsFromMintToEcosystem(sdkCtx, coins) if err != nil { - return err + return errorsmod.Wrap(err, "could not move coins from mint to ecosystem") } // then increment the recorded history of the amount of tokens minted err = k.AddEcosystemTokensMinted(ctx, tokensToMint) if err != nil { - return err + return errorsmod.Wrap(err, "could not add ecosystem tokens minted") } types.EmitNewEcosystemTokenMintSetEvent(sdkCtx, blockHeight, tokensToMint) } @@ -105,11 +105,11 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { coinsAlloraRewards := sdk.NewCoins(sdk.NewCoin(moduleParams.MintDenom, alloraRewardsCut)) err = k.PayValidatorsFromEcosystem(sdkCtx, coinsValidator) if err != nil { - return err + return errorsmod.Wrap(err, "could not pay validators from ecosystem") } err = k.PayAlloraRewardsFromEcosystem(sdkCtx, coinsAlloraRewards) if err != nil { - return err + return errorsmod.Wrap(err, "could not pay allora rewards from ecosystem") } types.EmitNewRewardCurrentBlockEmissionEvent(sdkCtx, blockHeight, alloraRewardsCut) return nil diff --git a/x/mint/module/module.go b/x/mint/module/module.go index 205d889c5..a2dbf5019 100644 --- a/x/mint/module/module.go +++ b/x/mint/module/module.go @@ -169,7 +169,7 @@ func (am AppModule) BeginBlock(ctx context.Context) error { if err != nil { am.keeper.Logger(ctx).Error("BeginBlocker error! ", err) } - return err + return nil } // From 84e06cbbcba4b11ddfc0ac0bdb7aa19447427ad2 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:41:45 +0100 Subject: [PATCH 3/4] docs: update changelog --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2be1949da..f85a824c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security +# v0.8.3 + +### Added + +### Changed + +### Deprecated + +### Removed + +### Fixed + +* [#749](https://github.com/allora-network/allora-chain/pull/749) Prevent conversion failure wither cosmos legacy dec + +### Security + # v0.8.2 ### Added From 57875e3ad11b8b05cbda28ba6c948cc6199b344b Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:59:51 +0100 Subject: [PATCH 4/4] ci(lint): force go version to 1.23.6 --- .github/workflows/golangci-lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 486693a65..92ee493ca 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: stable + go-version: 1.23.6 - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: