Skip to content

Commit

Permalink
Added maximum and minimum price for node
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Oct 10, 2022
1 parent 82156ab commit a151f6b
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 27 deletions.
8 changes: 8 additions & 0 deletions proto/sentinel/node/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ message Params {
cosmos.base.v1beta1.Coin deposit = 1 [ (gogoproto.nullable) = false ];
google.protobuf.Duration inactive_duration = 2
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
repeated cosmos.base.v1beta1.Coin max_price = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
repeated cosmos.base.v1beta1.Coin min_price = 4 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
46 changes: 46 additions & 0 deletions x/node/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,52 @@ func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
inactiveDuration = k.InactiveDuration(ctx)
)

if k.IsMaxPriceModified(ctx) {
price := k.MaxPrice(ctx)
k.IterateNodes(ctx, func(_ int, item types.Node) (stop bool) {
if item.Price == nil {
return false
}

for _, coin := range price {
amount := item.Price.AmountOf(coin.Denom)
if amount.GT(coin.Amount) {
item.Price = item.Price.Add(
sdk.NewCoin(coin.Denom, amount.Neg()),
).Add(coin)
}
}

k.SetNode(ctx, item)
return false
})

k.DeleteTransientKeyMaxPrice(ctx)
}

if k.IsMinPriceModified(ctx) {
price := k.MinPrice(ctx)
k.IterateNodes(ctx, func(_ int, item types.Node) (stop bool) {
if item.Price == nil {
return false
}

for _, coin := range price {
amount := item.Price.AmountOf(coin.Denom)
if amount.LT(coin.Amount) {
item.Price = item.Price.Add(
sdk.NewCoin(coin.Denom, amount.Neg()),
).Add(coin)
}
}

k.SetNode(ctx, item)
return false
})

k.DeleteTransientKeyMinPrice(ctx)
}

k.IterateInactiveNodesAt(ctx, ctx.BlockTime(), func(_ int, item types.Node) bool {
log.Info("inactive node", "value", item)

Expand Down
7 changes: 7 additions & 0 deletions x/node/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (k *msgServer) MsgRegister(c context.Context, msg *types.MsgRegisterRequest
return nil, types.ErrorProviderDoesNotExist
}
}
if msg.Price != nil && !k.IsValidPrice(ctx, msg.Price) {
return nil, types.ErrorInvalidPrice
}

deposit := k.Deposit(ctx)
if deposit.IsPositive() {
Expand Down Expand Up @@ -137,6 +140,10 @@ func (k *msgServer) MsgUpdate(c context.Context, msg *types.MsgUpdateRequest) (*
}
}
if msg.Price != nil {
if !k.IsValidPrice(ctx, msg.Price) {
return nil, types.ErrorInvalidPrice
}

node.Provider = ""
node.Price = msg.Price
}
Expand Down
49 changes: 49 additions & 0 deletions x/node/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/sentinel-official/hub/x/node/types"
)
Expand All @@ -18,6 +19,16 @@ func (k *Keeper) InactiveDuration(ctx sdk.Context) (duration time.Duration) {
return
}

func (k *Keeper) MaxPrice(ctx sdk.Context) (price sdk.Coins) {
k.params.Get(ctx, types.KeyMaxPrice, &price)
return
}

func (k *Keeper) MinPrice(ctx sdk.Context) (price sdk.Coins) {
k.params.Get(ctx, types.KeyMinPrice, &price)
return
}

func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.params.SetParamSet(ctx, &params)
}
Expand All @@ -26,5 +37,43 @@ func (k *Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
k.Deposit(ctx),
k.InactiveDuration(ctx),
k.MaxPrice(ctx),
k.MinPrice(ctx),
)
}

func (k *Keeper) IsValidPrice(ctx sdk.Context, price sdk.Coins) bool {
var (
maxPrice = k.MaxPrice(ctx)
minPrice = k.MinPrice(ctx)
)

return price.IsAllLTE(maxPrice) &&
price.IsAllGTE(minPrice)
}

func (k *Keeper) IsMaxPriceModified(ctx sdk.Context) bool {
return k.params.Modified(ctx, types.KeyMaxPrice)
}

func (k *Keeper) IsMinPriceModified(ctx sdk.Context) bool {
return k.params.Modified(ctx, types.KeyMinPrice)
}

func (k *Keeper) DeleteTransientKeyMaxPrice(ctx sdk.Context) {
var (
tkey = sdk.NewTransientStoreKey(paramstypes.TStoreKey)
store = ctx.TransientStore(tkey)
)

store.Delete(types.KeyMaxPrice)
}

