Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Jan 11, 2024
1 parent 879141b commit b13199a
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 187 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Duration, DurationSDKType } from "../../google/protobuf/duration";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** LimitParams defines rate limit params on a denom. */
Expand Down Expand Up @@ -34,10 +35,10 @@ export interface LimitParamsSDKType {

export interface Limiter {
/**
* period_sec is the rolling time period for which the limit applies
* period is the rolling time period for which the limit applies
* e.g. 3600 (an hour)
*/
periodSec: number;
period?: Duration;
/**
* baseline_minimum is the minimum maximum withdrawal coin amount within the
* time period.
Expand All @@ -57,10 +58,10 @@ export interface Limiter {

export interface LimiterSDKType {
/**
* period_sec is the rolling time period for which the limit applies
* period is the rolling time period for which the limit applies
* e.g. 3600 (an hour)
*/
period_sec: number;
period?: DurationSDKType;
/**
* baseline_minimum is the minimum maximum withdrawal coin amount within the
* time period.
Expand Down Expand Up @@ -134,16 +135,16 @@ export const LimitParams = {

function createBaseLimiter(): Limiter {
return {
periodSec: 0,
period: undefined,
baselineMinimum: new Uint8Array(),
baselineTvlPpm: 0
};
}

export const Limiter = {
encode(message: Limiter, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.periodSec !== 0) {
writer.uint32(16).uint32(message.periodSec);
if (message.period !== undefined) {
Duration.encode(message.period, writer.uint32(10).fork()).ldelim();
}

if (message.baselineMinimum.length !== 0) {
Expand All @@ -166,8 +167,8 @@ export const Limiter = {
const tag = reader.uint32();

switch (tag >>> 3) {
case 2:
message.periodSec = reader.uint32();
case 1:
message.period = Duration.decode(reader, reader.uint32());
break;

case 3:
Expand All @@ -189,7 +190,7 @@ export const Limiter = {

fromPartial(object: DeepPartial<Limiter>): Limiter {
const message = createBaseLimiter();
message.periodSec = object.periodSec ?? 0;
message.period = object.period !== undefined && object.period !== null ? Duration.fromPartial(object.period) : undefined;
message.baselineMinimum = object.baselineMinimum ?? new Uint8Array();
message.baselineTvlPpm = object.baselineTvlPpm ?? 0;
return message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LimitParams, LimitParamsSDKType } from "./limit_params";
import { Duration, DurationSDKType } from "../../google/protobuf/duration";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** ListLimitParamsRequest is a request type of the ListLimitParams RPC method. */
Expand Down Expand Up @@ -46,13 +47,13 @@ export interface QueryCapacityByDenomRequestSDKType {
/** CapacityResult is a specific rate limit for a denom. */

export interface CapacityResult {
periodSec: number;
period?: Duration;
capacity: Uint8Array;
}
/** CapacityResult is a specific rate limit for a denom. */

export interface CapacityResultSDKType {
period_sec: number;
period?: DurationSDKType;
capacity: Uint8Array;
}
/**
Expand Down Expand Up @@ -206,15 +207,15 @@ export const QueryCapacityByDenomRequest = {

function createBaseCapacityResult(): CapacityResult {
return {
periodSec: 0,
period: undefined,
capacity: new Uint8Array()
};
}

export const CapacityResult = {
encode(message: CapacityResult, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.periodSec !== 0) {
writer.uint32(8).uint32(message.periodSec);
if (message.period !== undefined) {
Duration.encode(message.period, writer.uint32(10).fork()).ldelim();
}

if (message.capacity.length !== 0) {
Expand All @@ -234,7 +235,7 @@ export const CapacityResult = {

switch (tag >>> 3) {
case 1:
message.periodSec = reader.uint32();
message.period = Duration.decode(reader, reader.uint32());
break;

case 2:
Expand All @@ -252,7 +253,7 @@ export const CapacityResult = {

fromPartial(object: DeepPartial<CapacityResult>): CapacityResult {
const message = createBaseCapacityResult();
message.periodSec = object.periodSec ?? 0;
message.period = object.period !== undefined && object.period !== null ? Duration.fromPartial(object.period) : undefined;
message.capacity = object.capacity ?? new Uint8Array();
return message;
}
Expand Down
6 changes: 4 additions & 2 deletions proto/dydxprotocol/ratelimit/limit_params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package dydxprotocol.ratelimit;

import "gogoproto/gogo.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types";

Expand All @@ -17,9 +18,10 @@ message LimitParams {

// Limiter defines one rate-limiter on a specfic denom.
message Limiter {
// period_sec is the rolling time period for which the limit applies
// period is the rolling time period for which the limit applies
// e.g. 3600 (an hour)
uint32 period_sec = 2;
google.protobuf.Duration period = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
// baseline_minimum is the minimum maximum withdrawal coin amount within the
// time period.
// e.g. 100_000_000_000 uusdc for 100k USDC; 5e22 adv4tnt for 50k DV4TNT
Expand Down
4 changes: 3 additions & 1 deletion proto/dydxprotocol/ratelimit/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package dydxprotocol.ratelimit;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "dydxprotocol/ratelimit/limit_params.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit/types";

Expand Down Expand Up @@ -36,7 +37,8 @@ message QueryCapacityByDenomRequest { string denom = 1; }

// CapacityResult is a specific rate limit for a denom.
message CapacityResult {
uint32 period_sec = 1;
google.protobuf.Duration period = 1
[ (gogoproto.nullable) = false, (gogoproto.stdduration) = true ];
bytes capacity = 2 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
Expand Down
23 changes: 11 additions & 12 deletions protocol/x/ratelimit/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,26 +259,25 @@ func (k Keeper) updateCapacityForLimitParams(
tvl := k.bankKeeper.GetSupply(ctx, limitParams.Denom)

capacityList := k.GetDenomCapacity(ctx, limitParams.Denom).CapacityList
if len(capacityList) != len(limitParams.Limiters) {
// This violates an invariant. Since this is in the `EndBlocker`, we log an error instead of panicking.

newCapacityList, err := ratelimitutil.CalculateNewCapacityList(
tvl.Amount.BigInt(),
limitParams,
capacityList,
timeSinceLastBlock,
)

if err != nil {
k.Logger(ctx).Error(
fmt.Sprintf(
"denom (%s) capacity list length (%v) != limiters length (%v); skipping capacity update",
"error calculating new capacity list for denom %v: %v. Skipping update.",
limitParams.Denom,
len(capacityList),
len(limitParams.Limiters),
err,
),
)
return
}

newCapacityList := ratelimitutil.CalculateNewCapacityList(
tvl.Amount.BigInt(),
limitParams,
capacityList,
timeSinceLastBlock,
)

k.SetDenomCapacity(ctx, types.DenomCapacity{
Denom: limitParams.Denom,
CapacityList: newCapacityList,
Expand Down
Loading

0 comments on commit b13199a

Please sign in to comment.