From 3d6aef2361aae10fae68056073d6d5e3fbc5e934 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 20 Nov 2023 17:55:28 +0100 Subject: [PATCH] ai suggestions --- x/uibc/genesis.go | 4 ++-- x/uibc/quota/keeper/genesis.go | 4 ++-- x/uibc/quota/keeper/quota.go | 29 ++++++++++++++--------------- x/uibc/quota/keeper/quota_test.go | 10 +++++----- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/x/uibc/genesis.go b/x/uibc/genesis.go index d5e544e094..51a7643b9e 100644 --- a/x/uibc/genesis.go +++ b/x/uibc/genesis.go @@ -50,11 +50,11 @@ func (gs GenesisState) Validate() error { } if gs.OutflowSum.IsNegative() { - return fmt.Errorf("total outflow sum cannot be negative : %s ", gs.OutflowSum.String()) + return fmt.Errorf("outflow sum cannot be negative : %s ", gs.OutflowSum.String()) } if gs.InflowSum.IsNegative() { - return fmt.Errorf("total inflow sum cannot be negative : %s ", gs.InflowSum.String()) + return fmt.Errorf("inflow sum cannot be negative : %s ", gs.InflowSum.String()) } return nil diff --git a/x/uibc/quota/keeper/genesis.go b/x/uibc/quota/keeper/genesis.go index 52740f5756..f8814d2797 100644 --- a/x/uibc/quota/keeper/genesis.go +++ b/x/uibc/quota/keeper/genesis.go @@ -16,7 +16,7 @@ func (kb Builder) InitGenesis(ctx sdk.Context, genState uibc.GenesisState) { k.SetTokenOutflows(genState.Outflows) k.SetTokenInflows(genState.Inflows) k.SetOutflowSum(genState.OutflowSum) - k.SetTotalInflow(genState.InflowSum) + k.SetInflowSum(genState.InflowSum) err = k.SetExpire(genState.QuotaExpires) util.Panic(err) @@ -38,6 +38,6 @@ func (kb Builder) ExportGenesis(ctx sdk.Context) *uibc.GenesisState { OutflowSum: k.GetTotalOutflow(), QuotaExpires: *quotaExpires, Inflows: inflows, - InflowSum: k.GetTotalInflow(), + InflowSum: k.GetInflowSum(), } } diff --git a/x/uibc/quota/keeper/quota.go b/x/uibc/quota/keeper/quota.go index 2c9aa55a8a..031da8275d 100644 --- a/x/uibc/quota/keeper/quota.go +++ b/x/uibc/quota/keeper/quota.go @@ -89,15 +89,15 @@ func (k Keeper) GetTokenInflow(denom string) sdk.DecCoin { return sdk.NewDecCoinFromDec(denom, amount) } -// GetTotalInflow returns the total inflow of ibc-transfer amount. -func (k Keeper) GetTotalInflow() sdk.Dec { +// GetInflowSum returns the total inflow of ibc-transfer amount. +func (k Keeper) GetInflowSum() sdk.Dec { // When total inflow is not stored in store it will return 0 amount, _ := store.GetDec(k.store, keyTotalInflows, "total_inflows") return amount } -// SetTotalInflow save the total inflow of ibc-transfer amount. -func (k Keeper) SetTotalInflow(amount sdk.Dec) { +// SetInflowSum save the total inflow of ibc-transfer amount. +func (k Keeper) SetInflowSum(amount sdk.Dec) { err := store.SetDec(k.store, keyTotalInflows, amount, "total_inflows") util.Panic(err) } @@ -126,7 +126,7 @@ func (k Keeper) ResetAllQuotas() error { store.DeleteByPrefixStore(ps) // inflows - k.SetTotalInflow(zero) + k.SetInflowSum(zero) ps = k.PrefixStore(keyPrefixDenomInflows) store.DeleteByPrefixStore(ps) return nil @@ -156,19 +156,18 @@ func (k Keeper) CheckAndUpdateQuota(denom string, newOutflow sdkmath.Int) error } // Allow outflow either of two conditions - // 1. Total Outflow Sum <= Total Outflow Quota - // or - // 2. Total Outflow Sum <= params.InflowOutflowQuotaBase + (params.InflowOutflowQuotaRate * sum of all inflows) - totalOutflowSum := k.GetTotalOutflow().Add(exchangePrice) - ttlInSum := k.GetTotalInflow() + // 1. Outflow Sum <= Total Outflow Quota + // 2. OR Outflow Sum <= params.InflowOutflowQuotaBase + (params.InflowOutflowQuotaRate * sum of all inflows) + outflowSum := k.GetTotalOutflow().Add(exchangePrice) + inflowSum := k.GetInflowSum() if !params.TotalQuota.IsZero() { - if totalOutflowSum.GT(params.TotalQuota) || - totalOutflowSum.GT(params.InflowOutflowQuotaBase.Add(ttlInSum.Mul(params.InflowOutflowQuotaRate))) { + if outflowSum.GT(params.TotalQuota) || + outflowSum.GT(params.InflowOutflowQuotaBase.Add(inflowSum.Mul(params.InflowOutflowQuotaRate))) { return uibc.ErrQuotaExceeded } } k.SetTokenOutflow(o) - k.SetOutflowSum(totalOutflowSum) + k.SetOutflowSum(outflowSum) return nil } @@ -261,8 +260,8 @@ func (k Keeper) RecordIBCInflow(ctx sdk.Context, tokenInflow := sdk.NewDecCoinFromDec(ibcDenom, inflowInUSD) k.SetTokenInflow(tokenInflow) - totalInflowSum := k.GetTotalInflow() - k.SetTotalInflow(totalInflowSum.Add(inflowInUSD)) + totalInflowSum := k.GetInflowSum() + k.SetInflowSum(totalInflowSum.Add(inflowInUSD)) } return nil diff --git a/x/uibc/quota/keeper/quota_test.go b/x/uibc/quota/keeper/quota_test.go index 2591fd5701..e6fce7096f 100644 --- a/x/uibc/quota/keeper/quota_test.go +++ b/x/uibc/quota/keeper/quota_test.go @@ -47,7 +47,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) { k.SetTokenOutflow(sdk.NewInt64DecCoin(umee, 6)) k.SetTokenInflow(sdk.NewInt64DecCoin(umee, 6)) k.SetOutflowSum(sdk.NewDec(50)) - k.SetTotalInflow(sdk.NewDec(50)) + k.SetInflowSum(sdk.NewDec(50)) k.SetTokenInflow(sdk.NewDecCoin(umee, math.NewInt(50))) err := k.CheckAndUpdateQuota(umee, sdk.NewInt(1)) @@ -125,7 +125,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) { err = k.SetParams(dp) k.SetOutflowSum(sdk.NewDec(80)) // 80+(20*2) > Total Outflow Quota Limit (100) - err = k.CheckAndUpdateQuota(umee, sdk.NewInt(20)) // exceeds token quota + err = k.CheckAndUpdateQuota(umee, sdk.NewInt(20)) assert.ErrorContains(t, err, "quota") err = k.CheckAndUpdateQuota(umee, sdk.NewInt(10)) assert.NilError(t, err) @@ -134,7 +134,7 @@ func TestUnitCheckAndUpdateQuota(t *testing.T) { err = k.SetParams(dp) assert.NilError(t, err) - k.SetTotalInflow(sdk.NewDec(100)) + k.SetInflowSum(sdk.NewDec(100)) // 80+(80*2) > InflowOutflowQuotaBase(100) + 25% of Total Inflow Sum (100) = 240 > 100+25 err = k.CheckAndUpdateQuota(umee, sdk.NewInt(80)) // exceeds token quota assert.ErrorContains(t, err, "quota") @@ -161,9 +161,9 @@ func TestUnitGetExchangePrice(t *testing.T) { func TestSetAndGetIBCInflows(t *testing.T) { k := initKeeperSimpleMock(t) inflowSum := sdk.MustNewDecFromStr("123123") - k.SetTotalInflow(inflowSum) + k.SetInflowSum(inflowSum) - rv := k.GetTotalInflow() + rv := k.GetInflowSum() assert.DeepEqual(t, inflowSum, rv) // inflow of token