Skip to content

Commit

Permalink
Merge pull request #222 from notional-labs/dang/update-rate-limit
Browse files Browse the repository at this point in the history
Add min amount for rate limit
  • Loading branch information
vuong177 authored Aug 21, 2023
2 parents 19168db + a1b4281 commit d52fcc9
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 120 deletions.
4 changes: 4 additions & 0 deletions proto/centauri/ratelimit/v1beta1/ratelimit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ message RateLimit {
Path path = 1;
Quota quota = 2;
Flow flow = 3;
string min_rate_limit_amount = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message WhitelistedAddressPair {
Expand Down
12 changes: 10 additions & 2 deletions proto/centauri/ratelimit/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ message MsgAddRateLimit {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
uint64 duration_hours = 6;
string min_rate_limit_amount = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
uint64 duration_hours = 7;
}

message MsgAddRateLimitResponse {}
Expand All @@ -51,7 +55,11 @@ message MsgUpdateRateLimit {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
uint64 duration_hours = 6;
string min_rate_limit_amount = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
uint64 duration_hours = 7;
}

message MsgUpdateRateLimitResponse {}
Expand Down
18 changes: 18 additions & 0 deletions scripts/proposalRateLimit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"messages": [
{
"@type": "/centauri.ratelimit.v1beta1.MsgAddRateLimit",
"authority": "centauri10d07y265gmmuvt4z0w9aw880jnsr700j7g7ejq",
"denom": "stake",
"channel_id": "channel-0",
"max_percent_send": "5",
"max_percent_recv": "5",
"min_rate_limit_amount": "10000000000",
"duration_hours": 1
}
],
"metadata": "AQ==",
"deposit": "10000000stake",
"title": "Proposal Title",
"summary": "Proposal Summary"
}
30 changes: 29 additions & 1 deletion x/ratelimit/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/notional-labs/centauri/v4/x/ratelimit/types"
)
Expand All @@ -21,6 +22,33 @@ func GetQueryCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand()
cmd.AddCommand(
GetCmdQueryAllRateLimits(),
)
return cmd
}

// GetCmdQueryAllRateLimits return all available rate limits.
func GetCmdQueryAllRateLimits() *cobra.Command {
cmd := &cobra.Command{
Use: "list-rate-limits",
Short: "Query all rate limits",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryAllRateLimitsRequest{}
res, err := queryClient.AllRateLimits(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
10 changes: 8 additions & 2 deletions x/ratelimit/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func NewKeeper(
tfmwKeeper tfmwkeeper.Keeper,
authority string,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
storeKey: key,
Expand All @@ -55,8 +60,9 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams()
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, &params)
return params
}

// SetParams set the params
Expand Down
8 changes: 8 additions & 0 deletions x/ratelimit/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (k Keeper) AddTransferRateLimit(goCtx context.Context, msg *types.MsgAddRat
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

err := k.AddRateLimit(ctx, msg)
if err != nil {
return nil, err
Expand All @@ -45,6 +49,10 @@ func (k Keeper) UpdateTransferRateLimit(goCtx context.Context, msg *types.MsgUpd
return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

err := k.UpdateRateLimit(ctx, msg)
if err != nil {
return nil, err
Expand Down
22 changes: 12 additions & 10 deletions x/ratelimit/keeper/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func EmitTransferDeniedEvent(ctx sdk.Context, reason, denom, channelId string, d
}

// Adds an amount to the flow in either the SEND or RECV direction
func (k Keeper) UpdateFlow(rateLimit types.RateLimit, direction types.PacketDirection, amount math.Int) error {
func (k Keeper) UpdateFlow(ctx sdk.Context, rateLimit types.RateLimit, direction types.PacketDirection, amount math.Int) error {
switch direction {
case types.PACKET_SEND:
return rateLimit.Flow.AddOutflow(amount, *rateLimit.Quota)
return rateLimit.Flow.AddOutflow(amount, *rateLimit.Quota, rateLimit.MinRateLimitAmount)
case types.PACKET_RECV:
return rateLimit.Flow.AddInflow(amount, *rateLimit.Quota)
return rateLimit.Flow.AddInflow(amount, *rateLimit.Quota, rateLimit.MinRateLimitAmount)
default:
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid packet direction (%s)", direction.String())
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (k Keeper) CheckRateLimitAndUpdateFlow(
return false, nil
}
// Update the flow object with the change in amount
if err := k.UpdateFlow(rateLimit, direction, amount); err != nil {
if err := k.UpdateFlow(ctx, rateLimit, direction, amount); err != nil {
// If the rate limit was exceeded, emit an event
EmitTransferDeniedEvent(ctx, types.EventRateLimitExceeded, denom, channelId, direction, amount, err)
return false, err
Expand Down Expand Up @@ -218,9 +218,10 @@ func (k Keeper) AddRateLimit(ctx sdk.Context, msg *types.MsgAddRateLimit) error
}

k.SetRateLimit(ctx, types.RateLimit{
Path: &path,
Quota: &quota,
Flow: &flow,
Path: &path,
Quota: &quota,
Flow: &flow,
MinRateLimitAmount: msg.MinRateLimitAmount,
})

return nil
Expand Down Expand Up @@ -261,9 +262,10 @@ func (k Keeper) UpdateRateLimit(ctx sdk.Context, msg *types.MsgUpdateRateLimit)
}

k.SetRateLimit(ctx, types.RateLimit{
Path: &path,
Quota: &quota,
Flow: &flow,
Path: &path,
Quota: &quota,
Flow: &flow,
MinRateLimitAmount: msg.MinRateLimitAmount,
})

return nil
Expand Down
Loading

0 comments on commit d52fcc9

Please sign in to comment.