Skip to content

Commit

Permalink
adding inflow quota check
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Oct 27, 2023
1 parent aeaf433 commit 8663c38
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ linters:
- dogsled
- errcheck
- exportloopref
- goconst
# - goconst
- gocritic
- gofmt
- goimports
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/simulations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
OpWeightMsgAggregateExchangeRateVote = "op_weight_msg_exchange_rate_aggregate_vote" //nolint: gosec
OpWeightMsgDelegateFeedConsent = "op_weight_msg_exchange_feed_consent" //nolint: gosec

salt = "89b8164ca0b4b8703ae9ab25962f3dd6d1de5d656f5442971a93b2ca7893f654" //nolint: gosec
salt = "89b8164ca0b4b8703ae9ab25962f3dd6d1de5d656f5442971a93b2ca7893f654"
)

var (
Expand Down
15 changes: 15 additions & 0 deletions x/uibc/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ func NewGenesisState(params Params, outflows sdk.DecCoins, outflowSum sdk.Dec) *
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
Inflows: nil,
Outflows: nil,
TotalOutflowSum: sdk.NewDec(0),
TotalInflowSum: sdk.NewDec(0),
}
}

Expand All @@ -38,9 +40,22 @@ func (gs GenesisState) Validate() error {
}
}

for _, o := range gs.Inflows {
if o.Amount.IsNil() {
return sdkerrors.ErrInvalidRequest.Wrap("ibc denom inflow must be defined")
}
if err := o.Validate(); err != nil {
return err
}
}

if gs.TotalOutflowSum.IsNegative() {
return fmt.Errorf("total outflow sum cannot be negative : %s ", gs.TotalOutflowSum.String())
}

if gs.TotalInflowSum.IsNegative() {
return fmt.Errorf("total inflow sum cannot be negative : %s ", gs.TotalInflowSum.String())
}

return nil
}
3 changes: 2 additions & 1 deletion x/uibc/quota/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestUnitParams(t *testing.T) {
params.IbcStatus = uibc.IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_DISABLED
params.TokenQuota = sdk.MustNewDecFromStr("12.23")
params.TotalQuota = sdk.MustNewDecFromStr("3.4321")
params.TotalInflowQuota = sdk.MustNewDecFromStr("3.4321")
err := k.SetParams(params)
require.NoError(err)
// check the updated params
Expand All @@ -48,7 +49,7 @@ func TestValidateEmergencyQuotaParamsUpdate(t *testing.T) {
{"valid total quota update", mkParams(99, 10, 50), ""},
{"valid update", mkParams(0, 0, 50), ""},

{"invalid update", mkParams(201, 11, 50), "can't increase"},
{"invalid update", mkParams(201, 11, 50), "can't increase"},
{"invalid total quota update", mkParams(101, 10, 50), "can't increase"},
{"invalid token quota update", mkParams(10, 12, 50), "can't increase"},
{"invalid quota duration update1", mkParams(100, 10, 51), "can't change QuotaDuration"},
Expand Down
14 changes: 5 additions & 9 deletions x/uibc/quota/keeper/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (k Keeper) GetAllOutflows() (sdk.DecCoins, error) {
func (k Keeper) GetAllInflows() (sdk.DecCoins, error) {
var inflows sdk.DecCoins
// creating PrefixStore upfront will remove the prefix from the key when running the iterator.
store := k.PrefixStore(keyPrefixDenomOutflows)
store := k.PrefixStore(keyPrefixDenomInflows)
iter := sdk.KVStorePrefixIterator(store, nil)
defer iter.Close()

Expand Down Expand Up @@ -177,23 +177,19 @@ func (k Keeper) CheckAndUpdateQuota(denom string, newOutflow sdkmath.Int) error
if !params.TokenQuota.IsZero() && o.Amount.GT(params.TokenQuota) {
return uibc.ErrQuotaExceeded
}

// Allow outflow either of two conditions
// 1. Total Outflow Sum <= Total Outflow Quota
// or
// 2 . Total Outflow Sum <= $1M + params.TotalInflowQuota * sum of all inflows
totalOutflowSum := k.GetTotalOutflow().Add(exchangePrice)
if !params.TotalQuota.IsZero() && totalOutflowSum.GT(params.TotalQuota) {
ttlInSum := k.GetTotalInflow()
if !(!params.TotalQuota.IsZero() && totalOutflowSum.LTE(params.TotalQuota) ||
!ttlInSum.IsZero() && totalOutflowSum.LTE(sdk.NewDec(10_000_000).Mul(ttlInSum).Add(params.TotalInflowQuota))) {
return uibc.ErrQuotaExceeded
}

totalInflowSum := k.GetTotalInflow()
if totalOutflowSum.GT(sdk.NewDec(10_000_000).Mul(totalInflowSum).Add(params.TotalInflowQuota)) {
return uibc.ErrQuotaExceeded
}

k.SetTokenOutflow(o)
k.SetTotalOutflowSum(totalOutflowSum)

return nil
}

Expand Down
19 changes: 13 additions & 6 deletions x/uibc/quota/keeper/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,31 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) {
require.NoError(t, err)
k.checkOutflows(umee, 50, 94)

// transferring additional 5 umee => 10USD, will fail total quota check
// transferring additional 5 umee => 10USD, will fail total quota check but it will pass inflow quota check
// sum of outflows <= $1M + params.InflowOutflowQuota * sum of all inflows = (10_000_000) * 50 + (0) = 500000000
// 104 <= 500000000
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(5))
require.NoError(t, err)
k.checkOutflows(umee, 60, 104)

// it will fail total quota check and inflow quota check also
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(5000000000))
require.ErrorContains(t, err, "quota")
k.checkOutflows(umee, 50, 94)
k.checkOutflows(umee, 60, 104)

// 3. Setting TotalQuota param to 0 should unlimit the total quota check
//
k.setQuotaParams(0, 0)
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(5))
require.NoError(t, err)
k.checkOutflows(umee, 60, 104)
k.checkOutflows(umee, 70, 114)

// 4. Setting TokenQuota to 65
// 4. Setting TokenQuota to 75
//
k.setQuotaParams(65, 0)
k.setQuotaParams(75, 0)
err = k.CheckAndUpdateQuota(umee, sdk.NewInt(1))
require.NoError(t, err)
k.checkOutflows(umee, 62, 106)
k.checkOutflows(umee, 72, 116)

err = k.CheckAndUpdateQuota(umee, sdk.NewInt(2)) // exceeds token quota
require.ErrorContains(t, err, "quota")
Expand Down

0 comments on commit 8663c38

Please sign in to comment.