Skip to content

Commit

Permalink
Fix denom key collision (sei-protocol#878)
Browse files Browse the repository at this point in the history
* Fix denom key collision

* fix pair string

* upgrade handler

* remove test upgrade handle
  • Loading branch information
codchen authored Jun 16, 2023
1 parent 7f21417 commit 624eed3
Show file tree
Hide file tree
Showing 29 changed files with 399 additions and 202 deletions.
1 change: 0 additions & 1 deletion cmd/seid/cmd/iavl_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func MatchAndExtractDexAddressPrefixKeys(key []byte) (bool, []string, []byte, er
// Source of truth: github.com/sei-protocol/sei-chain/x/dex/types/keys.go - contains key constants represented here
dextypes.LongBookKey,
dextypes.ShortBookKey,
dextypes.TriggerBookKey,
dextypes.PriceKey,
dextypes.TwapKey,
dextypes.SettlementEntryKey,
Expand Down
15 changes: 6 additions & 9 deletions x/dex/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ func (s *MemState) GetAllBlockOrders(ctx sdk.Context, contractAddr types.Contrac
return
}

func (s *MemState) GetBlockOrders(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) *BlockOrders {
func (s *MemState) GetBlockOrders(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) *BlockOrders {
s.SynchronizeAccess(ctx, contractAddr)
return NewOrders(
prefix.NewStore(
ctx.KVStore(s.storeKey),
types.MemOrderPrefixForPair(
string(contractAddr), string(pair),
string(contractAddr), pair.PriceDenom, pair.AssetDenom,
),
),
)
}

func (s *MemState) GetBlockCancels(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) *BlockCancellations {
func (s *MemState) GetBlockCancels(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) *BlockCancellations {
s.SynchronizeAccess(ctx, contractAddr)
return NewCancels(
prefix.NewStore(
ctx.KVStore(s.storeKey),
types.MemCancelPrefixForPair(
string(contractAddr), string(pair),
string(contractAddr), pair.PriceDenom, pair.AssetDenom,
),
),
)
Expand Down Expand Up @@ -123,17 +123,14 @@ func (s *MemState) Clear(ctx sdk.Context) {
s.contractsToProcess = &newContractToDependencies
}

func (s *MemState) ClearCancellationForPair(ctx sdk.Context, contractAddr types.ContractAddress, pair types.PairString) {
func (s *MemState) ClearCancellationForPair(ctx sdk.Context, contractAddr types.ContractAddress, pair types.Pair) {
s.SynchronizeAccess(ctx, contractAddr)
DeepDelete(ctx.KVStore(s.storeKey), types.KeyPrefix(types.MemCancelKey), func(v []byte) bool {
var c types.Cancellation
if err := c.Unmarshal(v); err != nil {
panic(err)
}
return c.ContractAddr == string(contractAddr) && types.GetPairString(&types.Pair{
AssetDenom: c.AssetDenom,
PriceDenom: c.PriceDenom,
}) == pair
return c.ContractAddr == string(contractAddr) && c.PriceDenom == pair.PriceDenom && c.AssetDenom == pair.AssetDenom
})
}

Expand Down
43 changes: 21 additions & 22 deletions x/dex/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,47 @@ import (

const (
TEST_CONTRACT = "test"
TEST_PAIR = "pair"
)

func TestDeepCopy(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 1,
Account: "test",
ContractAddr: TEST_CONTRACT,
})
stateTwo := stateOne.DeepCopy()
cachedCtx, _ := store.GetCachedContext(ctx)
stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 2,
Account: "test",
ContractAddr: TEST_CONTRACT,
})
// old state must not be changed
require.Equal(t, 1, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, 1, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
// new state must be changed
require.Equal(t, 2, len(stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, 2, len(stateTwo.GetBlockOrders(cachedCtx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
}

func TestDeepFilterAccounts(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 1,
Account: "test",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 2,
Account: "test2",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 1,
Creator: "test",
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 2,
Creator: "test2",
})
Expand All @@ -68,14 +67,14 @@ func TestDeepFilterAccounts(t *testing.T) {

stateOne.DeepFilterAccount(ctx, "test")
require.Equal(t, 1, len(stateOne.GetAllBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT))))
require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
require.Equal(t, 1, len(stateOne.GetDepositInfo(ctx, types.ContractAddress(TEST_CONTRACT)).Get()))
}

func TestDeepDelete(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 1,
Account: "test",
ContractAddr: TEST_CONTRACT,
Expand All @@ -86,47 +85,47 @@ func TestDeepDelete(t *testing.T) {
func TestClear(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Order{
stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Order{
Id: 1,
Account: "test",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 2,
ContractAddr: TEST_CONTRACT,
})
stateOne.Clear(ctx)
require.Equal(t, 0, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, 0, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, 0, len(stateOne.GetBlockOrders(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
require.Equal(t, 0, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
}

func TestClearCancellationForPair(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 1,
ContractAddr: TEST_CONTRACT,
PriceDenom: "USDC",
AssetDenom: "ATOM",
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 2,
ContractAddr: TEST_CONTRACT,
PriceDenom: "USDC",
AssetDenom: "ATOM",
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 3,
ContractAddr: TEST_CONTRACT,
PriceDenom: "USDC",
AssetDenom: "SEI",
})
stateOne.ClearCancellationForPair(ctx, TEST_CONTRACT, types.GetPairString(&types.Pair{
stateOne.ClearCancellationForPair(ctx, TEST_CONTRACT, types.Pair{
PriceDenom: "USDC",
AssetDenom: "ATOM",
}))
require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()))
require.Equal(t, uint64(3), stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()[0].Id)
})
require.Equal(t, 1, len(stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()))
require.Equal(t, uint64(3), stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()[0].Id)
}

func TestSynchronization(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions x/dex/cache/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,63 @@ import (
func TestCancelGetIdsToCancel(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 1,
Creator: "abc",
ContractAddr: TEST_CONTRACT,
})
ids := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).GetIdsToCancel()
ids := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).GetIdsToCancel()
require.Equal(t, 1, len(ids))
require.Equal(t, uint64(1), ids[0])
}

func TestCancelGetCancels(t *testing.T) {
keeper, ctx := keepertest.DexKeeper(t)
stateOne := dex.NewMemState(keeper.GetMemStoreKey())
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 1,
Creator: "abc",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 2,
Creator: "def",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 3,
Creator: "efg",
ContractAddr: TEST_CONTRACT,
})
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Add(&types.Cancellation{
stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Add(&types.Cancellation{
Id: 4,
Creator: "efg",
ContractAddr: TEST_CONTRACT,
})

cancels := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Get()
cancels := stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Get()
require.Equal(t, 4, len(cancels))
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{
Id: 1,
Creator: "abc",
ContractAddr: TEST_CONTRACT,
}))
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{
Id: 2,
Creator: "def",
ContractAddr: TEST_CONTRACT,
}))
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{
Id: 3,
Creator: "efg",
ContractAddr: TEST_CONTRACT,
}))
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{
require.True(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{
Id: 4,
Creator: "efg",
ContractAddr: TEST_CONTRACT,
}))
require.False(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), types.PairString(TEST_PAIR)).Has(&types.Cancellation{
require.False(t, stateOne.GetBlockCancels(ctx, types.ContractAddress(TEST_CONTRACT), keepertest.TestPair).Has(&types.Cancellation{
Id: 5,
Creator: "efg",
ContractAddr: TEST_CONTRACT,
Expand Down
Loading

0 comments on commit 624eed3

Please sign in to comment.