Skip to content

Commit

Permalink
Address nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Mar 22, 2024
1 parent 4f37c5e commit 715dbe0
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 108 deletions.
6 changes: 1 addition & 5 deletions protocol/x/clob/keeper/process_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,9 @@ func (k Keeper) PersistMatchDeleveragingToState(
metrics.GetLabelForBoolValue(metrics.IsLong, position.GetIsLong()),
metrics.GetLabelForBoolValue(metrics.DeliverTx, true),
)
collateralPoolAddress, err := k.subaccountsKeeper.GetCollateralPoolFromPerpetualId(ctx, perpetualId)
if err != nil {
return err
}
k.subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(
ctx,
collateralPoolAddress,
perpetualId,
lib.MustConvertIntegerToUint32(ctx.BlockHeight()),
)
return nil
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type SubaccountsKeeper interface {
)
SetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
collateralPoolAddr sdk.AccAddress,
perpetualId uint32,
blockHeight uint32,
)
TransferFeesToFeeCollectorModule(ctx sdk.Context, assetId uint32, amount *big.Int, perpetualId uint32) error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
`Negative TNC subaccount seen in state returns withdrawals and transfers unblocked
after the delay`: {
setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) {
sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 7)
sk.SetNegativeTncSubaccountSeenAtBlock(
ctx,
constants.BtcUsd_NoMarginRequirement.Params.Id,
7,
)
},