func (k *Keeper) DeleteTransientKeyMinPrice(ctx sdk.Context) {
var (
tkey = sdk.NewTransientStoreKey(paramstypes.TStoreKey)
store = ctx.TransientStore(tkey)
)

store.Delete(types.KeyMinPrice)
}
34 changes: 32 additions & 2 deletions x/node/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
var (
deposit sdk.Coin
inactiveDuration time.Duration
maxPrice sdk.Coins
minPrice sdk.Coins
)

state.AppParams.GetOrGenerate(
Expand All @@ -24,7 +26,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
func(r *rand.Rand) {
deposit = sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxDepositAmount),
r.Int63n(MaxAmount),
)
},
)
Expand All @@ -37,9 +39,37 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
inactiveDuration = time.Duration(r.Int63n(MaxInactiveDuration)) * time.Millisecond
},
)
state.AppParams.GetOrGenerate(
state.Cdc,
string(types.KeyMaxPrice),
&maxPrice,
state.Rand,
func(r *rand.Rand) {
maxPrice = sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
),
)
},
)
state.AppParams.GetOrGenerate(
state.Cdc,
string(types.KeyMinPrice),
&minPrice,
state.Rand,
func(r *rand.Rand) {
minPrice = sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
),
)
},
)

return types.NewGenesisState(
RandomNodes(state.Rand, state.Accounts),
types.NewParams(deposit, inactiveDuration),
types.NewParams(deposit, inactiveDuration, maxPrice, minPrice),
)
}
4 changes: 2 additions & 2 deletions x/node/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
MaxDepositAmount = 1 << 18
MaxAmount = 1 << 18
MaxInactiveDuration = 1 << 18
)

Expand All @@ -25,7 +25,7 @@ func ParamChanges(_ *rand.Rand) []simulationtypes.ParamChange {
func(r *rand.Rand) string {
return sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxDepositAmount),
r.Int63n(MaxAmount),
).String()
},
),
Expand Down
58 changes: 56 additions & 2 deletions x/node/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ const (
)

var (
DefaultDeposit = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000))
DefaultDeposit = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000))
DefaultMaxPrice = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)))
DefaultMinPrice = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10)))
)

var (
KeyDeposit = []byte("Deposit")
KeyInactiveDuration = []byte("InactiveDuration")
KeyMaxPrice = []byte("MaxPrice")
KeyMinPrice = []byte("MinPrice")
)

var (
Expand All @@ -38,6 +42,16 @@ func (m *Params) Validate() error {
if m.InactiveDuration == 0 {
return fmt.Errorf("inactive_duration cannot be zero")
}
if m.MaxPrice != nil {
if !m.MaxPrice.IsValid() {
return fmt.Errorf("max_price must be valid")
}
}
if m.MinPrice != nil {
if !m.MinPrice.IsValid() {
return fmt.Errorf("min_price must be valid")
}
}

return nil
}
Expand Down Expand Up @@ -79,23 +93,63 @@ func (m *Params) ParamSetPairs() params.ParamSetPairs {
return fmt.Errorf("inactive_duration cannot be zero")
}

return nil
},
},
{
Key: KeyMaxPrice,
Value: &m.MaxPrice,
ValidatorFn: func(v interface{}) error {
value, ok := v.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type %T", v)
}

if value != nil {
if !value.IsValid() {
return fmt.Errorf("max_price must be valid")
}
}

return nil
},
},
{
Key: KeyMinPrice,
Value: &m.MinPrice,
ValidatorFn: func(v interface{}) error {
value, ok := v.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type %T", v)
}

if value != nil {
if !value.IsValid() {
return fmt.Errorf("min_price must be valid")
}
}

return nil
},
},
}
}

func NewParams(deposit sdk.Coin, inactiveDuration time.Duration) Params {
func NewParams(deposit sdk.Coin, inactiveDuration time.Duration, maxPrice, minPrice sdk.Coins) Params {
return Params{
Deposit: deposit,
InactiveDuration: inactiveDuration,
MaxPrice: maxPrice,
MinPrice: minPrice,
}
}

func DefaultParams() Params {
return Params{
Deposit: DefaultDeposit,
InactiveDuration: DefaultInactiveDuration,
MaxPrice: DefaultMaxPrice,
MinPrice: DefaultMinPrice,
}
}

Expand Down
Loading

0 comments on commit a151f6b

Please sign in to comment.