Skip to content

Commit

Permalink
feat: change Coins to DecCoins for prices
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Nov 30, 2024
1 parent 25a08a7 commit 3e10005
Show file tree
Hide file tree
Showing 49 changed files with 2,911 additions and 407 deletions.
2 changes: 1 addition & 1 deletion app/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func NewKeepers(
encCfg.Codec, keys.KV(swaptypes.StoreKey), k.Subspace(swaptypes.ModuleName), k.AccountKeeper, k.BankKeeper,
)
k.VPNKeeper = vpnkeeper.NewKeeper(
encCfg.Codec, keys.KV(vpntypes.StoreKey), k.AccountKeeper, k.BankKeeper, k.DistributionKeeper,
encCfg.Codec, keys.KV(vpntypes.StoreKey), k.AccountKeeper, k.BankKeeper, k.DistributionKeeper, &k.OracleKeeper,
app.MsgServiceRouter(), govModuleAddr, authtypes.FeeCollectorName,
)

Expand Down
4 changes: 2 additions & 2 deletions proto/sentinel/node/v3/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ syntax = "proto3";
package sentinel.node.v3;

import "gogoproto/gogo.proto";
import "sentinel/node/v2/node.proto";
import "sentinel/node/v3/node.proto";
import "sentinel/node/v3/params.proto";

option go_package = "github.com/sentinel-official/hub/v12/x/node/types/v3";
option (gogoproto.equal_all) = false;
option (gogoproto.goproto_getters_all) = false;

message GenesisState {
repeated sentinel.node.v2.Node nodes = 1 [(gogoproto.nullable) = false];
repeated sentinel.node.v3.Node nodes = 1 [(gogoproto.nullable) = false];
Params params = 3 [(gogoproto.nullable) = false];
}
16 changes: 8 additions & 8 deletions proto/sentinel/node/v3/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ option (gogoproto.goproto_getters_all) = false;

