Skip to content

Commit

Permalink
Use ImmutableAppend for migration 16 to 17 (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored Jun 30, 2023
1 parent ab785df commit 6089dc0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.8.2
github.com/rs/zerolog v1.27.0
github.com/sei-protocol/goutils v0.0.2
github.com/sirkon/goproxy v1.4.8
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.4
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint v0.37.0-dev
github.com/tendermint/tm-db v0.6.8-0.20220519162814-e24b96538a12
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
github.com/sei-protocol/sei-cosmos v0.2.53 h1:MSH00lb3ahioaOnJNC4yvMNNPuC9rC0kBZcY30ay5QI=
github.com/sei-protocol/sei-cosmos v0.2.53/go.mod h1:XSmrSNlBQ7OQrk6VZMgARWsnD+1JSnQeDEZ7LEmexbo=
github.com/sei-protocol/sei-iavl v0.1.7 h1:cUdHDBkxs0FF/kOt1qCVLm0K+Bqaw92/dbZSgn4kxiA=
Expand Down Expand Up @@ -1165,8 +1167,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
Expand Down
23 changes: 12 additions & 11 deletions x/dex/migrations/v16_to_v17.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package migrations
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/goutils"
"github.com/sei-protocol/sei-chain/x/dex/keeper"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

const OldPairSeparator = '|'

func OldPairPrefix(priceDenom string, assetDenom string) []byte {
return append(append([]byte(priceDenom), OldPairSeparator), []byte(assetDenom)...)
return goutils.ImmutableAppend(goutils.ImmutableAppend([]byte(priceDenom), OldPairSeparator), []byte(assetDenom)...)
}

func V16ToV17(ctx sdk.Context, dexkeeper keeper.Keeper) error {
Expand All @@ -27,15 +28,15 @@ func V16ToV17(ctx sdk.Context, dexkeeper keeper.Keeper) error {
for k, v := range kv {
kbz := []byte(k)
store.Delete(kbz)
rootStore.Set(append(newPref, kbz...), v)
rootStore.Set(goutils.ImmutableAppend(newPref, kbz...), v)
}
}

for _, c := range dexkeeper.GetAllContractInfo(ctx) {
for _, p := range dexkeeper.GetAllRegisteredPairs(ctx, c.ContractAddr) {
// long order book
handler(
append(
goutils.ImmutableAppend(
types.OrderBookContractPrefix(true, c.ContractAddr),
OldPairPrefix(p.PriceDenom, p.AssetDenom)...,
),
Expand All @@ -44,7 +45,7 @@ func V16ToV17(ctx sdk.Context, dexkeeper keeper.Keeper) error {

// short order book
handler(
append(
goutils.ImmutableAppend(
types.OrderBookContractPrefix(false, c.ContractAddr),
OldPairPrefix(p.PriceDenom, p.AssetDenom)...,
),
Expand All @@ -53,7 +54,7 @@ func V16ToV17(ctx sdk.Context, dexkeeper keeper.Keeper) error {

// price
handler(
append(
goutils.ImmutableAppend(
types.PriceContractPrefix(c.ContractAddr),
OldPairPrefix(p.PriceDenom, p.AssetDenom)...,
),
Expand All @@ -62,28 +63,28 @@ func V16ToV17(ctx sdk.Context, dexkeeper keeper.Keeper) error {

// order count (long)
handler(
append(
append(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(c.ContractAddr)...),
goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(c.ContractAddr)...),
OldPairPrefix(p.PriceDenom, p.AssetDenom)...,
),
types.OrderCountPrefix(c.ContractAddr, p.PriceDenom, p.AssetDenom, true),
)

// order count (short)
handler(
append(
append(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(c.ContractAddr)...),
goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(c.ContractAddr)...),
OldPairPrefix(p.PriceDenom, p.AssetDenom)...,
),
types.OrderCountPrefix(c.ContractAddr, p.PriceDenom, p.AssetDenom, false),
)

// registered pair
k := append(types.RegisteredPairPrefix(c.ContractAddr), OldPairPrefix(p.PriceDenom, p.AssetDenom)...)
k := goutils.ImmutableAppend(types.RegisteredPairPrefix(c.ContractAddr), OldPairPrefix(p.PriceDenom, p.AssetDenom)...)
pair := rootStore.Get(k)
rootStore.Delete(k)
rootStore.Set(
append(types.RegisteredPairPrefix(c.ContractAddr), types.PairPrefix(p.PriceDenom, p.AssetDenom)...),
goutils.ImmutableAppend(types.RegisteredPairPrefix(c.ContractAddr), types.PairPrefix(p.PriceDenom, p.AssetDenom)...),
pair,
)
}
Expand Down
39 changes: 20 additions & 19 deletions x/dex/migrations/v16_to_v17_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrations_test
import (
"testing"

"github.com/sei-protocol/goutils"
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/dex/migrations"
"github.com/sei-protocol/sei-chain/x/dex/types"
Expand All @@ -16,61 +17,61 @@ func TestMigrate16to17(t *testing.T) {
// add registered pair using the old key
pair := types.Pair{PriceDenom: keepertest.TestPair.PriceDenom, AssetDenom: keepertest.TestPair.AssetDenom}
store.Set(
append(
goutils.ImmutableAppend(
types.RegisteredPairPrefix(keepertest.TestContract),
migrations.OldPairPrefix(keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom)...,
),
dexkeeper.Cdc.MustMarshal(&pair),
)

value := []byte("test_value")
store.Set(append(
store.Set(goutils.ImmutableAppend(
types.OrderBookContractPrefix(true, keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
), value)
store.Set(append(
store.Set(goutils.ImmutableAppend(
types.OrderBookContractPrefix(false, keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
), value)
store.Set(append(
store.Set(goutils.ImmutableAppend(
types.PriceContractPrefix(keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
), value)
store.Set(append(
append(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
store.Set(goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
), value)
store.Set(append(
append(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
store.Set(goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
), value)
store.Set(append(types.KeyPrefix(types.AssetListKey), []byte(pair.PriceDenom)...), value)
store.Set(goutils.ImmutableAppend(types.KeyPrefix(types.AssetListKey), []byte(pair.PriceDenom)...), value)

err := migrations.V16ToV17(ctx, *dexkeeper)
require.NoError(t, err)

require.False(t, store.Has(append(
require.False(t, store.Has(goutils.ImmutableAppend(
types.OrderBookContractPrefix(true, keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
)))
require.False(t, store.Has(append(
require.False(t, store.Has(goutils.ImmutableAppend(
types.OrderBookContractPrefix(false, keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
)))
require.False(t, store.Has(append(
require.False(t, store.Has(goutils.ImmutableAppend(
types.PriceContractPrefix(keepertest.TestContract),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
)))
require.False(t, store.Has(append(
append(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
require.False(t, store.Has(goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.LongOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
)))
require.False(t, store.Has(append(
append(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
require.False(t, store.Has(goutils.ImmutableAppend(
goutils.ImmutableAppend(types.KeyPrefix(types.ShortOrderCountKey), types.AddressKeyPrefix(keepertest.TestContract)...),
migrations.OldPairPrefix(pair.PriceDenom, pair.AssetDenom)...,
)))
require.False(t, store.Has(append(types.KeyPrefix(types.AssetListKey), []byte(pair.PriceDenom)...)))
require.False(t, store.Has(append(
require.False(t, store.Has(goutils.ImmutableAppend(types.KeyPrefix(types.AssetListKey), []byte(pair.PriceDenom)...)))
require.False(t, store.Has(goutils.ImmutableAppend(
types.RegisteredPairPrefix(keepertest.TestContract),
migrations.OldPairPrefix(keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom)...,
)))
Expand All @@ -81,7 +82,7 @@ func TestMigrate16to17(t *testing.T) {
require.True(t, store.Has(types.OrderCountPrefix(keepertest.TestContract, pair.PriceDenom, pair.AssetDenom, true)))
require.True(t, store.Has(types.OrderCountPrefix(keepertest.TestContract, pair.PriceDenom, pair.AssetDenom, false)))
require.True(t, store.Has(types.AssetListPrefix(pair.PriceDenom)))
require.True(t, store.Has(append(
require.True(t, store.Has(goutils.ImmutableAppend(
types.RegisteredPairPrefix(keepertest.TestContract),
types.PairPrefix(keepertest.TestPair.PriceDenom, keepertest.TestPair.AssetDenom)...,
)))
Expand Down

0 comments on commit 6089dc0

Please sign in to comment.