request: &types.QueryGetWithdrawalAndTransfersBlockedInfoRequest{
Expand All @@ -66,9 +70,13 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
},
},
`Negative TNC subaccount seen in state returns withdrawals and transfers unblocked
after the delay (for isolated collateral pool address)`: {
after the delay (for isolated perpetual)`: {
setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) {
sk.SetNegativeTncSubaccountSeenAtBlock(ctx, constants.IsoCollateralPoolAddress, 5)
sk.SetNegativeTncSubaccountSeenAtBlock(
ctx,
constants.IsoUsd_IsolatedMarket.Params.Id,
5,
)
},

request: &types.QueryGetWithdrawalAndTransfersBlockedInfoRequest{
Expand Down Expand Up @@ -120,7 +128,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
`Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers
unblocked after the max block number + delay (negative TNC subaccount block greater)`: {
setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) {
sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 27)
sk.SetNegativeTncSubaccountSeenAtBlock(
ctx,
constants.BtcUsd_NoMarginRequirement.Params.Id,
27,
)
bk.SetAllDowntimeInfo(
ctx,
&blocktimetypes.AllDowntimeInfo{
Expand Down Expand Up @@ -157,7 +169,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
`Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers
unblocked after the max block number + delay (chain outage block greater)`: {
setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) {
sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 37)
sk.SetNegativeTncSubaccountSeenAtBlock(
ctx,
constants.BtcUsd_NoMarginRequirement.Params.Id,
37,
)
bk.SetAllDowntimeInfo(
ctx,
&blocktimetypes.AllDowntimeInfo{
Expand Down Expand Up @@ -194,7 +210,11 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
`Negative TNC subaccount and chain outage seen in state returns withdrawals and transfers
unblocked after the max block number + delay (both blocks equal)`: {
setup: func(ctx sdktypes.Context, sk sakeeper.Keeper, bk btkeeper.Keeper) {
sk.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 3)
sk.SetNegativeTncSubaccountSeenAtBlock(
ctx,
constants.BtcUsd_NoMarginRequirement.Params.Id,
3,
)
bk.SetAllDowntimeInfo(
ctx,
&blocktimetypes.AllDowntimeInfo{
Expand Down Expand Up @@ -230,7 +250,10 @@ func TestQueryWithdrawalAndTransfersBlockedInfo(t *testing.T) {
},
} {
t.Run(testName, func(t *testing.T) {
ctx, keeper, _, _, _, _, _, blocktimeKeeper, _ := keepertest.SubaccountsKeepers(t, true)
ctx, keeper, pricesKeeper, perpetualsKeeper, _, _, _, blocktimeKeeper, _ := keepertest.SubaccountsKeepers(t, true)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper)
keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper)
tc.setup(ctx, *keeper, *blocktimeKeeper)
response, err := keeper.GetWithdrawalAndTransfersBlockedInfo(ctx, tc.request)
if tc.err != nil {
Expand Down
24 changes: 17 additions & 7 deletions protocol/x/subaccounts/keeper/negative_tnc_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
// seen in state for the given collateral pool address and a boolean for whether it exists in state.
func (k Keeper) GetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
collateralPoolAddr sdk.AccAddress,
collateralPoolAddress sdk.AccAddress,
) (uint32, bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
[]byte(types.NegativeTncSubaccountForCollateralPoolSeenAtBlockKeyPrefix),
)
return k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddr)
return k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddress)
}

// getNegativeTncSubaccountSeenAtBlock is a helper function that takes a store and returns the last
// block height a negative TNC subaccount was seen in state for the given collateral pool address
// and a boolean for whether it exists in state.
func (k Keeper) getNegativeTncSubaccountSeenAtBlock(
store storetypes.KVStore,
collateralPoolAddr sdk.AccAddress,
collateralPoolAddress sdk.AccAddress,
) (uint32, bool) {
b := store.Get(
collateralPoolAddr,
collateralPoolAddress,
)
blockHeight := gogotypes.UInt32Value{Value: 0}
exists := false
Expand All @@ -48,16 +48,26 @@ func (k Keeper) getNegativeTncSubaccountSeenAtBlock(
// This function will panic if the old block height is greater than the new block height.
func (k Keeper) SetNegativeTncSubaccountSeenAtBlock(
ctx sdk.Context,
collateralPoolAddr sdk.AccAddress,
perpetualId uint32,
blockHeight uint32,
) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
[]byte(types.NegativeTncSubaccountForCollateralPoolSeenAtBlockKeyPrefix),
)

collateralPoolAddress, err := k.GetCollateralPoolFromPerpetualId(ctx, perpetualId)
if err != nil {
panic(
fmt.Sprintf(
"SetNegativeTncSubaccountSeenAtBlock: non-existent perpetual id %d",
perpetualId,
),
)
}

// Panic if the stored block height value exists and is greater than the new block height value.
currentValue, exists := k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddr)
currentValue, exists := k.getNegativeTncSubaccountSeenAtBlock(store, collateralPoolAddress)
if exists && blockHeight < currentValue {
panic(
fmt.Sprintf(
Expand All @@ -70,7 +80,7 @@ func (k Keeper) SetNegativeTncSubaccountSeenAtBlock(

blockHeightValue := gogotypes.UInt32Value{Value: blockHeight}
store.Set(
collateralPoolAddr.Bytes(),
collateralPoolAddress.Bytes(),
k.cdc.MustMarshal(&blockHeightValue),
)
}
44 changes: 27 additions & 17 deletions protocol/x/subaccounts/keeper/negative_tnc_subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
)

func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
testPerpetualIds := []uint32{
constants.IsoUsd_IsolatedMarket.Params.Id,
constants.Iso2Usd_IsolatedMarket.Params.Id,
constants.BtcUsd_NoMarginRequirement.Params.Id,
}
testCollateralPoolAddresses := []sdk.AccAddress{
constants.IsoCollateralPoolAddress,
constants.Iso2CollateralPoolAddress,
Expand Down Expand Up @@ -42,8 +47,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
},
"Block height can be updated": {
setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) {
for _, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 1)
for i, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 1)
block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -62,8 +67,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
},
"Block height can be updated more than once": {
setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) {
for _, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 1)
for i, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 1)
block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -72,7 +77,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
block,
)

k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 2)
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 2)
block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -81,7 +86,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
block,
)

k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 3)
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 3)
block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -90,7 +95,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
block,
)

k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 10)
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 10)
block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -111,8 +116,8 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
},
"Block height can be updated to same block height": {
setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) {
for _, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 0)
for i, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 0)
block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -121,7 +126,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
block,
)

k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, 0)
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], 0)
block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -143,7 +148,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
"Block height can be updated to different block heights for each collateral pool address": {
setupTestAndPerformAssertions: func(ctx sdk.Context, k keeper.Keeper) {
for i, collateralPoolAddr := range testCollateralPoolAddresses {
k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, uint32(i))
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], uint32(i))
block, exists := k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -152,7 +157,7 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
block,
)

k.SetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr, uint32(2*i+1))
k.SetNegativeTncSubaccountSeenAtBlock(ctx, testPerpetualIds[i], uint32(2*i+1))
block, exists = k.GetNegativeTncSubaccountSeenAtBlock(ctx, collateralPoolAddr)
require.True(t, exists)
require.Equal(
Expand All @@ -176,7 +181,10 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup keeper state and test parameters.
ctx, subaccountsKeeper, _, _, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true)
ctx, subaccountsKeeper, pricesKeeper, perpetualsKeeper, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper)
keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper)

// Set the tracer on the multistore to verify the performed writes are correct.
traceDecoder := &tracer.TraceDecoder{}
Expand All @@ -195,13 +203,15 @@ func TestGetSetNegativeTncSubaccountSeenAtBlock(t *testing.T) {

func TestGetSetNegativeTncSubaccountSeenAtBlock_PanicsOnDecreasingBlock(t *testing.T) {
// Setup keeper state and test parameters.
ctx, subaccountsKeeper, _, _, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true)

subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 2)
ctx, subaccountsKeeper, pricesKeeper, perpetualsKeeper, _, _, _, _, _ := keepertest.SubaccountsKeepers(t, true)
keepertest.CreateTestMarkets(t, ctx, pricesKeeper)
keepertest.CreateTestLiquidityTiers(t, ctx, perpetualsKeeper)
keepertest.CreateTestPerpetuals(t, ctx, perpetualsKeeper)
subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, uint32(0), 2)
require.PanicsWithValue(
t,
"SetNegativeTncSubaccountSeenAtBlock: new block height (1) is less than the current block height (2)",
func() { subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, types.ModuleAddress, 1) },
func() { subaccountsKeeper.SetNegativeTncSubaccountSeenAtBlock(ctx, uint32(0), 1) },
)
}

Expand Down
Loading

0 comments on commit 715dbe0

Please sign in to comment.