Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix feature 0.8.3 #749

Merged
merged 4 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 38 additions & 3 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
28 changes: 14 additions & 14 deletions x/mint/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/mint/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

//
Expand Down
Loading