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

Fix/ Code optimization #284

Merged
merged 3 commits into from
Aug 8, 2023
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
32 changes: 1 addition & 31 deletions x/pot/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions x/pot/keeper/formula.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion x/pot/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
23 changes: 6 additions & 17 deletions x/register/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 5 additions & 18 deletions x/register/keeper/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions x/sds/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions x/sds/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Loading