From a9d39270ebc8118648106d75326198d6c600aba1 Mon Sep 17 00:00:00 2001 From: Xiong Date: Mon, 19 Jun 2023 14:56:00 -0400 Subject: [PATCH 1/2] 1, Optimize redundant formula calculation code 2, Move POT formula related code to pot/keeper/formula.go --- x/evm/keeper/store.go | 17 ++---- x/evm/types/interfaces.go | 1 + x/pot/keeper/distribute.go | 32 +---------- x/pot/keeper/formula.go | 68 +++++++++++++++++++++++ x/pot/types/expected_keepers.go | 2 +- x/register/keeper/keeper.go | 23 ++------ x/register/keeper/store.go | 23 ++------ x/sds/keeper/grpc_query.go | 5 +- x/sds/keeper/keeper.go | 16 ++---- x/sds/keeper/querier.go | 5 +- x/sds/oz_price_test.go | 97 +++++++++++++++++---------------- 11 files changed, 148 insertions(+), 141 deletions(-) create mode 100644 x/pot/keeper/formula.go diff --git a/x/evm/keeper/store.go b/x/evm/keeper/store.go index 1f4109d6..cb98551c 100644 --- a/x/evm/keeper/store.go +++ b/x/evm/keeper/store.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" "github.com/stratosnet/stratos-chain/x/evm/vm" @@ -26,17 +24,10 @@ func (k *Keeper) KeeCalculatePrepayPurchaseAmount(statedb vm.StateDB, amount sdk Pt := k.KeeGetTotalUnissuedPrepay(statedb) Lt := k.registerKeeper.KeeGetRemainingOzoneLimit(kdb) - purchase := Lt.ToDec(). - Mul(amount.ToDec()). - Quo((St. - Add(Pt). - Add(amount)).ToDec()). - TruncateInt() - if purchase.GT(Lt) { - return sdk.NewInt(0), sdk.NewInt(0), fmt.Errorf("not enough remaining ozone limit to complete prepay") + purchasedNoz, remainingNoz, err := k.potKeeper.GetPrepayAmount(Lt, amount, St, Pt) + if err != nil { + return sdk.ZeroInt(), sdk.ZeroInt(), err } - remaining := Lt.Sub(purchase) - - return purchase, remaining, nil + return purchasedNoz, remainingNoz, nil } diff --git a/x/evm/types/interfaces.go b/x/evm/types/interfaces.go index 63c58100..0f7e3a33 100644 --- a/x/evm/types/interfaces.go +++ b/x/evm/types/interfaces.go @@ -55,6 +55,7 @@ type PotKeeper interface { InitialTotalSupply(ctx sdk.Context) sdk.Coin BondDenom(ctx sdk.Context) string SafeMintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + GetPrepayAmount(Lt, tokenAmount, St, Pt sdk.Int) (purchaseNoz, remainingNoz sdk.Int, err error) } // Event Hooks diff --git a/x/pot/keeper/distribute.go b/x/pot/keeper/distribute.go index b451d4bf..edade691 100644 --- a/x/pot/keeper/distribute.go +++ b/x/pot/keeper/distribute.go @@ -27,7 +27,7 @@ func (k Keeper) DistributePotReward(ctx sdk.Context, trafficList []types.SingleW //1, calc traffic reward in total totalConsumedNoz := k.GetTotalConsumedNoz(trafficList).ToDec() - remaining, total := k.registerKeeper.NozSupply(ctx) + remaining, total := k.NozSupply(ctx) if totalConsumedNoz.Add(remaining.ToDec()).GT(total.ToDec()) { return errors.New("remaining+consumed Noz exceeds total Noz supply") } @@ -101,36 +101,6 @@ func (k Keeper) CalcTrafficRewardInTotal( return distributeGoal, nil } -// [S] is initial genesis deposit by all resource nodes and meta nodes at t=0 -// The current unissued prepay Volume Pool [pt] is the total remaining prepay wei kept by Stratos Network but not issued to Resource Node as rewards. At time t=0, pt=0 -// total consumed Ozone is [Y] -// The remaining total Ozone limit [lt] is the upper bound of total Ozone that users can purchase from Stratos blockchain. -// the total generated traffic rewards as [R] -// R = (S + Pt) * Y / (Lt + Y) -func (k Keeper) GetTrafficReward(ctx sdk.Context, totalConsumedNoz sdk.Dec) (result sdk.Dec) { - St := k.registerKeeper.GetEffectiveTotalDeposit(ctx).ToDec() - if St.Equal(sdk.ZeroDec()) { - ctx.Logger().Info("effective genesis deposit by all resource nodes and meta nodes is 0") - } - Pt := k.registerKeeper.GetTotalUnissuedPrepay(ctx).Amount.ToDec() - if Pt.Equal(sdk.ZeroDec()) { - ctx.Logger().Info("total remaining prepay not issued is 0") - } - Y := totalConsumedNoz - if Y.Equal(sdk.ZeroDec()) { - ctx.Logger().Info("total consumed noz is 0") - } - Lt := k.registerKeeper.GetRemainingOzoneLimit(ctx).ToDec() - if Lt.Equal(sdk.ZeroDec()) { - ctx.Logger().Info("remaining total noz limit is 0") - } - R := St.Add(Pt).Mul(Y).Quo(Lt.Add(Y)) - if R.Equal(sdk.ZeroDec()) { - ctx.Logger().Info("traffic reward to distribute is 0") - } - return R -} - // allocate mining reward from foundation account func (k Keeper) CalcMiningRewardInTotal(ctx sdk.Context, distributeGoal types.DistributeGoal) (types.DistributeGoal, error) { totalMinedTokens := k.GetTotalMinedTokens(ctx) diff --git a/x/pot/keeper/formula.go b/x/pot/keeper/formula.go new file mode 100644 index 00000000..bfb0224f --- /dev/null +++ b/x/pot/keeper/formula.go @@ -0,0 +1,68 @@ +package keeper + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// [S] is initial genesis deposit by all resource nodes and meta nodes at t=0 +// The current unissued prepay Volume Pool [pt] is the total remaining prepay wei kept by Stratos Network but not issued to Resource Node as rewards. At time t=0, pt=0 +// total consumed Ozone is [Y] +// The remaining total Ozone limit [lt] is the upper bound of total Ozone that users can purchase from Stratos blockchain. +// the total generated traffic rewards as [R] +// R = (S + Pt) * Y / (Lt + Y) +func (k Keeper) GetTrafficReward(ctx sdk.Context, totalConsumedNoz sdk.Dec) (result sdk.Dec) { + St := k.registerKeeper.GetEffectiveTotalDeposit(ctx).ToDec() + if St.Equal(sdk.ZeroDec()) { + ctx.Logger().Info("effective genesis deposit by all resource nodes and meta nodes is 0") + } + Pt := k.registerKeeper.GetTotalUnissuedPrepay(ctx).Amount.ToDec() + if Pt.Equal(sdk.ZeroDec()) { + ctx.Logger().Info("total remaining prepay not issued is 0") + } + Y := totalConsumedNoz + if Y.Equal(sdk.ZeroDec()) { + ctx.Logger().Info("total consumed noz is 0") + } + Lt := k.registerKeeper.GetRemainingOzoneLimit(ctx).ToDec() + if Lt.Equal(sdk.ZeroDec()) { + ctx.Logger().Info("remaining total noz limit is 0") + } + R := St.Add(Pt).Mul(Y).Quo(Lt.Add(Y)) + if R.Equal(sdk.ZeroDec()) { + ctx.Logger().Info("traffic reward to distribute is 0") + } + return R +} + +func (k Keeper) GetPrepayAmount(Lt, tokenAmount, St, Pt sdk.Int) (purchaseNoz, remainingNoz sdk.Int, err error) { + purchaseNoz = Lt.ToDec(). + Mul(tokenAmount.ToDec()). + Quo((St. + Add(Pt). + Add(tokenAmount)).ToDec()). + TruncateInt() + + if purchaseNoz.GT(Lt) { + return sdk.ZeroInt(), sdk.ZeroInt(), fmt.Errorf("not enough remaining ozone limit to complete prepay") + } + remainingNoz = Lt.Sub(purchaseNoz) + + return purchaseNoz, remainingNoz, nil +} + +func (k Keeper) GetCurrentNozPrice(St, Pt, Lt sdk.Int) (currentNozPrice sdk.Dec) { + currentNozPrice = (St.Add(Pt)).ToDec(). + Quo(Lt.ToDec()) + return +} + +// NozSupply calc remaining/total supply for noz +func (k Keeper) NozSupply(ctx sdk.Context) (remaining, total sdk.Int) { + remaining = k.registerKeeper.GetRemainingOzoneLimit(ctx) // Lt + depositNozRate := k.registerKeeper.GetDepositNozRate(ctx) + St := k.registerKeeper.GetEffectiveTotalDeposit(ctx) + total = St.ToDec().Quo(depositNozRate).TruncateInt() + return remaining, total +} diff --git a/x/pot/types/expected_keepers.go b/x/pot/types/expected_keepers.go index 758ba701..5bc1f37b 100644 --- a/x/pot/types/expected_keepers.go +++ b/x/pot/types/expected_keepers.go @@ -38,6 +38,7 @@ type RegisterKeeper interface { GetRemainingOzoneLimit(ctx sdk.Context) (value sdk.Int) SetRemainingOzoneLimit(ctx sdk.Context, value sdk.Int) GetTotalUnissuedPrepay(ctx sdk.Context) (totalUnissuedPrepay sdk.Coin) + GetDepositNozRate(ctx sdk.Context) (depositNozRate sdk.Dec) GetResourceNodeBondedToken(ctx sdk.Context) (token sdk.Coin) GetMetaNodeBondedToken(ctx sdk.Context) (token sdk.Coin) @@ -52,7 +53,6 @@ type RegisterKeeper interface { IncreaseOzoneLimitByAddDeposit(ctx sdk.Context, deposit sdk.Int) (ozoneLimitChange sdk.Int) GetUnbondingNodeBalance(ctx sdk.Context, networkAddr stratos.SdsAddress) sdk.Int - NozSupply(ctx sdk.Context) (remaining, total sdk.Int) OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr stratos.SdsAddress) (found bool) } diff --git a/x/register/keeper/keeper.go b/x/register/keeper/keeper.go index fea0203f..a9c31105 100644 --- a/x/register/keeper/keeper.go +++ b/x/register/keeper/keeper.go @@ -438,21 +438,10 @@ func (k Keeper) GetUnbondingNodeBalance(ctx sdk.Context, networkAddr stratos.Sds return balance } -// CurrNozPrice calcs current noz price -func (k Keeper) CurrNozPrice(ctx sdk.Context) sdk.Dec { - St := k.GetEffectiveTotalDeposit(ctx) - Pt := k.GetTotalUnissuedPrepay(ctx).Amount - Lt := k.GetRemainingOzoneLimit(ctx) - currNozPrice := (St.Add(Pt)).ToDec(). - Quo(Lt.ToDec()) - return currNozPrice -} - -// NozSupply calc remaining/total supply for noz -func (k Keeper) NozSupply(ctx sdk.Context) (remaining, total sdk.Int) { - remaining = k.GetRemainingOzoneLimit(ctx) // Lt - depositNozRate := k.GetDepositNozRate(ctx) - St := k.GetEffectiveTotalDeposit(ctx) - total = St.ToDec().Quo(depositNozRate).TruncateInt() - return remaining, total +// GetCurrNozPriceParams calcs current noz price +func (k Keeper) GetCurrNozPriceParams(ctx sdk.Context) (St, Pt, Lt sdk.Int) { + St = k.GetEffectiveTotalDeposit(ctx) + Pt = k.GetTotalUnissuedPrepay(ctx).Amount + Lt = k.GetRemainingOzoneLimit(ctx) + return } diff --git a/x/register/keeper/store.go b/x/register/keeper/store.go index 9e14e344..2f4271a1 100644 --- a/x/register/keeper/store.go +++ b/x/register/keeper/store.go @@ -1,7 +1,6 @@ package keeper import ( - "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -46,23 +45,11 @@ func (k Keeper) GetRemainingOzoneLimit(ctx sdk.Context) (value sdk.Int) { return } -func (k *Keeper) CalculatePurchaseAmount(ctx sdk.Context, amount sdk.Int) (sdk.Int, sdk.Int, error) { - St := k.GetEffectiveTotalDeposit(ctx) - Pt := k.GetTotalUnissuedPrepay(ctx).Amount - Lt := k.GetRemainingOzoneLimit(ctx) - - purchase := Lt.ToDec(). - Mul(amount.ToDec()). - Quo((St. - Add(Pt). - Add(amount)).ToDec()). - TruncateInt() - if purchase.GT(Lt) { - return sdk.NewInt(0), sdk.NewInt(0), fmt.Errorf("not enough remaining ozone limit to complete prepay") - } - remaining := Lt.Sub(purchase) - - return purchase, remaining, nil +func (k *Keeper) GetPrepayParams(ctx sdk.Context) (St, Pt, Lt sdk.Int) { + St = k.GetEffectiveTotalDeposit(ctx) + Pt = k.GetTotalUnissuedPrepay(ctx).Amount + Lt = k.GetRemainingOzoneLimit(ctx) + return } func (k Keeper) IsUnbondable(ctx sdk.Context, unbondAmt sdk.Int) bool { diff --git a/x/sds/keeper/grpc_query.go b/x/sds/keeper/grpc_query.go index 8815abb6..87d511d6 100644 --- a/x/sds/keeper/grpc_query.go +++ b/x/sds/keeper/grpc_query.go @@ -58,13 +58,14 @@ func (q Querier) SimPrepay(c context.Context, request *types.QuerySimPrepayReque func (q Querier) NozPrice(c context.Context, _ *types.QueryNozPriceRequest) (*types.QueryNozPriceResponse, error) { ctx := sdk.UnwrapSDKContext(c) - nozPrice := q.registerKeeper.CurrNozPrice(ctx) + St, Pt, Lt := q.registerKeeper.GetCurrNozPriceParams(ctx) + nozPrice := q.potKeeper.GetCurrentNozPrice(St, Pt, Lt) return &types.QueryNozPriceResponse{Price: nozPrice}, nil } func (q Querier) NozSupply(c context.Context, request *types.QueryNozSupplyRequest) (*types.QueryNozSupplyResponse, error) { ctx := sdk.UnwrapSDKContext(c) - remaining, total := q.registerKeeper.NozSupply(ctx) + remaining, total := q.potKeeper.NozSupply(ctx) return &types.QueryNozSupplyResponse{Remaining: remaining, Total: total}, nil } diff --git a/x/sds/keeper/keeper.go b/x/sds/keeper/keeper.go index 7615ade0..3e4b8043 100644 --- a/x/sds/keeper/keeper.go +++ b/x/sds/keeper/keeper.go @@ -90,7 +90,8 @@ func (k Keeper) FileUpload(ctx sdk.Context, fileHash string, reporter stratos.Sd // [X] is the total amount of STOS token prepaid by user at time t // the total amount of Ozone the user gets = Lt * X / (S + Pt + X) func (k Keeper) purchaseNozAndSubCoins(ctx sdk.Context, from sdk.AccAddress, amount sdk.Int) (sdk.Int, error) { - purchased, newRemainingOzoneLimit, err := k.registerKeeper.CalculatePurchaseAmount(ctx, amount) + St, Pt, Lt := k.registerKeeper.GetPrepayParams(ctx) + purchasedNoz, newRemainingOzoneLimit, err := k.potKeeper.GetPrepayAmount(Lt, amount, St, Pt) if err != nil { return sdk.ZeroInt(), err } @@ -104,21 +105,14 @@ func (k Keeper) purchaseNozAndSubCoins(ctx sdk.Context, from sdk.AccAddress, amo // update remaining noz limit k.registerKeeper.SetRemainingOzoneLimit(ctx, newRemainingOzoneLimit) - return purchased, nil + return purchasedNoz, nil } func (k Keeper) simulatePurchaseNoz(ctx sdk.Context, coins sdk.Coins) sdk.Int { amount := coins.AmountOf(k.BondDenom(ctx)) + St, Pt, Lt := k.registerKeeper.GetPrepayParams(ctx) + purchased, _, _ := k.potKeeper.GetPrepayAmount(Lt, amount, St, Pt) - St := k.registerKeeper.GetEffectiveTotalDeposit(ctx) - Pt := k.registerKeeper.GetTotalUnissuedPrepay(ctx).Amount - Lt := k.registerKeeper.GetRemainingOzoneLimit(ctx) - purchased := Lt.ToDec(). - Mul(amount.ToDec()). - Quo((St. - Add(Pt). - Add(amount)).ToDec()). - TruncateInt() return purchased } diff --git a/x/sds/keeper/querier.go b/x/sds/keeper/querier.go index 77e8a5fb..2e2cf045 100644 --- a/x/sds/keeper/querier.go +++ b/x/sds/keeper/querier.go @@ -78,7 +78,8 @@ func querySimulatePrepay(ctx sdk.Context, req abci.RequestQuery, k Keeper, _ *co // queryCurrNozPrice fetch current noz price. func queryCurrNozPrice(ctx sdk.Context, _ abci.RequestQuery, k Keeper, _ *codec.LegacyAmino) ([]byte, error) { - nozPrice := k.registerKeeper.CurrNozPrice(ctx) + St, Pt, Lt := k.registerKeeper.GetCurrNozPriceParams(ctx) + nozPrice := k.potKeeper.GetCurrentNozPrice(St, Pt, Lt) nozPriceByte, _ := nozPrice.MarshalJSON() return nozPriceByte, nil } @@ -90,7 +91,7 @@ func queryNozSupply(ctx sdk.Context, _ abci.RequestQuery, k Keeper, _ *codec.Leg Total sdk.Int } var nozSupply Supply - nozSupply.Remaining, nozSupply.Total = k.registerKeeper.NozSupply(ctx) + nozSupply.Remaining, nozSupply.Total = k.potKeeper.NozSupply(ctx) nozSupplyByte, _ := json.Marshal(nozSupply) return nozSupplyByte, nil } diff --git a/x/sds/oz_price_test.go b/x/sds/oz_price_test.go index ce1592ff..5c69dd4f 100644 --- a/x/sds/oz_price_test.go +++ b/x/sds/oz_price_test.go @@ -258,9 +258,10 @@ func TestOzPriceChange(t *testing.T) { require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, valInitialStake, validator.BondedTokens())) - _, nozSupply := registerKeeper.NozSupply(ctx) - nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - NOzonePrice: registerKeeper.CurrNozPrice(ctx), + _, nozSupply := potKeeper.NozSupply(ctx) + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, NozPriceFactors{ + NOzonePrice: potKeeper.GetCurrentNozPrice(St, Pt, Lt), InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -285,7 +286,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq1, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq0) + nozPriceFactorsSeq1, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq0) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after PREPAY") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should decrease after PREPAY") t.Log("********************************* Deliver Prepay Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -305,7 +306,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq2, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq1) + nozPriceFactorsSeq2, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq1) require.True(t, nozPricePercentage.Equal(sdk.ZeroDec()), "noz price shouldn't change after CreateResourceNode") require.True(t, ozoneLimitPercentage.Equal(sdk.ZeroDec()), "OzLimit shouldn't change after CreateResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -333,7 +334,7 @@ func TestOzPriceChange(t *testing.T) { t.Log("slashingAmtSetup=" + slashingAmtSetup.String()) require.Equal(t, slashingAmtSetup, slashingAmtCheck.TruncateInt()) - nozPriceFactorsSeq3, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq2) + nozPriceFactorsSeq3, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq2) require.True(t, nozPricePercentage.LT(sdk.ZeroDec()), "noz price should decrease after UnsuspendResourceNode") require.True(t, ozoneLimitPercentage.GT(sdk.ZeroDec()), "OzLimit should increase after UnsuspendResourceNode") t.Log("********************************* Deliver UnsuspendResourceNode Tx (Slashing) END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -361,11 +362,11 @@ func TestOzPriceChange(t *testing.T) { //t.Log("slashingAmtSetup=" + slashingAmtSetup.String()) //require.Equal(t, slashingAmtSetup, slashingAmtCheck.TruncateInt()) - nozPriceFactorsSeq4, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq3) + nozPriceFactorsSeq4, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq3) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after SlashResourceNode") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should decrease after SlashResourceNode") - _, nozPricePercentage42, ozoneLimitPercentage42 := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq2) + _, nozPricePercentage42, ozoneLimitPercentage42 := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq2) require.True(t, nozPricePercentage42.Equal(sdk.ZeroDec()), "noz price after SlashResourceNode should be same with the price when node hadn't been activated") require.True(t, ozoneLimitPercentage42.Equal(sdk.ZeroDec()), "OzLimit after SlashResourceNode should be same with the ozLimit when node hadn't been activated") t.Log("********************************* Deliver SuspendResourceNode Tx (Slashing) END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -380,18 +381,18 @@ func TestOzPriceChange(t *testing.T) { require.Equal(t, ok, true) totalConsumedNoz = potKeeper.GetTotalConsumedNoz(volumeReportMsg.WalletVolumes).ToDec() - remaining, total := registerKeeper.NozSupply(ctx) + remaining, total := potKeeper.NozSupply(ctx) require.True(t, potKeeper.GetTotalConsumedNoz(volumeReportMsg.WalletVolumes).Add(remaining).LTE(total), "remaining+consumed Noz exceeds total Noz supply") /********************* print info *********************/ t.Log("epoch " + volumeReportMsg.Epoch.String()) - St := registerKeeper.GetEffectiveTotalDeposit(ctx).ToDec() - Pt := registerKeeper.GetTotalUnissuedPrepay(ctx).Amount.ToDec() + StDec := registerKeeper.GetEffectiveTotalDeposit(ctx).ToDec() + PtDec := registerKeeper.GetTotalUnissuedPrepay(ctx).Amount.ToDec() Y := totalConsumedNoz - Lt := registerKeeper.GetRemainingOzoneLimit(ctx).ToDec() - R := St.Add(Pt).Mul(Y).Quo(Lt.Add(Y)) + LtDec := registerKeeper.GetRemainingOzoneLimit(ctx).ToDec() + R := StDec.Add(PtDec).Mul(Y).Quo(LtDec.Add(Y)) //t.Log("R = (S + Pt) * Y / (Lt + Y)") - t.Log("St=" + St.String() + "\nPt=" + Pt.String() + "\nY=" + Y.String() + "\nLt=" + Lt.String() + "\nR=" + R.String() + "\n") + t.Log("St=" + StDec.String() + "\nPt=" + PtDec.String() + "\nY=" + Y.String() + "\nLt=" + LtDec.String() + "\nR=" + R.String() + "\n") t.Log("---------------------------") potKeeper.InitVariable(ctx) @@ -486,7 +487,7 @@ func TestOzPriceChange(t *testing.T) { feeCollectorToFeePoolAtBeginBlock, ) - nozPriceFactorsSeq5, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq4) + nozPriceFactorsSeq5, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq4) require.True(t, nozPricePercentage.LT(sdk.ZeroDec()), "noz price should decrease after VolumeReport") require.True(t, ozoneLimitPercentage.GT(sdk.ZeroDec()), "OzLimit shouldn't change after VolumeReport") t.Log("********************************* Deliver VolumeReport Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -506,7 +507,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq6, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq5) + nozPriceFactorsSeq6, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq5) require.True(t, nozPricePercentage.Equal(sdk.ZeroDec()), "noz price shouldn't change after CreateResourceNode") require.True(t, ozoneLimitPercentage.Equal(sdk.ZeroDec()), "OzLimit shouldn't change after CreateResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -526,7 +527,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq7, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq6) + nozPriceFactorsSeq7, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq6) require.True(t, nozPricePercentage.Equal(sdk.ZeroDec()), "noz price shouldn't change after CreateResourceNode") require.True(t, ozoneLimitPercentage.Equal(sdk.ZeroDec()), "OzLimit shouldn't change after CreateResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -546,7 +547,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq8, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq7) + nozPriceFactorsSeq8, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq7) require.True(t, nozPricePercentage.Equal(sdk.ZeroDec()), "noz price shouldn't change after CreateResourceNode") require.True(t, ozoneLimitPercentage.Equal(sdk.ZeroDec()), "OzLimit shouldn't change after CreateResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -566,7 +567,7 @@ func TestOzPriceChange(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - _, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, nozPriceFactorsSeq8) + _, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, nozPriceFactorsSeq8) require.True(t, nozPricePercentage.Equal(sdk.ZeroDec()), "noz price shouldn't change after CreateResourceNode") require.True(t, ozoneLimitPercentage.Equal(sdk.ZeroDec()), "OzLimit shouldn't change after CreateResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -686,15 +687,16 @@ func setupMsgCreateResourceNode5() *registertypes.MsgCreateResourceNode { return createResourceNodeMsg } -func printCurrNozPrice(t *testing.T, ctx sdk.Context, registerKeeper registerKeeper.Keeper, nozPriceFactorsBefore NozPriceFactors) (NozPriceFactors, sdk.Dec, sdk.Dec) { +func printCurrNozPrice(t *testing.T, ctx sdk.Context, potKeeper potKeeper.Keeper, registerKeeper registerKeeper.Keeper, nozPriceFactorsBefore NozPriceFactors) (NozPriceFactors, sdk.Dec, sdk.Dec) { nozPriceFactorsAfter := NozPriceFactors{} nozPriceFactorsAfter.InitialTotalDeposit = registerKeeper.GetInitialGenesisDepositTotal(ctx) nozPriceFactorsAfter.EffectiveTotalDeposit = registerKeeper.GetEffectiveTotalDeposit(ctx) nozPriceFactorsAfter.TotalUnissuedPrepay = registerKeeper.GetTotalUnissuedPrepay(ctx).Amount nozPriceFactorsAfter.DepositAndPrepay = nozPriceFactorsAfter.InitialTotalDeposit.Add(nozPriceFactorsAfter.TotalUnissuedPrepay) nozPriceFactorsAfter.OzoneLimit = registerKeeper.GetRemainingOzoneLimit(ctx) - nozPriceFactorsAfter.NOzonePrice = registerKeeper.CurrNozPrice(ctx) - _, nozPriceFactorsAfter.NozSupply = registerKeeper.NozSupply(ctx) + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsAfter.NOzonePrice = potKeeper.GetCurrentNozPrice(St, Pt, Lt) + _, nozPriceFactorsAfter.NozSupply = potKeeper.NozSupply(ctx) nozPriceDelta := nozPriceFactorsAfter.NOzonePrice.Sub(nozPriceFactorsBefore.NOzonePrice) initialTotalDepositDelta := nozPriceFactorsAfter.InitialTotalDeposit.Sub(nozPriceFactorsBefore.InitialTotalDeposit) @@ -1147,7 +1149,7 @@ func TestOzPriceChangePrepay(t *testing.T) { accountKeeper := stApp.GetAccountKeeper() //bankKeeper := stApp.GetBankKeeper() registerKeeper := stApp.GetRegisterKeeper() - //potKeeper := stApp.GetPotKeeper() + potKeeper := stApp.GetPotKeeper() /********************* foundation account deposit *********************/ header := tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} @@ -1189,9 +1191,10 @@ func TestOzPriceChangePrepay(t *testing.T) { validator := checkValidator(t, stApp, valOpValAddr1, true) require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, valInitialStake, validator.BondedTokens())) - _, nozSupply := registerKeeper.NozSupply(ctx) - nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - NOzonePrice: registerKeeper.CurrNozPrice(ctx), + _, nozSupply := potKeeper.NozSupply(ctx) + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, NozPriceFactors{ + NOzonePrice: potKeeper.GetCurrentNozPrice(St, Pt, Lt), InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1221,7 +1224,7 @@ func TestOzPriceChangePrepay(t *testing.T) { header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) dataToExcel = append(dataToExcel, priceAfter) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after PREPAY") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should not change after PREPAY") @@ -1293,9 +1296,10 @@ func TestOzPriceChangeVolumeReport(t *testing.T) { validator := checkValidator(t, stApp, valOpValAddr1, true) require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, valInitialStake, validator.BondedTokens())) - _, nozSupply := registerKeeper.NozSupply(ctx) - nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - NOzonePrice: registerKeeper.CurrNozPrice(ctx), + _, nozSupply := potKeeper.NozSupply(ctx) + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, NozPriceFactors{ + NOzonePrice: potKeeper.GetCurrentNozPrice(St, Pt, Lt), InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1325,7 +1329,7 @@ func TestOzPriceChangeVolumeReport(t *testing.T) { header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) dataToExcel = append(dataToExcel, priceAfter) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after PREPAY") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should decrease after PREPAY") @@ -1434,7 +1438,7 @@ func TestOzPriceChangeVolumeReport(t *testing.T) { header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) dataToExcel = append(dataToExcel, priceAfter) priceBefore = priceAfter } @@ -1475,7 +1479,7 @@ func TestOzPriceChangeAddMultipleResourceNodeAndThenRemove(t *testing.T) { accountKeeper := stApp.GetAccountKeeper() //bankKeeper := stApp.GetBankKeeper() registerKeeper := stApp.GetRegisterKeeper() - //potKeeper := stApp.GetPotKeeper() + potKeeper := stApp.GetPotKeeper() /********************* foundation account deposit *********************/ header := tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} @@ -1517,9 +1521,9 @@ func TestOzPriceChangeAddMultipleResourceNodeAndThenRemove(t *testing.T) { validator := checkValidator(t, stApp, valOpValAddr1, true) require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, valInitialStake, validator.BondedTokens())) - _, nozSupply := registerKeeper.NozSupply(ctx) + _, nozSupply := potKeeper.NozSupply(ctx) //nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - // NOzonePrice: registerKeeper.CurrNozPrice(ctx), + // NOzonePrice: registerKeeper.GetCurrNozPriceParams(ctx), // InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), // EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), // TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1545,8 +1549,9 @@ func TestOzPriceChangeAddMultipleResourceNodeAndThenRemove(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - NOzonePrice: registerKeeper.CurrNozPrice(ctx), + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, NozPriceFactors{ + NOzonePrice: potKeeper.GetCurrentNozPrice(St, Pt, Lt), InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1591,7 +1596,7 @@ func TestOzPriceChangeAddMultipleResourceNodeAndThenRemove(t *testing.T) { stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) require.True(t, nozPricePercentage.LT(sdk.ZeroDec()), "noz price should decrease after CreateResourceNode") require.True(t, ozoneLimitPercentage.GT(sdk.ZeroDec()), "OzLimit should increase after CreateResourceNode") t.Log("********************************* Deliver Create and unsuspend ResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -1629,7 +1634,7 @@ func TestOzPriceChangeAddMultipleResourceNodeAndThenRemove(t *testing.T) { //stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) //ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after RemoveResourceNode") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should decrease after RemoveResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") @@ -1675,7 +1680,7 @@ func TestOzPriceChangeRemoveMultipleResourceNodeAfterGenesis(t *testing.T) { accountKeeper := stApp.GetAccountKeeper() //bankKeeper := stApp.GetBankKeeper() registerKeeper := stApp.GetRegisterKeeper() - //potKeeper := stApp.GetPotKeeper() + potKeeper := stApp.GetPotKeeper() /********************* foundation account deposit *********************/ header := tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} @@ -1717,9 +1722,9 @@ func TestOzPriceChangeRemoveMultipleResourceNodeAfterGenesis(t *testing.T) { validator := checkValidator(t, stApp, valOpValAddr1, true) require.Equal(t, stakingtypes.Bonded, validator.Status) require.True(sdk.IntEq(t, valInitialStake, validator.BondedTokens())) - _, nozSupply := registerKeeper.NozSupply(ctx) + _, nozSupply := potKeeper.NozSupply(ctx) //nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - // NOzonePrice: registerKeeper.CurrNozPrice(ctx), + // NOzonePrice: registerKeeper.GetCurrNozPriceParams(ctx), // InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), // EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), // TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1761,9 +1766,9 @@ func TestOzPriceChangeRemoveMultipleResourceNodeAfterGenesis(t *testing.T) { header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID} stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx = stApp.BaseApp.NewContext(true, header) - - nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, registerKeeper, NozPriceFactors{ - NOzonePrice: registerKeeper.CurrNozPrice(ctx), + St, Pt, Lt := registerKeeper.GetCurrNozPriceParams(ctx) + nozPriceFactorsSeq0, nozPricePercentage, ozoneLimitPercentage := printCurrNozPrice(t, ctx, potKeeper, registerKeeper, NozPriceFactors{ + NOzonePrice: potKeeper.GetCurrentNozPrice(St, Pt, Lt), InitialTotalDeposit: registerKeeper.GetInitialGenesisDepositTotal(ctx), EffectiveTotalDeposit: registerKeeper.GetEffectiveTotalDeposit(ctx), TotalUnissuedPrepay: registerKeeper.GetTotalUnissuedPrepay(ctx).Amount, @@ -1804,7 +1809,7 @@ func TestOzPriceChangeRemoveMultipleResourceNodeAfterGenesis(t *testing.T) { //stApp.BeginBlock(abci.RequestBeginBlock{Header: header}) //ctx = stApp.BaseApp.NewContext(true, header) - priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, registerKeeper, priceBefore) + priceAfter, nozPricePercentage, ozoneLimitPercentage = printCurrNozPrice(t, ctx, potKeeper, registerKeeper, priceBefore) require.True(t, nozPricePercentage.GT(sdk.ZeroDec()), "noz price should increase after RemoveResourceNode") require.True(t, ozoneLimitPercentage.LT(sdk.ZeroDec()), "OzLimit should decrease after RemoveResourceNode") t.Log("********************************* Deliver CreateResourceNode Tx END ********************************************\n\n...\n[NEXT TEST CASE]") From 205c4eb6cbe25f363e91c5e0b37768b6850f192c Mon Sep 17 00:00:00 2001 From: Xiong Date: Tue, 8 Aug 2023 14:49:03 -0400 Subject: [PATCH 2/2] merge dev changes --- x/evm/types/interfaces.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/evm/types/interfaces.go b/x/evm/types/interfaces.go index a2bb1262..f840c936 100644 --- a/x/evm/types/interfaces.go +++ b/x/evm/types/interfaces.go @@ -44,7 +44,6 @@ type PotKeeper interface { InitialTotalSupply(ctx sdk.Context) sdk.Coin BondDenom(ctx sdk.Context) string SafeMintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - GetPrepayAmount(Lt, tokenAmount, St, Pt sdk.Int) (purchaseNoz, remainingNoz sdk.Int, err error) } // Event Hooks