message MsgRegisterNodeRequest {
string from = 1;
repeated cosmos.base.v1beta1.Coin gigabyte_prices = 2 [
repeated cosmos.base.v1beta1.DecCoin gigabyte_prices = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
repeated cosmos.base.v1beta1.Coin hourly_prices = 3 [
repeated cosmos.base.v1beta1.DecCoin hourly_prices = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
string remote_url = 4 [(gogoproto.customname) = "RemoteURL"];
}

message MsgUpdateNodeDetailsRequest {
string from = 1;
repeated cosmos.base.v1beta1.Coin gigabyte_prices = 2 [
repeated cosmos.base.v1beta1.DecCoin gigabyte_prices = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
repeated cosmos.base.v1beta1.Coin hourly_prices = 3 [
repeated cosmos.base.v1beta1.DecCoin hourly_prices = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
string remote_url = 4 [(gogoproto.customname) = "RemoteURL"];
}
Expand Down
33 changes: 33 additions & 0 deletions proto/sentinel/node/v3/node.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
syntax = "proto3";
package sentinel.node.v3;

import "cosmos/base/v1beta1/coin.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "sentinel/types/v1/status.proto";

option go_package = "github.com/sentinel-official/hub/v12/x/node/types/v3";
option (gogoproto.equal_all) = false;
option (gogoproto.goproto_getters_all) = false;

message Node {
string address = 1;
repeated cosmos.base.v1beta1.DecCoin gigabyte_prices = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
repeated cosmos.base.v1beta1.DecCoin hourly_prices = 3 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
string remote_url = 4 [(gogoproto.customname) = "RemoteURL"];
google.protobuf.Timestamp inactive_at = 5 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
sentinel.types.v1.Status status = 6;
google.protobuf.Timestamp status_at = 7 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
}
8 changes: 4 additions & 4 deletions proto/sentinel/node/v3/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ message Params {
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true
];
repeated cosmos.base.v1beta1.Coin min_gigabyte_prices = 3 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
repeated cosmos.base.v1beta1.DecCoin min_gigabyte_prices = 3 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins",
(gogoproto.nullable) = false
];
repeated cosmos.base.v1beta1.Coin min_hourly_prices = 4 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
repeated cosmos.base.v1beta1.DecCoin min_hourly_prices = 4 [
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins",
(gogoproto.nullable) = false
];
int64 max_session_gigabytes = 5;
Expand Down
41 changes: 41 additions & 0 deletions proto/sentinel/node/v3/querier.proto
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
syntax = "proto3";
package sentinel.node.v3;

import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "sentinel/node/v3/node.proto";
import "sentinel/node/v3/params.proto";
import "sentinel/types/v1/status.proto";

option go_package = "github.com/sentinel-official/hub/v12/x/node/types/v3";
option (gogoproto.equal_all) = false;
option (gogoproto.goproto_getters_all) = false;

message QueryNodesRequest {
sentinel.types.v1.Status status = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryNodesForPlanRequest {
uint64 id = 1;
sentinel.types.v1.Status status = 2;
cosmos.base.query.v1beta1.PageRequest pagination = 3;
}

message QueryNodeRequest {
string address = 1;
}

message QueryNodesResponse {
repeated Node nodes = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryNodesForPlanResponse {
repeated Node nodes = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryNodeResponse {
Node node = 1 [(gogoproto.nullable) = false];
}

message QueryParamsRequest {}

message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];
}

service QueryService {
rpc QueryNodes(QueryNodesRequest) returns (QueryNodesResponse) {
option (google.api.http).get = "/sentinel/node/v3/nodes";
}
rpc QueryNodesForPlan(QueryNodesForPlanRequest) returns (QueryNodesForPlanResponse) {
option (google.api.http).get = "/sentinel/node/v3/plans/{id}/nodes";
}
rpc QueryNode(QueryNodeRequest) returns (QueryNodeResponse) {
option (google.api.http).get = "/sentinel/node/v3/nodes/{address}";
}
rpc QueryParams(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/sentinel/node/v3/params";
}
Expand Down
4 changes: 2 additions & 2 deletions proto/sentinel/plan/v3/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ message MsgCreatePlanRequest {
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true
];
repeated cosmos.base.v1beta1.Coin prices = 4 [
repeated cosmos.base.v1beta1.DecCoin prices = 4 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
bool private = 5;
}
Expand Down
4 changes: 2 additions & 2 deletions proto/sentinel/plan/v3/plan.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ message Plan {
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true
];
repeated cosmos.base.v1beta1.Coin prices = 5 [
repeated cosmos.base.v1beta1.DecCoin prices = 5 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
];
bool private = 6;
sentinel.types.v1.Status status = 7;
Expand Down
6 changes: 5 additions & 1 deletion x/lease/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

base "github.com/sentinel-official/hub/v12/types"
nodetypes "github.com/sentinel-official/hub/v12/x/node/types/v2"
nodetypes "github.com/sentinel-official/hub/v12/x/node/types/v3"
providertypes "github.com/sentinel-official/hub/v12/x/provider/types/v2"
)

Expand All @@ -28,6 +28,10 @@ type NodeKeeper interface {
GetNode(ctx sdk.Context, addr base.NodeAddress) (nodetypes.Node, bool)
}

type OracleKeeper interface {
GetQuote(ctx sdk.Context, coin sdk.DecCoin) (sdk.Coin, error)
}

type PlanKeeper interface {
LeaseInactivePreHook(ctx sdk.Context, id uint64) error
}
Expand Down
6 changes: 5 additions & 1 deletion x/lease/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

base "github.com/sentinel-official/hub/v12/types"
nodetypes "github.com/sentinel-official/hub/v12/x/node/types/v2"
nodetypes "github.com/sentinel-official/hub/v12/x/node/types/v3"
providertypes "github.com/sentinel-official/hub/v12/x/provider/types/v2"
)

Expand Down Expand Up @@ -44,6 +44,10 @@ func (k *Keeper) GetNode(ctx sdk.Context, addr base.NodeAddress) (nodetypes.Node
return k.node.GetNode(ctx, addr)
}

func (k *Keeper) GetQuote(ctx sdk.Context, coin sdk.DecCoin) (sdk.Coin, error) {
return k.oracle.GetQuote(ctx, coin)
}

func (k *Keeper) GetProvider(ctx sdk.Context, addr base.ProvAddress) (providertypes.Provider, bool) {
return k.provider.GetProvider(ctx, addr)
}
Expand Down
5 changes: 5 additions & 0 deletions x/lease/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Keeper struct {

deposit expected.DepositKeeper
node expected.NodeKeeper
oracle expected.OracleKeeper
plan expected.PlanKeeper
provider expected.ProviderKeeper
}
Expand All @@ -45,6 +46,10 @@ func (k *Keeper) WithNodeKeeper(keeper expected.NodeKeeper) {
k.node = keeper
}

func (k *Keeper) WithOracleKeeper(keeper expected.OracleKeeper) {
k.oracle = keeper
}

func (k *Keeper) WithPlanKeeper(keeper expected.PlanKeeper) {
k.plan = keeper
}
Expand Down
18 changes: 12 additions & 6 deletions x/lease/keeper/msg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ func (k *Keeper) HandleMsgRenewLease(ctx sdk.Context, msg *v1.MsgRenewLeaseReque
return nil, types.NewErrorInvalidNodeStatus(nodeAddr, node.Status)
}

price, found := node.HourlyPrice(msg.Denom)
basePrice, found := node.HourlyPrice(msg.Denom)
if !found {
return nil, types.NewErrorPriceNotFound(msg.Denom)
}

// TODO: convert the price
quotePrice, err := k.GetQuote(ctx, basePrice)
if err != nil {
return nil, err
}

refund := lease.RefundAmount()
if err := k.SubtractDeposit(ctx, provAddr.Bytes(), refund); err != nil {
Expand All @@ -130,7 +133,7 @@ func (k *Keeper) HandleMsgRenewLease(ctx sdk.Context, msg *v1.MsgRenewLeaseReque
ID: lease.ID,
ProvAddress: lease.ProvAddress,
NodeAddress: lease.NodeAddress,
Price: price,
Price: quotePrice,
Hours: 0,
MaxHours: msg.Hours,
Renewable: lease.Renewable,
Expand Down Expand Up @@ -192,12 +195,15 @@ func (k *Keeper) HandleMsgStartLease(ctx sdk.Context, msg *v1.MsgStartLeaseReque
return nil, types.NewErrorInvalidNodeStatus(nodeAddr, node.Status)
}

price, found := node.HourlyPrice(msg.Denom)
basePrice, found := node.HourlyPrice(msg.Denom)
if !found {
return nil, types.NewErrorPriceNotFound(msg.Denom)
}

// TODO: convert the price
quotePrice, err := k.GetQuote(ctx, basePrice)
if err != nil {
return nil, err
}

leaseExists := false
k.IterateLeasesForNodeByProvider(ctx, nodeAddr, provAddr, func(_ int, _ v1.Lease) bool {
Expand All @@ -214,7 +220,7 @@ func (k *Keeper) HandleMsgStartLease(ctx sdk.Context, msg *v1.MsgStartLeaseReque
ID: count + 1,
ProvAddress: provAddr.String(),
NodeAddress: nodeAddr.String(),
Price: price,
Price: quotePrice,
Hours: 0,
MaxHours: msg.Hours,
Renewable: msg.Renewable,
Expand Down
8 changes: 4 additions & 4 deletions x/node/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
flagRemoteURL = "remote-url"
)

func GetGigabytePrices(flags *pflag.FlagSet) (sdk.Coins, error) {
func GetGigabytePrices(flags *pflag.FlagSet) (sdk.DecCoins, error) {
s, err := flags.GetString(flagGigabytePrices)
if err != nil {
return nil, err
Expand All @@ -20,10 +20,10 @@ func GetGigabytePrices(flags *pflag.FlagSet) (sdk.Coins, error) {
return nil, nil
}

return sdk.ParseCoinsNormalized(s)
return sdk.ParseDecCoins(s)
}

func GetHourlyPrices(flags *pflag.FlagSet) (sdk.Coins, error) {
func GetHourlyPrices(flags *pflag.FlagSet) (sdk.DecCoins, error) {
s, err := flags.GetString(flagHourlyPrices)
if err != nil {
return nil, err
Expand All @@ -32,5 +32,5 @@ func GetHourlyPrices(flags *pflag.FlagSet) (sdk.Coins, error) {
return nil, nil
}

return sdk.ParseCoinsNormalized(s)
return sdk.ParseDecCoins(s)
}
4 changes: 2 additions & 2 deletions x/node/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func txRegisterNode() *cobra.Command {
return err
}

gigabytePrices, err := sdk.ParseCoinsNormalized(args[1])
gigabytePrices, err := sdk.ParseDecCoins(args[1])
if err != nil {
return err
}

hourlyPrices, err := sdk.ParseCoinsNormalized(args[2])
hourlyPrices, err := sdk.ParseDecCoins(args[2])
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions x/node/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type LeaseKeeper interface {
NodeInactivePreHook(ctx sdk.Context, addr base.NodeAddress) error
}

type OracleKeeper interface {
GetQuote(ctx sdk.Context, coin sdk.DecCoin) (sdk.Coin, error)
}

type SessionKeeper interface {
DeleteSession(ctx sdk.Context, id uint64)
DeleteSessionForAccount(ctx sdk.Context, addr sdk.AccAddress, id uint64)
Expand Down
3 changes: 1 addition & 2 deletions x/node/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (

base "github.com/sentinel-official/hub/v12/types"
v1base "github.com/sentinel-official/hub/v12/types/v1"
"github.com/sentinel-official/hub/v12/x/node/types/v2"
"github.com/sentinel-official/hub/v12/x/node/types/v3"
)

func (k *Keeper) handleInactiveNodes(ctx sdk.Context) {
k.IterateNodesForInactiveAt(ctx, ctx.BlockTime(), func(_ int, item v2.Node) bool {
k.IterateNodesForInactiveAt(ctx, ctx.BlockTime(), func(_ int, item v3.Node) bool {
nodeAddr, err := base.NodeAddressFromBech32(item.Address)
if err != nil {
panic(err)
Expand Down
4 changes: 4 additions & 0 deletions x/node/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (k *Keeper) SubtractDeposit(ctx sdk.Context, addr sdk.AccAddress, coin sdk.
return k.deposit.SubtractDeposit(ctx, addr, sdk.NewCoins(coin))
}

func (k *Keeper) GetQuote(ctx sdk.Context, coin sdk.DecCoin) (sdk.Coin, error) {
return k.oracle.GetQuote(ctx, coin)
}

func (k *Keeper) DeleteSession(ctx sdk.Context, id uint64) {
k.session.DeleteSession(ctx, id)
}
Expand Down
Loading

0 comments on commit 3e10005

Please sign in to comment.