Skip to content

Commit

Permalink
wip: custom modules refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockwarr committed Dec 22, 2023
1 parent 7e9c182 commit 71cc05f
Show file tree
Hide file tree
Showing 32 changed files with 769 additions and 764 deletions.
5 changes: 3 additions & 2 deletions testutil/keeper/tax.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
sdktypes "github.com/cosmos/cosmos-sdk/store/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
Expand All @@ -23,8 +24,8 @@ func TaxKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
stateStore.MountStoreWithDB(storeKey, sdktypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdktypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
Expand Down
23 changes: 12 additions & 11 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

sdkmath "cosmossdk.io/math"
"github.com/Nolus-Protocol/nolus-core/x/mint/keeper"
"github.com/Nolus-Protocol/nolus-core/x/mint/types"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -43,13 +44,13 @@ func calcTimeDifference(blockTime sdk.Uint, prevBlockTime sdk.Uint, maxMintableS

func calcTokens(blockTime sdk.Uint, minter *types.Minter, maxMintableSeconds sdk.Uint) sdk.Uint {
if minter.TotalMinted.GTE(types.MintingCap) {
return sdk.ZeroUint()
return sdkmath.ZeroUint()
}

if minter.PrevBlockTimestamp.IsZero() {
// we do not know how much time has passed since the previous block, thus nothing will be mined
minter.PrevBlockTimestamp = blockTime
return sdk.ZeroUint()
return sdkmath.ZeroUint()
}

nsecPassed := calcTimeDifference(blockTime, minter.PrevBlockTimestamp, maxMintableSeconds)
Expand Down Expand Up @@ -78,11 +79,11 @@ func calcTokens(blockTime sdk.Uint, minter *types.Minter, maxMintableSeconds sdk
}

func updateMinter(minter *types.Minter, blockTime sdk.Uint, newNormTime sdk.Dec, newlyMinted sdk.Uint) sdk.Uint {
if newlyMinted.LT(sdk.ZeroUint()) {
if newlyMinted.LT(sdkmath.ZeroUint()) {
// Sanity check, should not happen. However, if this were to happen,
// do not update the minter state (primary the previous block timestamp)
// and wait for a new block which should increase the minted amount
return sdk.ZeroUint()
return sdkmath.ZeroUint()
}
minter.NormTimePassed = newNormTime
minter.PrevBlockTimestamp = blockTime
Expand All @@ -96,11 +97,11 @@ func predictMintedByIntegral(totalMinted sdk.Uint, normTimePassed, timeAhead sdk
timeAheadNs := timeAhead.Mul(nanoSecondsInMonth).TruncateInt()
normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdk.Uint(timeAheadNs)))
if normTimePassed.GT(normTimeInFuture) {
return sdk.ZeroUint(), errTimeInFutureBeforeTimePassed
return sdkmath.ZeroUint(), errTimeInFutureBeforeTimePassed
}

if normTimePassed.GTE(types.MonthsInFormula) {
return sdk.ZeroUint(), nil
return sdkmath.ZeroUint(), nil
}

// integral minting is caped to the 96th month
Expand All @@ -118,12 +119,12 @@ func predictMintedByFixedAmount(totalMinted sdk.Uint, normTimePassed, timeAhead

normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdk.Uint(timeAheadNs)))
if normTimePassed.GT(normTimeInFuture) {
return sdk.ZeroUint(), errTimeInFutureBeforeTimePassed
return sdkmath.ZeroUint(), errTimeInFutureBeforeTimePassed
}

normFixedPeriod := normTimeInFuture.Sub(calcFunctionIncrement(sdk.Uint(nanoSecondsInFormula.TruncateInt())))
if normFixedPeriod.LTE(sdk.ZeroDec()) {
return sdk.ZeroUint(), nil
return sdkmath.ZeroUint(), nil
}

// convert norm time to non norm time
Expand All @@ -144,12 +145,12 @@ func predictMintedByFixedAmount(totalMinted sdk.Uint, normTimePassed, timeAhead
func predictTotalMinted(totalMinted sdk.Uint, normTimePassed, timeAhead sdk.Dec) sdk.Uint {
integralAmount, err := predictMintedByIntegral(totalMinted, normTimePassed, timeAhead)
if err != nil {
return sdk.ZeroUint()
return sdkmath.ZeroUint()
}

fixedAmount, err := predictMintedByFixedAmount(totalMinted, normTimePassed, timeAhead)
if err != nil {
return sdk.ZeroUint()
return sdkmath.ZeroUint()
}

return fixedAmount.Add(integralAmount)
Expand All @@ -173,7 +174,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
ctx.Logger().Debug(fmt.Sprintf("miner: %v total, %v norm time, %v minted", minter.TotalMinted.String(), minter.NormTimePassed.String(), coinAmount.String()))

k.SetMinter(ctx, minter)
if coinAmount.GT(sdk.ZeroUint()) {
if coinAmount.GT(sdkmath.ZeroUint()) {
// mint coins, update supply
mintedCoins := sdk.NewCoins(sdk.NewCoin(params.MintDenom, sdk.NewIntFromBigInt(coinAmount.BigInt())))

Expand Down
83 changes: 42 additions & 41 deletions x/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"

sdkmath "cosmossdk.io/math"
"github.com/Nolus-Protocol/nolus-core/x/mint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -79,7 +80,7 @@ func Test_CalcTokensDuringFormula_WhenUsingConstantIncrements_OutputsPredetermin
if i%minutesInMonth == 0 {
fmt.Printf("%v Month, %v Minted, %v Total Minted(in store), %v Returned Total, %v Norm Time, %v Received in this block \n",
i/minutesInMonth, mintedMonth, minter.TotalMinted, mintedCoins, minter.NormTimePassed, coins)
mintedMonth = sdk.ZeroUint()
mintedMonth = sdkmath.ZeroUint()
}
}

Expand Down Expand Up @@ -107,7 +108,7 @@ func Test_CalcTokensDuringFormula_WhenUsingVaryingIncrements_OutputExpectedToken
i := sdk.NewUint(randomTimeBetweenBlocks(5, 60, r))

coins := calcTokens(timeOffset.Add(i), &minter, fiveMinutesInNano)
if coins.LT(sdk.ZeroUint()) {
if coins.LT(sdkmath.ZeroUint()) {
t.Errorf("Minted negative %v coins", coins)
}

Expand All @@ -132,7 +133,7 @@ func Test_CalcTokensDuringFormula_WhenUsingVaryingIncrements_OutputExpectedToken
}

prevOffset = timeOffset
mintedMonth = sdk.ZeroUint()
mintedMonth = sdkmath.ZeroUint()
r = rand.New(rand.NewSource(util.GetCurrentTimeUnixNano()))
}

Expand All @@ -157,15 +158,15 @@ func Test_CalcTokensFixed_WhenNotHittingMintCapInAMonth_OutputsExpectedTokensWit
_, _, _, timeOffset := defaultParams()

offsetNanoInMonth := timeOffset.Add(uintFromDec(nanoSecondsInMonth))
minter := types.NewMinter(types.MonthsInFormula, sdk.ZeroUint(), timeOffset, sdk.ZeroUint())
mintedCoins := sdk.ZeroUint()
minter := types.NewMinter(types.MonthsInFormula, sdkmath.ZeroUint(), timeOffset, sdkmath.ZeroUint())
mintedCoins := sdkmath.ZeroUint()
r := rand.New(rand.NewSource(util.GetCurrentTimeUnixNano()))

for timeOffset.LT(offsetNanoInMonth) {
i := sdk.NewUint(randomTimeBetweenBlocks(5, 60, r))
coins := calcTokens(timeOffset.Add(i), &minter, fiveMinutesInNano)

if coins.LT(sdk.ZeroUint()) {
if coins.LT(sdkmath.ZeroUint()) {
t.Errorf("Minted negative %v coins", coins)
}

Expand Down Expand Up @@ -194,7 +195,7 @@ func Test_CalcTokensFixed_WhenHittingMintCapInAMonth_DoesNotExceedMaxMintingCap(

halfFixedAmount := types.FixedMintedAmount.Quo(sdk.NewUint(2))
totalMinted := types.MintingCap.Sub(halfFixedAmount)
minter := types.NewMinter(types.MonthsInFormula, totalMinted, timeOffset, sdk.ZeroUint())
minter := types.NewMinter(types.MonthsInFormula, totalMinted, timeOffset, sdkmath.ZeroUint())
mintedCoins := sdk.NewUint(0)
r := rand.New(rand.NewSource(util.GetCurrentTimeUnixNano()))

Expand All @@ -209,7 +210,7 @@ func Test_CalcTokensFixed_WhenHittingMintCapInAMonth_DoesNotExceedMaxMintingCap(
fmt.Printf("%v Returned Total, %v Total Minted(in store), %v Norm Time \n",
mintedCoins, minter.TotalMinted, minter.NormTimePassed)
mintThreshold := sdk.NewUint(1_000_000) // 1 token
if types.MintingCap.Sub(minter.TotalMinted).GT(sdk.ZeroUint()) {
if types.MintingCap.Sub(minter.TotalMinted).GT(sdkmath.ZeroUint()) {
t.Errorf("Minting Cap exeeded, minted total %v, with minting cap %v",
minter.TotalMinted, types.MintingCap)
}
Expand Down Expand Up @@ -250,7 +251,7 @@ func Test_CalcTokens_WhenMintingAllTokens_OutputsExactExpectedTokens(t *testing.
fmt.Printf("%v Month, %v Minted, %v Total Minted(in store), %v Returned Total, %v Norm Time, %v Received in this block \n",
month, mintedMonth, minter.TotalMinted, mintedCoins, minter.NormTimePassed, coins)
prevOffset = timeOffset
mintedMonth = sdk.ZeroUint()
mintedMonth = sdkmath.ZeroUint()
}

timeOffset = timeOffset.Add(i)
Expand Down Expand Up @@ -347,64 +348,64 @@ func Test_PredictMintedByIntegral_TwelveMonthsAhead(t *testing.T) {
title: "start from genesis, 1 month calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("0.47"),
timeAhead: sdk.MustNewDecFromStr("1"),
totalMinted: sdk.ZeroUint(),
expIntegralMinted: sdk.NewUintFromString("3_760_114_000_000"),
totalMinted: sdkmath.ZeroUint(),
expIntegralMinted: sdkmath.NewUintFromString("3_760_114_000_000"),
expError: false,
},
{
title: "start from genesis, 12 months calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("0.47"),
timeAhead: twelveMonths,
totalMinted: sdk.ZeroUint(),
expIntegralMinted: sdk.NewUintFromString("39_897_845_000_000"),
totalMinted: sdkmath.ZeroUint(),
expIntegralMinted: sdkmath.NewUintFromString("39_897_845_000_000"),
expError: false,
},
{
title: "in the 96 months range, 12 months calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("5.44552083"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("14_537_732_000_000"),
expIntegralMinted: sdk.NewUintFromString("38_996_481_000_000"),
totalMinted: sdkmath.NewUintFromString("14_537_732_000_000"),
expIntegralMinted: sdkmath.NewUintFromString("38_996_481_000_000"),
expError: false,
},
{
title: "ends on the 96th month, 12 months calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("84.05875000"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("142_977_230_000_000"),
expIntegralMinted: sdk.NewUintFromString("4_558_027_000_000"),
totalMinted: sdkmath.NewUintFromString("142_977_230_000_000"),
expIntegralMinted: sdkmath.NewUintFromString("4_558_027_000_000"),
expError: false,
},
{
title: "partially in the 96 months range, 1 month calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("95.00489583"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("147_290_028_000_000"),
expIntegralMinted: sdk.NewUintFromString("245_229_000_000"),
totalMinted: sdkmath.NewUintFromString("147_290_028_000_000"),
expIntegralMinted: sdkmath.NewUintFromString("245_229_000_000"),
expError: false,
},
{
title: "after 96th months, 0 months calculated by integral",
normTimePassed: sdk.MustNewDecFromStr("98"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("147_741_507_000_000"),
expIntegralMinted: sdk.ZeroUint(),
totalMinted: sdkmath.NewUintFromString("147_741_507_000_000"),
expIntegralMinted: sdkmath.ZeroUint(),
expError: false,
},
{
title: "negative time ahead should result in error",
normTimePassed: sdk.MustNewDecFromStr("98"),
timeAhead: sdk.MustNewDecFromStr("-1.0"),
totalMinted: sdk.ZeroUint(),
expIntegralMinted: sdk.ZeroUint(),
totalMinted: sdkmath.ZeroUint(),
expIntegralMinted: sdkmath.ZeroUint(),
expError: true,
},
{
title: "zero time ahead should not mint tokens",
normTimePassed: sdk.MustNewDecFromStr("85.05385417"),
timeAhead: sdk.ZeroDec(),
totalMinted: sdk.NewUintFromString("143_483_520_000_000"),
expIntegralMinted: sdk.ZeroUint(),
totalMinted: sdkmath.NewUintFromString("143_483_520_000_000"),
expIntegralMinted: sdkmath.ZeroUint(),
expError: false,
},
} {
Expand Down Expand Up @@ -443,64 +444,64 @@ func Test_PredictMintedByFixedAmount_TwelveMonthsAhead(t *testing.T) {
title: "in the 96 months range, 0 months calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("0.47"),
timeAhead: twelveMonths,
totalMinted: sdk.ZeroUint(),
expFixedMinted: sdk.ZeroUint(),
totalMinted: sdkmath.ZeroUint(),
expFixedMinted: sdkmath.ZeroUint(),
expError: false,
},
{
title: "starts on the 96th month, 1 month calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("96"),
timeAhead: sdk.MustNewDecFromStr("1"),
totalMinted: sdk.NewUintFromString("147_535_257_000_000"),
expFixedMinted: sdk.NewUintFromString("103_125_000_000"),
totalMinted: sdkmath.NewUintFromString("147_535_257_000_000"),
expFixedMinted: sdkmath.NewUintFromString("103_125_000_000"),
expError: false,
},
{
title: "partially in the 96 months range, 1 month calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("85.05385417"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("143_483_520_000_000"),
expFixedMinted: sdk.NewUintFromString("103_125_000_000"),
totalMinted: sdkmath.NewUintFromString("143_483_520_000_000"),
expFixedMinted: sdkmath.NewUintFromString("103_125_000_000"),
expError: false,
},
{
title: "starts on the 96th month, all months calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("96"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("147_535_257_000_000"),
expFixedMinted: sdk.NewUintFromString("103_125_000_000").MulUint64(12),
totalMinted: sdkmath.NewUintFromString("147_535_257_000_000"),
expFixedMinted: sdkmath.NewUintFromString("103_125_000_000").MulUint64(12),
expError: false,
},
{
title: "partially in the 96-120 month range, few days calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("119.0"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("149_900_000_000_000"),
expFixedMinted: sdk.NewUintFromString("100_000_000_000"),
totalMinted: sdkmath.NewUintFromString("149_900_000_000_000"),
expFixedMinted: sdkmath.NewUintFromString("100_000_000_000"),
expError: false,
},
{
title: "after minting cap reached, 0 months calculated by fixed amount",
normTimePassed: sdk.MustNewDecFromStr("119.9"),
timeAhead: twelveMonths,
totalMinted: sdk.NewUintFromString("150_000_000_000_000"),
expFixedMinted: sdk.ZeroUint(),
totalMinted: sdkmath.NewUintFromString("150_000_000_000_000"),
expFixedMinted: sdkmath.ZeroUint(),
expError: false,
},
{
title: "negative time ahead should result in error",
normTimePassed: sdk.MustNewDecFromStr("98"),
timeAhead: sdk.MustNewDecFromStr("-1.0"),
totalMinted: sdk.ZeroUint(),
expFixedMinted: sdk.ZeroUint(),
totalMinted: sdkmath.ZeroUint(),
expFixedMinted: sdkmath.ZeroUint(),
expError: true,
},
{
title: "zero time ahead should not mint tokens",
normTimePassed: sdk.MustNewDecFromStr("85.05385417"),
timeAhead: sdk.ZeroDec(),
totalMinted: sdk.NewUintFromString("143_483_520_000_000"),
expFixedMinted: sdk.ZeroUint(),
totalMinted: sdkmath.NewUintFromString("143_483_520_000_000"),
expFixedMinted: sdkmath.ZeroUint(),
expError: false,
},
} {
Expand Down
3 changes: 2 additions & 1 deletion x/mint/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Nolus-Protocol/nolus-core/testutil/network"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (s *IntegrationTestSuite) TestQueryGRPC() {
&minttypes.QueryMintStateResponse{},
&minttypes.QueryMintStateResponse{
NormTimePassed: minttypes.NormOffset,
TotalMinted: sdk.ZeroUint(),
TotalMinted: sdkmath.ZeroUint(),
},
},
}
Expand Down
Loading

0 comments on commit 71cc05f

Please sign in to comment.