From 4814fb3514bc95ebd20c6bcba7e07ac397920ac2 Mon Sep 17 00:00:00 2001 From: kogisin Date: Thu, 26 Aug 2021 21:39:10 +0900 Subject: [PATCH 01/13] test: debugging diffs of two different genesis files --- app/sim_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/app/sim_test.go b/app/sim_test.go index f618082e7..8732e6346 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -16,10 +16,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" + "github.com/cosmos/cosmos-sdk/x/genutil" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" @@ -32,6 +34,8 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" dbm "github.com/tendermint/tm-db" + tmtypes "github.com/tendermint/tendermint/types" + liquiditytypes "github.com/tendermint/liquidity/x/liquidity/types" ) @@ -138,6 +142,17 @@ func TestAppImportExport(t *testing.T) { exported, err := app.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err) + // TEST + genDoc := &tmtypes.GenesisDoc{} + genDoc.ChainID = "simulation-chain" + genDoc.Validators = nil + genDoc.AppState = exported.AppState + + err = genutil.ExportGenesisFile(genDoc, "../genesis.json") + if err != nil { + panic(err) + } + fmt.Printf("importing genesis...\n") _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") @@ -148,7 +163,7 @@ func TestAppImportExport(t *testing.T) { require.NoError(t, os.RemoveAll(newDir)) }() - newApp := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt) + newApp := NewLiquidityApp(log.NewNopLogger(), newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt) require.Equal(t, appName, app.Name()) var genesisState GenesisState @@ -160,15 +175,30 @@ func TestAppImportExport(t *testing.T) { newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState) newApp.StoreConsensusParams(ctxB, exported.ConsensusParams) + // TEST + exported2, err := newApp.ExportAppStateAndValidators(false, []string{}) + require.NoError(t, err) + + newgenDoc := &tmtypes.GenesisDoc{} + newgenDoc.ChainID = "simulation-chain" + newgenDoc.Validators = nil + newgenDoc.AppState = exported2.AppState + + err = genutil.ExportGenesisFile(newgenDoc, "../genesis-after.json") + if err != nil { + panic(err) + } + fmt.Printf("comparing stores...\n") storeKeysPrefixes := []StoreKeysPrefixes{ {app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}}, {app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey], [][]byte{ - stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey, - stakingtypes.HistoricalInfoKey, - }}, // ordering may change but it doesn't matter + stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, + stakingtypes.ValidatorQueueKey, stakingtypes.HistoricalInfoKey, + }, + }, // ordering may change but it doesn't matter {app.keys[slashingtypes.StoreKey], newApp.keys[slashingtypes.StoreKey], [][]byte{}}, {app.keys[minttypes.StoreKey], newApp.keys[minttypes.StoreKey], [][]byte{}}, {app.keys[distrtypes.StoreKey], newApp.keys[distrtypes.StoreKey], [][]byte{}}, @@ -177,7 +207,19 @@ func TestAppImportExport(t *testing.T) { {app.keys[govtypes.StoreKey], newApp.keys[govtypes.StoreKey], [][]byte{}}, {app.keys[evidencetypes.StoreKey], newApp.keys[evidencetypes.StoreKey], [][]byte{}}, {app.keys[capabilitytypes.StoreKey], newApp.keys[capabilitytypes.StoreKey], [][]byte{}}, - {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{}}, + {app.keys[authzkeeper.StoreKey], newApp.keys[authzkeeper.StoreKey], [][]byte{}}, + {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], + [][]byte{ + // liquiditytypes.GlobalLiquidityPoolIDKey, + // liquiditytypes.PoolKeyPrefix, + liquiditytypes.PoolByReserveAccIndexKeyPrefix, + liquiditytypes.PoolBatchIndexKeyPrefix, + liquiditytypes.PoolBatchKeyPrefix, + // liquiditytypes.PoolBatchDepositMsgStateIndexKeyPrefix, + // liquiditytypes.PoolBatchWithdrawMsgStateIndexKeyPrefix, + // liquiditytypes.PoolBatchSwapMsgStateIndexKeyPrefix, + }, + }, } for _, skp := range storeKeysPrefixes { @@ -188,6 +230,7 @@ func TestAppImportExport(t *testing.T) { require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B) + require.Equal(t, len(failedKVAs), 0, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs)) } } From 02978a7e385332eccc8236c118a854aacd08d3c2 Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 27 Aug 2021 17:41:18 +0900 Subject: [PATCH 02/13] test: add more store keys in decoder --- x/liquidity/simulation/decoder.go | 8 ++++++++ x/liquidity/simulation/decoder_test.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index bb2b6aab4..483c6d4e9 100644 --- a/x/liquidity/simulation/decoder.go +++ b/x/liquidity/simulation/decoder.go @@ -29,6 +29,14 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { cdc.MustUnmarshal(kvA.Value, &lpbB) return fmt.Sprintf("%v\n%v", lpbA, lpbB) + case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix), + bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix), + bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): + var lpbA, lpbB types.PoolBatch + cdc.MustUnmarshal(kvA.Value, &lpbA) + cdc.MustUnmarshal(kvA.Value, &lpbB) + return fmt.Sprintf("%v\n%v", lpbA, lpbB) + default: panic(fmt.Sprintf("invalid liquidity key prefix %X", kvA.Key[:1])) } diff --git a/x/liquidity/simulation/decoder_test.go b/x/liquidity/simulation/decoder_test.go index fc499d880..468b03d40 100644 --- a/x/liquidity/simulation/decoder_test.go +++ b/x/liquidity/simulation/decoder_test.go @@ -24,6 +24,9 @@ func TestDecodeLiquidityStore(t *testing.T) { Pairs: []kv.Pair{ {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&liquidityPool)}, {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, + {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, + {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, + {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, {Key: []byte{0x99}, Value: []byte{0x99}}, }, } @@ -34,6 +37,9 @@ func TestDecodeLiquidityStore(t *testing.T) { }{ {"Pool", fmt.Sprintf("%v\n%v", liquidityPool, liquidityPool)}, {"PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, + {"Deposit PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, + {"Withdraw PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, + {"Swap PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, {"other", ""}, } for i, tt := range tests { From 4c2fc0d06c9811589f5d6bab598bc846af1e3eeb Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 27 Aug 2021 20:46:54 +0900 Subject: [PATCH 03/13] test: fix decoder to have msg states --- x/liquidity/simulation/decoder.go | 18 ++++++++++++++---- x/liquidity/simulation/decoder_test.go | 15 +++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index 483c6d4e9..0efd63852 100644 --- a/x/liquidity/simulation/decoder.go +++ b/x/liquidity/simulation/decoder.go @@ -29,10 +29,20 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { cdc.MustUnmarshal(kvA.Value, &lpbB) return fmt.Sprintf("%v\n%v", lpbA, lpbB) - case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix), - bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix), - bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): - var lpbA, lpbB types.PoolBatch + case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): + var lpbA, lpbB types.DepositMsgState + cdc.MustUnmarshal(kvA.Value, &lpbA) + cdc.MustUnmarshal(kvA.Value, &lpbB) + return fmt.Sprintf("%v\n%v", lpbA, lpbB) + + case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): + var lpbA, lpbB types.WithdrawMsgState + cdc.MustUnmarshal(kvA.Value, &lpbA) + cdc.MustUnmarshal(kvA.Value, &lpbB) + return fmt.Sprintf("%v\n%v", lpbA, lpbB) + + case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): + var lpbA, lpbB types.SwapMsgState cdc.MustUnmarshal(kvA.Value, &lpbA) cdc.MustUnmarshal(kvA.Value, &lpbB) return fmt.Sprintf("%v\n%v", lpbA, lpbB) diff --git a/x/liquidity/simulation/decoder_test.go b/x/liquidity/simulation/decoder_test.go index 468b03d40..28e3b2e3a 100644 --- a/x/liquidity/simulation/decoder_test.go +++ b/x/liquidity/simulation/decoder_test.go @@ -19,14 +19,17 @@ func TestDecodeLiquidityStore(t *testing.T) { liquidityPool := types.Pool{} liquidityPool.Id = 1 liquidityPoolBatch := types.NewPoolBatch(1, 1) + depositMsgState := types.DepositMsgState{} + withdrawMsgState := types.WithdrawMsgState{} + swapMsgState := types.SwapMsgState{} kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&liquidityPool)}, {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, - {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, - {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, - {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, + {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositMsgState)}, + {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawMsgState)}, + {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapMsgState)}, {Key: []byte{0x99}, Value: []byte{0x99}}, }, } @@ -37,9 +40,9 @@ func TestDecodeLiquidityStore(t *testing.T) { }{ {"Pool", fmt.Sprintf("%v\n%v", liquidityPool, liquidityPool)}, {"PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, - {"Deposit PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, - {"Withdraw PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, - {"Swap PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, + {"Deposit PoolBatch", fmt.Sprintf("%v\n%v", depositMsgState, depositMsgState)}, + {"Withdraw PoolBatch", fmt.Sprintf("%v\n%v", withdrawMsgState, withdrawMsgState)}, + {"Swap PoolBatch", fmt.Sprintf("%v\n%v", swapMsgState, swapMsgState)}, {"other", ""}, } for i, tt := range tests { From 20bc3bba04a3632bf1a50037d0b6eaf593a81216 Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 27 Aug 2021 21:27:01 +0900 Subject: [PATCH 04/13] fix: context block height --- x/liquidity/keeper/liquidity_pool.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/liquidity/keeper/liquidity_pool.go b/x/liquidity/keeper/liquidity_pool.go index d6e07ad94..6c0795f92 100644 --- a/x/liquidity/keeper/liquidity_pool.go +++ b/x/liquidity/keeper/liquidity_pool.go @@ -551,9 +551,11 @@ func (k Keeper) GetPoolRecord(ctx sdk.Context, pool types.Pool) (types.PoolRecor // SetPoolRecord stores liquidity pool states func (k Keeper) SetPoolRecord(ctx sdk.Context, record types.PoolRecord) types.PoolRecord { + if record.PoolBatch.BeginHeight > ctx.BlockHeight() { + record.PoolBatch.BeginHeight = ctx.BlockHeight() + } k.SetPoolAtomic(ctx, record.Pool) k.GetNextPoolBatchIndexWithUpdate(ctx, record.Pool.Id) - record.PoolBatch.BeginHeight = ctx.BlockHeight() k.SetPoolBatch(ctx, record.PoolBatch) k.SetPoolBatchDepositMsgStates(ctx, record.Pool.Id, record.DepositMsgStates) k.SetPoolBatchWithdrawMsgStates(ctx, record.Pool.Id, record.WithdrawMsgStates) From 7c89d857a214fc427ab4a35bb806b3c280dffa75 Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 27 Aug 2021 21:51:02 +0900 Subject: [PATCH 05/13] test: debugging proto: wrong wireType = 0 for field Msg --- app/sim_test.go | 20 +++++++++++++++----- x/liquidity/simulation/decoder.go | 12 ++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/sim_test.go b/app/sim_test.go index 8732e6346..567e7bd4a 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -8,6 +8,7 @@ import ( "math/rand" "os" "testing" + "time" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/simapp" @@ -142,9 +143,13 @@ func TestAppImportExport(t *testing.T) { exported, err := app.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err) - // TEST + // + // TEST: export genesis file + // + now := time.Now() genDoc := &tmtypes.GenesisDoc{} genDoc.ChainID = "simulation-chain" + genDoc.GenesisTime = now genDoc.Validators = nil genDoc.AppState = exported.AppState @@ -152,6 +157,7 @@ func TestAppImportExport(t *testing.T) { if err != nil { panic(err) } + // fmt.Printf("importing genesis...\n") @@ -175,19 +181,23 @@ func TestAppImportExport(t *testing.T) { newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState) newApp.StoreConsensusParams(ctxB, exported.ConsensusParams) - // TEST + // + // TEST: export genesis file + // exported2, err := newApp.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err) newgenDoc := &tmtypes.GenesisDoc{} newgenDoc.ChainID = "simulation-chain" + newgenDoc.GenesisTime = now newgenDoc.Validators = nil newgenDoc.AppState = exported2.AppState - err = genutil.ExportGenesisFile(newgenDoc, "../genesis-after.json") + err = genutil.ExportGenesisFile(newgenDoc, "../genesis-new.json") if err != nil { panic(err) } + // fmt.Printf("comparing stores...\n") @@ -210,11 +220,11 @@ func TestAppImportExport(t *testing.T) { {app.keys[authzkeeper.StoreKey], newApp.keys[authzkeeper.StoreKey], [][]byte{}}, {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{ - // liquiditytypes.GlobalLiquidityPoolIDKey, + liquiditytypes.GlobalLiquidityPoolIDKey, // liquiditytypes.PoolKeyPrefix, liquiditytypes.PoolByReserveAccIndexKeyPrefix, liquiditytypes.PoolBatchIndexKeyPrefix, - liquiditytypes.PoolBatchKeyPrefix, + // liquiditytypes.PoolBatchKeyPrefix, // liquiditytypes.PoolBatchDepositMsgStateIndexKeyPrefix, // liquiditytypes.PoolBatchWithdrawMsgStateIndexKeyPrefix, // liquiditytypes.PoolBatchSwapMsgStateIndexKeyPrefix, diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index 0efd63852..ed5fab239 100644 --- a/x/liquidity/simulation/decoder.go +++ b/x/liquidity/simulation/decoder.go @@ -31,20 +31,20 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): var lpbA, lpbB types.DepositMsgState - cdc.MustUnmarshal(kvA.Value, &lpbA) - cdc.MustUnmarshal(kvA.Value, &lpbB) + lpbA = types.MustUnmarshalDepositMsgState(cdc, kvA.Value) + lpbB = types.MustUnmarshalDepositMsgState(cdc, kvB.Value) return fmt.Sprintf("%v\n%v", lpbA, lpbB) case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): var lpbA, lpbB types.WithdrawMsgState - cdc.MustUnmarshal(kvA.Value, &lpbA) - cdc.MustUnmarshal(kvA.Value, &lpbB) + lpbA = types.MustUnmarshalWithdrawMsgState(cdc, kvA.Value) + lpbB = types.MustUnmarshalWithdrawMsgState(cdc, kvB.Value) return fmt.Sprintf("%v\n%v", lpbA, lpbB) case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): var lpbA, lpbB types.SwapMsgState - cdc.MustUnmarshal(kvA.Value, &lpbA) - cdc.MustUnmarshal(kvA.Value, &lpbB) + lpbA = types.MustUnmarshalSwapMsgState(cdc, kvA.Value) + lpbB = types.MustUnmarshalSwapMsgState(cdc, kvB.Value) return fmt.Sprintf("%v\n%v", lpbA, lpbB) default: From 32c6314906ff6ce67296cebcaf1242b63d68ea08 Mon Sep 17 00:00:00 2001 From: kogisin Date: Fri, 27 Aug 2021 23:25:06 +0900 Subject: [PATCH 06/13] fix comment --- x/liquidity/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/liquidity/module.go b/x/liquidity/module.go index 2725753fc..73eb9c3ba 100644 --- a/x/liquidity/module.go +++ b/x/liquidity/module.go @@ -191,7 +191,7 @@ func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { return simulation.ParamChanges(r) } -// RegisterStoreDecoder registers a decoder for supply module's types +// RegisterStoreDecoder registers a decoder for liquidity module's types func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) } From d8e8461f785b65ba96a66a8a73b8595240d60a1f Mon Sep 17 00:00:00 2001 From: kogisin Date: Sat, 28 Aug 2021 00:02:05 +0900 Subject: [PATCH 07/13] test: debugging --- x/liquidity/keeper/liquidity_pool.go | 4 +- x/liquidity/simulation/decoder.go | 62 +++++++++++------ x/liquidity/simulation/decoder_test.go | 93 +++++++++++++++++++++----- 3 files changed, 120 insertions(+), 39 deletions(-) diff --git a/x/liquidity/keeper/liquidity_pool.go b/x/liquidity/keeper/liquidity_pool.go index 6c0795f92..eea801870 100644 --- a/x/liquidity/keeper/liquidity_pool.go +++ b/x/liquidity/keeper/liquidity_pool.go @@ -551,11 +551,11 @@ func (k Keeper) GetPoolRecord(ctx sdk.Context, pool types.Pool) (types.PoolRecor // SetPoolRecord stores liquidity pool states func (k Keeper) SetPoolRecord(ctx sdk.Context, record types.PoolRecord) types.PoolRecord { + k.SetPoolAtomic(ctx, record.Pool) + k.GetNextPoolBatchIndexWithUpdate(ctx, record.Pool.Id) if record.PoolBatch.BeginHeight > ctx.BlockHeight() { record.PoolBatch.BeginHeight = ctx.BlockHeight() } - k.SetPoolAtomic(ctx, record.Pool) - k.GetNextPoolBatchIndexWithUpdate(ctx, record.Pool.Id) k.SetPoolBatch(ctx, record.PoolBatch) k.SetPoolBatchDepositMsgStates(ctx, record.Pool.Id, record.DepositMsgStates) k.SetPoolBatchWithdrawMsgStates(ctx, record.Pool.Id, record.WithdrawMsgStates) diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index ed5fab239..7c139d67f 100644 --- a/x/liquidity/simulation/decoder.go +++ b/x/liquidity/simulation/decoder.go @@ -17,35 +17,57 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { switch { case bytes.Equal(kvA.Key[:1], types.PoolKeyPrefix), bytes.Equal(kvA.Key[:1], types.PoolByReserveAccIndexKeyPrefix): - var lpA, lpB types.Pool - cdc.MustUnmarshal(kvA.Value, &lpA) - cdc.MustUnmarshal(kvA.Value, &lpB) - return fmt.Sprintf("%v\n%v", lpA, lpB) + var poolA, poolB types.Pool + cdc.MustUnmarshal(kvA.Value, &poolA) + cdc.MustUnmarshal(kvA.Value, &poolB) + return fmt.Sprintf("%v\n%v", poolA, poolB) case bytes.Equal(kvA.Key[:1], types.PoolBatchIndexKeyPrefix), bytes.Equal(kvA.Key[:1], types.PoolBatchKeyPrefix): - var lpbA, lpbB types.PoolBatch - cdc.MustUnmarshal(kvA.Value, &lpbA) - cdc.MustUnmarshal(kvA.Value, &lpbB) - return fmt.Sprintf("%v\n%v", lpbA, lpbB) + var batchA, batchB types.PoolBatch + cdc.MustUnmarshal(kvA.Value, &batchA) + cdc.MustUnmarshal(kvA.Value, &batchB) + return fmt.Sprintf("%v\n%v", batchA, batchB) case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): - var lpbA, lpbB types.DepositMsgState - lpbA = types.MustUnmarshalDepositMsgState(cdc, kvA.Value) - lpbB = types.MustUnmarshalDepositMsgState(cdc, kvB.Value) - return fmt.Sprintf("%v\n%v", lpbA, lpbB) + var msgA, msgB types.MsgDepositWithinBatch + cdc.MustUnmarshal(kvA.Value, &msgA) + cdc.MustUnmarshal(kvA.Value, &msgB) + return fmt.Sprintf("%v\n%v", msgA, msgB) case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): - var lpbA, lpbB types.WithdrawMsgState - lpbA = types.MustUnmarshalWithdrawMsgState(cdc, kvA.Value) - lpbB = types.MustUnmarshalWithdrawMsgState(cdc, kvB.Value) - return fmt.Sprintf("%v\n%v", lpbA, lpbB) + var msgA, msgB types.MsgWithdrawWithinBatch + cdc.MustUnmarshal(kvA.Value, &msgA) + cdc.MustUnmarshal(kvA.Value, &msgB) + return fmt.Sprintf("%v\n%v", msgA, msgB) case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): - var lpbA, lpbB types.SwapMsgState - lpbA = types.MustUnmarshalSwapMsgState(cdc, kvA.Value) - lpbB = types.MustUnmarshalSwapMsgState(cdc, kvB.Value) - return fmt.Sprintf("%v\n%v", lpbA, lpbB) + var msgA, msgB types.MsgSwapWithinBatch + cdc.MustUnmarshal(kvA.Value, &msgA) + cdc.MustUnmarshal(kvA.Value, &msgB) + return fmt.Sprintf("%v\n%v", msgA, msgB) + + // + // panic: proto: wrong wireType = 2 for field "" [recovered] + // panic: proto: wrong wireType = 2 for field "" + // + // case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): + // var msgStateA, msgStateB types.DepositMsgState + // cdc.MustUnmarshal(kvA.Value, &msgStateA) + // cdc.MustUnmarshal(kvA.Value, &msgStateB) + // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) + + // case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): + // var msgStateA, msgStateB types.WithdrawMsgState + // cdc.MustUnmarshal(kvA.Value, &msgStateA) + // cdc.MustUnmarshal(kvA.Value, &msgStateB) + // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) + + // case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): + // var msgStateA, msgStateB types.SwapMsgState + // cdc.MustUnmarshal(kvA.Value, &msgStateA) + // cdc.MustUnmarshal(kvA.Value, &msgStateB) + // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) default: panic(fmt.Sprintf("invalid liquidity key prefix %X", kvA.Key[:1])) diff --git a/x/liquidity/simulation/decoder_test.go b/x/liquidity/simulation/decoder_test.go index 28e3b2e3a..9cee9976a 100644 --- a/x/liquidity/simulation/decoder_test.go +++ b/x/liquidity/simulation/decoder_test.go @@ -4,9 +4,10 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/types/kv" - "github.com/stretchr/testify/require" "github.com/tendermint/liquidity/x/liquidity/simulation" "github.com/tendermint/liquidity/x/liquidity/types" @@ -16,20 +17,51 @@ func TestDecodeLiquidityStore(t *testing.T) { cdc := simapp.MakeTestEncodingConfig().Marshaler dec := simulation.NewDecodeStore(cdc) - liquidityPool := types.Pool{} - liquidityPool.Id = 1 - liquidityPoolBatch := types.NewPoolBatch(1, 1) - depositMsgState := types.DepositMsgState{} - withdrawMsgState := types.WithdrawMsgState{} - swapMsgState := types.SwapMsgState{} + pool := types.Pool{ + Id: uint64(1), + TypeId: uint32(1), + ReserveCoinDenoms: []string{"dzkiv", "imwo"}, + ReserveAccountAddress: "cosmos1ldxcd2qkjnhhu4avkpt378aupqazex6qh0eg20", + PoolCoinDenom: "poolFB4D86A81694EF7E57ACB0571F1FBC083A2C9B403D6127DFA7B2D9212EA85D72", + } + batch := types.NewPoolBatch(1, 1) + // depositMsgState := types.DepositMsgState{ + // MsgHeight: int64(50), + // MsgIndex: uint64(1), + // Executed: true, + // Succeeded: true, + // ToBeDeleted: true, + // Msg: &types.MsgDepositWithinBatch{PoolId: uint64(1)}, + // } + // withdrawMsgState := types.WithdrawMsgState{ + // MsgHeight: int64(50), + // MsgIndex: uint64(1), + // Executed: true, + // Succeeded: true, + // ToBeDeleted: true, + // Msg: &types.MsgWithdrawWithinBatch{PoolId: uint64(1)}, + // } + // swapMsgState := types.SwapMsgState{ + // MsgHeight: int64(50), + // MsgIndex: uint64(1), + // Executed: true, + // Succeeded: true, + // ToBeDeleted: true, + // Msg: &types.MsgSwapWithinBatch{PoolId: uint64(1)}, + // } + depositBatch := types.MsgDepositWithinBatch{} + withdrawBatch := types.MsgWithdrawWithinBatch{} + swapBatch := types.MsgSwapWithinBatch{} kvPairs := kv.Pairs{ Pairs: []kv.Pair{ - {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&liquidityPool)}, - {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, - {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositMsgState)}, - {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawMsgState)}, - {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapMsgState)}, + {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&pool)}, + {Key: types.PoolByReserveAccIndexKeyPrefix, Value: cdc.MustMarshal(&pool)}, + {Key: types.PoolBatchIndexKeyPrefix, Value: cdc.MustMarshal(&batch)}, + {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&batch)}, + {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositBatch)}, + {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawBatch)}, + {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapBatch)}, {Key: []byte{0x99}, Value: []byte{0x99}}, }, } @@ -38,11 +70,13 @@ func TestDecodeLiquidityStore(t *testing.T) { name string expectedLog string }{ - {"Pool", fmt.Sprintf("%v\n%v", liquidityPool, liquidityPool)}, - {"PoolBatch", fmt.Sprintf("%v\n%v", liquidityPoolBatch, liquidityPoolBatch)}, - {"Deposit PoolBatch", fmt.Sprintf("%v\n%v", depositMsgState, depositMsgState)}, - {"Withdraw PoolBatch", fmt.Sprintf("%v\n%v", withdrawMsgState, withdrawMsgState)}, - {"Swap PoolBatch", fmt.Sprintf("%v\n%v", swapMsgState, swapMsgState)}, + {"Pool", fmt.Sprintf("%v\n%v", pool, pool)}, + {"PoolByReserveAccIndex", fmt.Sprintf("%v\n%v", pool, pool)}, + {"PoolBatchIndex", fmt.Sprintf("%v\n%v", batch, batch)}, + {"PoolBatchKey", fmt.Sprintf("%v\n%v", batch, batch)}, + {"PoolBatchDepositMsgStateIndex PoolBatch", fmt.Sprintf("%v\n%v", depositBatch, depositBatch)}, + {"PoolBatchWithdrawMsgStateIndex", fmt.Sprintf("%v\n%v", withdrawBatch, withdrawBatch)}, + {"PoolBatchSwapMsgStateIndex", fmt.Sprintf("%v\n%v", swapBatch, swapBatch)}, {"other", ""}, } for i, tt := range tests { @@ -57,3 +91,28 @@ func TestDecodeLiquidityStore(t *testing.T) { }) } } + +// func TestDecodeMsgStateArray(t *testing.T) { +// cdc := simapp.MakeTestEncodingConfig().Marshaler + +// state := []types.DepositMsgState{ +// types.DepositMsgState{ +// MsgHeight: int64(50), +// MsgIndex: uint64(1), +// Executed: true, +// Succeeded: true, +// ToBeDeleted: true, +// Msg: &types.MsgDepositWithinBatch{ +// PoolId: uint64(25), +// DepositorAddress: "cosmos1p63k4hmsw3z5frfpxestkp690wc8whcus70dff", +// DepositCoins: sdk.NewCoins( +// sdk.NewInt64Coin("mklp", 96071022), +// sdk.NewInt64Coin("uzme", 78171341), +// ), +// }, +// }, +// } + +// bz, err := cdc.Marshal(state) + +// } From de1cec88477ab36b4b10c76b2be2d8e7e8a1a7ff Mon Sep 17 00:00:00 2001 From: kogisin Date: Sat, 28 Aug 2021 19:11:04 +0900 Subject: [PATCH 08/13] chore: debugging pool batch decoding issue --- app/sim_test.go | 10 +-- x/liquidity/simulation/decoder.go | 60 +++++--------- x/liquidity/simulation/decoder_test.go | 110 ++++++++++--------------- 3 files changed, 70 insertions(+), 110 deletions(-) diff --git a/app/sim_test.go b/app/sim_test.go index 567e7bd4a..fe039b403 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -221,13 +221,13 @@ func TestAppImportExport(t *testing.T) { {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{ liquiditytypes.GlobalLiquidityPoolIDKey, - // liquiditytypes.PoolKeyPrefix, - liquiditytypes.PoolByReserveAccIndexKeyPrefix, liquiditytypes.PoolBatchIndexKeyPrefix, + // liquiditytypes.PoolKeyPrefix, + // liquiditytypes.PoolByReserveAccIndexKeyPrefix, // liquiditytypes.PoolBatchKeyPrefix, - // liquiditytypes.PoolBatchDepositMsgStateIndexKeyPrefix, - // liquiditytypes.PoolBatchWithdrawMsgStateIndexKeyPrefix, - // liquiditytypes.PoolBatchSwapMsgStateIndexKeyPrefix, + liquiditytypes.PoolBatchDepositMsgStateIndexKeyPrefix, + liquiditytypes.PoolBatchWithdrawMsgStateIndexKeyPrefix, + liquiditytypes.PoolBatchSwapMsgStateIndexKeyPrefix, }, }, } diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index 7c139d67f..1a28ee601 100644 --- a/x/liquidity/simulation/decoder.go +++ b/x/liquidity/simulation/decoder.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/tendermint/liquidity/x/liquidity/types" @@ -15,59 +16,38 @@ import ( func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { return func(kvA, kvB kv.Pair) string { switch { - case bytes.Equal(kvA.Key[:1], types.PoolKeyPrefix), - bytes.Equal(kvA.Key[:1], types.PoolByReserveAccIndexKeyPrefix): + case bytes.Equal(kvA.Key[:1], types.PoolKeyPrefix): var poolA, poolB types.Pool cdc.MustUnmarshal(kvA.Value, &poolA) - cdc.MustUnmarshal(kvA.Value, &poolB) + cdc.MustUnmarshal(kvB.Value, &poolB) return fmt.Sprintf("%v\n%v", poolA, poolB) - case bytes.Equal(kvA.Key[:1], types.PoolBatchIndexKeyPrefix), - bytes.Equal(kvA.Key[:1], types.PoolBatchKeyPrefix): + case bytes.Equal(kvA.Key[:1], types.PoolByReserveAccIndexKeyPrefix): + return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value)) + + case bytes.Equal(kvA.Key[:1], types.PoolBatchKeyPrefix): var batchA, batchB types.PoolBatch cdc.MustUnmarshal(kvA.Value, &batchA) - cdc.MustUnmarshal(kvA.Value, &batchB) + cdc.MustUnmarshal(kvB.Value, &batchB) return fmt.Sprintf("%v\n%v", batchA, batchB) case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): - var msgA, msgB types.MsgDepositWithinBatch - cdc.MustUnmarshal(kvA.Value, &msgA) - cdc.MustUnmarshal(kvA.Value, &msgB) - return fmt.Sprintf("%v\n%v", msgA, msgB) + var msgStateA, msgStateB types.DepositMsgState + cdc.MustUnmarshal(kvA.Value, &msgStateA) + cdc.MustUnmarshal(kvB.Value, &msgStateB) + return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): - var msgA, msgB types.MsgWithdrawWithinBatch - cdc.MustUnmarshal(kvA.Value, &msgA) - cdc.MustUnmarshal(kvA.Value, &msgB) - return fmt.Sprintf("%v\n%v", msgA, msgB) + var msgStateA, msgStateB types.WithdrawMsgState + cdc.MustUnmarshal(kvA.Value, &msgStateA) + cdc.MustUnmarshal(kvB.Value, &msgStateB) + return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): - var msgA, msgB types.MsgSwapWithinBatch - cdc.MustUnmarshal(kvA.Value, &msgA) - cdc.MustUnmarshal(kvA.Value, &msgB) - return fmt.Sprintf("%v\n%v", msgA, msgB) - - // - // panic: proto: wrong wireType = 2 for field "" [recovered] - // panic: proto: wrong wireType = 2 for field "" - // - // case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): - // var msgStateA, msgStateB types.DepositMsgState - // cdc.MustUnmarshal(kvA.Value, &msgStateA) - // cdc.MustUnmarshal(kvA.Value, &msgStateB) - // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) - - // case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix): - // var msgStateA, msgStateB types.WithdrawMsgState - // cdc.MustUnmarshal(kvA.Value, &msgStateA) - // cdc.MustUnmarshal(kvA.Value, &msgStateB) - // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) - - // case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix): - // var msgStateA, msgStateB types.SwapMsgState - // cdc.MustUnmarshal(kvA.Value, &msgStateA) - // cdc.MustUnmarshal(kvA.Value, &msgStateB) - // return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) + var msgStateA, msgStateB types.SwapMsgState + cdc.MustUnmarshal(kvA.Value, &msgStateA) + cdc.MustUnmarshal(kvB.Value, &msgStateB) + return fmt.Sprintf("%v\n%v", msgStateA, msgStateB) default: panic(fmt.Sprintf("invalid liquidity key prefix %X", kvA.Key[:1])) diff --git a/x/liquidity/simulation/decoder_test.go b/x/liquidity/simulation/decoder_test.go index 9cee9976a..fa7670e69 100644 --- a/x/liquidity/simulation/decoder_test.go +++ b/x/liquidity/simulation/decoder_test.go @@ -6,13 +6,23 @@ import ( "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/tendermint/liquidity/x/liquidity/simulation" "github.com/tendermint/liquidity/x/liquidity/types" ) +var ( + pk1 = ed25519.GenPrivKey().PubKey() + reserveAccAddr1 = sdk.AccAddress(pk1.Address()) + reserveCoinDenoms = []string{"dzkiv", "imwo"} + poolName = types.PoolName(reserveCoinDenoms, uint32(1)) + poolCoinDenom = types.GetPoolCoinDenom(poolName) +) + func TestDecodeLiquidityStore(t *testing.T) { cdc := simapp.MakeTestEncodingConfig().Marshaler dec := simulation.NewDecodeStore(cdc) @@ -20,48 +30,44 @@ func TestDecodeLiquidityStore(t *testing.T) { pool := types.Pool{ Id: uint64(1), TypeId: uint32(1), - ReserveCoinDenoms: []string{"dzkiv", "imwo"}, - ReserveAccountAddress: "cosmos1ldxcd2qkjnhhu4avkpt378aupqazex6qh0eg20", - PoolCoinDenom: "poolFB4D86A81694EF7E57ACB0571F1FBC083A2C9B403D6127DFA7B2D9212EA85D72", + ReserveCoinDenoms: reserveCoinDenoms, + ReserveAccountAddress: reserveAccAddr1.String(), + PoolCoinDenom: poolCoinDenom, } batch := types.NewPoolBatch(1, 1) - // depositMsgState := types.DepositMsgState{ - // MsgHeight: int64(50), - // MsgIndex: uint64(1), - // Executed: true, - // Succeeded: true, - // ToBeDeleted: true, - // Msg: &types.MsgDepositWithinBatch{PoolId: uint64(1)}, - // } - // withdrawMsgState := types.WithdrawMsgState{ - // MsgHeight: int64(50), - // MsgIndex: uint64(1), - // Executed: true, - // Succeeded: true, - // ToBeDeleted: true, - // Msg: &types.MsgWithdrawWithinBatch{PoolId: uint64(1)}, - // } - // swapMsgState := types.SwapMsgState{ - // MsgHeight: int64(50), - // MsgIndex: uint64(1), - // Executed: true, - // Succeeded: true, - // ToBeDeleted: true, - // Msg: &types.MsgSwapWithinBatch{PoolId: uint64(1)}, - // } - depositBatch := types.MsgDepositWithinBatch{} - withdrawBatch := types.MsgWithdrawWithinBatch{} - swapBatch := types.MsgSwapWithinBatch{} + depositMsgState := types.DepositMsgState{ + MsgHeight: int64(50), + MsgIndex: uint64(1), + Executed: true, + Succeeded: true, + ToBeDeleted: true, + Msg: &types.MsgDepositWithinBatch{PoolId: uint64(1)}, + } + withdrawMsgState := types.WithdrawMsgState{ + MsgHeight: int64(50), + MsgIndex: uint64(1), + Executed: true, + Succeeded: true, + ToBeDeleted: true, + Msg: &types.MsgWithdrawWithinBatch{PoolId: uint64(1)}, + } + swapMsgState := types.SwapMsgState{ + MsgHeight: int64(50), + MsgIndex: uint64(1), + Executed: true, + Succeeded: true, + ToBeDeleted: true, + Msg: &types.MsgSwapWithinBatch{PoolId: uint64(1)}, + } kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&pool)}, - {Key: types.PoolByReserveAccIndexKeyPrefix, Value: cdc.MustMarshal(&pool)}, - {Key: types.PoolBatchIndexKeyPrefix, Value: cdc.MustMarshal(&batch)}, + {Key: types.PoolByReserveAccIndexKeyPrefix, Value: reserveAccAddr1.Bytes()}, {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&batch)}, - {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositBatch)}, - {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawBatch)}, - {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapBatch)}, + {Key: types.PoolBatchDepositMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&depositMsgState)}, + {Key: types.PoolBatchWithdrawMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&withdrawMsgState)}, + {Key: types.PoolBatchSwapMsgStateIndexKeyPrefix, Value: cdc.MustMarshal(&swapMsgState)}, {Key: []byte{0x99}, Value: []byte{0x99}}, }, } @@ -71,12 +77,11 @@ func TestDecodeLiquidityStore(t *testing.T) { expectedLog string }{ {"Pool", fmt.Sprintf("%v\n%v", pool, pool)}, - {"PoolByReserveAccIndex", fmt.Sprintf("%v\n%v", pool, pool)}, - {"PoolBatchIndex", fmt.Sprintf("%v\n%v", batch, batch)}, + {"PoolByReserveAccIndex", fmt.Sprintf("%v\n%v", reserveAccAddr1, reserveAccAddr1)}, {"PoolBatchKey", fmt.Sprintf("%v\n%v", batch, batch)}, - {"PoolBatchDepositMsgStateIndex PoolBatch", fmt.Sprintf("%v\n%v", depositBatch, depositBatch)}, - {"PoolBatchWithdrawMsgStateIndex", fmt.Sprintf("%v\n%v", withdrawBatch, withdrawBatch)}, - {"PoolBatchSwapMsgStateIndex", fmt.Sprintf("%v\n%v", swapBatch, swapBatch)}, + {"PoolBatchDepositMsgStateIndex", fmt.Sprintf("%v\n%v", depositMsgState, depositMsgState)}, + {"PoolBatchWithdrawMsgStateIndex", fmt.Sprintf("%v\n%v", withdrawMsgState, withdrawMsgState)}, + {"PoolBatchSwapMsgStateIndex", fmt.Sprintf("%v\n%v", swapMsgState, swapMsgState)}, {"other", ""}, } for i, tt := range tests { @@ -91,28 +96,3 @@ func TestDecodeLiquidityStore(t *testing.T) { }) } } - -// func TestDecodeMsgStateArray(t *testing.T) { -// cdc := simapp.MakeTestEncodingConfig().Marshaler - -// state := []types.DepositMsgState{ -// types.DepositMsgState{ -// MsgHeight: int64(50), -// MsgIndex: uint64(1), -// Executed: true, -// Succeeded: true, -// ToBeDeleted: true, -// Msg: &types.MsgDepositWithinBatch{ -// PoolId: uint64(25), -// DepositorAddress: "cosmos1p63k4hmsw3z5frfpxestkp690wc8whcus70dff", -// DepositCoins: sdk.NewCoins( -// sdk.NewInt64Coin("mklp", 96071022), -// sdk.NewInt64Coin("uzme", 78171341), -// ), -// }, -// }, -// } - -// bz, err := cdc.Marshal(state) - -// } From f364dae1e9118def05997d33604674be5aacf858 Mon Sep 17 00:00:00 2001 From: dongsam Date: Mon, 30 Aug 2021 22:08:23 +0900 Subject: [PATCH 09/13] update: deprecate PoolBatchIndexKey and fix genesis logic --- .github/workflows/test.yml | 30 ++- Makefile | 2 +- app/app_test.go | 268 +++++++++++++++++++++ app/export.go | 2 +- app/genesis.go | 7 +- app/sim_test.go | 28 +-- app/test_helpers.go | 2 +- go.mod | 1 + x/liquidity/client/cli/cli_test.go | 4 +- x/liquidity/client/testutil/cli_helpers.go | 8 +- x/liquidity/genesis_test.go | 5 +- x/liquidity/keeper/batch.go | 3 +- x/liquidity/keeper/batch_test.go | 15 +- x/liquidity/keeper/genesis_test.go | 18 ++ x/liquidity/keeper/liquidity_pool.go | 3 +- x/liquidity/keeper/store.go | 27 --- x/liquidity/legacy/v042/keys.go | 10 + x/liquidity/legacy/v043/helpers.go | 12 + x/liquidity/legacy/v043/store.go | 3 + x/liquidity/legacy/v043/store_test.go | 14 +- x/liquidity/spec/02_state.md | 2 - x/liquidity/types/keys.go | 11 +- x/liquidity/types/keys_test.go | 4 - x/liquidity/types/liquidity_pool_test.go | 1 - 24 files changed, 388 insertions(+), 92 deletions(-) create mode 100644 app/app_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5627ac865..03f0fddfc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -90,15 +90,41 @@ jobs: with: file: ./coverage.txt - test-simulation: + test-sim-nondeterminism: runs-on: ubuntu-latest timeout-minutes: 15 steps: - uses: actions/checkout@v2 - uses: actions/setup-go@v2.1.3 with: - go-version: 1.15 + go-version: 1.16 - name: Display go version run: go version - name: Testing simulation run: make test-sim-nondeterminism + + test-sim-after-import: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2.1.3 + with: + go-version: 1.16 + - name: Display go version + run: go version + - name: Testing simulation + run: make test-sim-after-import + + test-sim-import-export: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2.1.3 + with: + go-version: 1.16 + - name: Display go version + run: go version + - name: Testing simulation + run: make test-sim-import-export diff --git a/Makefile b/Makefile index a4797f3fc..34ef73213 100644 --- a/Makefile +++ b/Makefile @@ -161,7 +161,7 @@ test-sim-nondeterminism: test-sim-import-export: runsim @echo "Running application import/export simulation. This may take several minutes..." - @$(BINDIR)/runsim -Jobs=1 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport test-sim-after-import: runsim @echo "Running application simulation-after-import. This may take several minutes..." diff --git a/app/app_test.go b/app/app_test.go new file mode 100644 index 000000000..b9d1ad85a --- /dev/null +++ b/app/app_test.go @@ -0,0 +1,268 @@ +package app + +import ( + "encoding/json" + "os" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/tests/mocks" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/vesting" + authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" + "github.com/cosmos/cosmos-sdk/x/bank" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/cosmos-sdk/x/capability" + "github.com/cosmos/cosmos-sdk/x/crisis" + "github.com/cosmos/cosmos-sdk/x/distribution" + "github.com/cosmos/cosmos-sdk/x/evidence" + feegrantmodule "github.com/cosmos/cosmos-sdk/x/feegrant/module" + "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/gov" + "github.com/cosmos/cosmos-sdk/x/mint" + "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/slashing" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/upgrade" + + "github.com/tendermint/liquidity/x/liquidity" +) + +func TestSimAppExportAndBlockedAddrs(t *testing.T) { + encCfg := MakeEncodingConfig() + db := dbm.NewMemDB() + app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + + for acc := range maccPerms { + require.True( + t, + app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)), + "ensure that blocked addresses are properly set in bank keeper", + ) + } + + genesisState := NewDefaultGenesisState(encCfg.Marshaler) + stateBytes, err := json.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + // Initialize the chain + app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + AppStateBytes: stateBytes, + }, + ) + app.Commit() + + // Making a new app object with the db, so that initchain hasn't been called + app2 := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + _, err = app2.ExportAppStateAndValidators(false, []string{}) + require.NoError(t, err, "ExportAppStateAndValidators should not have an error") +} + +func TestGetMaccPerms(t *testing.T) { + dup := GetMaccPerms() + require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions") +} + +func TestRunMigrations(t *testing.T) { + db := dbm.NewMemDB() + encCfg := MakeEncodingConfig() + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + + // Create a new baseapp and configurator for the purpose of this test. + bApp := baseapp.NewBaseApp(appName, logger, db, encCfg.TxConfig.TxDecoder()) + bApp.SetCommitMultiStoreTracer(nil) + bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry) + app.BaseApp = bApp + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) + + // We register all modules on the Configurator, except x/bank. x/bank will + // serve as the test subject on which we run the migration tests. + // + // The loop below is the same as calling `RegisterServices` on + // ModuleManager, except that we skip x/bank. + for _, module := range app.mm.Modules { + if module.Name() == banktypes.ModuleName { + continue + } + + module.RegisterServices(app.configurator) + } + + // Initialize the chain + app.InitChain(abci.RequestInitChain{}) + app.Commit() + + testCases := []struct { + name string + moduleName string + forVersion uint64 + expRegErr bool // errors while registering migration + expRegErrMsg string + expRunErr bool // errors while running migration + expRunErrMsg string + expCalled int + }{ + { + "cannot register migration for version 0", + "bank", 0, + true, "module migration versions should start at 1: invalid version", false, "", 0, + }, + { + "throws error on RunMigrations if no migration registered for bank", + "", 1, + false, "", true, "no migrations found for module bank: not found", 0, + }, + { + "can register and run migration handler for x/bank", + "bank", 1, + false, "", false, "", 1, + }, + { + "cannot register migration handler for same module & forVersion", + "bank", 1, + true, "another migration for module bank and version 1 already exists: internal logic error", false, "", 0, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + var err error + + // Since it's very hard to test actual in-place store migrations in + // tests (due to the difficulty of maintaining multiple versions of a + // module), we're just testing here that the migration logic is + // called. + called := 0 + + if tc.moduleName != "" { + // Register migration for module from version `forVersion` to `forVersion+1`. + err = app.configurator.RegisterMigration(tc.moduleName, tc.forVersion, func(sdk.Context) error { + called++ + + return nil + }) + + if tc.expRegErr { + require.EqualError(t, err, tc.expRegErrMsg) + + return + } + } + require.NoError(t, err) + + // Run migrations only for bank. That's why we put the initial + // version for bank as 1, and for all other modules, we put as + // their latest ConsensusVersion. + _, err = app.mm.RunMigrations( + app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}), app.configurator, + module.VersionMap{ + "bank": 1, + "auth": auth.AppModule{}.ConsensusVersion(), + "authz": authzmodule.AppModule{}.ConsensusVersion(), + "staking": staking.AppModule{}.ConsensusVersion(), + "mint": mint.AppModule{}.ConsensusVersion(), + "distribution": distribution.AppModule{}.ConsensusVersion(), + "slashing": slashing.AppModule{}.ConsensusVersion(), + "gov": gov.AppModule{}.ConsensusVersion(), + "params": params.AppModule{}.ConsensusVersion(), + "upgrade": upgrade.AppModule{}.ConsensusVersion(), + "vesting": vesting.AppModule{}.ConsensusVersion(), + "feegrant": feegrantmodule.AppModule{}.ConsensusVersion(), + "evidence": evidence.AppModule{}.ConsensusVersion(), + "crisis": crisis.AppModule{}.ConsensusVersion(), + "genutil": genutil.AppModule{}.ConsensusVersion(), + "capability": capability.AppModule{}.ConsensusVersion(), + "liquidity": liquidity.AppModule{}.ConsensusVersion(), + }, + ) + if tc.expRunErr { + require.EqualError(t, err, tc.expRunErrMsg) + } else { + require.NoError(t, err) + // Make sure bank's migration is called. + require.Equal(t, tc.expCalled, called) + } + }) + } +} + +func TestInitGenesisOnMigration(t *testing.T) { + db := dbm.NewMemDB() + encCfg := MakeEncodingConfig() + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) + + // Create a mock module. This module will serve as the new module we're + // adding during a migration. + mockCtrl := gomock.NewController(t) + t.Cleanup(mockCtrl.Finish) + mockModule := mocks.NewMockAppModule(mockCtrl) + mockDefaultGenesis := json.RawMessage(`{"key": "value"}`) + mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis) + mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil) + mockModule.EXPECT().ConsensusVersion().Times(1).Return(uint64(0)) + + app.mm.Modules["mock"] = mockModule + + // Run migrations only for "mock" module. We exclude it from + // the VersionMap to simulate upgrading with a new module. + _, err := app.mm.RunMigrations(ctx, app.configurator, + module.VersionMap{ + "bank": bank.AppModule{}.ConsensusVersion(), + "auth": auth.AppModule{}.ConsensusVersion(), + "authz": authzmodule.AppModule{}.ConsensusVersion(), + "staking": staking.AppModule{}.ConsensusVersion(), + "mint": mint.AppModule{}.ConsensusVersion(), + "distribution": distribution.AppModule{}.ConsensusVersion(), + "slashing": slashing.AppModule{}.ConsensusVersion(), + "gov": gov.AppModule{}.ConsensusVersion(), + "params": params.AppModule{}.ConsensusVersion(), + "upgrade": upgrade.AppModule{}.ConsensusVersion(), + "vesting": vesting.AppModule{}.ConsensusVersion(), + "feegrant": feegrantmodule.AppModule{}.ConsensusVersion(), + "evidence": evidence.AppModule{}.ConsensusVersion(), + "crisis": crisis.AppModule{}.ConsensusVersion(), + "genutil": genutil.AppModule{}.ConsensusVersion(), + "capability": capability.AppModule{}.ConsensusVersion(), + "liquidity": liquidity.AppModule{}.ConsensusVersion(), + }, + ) + require.NoError(t, err) +} + +func TestUpgradeStateOnGenesis(t *testing.T) { + encCfg := MakeEncodingConfig() + db := dbm.NewMemDB() + app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) + genesisState := NewDefaultGenesisState(encCfg.Marshaler) + stateBytes, err := json.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + // Initialize the chain + app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + AppStateBytes: stateBytes, + }, + ) + + // make sure the upgrade keeper has version map in state + ctx := app.NewContext(false, tmproto.Header{}) + vm := app.UpgradeKeeper.GetModuleVersionMap(ctx) + for v, i := range app.mm.Modules { + require.Equal(t, vm[v], i.ConsensusVersion()) + } +} diff --git a/app/export.go b/app/export.go index 0ca7cbae0..2f4943edb 100644 --- a/app/export.go +++ b/app/export.go @@ -145,7 +145,7 @@ func (app *LiquidityApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAd counter := int16(0) for ; iter.Valid(); iter.Next() { - addr := sdk.ValAddress(iter.Key()[1:]) + addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key())) validator, found := app.StakingKeeper.GetValidator(ctx, addr) if !found { panic("expected validator, not found") diff --git a/app/genesis.go b/app/genesis.go index b134ffa29..b5feb14cb 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -4,6 +4,8 @@ package app import ( "encoding/json" + + "github.com/cosmos/cosmos-sdk/codec" ) // The genesis state of the blockchain is represented here as a map of raw json @@ -16,7 +18,6 @@ import ( type GenesisState map[string]json.RawMessage // NewDefaultGenesisState generates the default state for the application. -func NewDefaultGenesisState() GenesisState { - encCfg := MakeEncodingConfig() - return ModuleBasics.DefaultGenesis(encCfg.Marshaler) +func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState { + return ModuleBasics.DefaultGenesis(cdc) } diff --git a/app/sim_test.go b/app/sim_test.go index fe039b403..4cc61070c 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -22,10 +22,11 @@ import ( capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" + "github.com/cosmos/cosmos-sdk/x/feegrant" "github.com/cosmos/cosmos-sdk/x/genutil" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/cosmos/cosmos-sdk/x/simulation" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -205,31 +206,20 @@ func TestAppImportExport(t *testing.T) { {app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}}, {app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey], [][]byte{ - stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, - stakingtypes.ValidatorQueueKey, stakingtypes.HistoricalInfoKey, - }, - }, // ordering may change but it doesn't matter + stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey, + stakingtypes.HistoricalInfoKey, + }}, // ordering may change but it doesn't matter {app.keys[slashingtypes.StoreKey], newApp.keys[slashingtypes.StoreKey], [][]byte{}}, {app.keys[minttypes.StoreKey], newApp.keys[minttypes.StoreKey], [][]byte{}}, {app.keys[distrtypes.StoreKey], newApp.keys[distrtypes.StoreKey], [][]byte{}}, {app.keys[banktypes.StoreKey], newApp.keys[banktypes.StoreKey], [][]byte{banktypes.BalancesPrefix}}, - {app.keys[paramstypes.StoreKey], newApp.keys[paramstypes.StoreKey], [][]byte{}}, + {app.keys[paramtypes.StoreKey], newApp.keys[paramtypes.StoreKey], [][]byte{}}, {app.keys[govtypes.StoreKey], newApp.keys[govtypes.StoreKey], [][]byte{}}, {app.keys[evidencetypes.StoreKey], newApp.keys[evidencetypes.StoreKey], [][]byte{}}, {app.keys[capabilitytypes.StoreKey], newApp.keys[capabilitytypes.StoreKey], [][]byte{}}, {app.keys[authzkeeper.StoreKey], newApp.keys[authzkeeper.StoreKey], [][]byte{}}, - {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], - [][]byte{ - liquiditytypes.GlobalLiquidityPoolIDKey, - liquiditytypes.PoolBatchIndexKeyPrefix, - // liquiditytypes.PoolKeyPrefix, - // liquiditytypes.PoolByReserveAccIndexKeyPrefix, - // liquiditytypes.PoolBatchKeyPrefix, - liquiditytypes.PoolBatchDepositMsgStateIndexKeyPrefix, - liquiditytypes.PoolBatchWithdrawMsgStateIndexKeyPrefix, - liquiditytypes.PoolBatchSwapMsgStateIndexKeyPrefix, - }, - }, + {app.keys[feegrant.StoreKey], newApp.keys[feegrant.StoreKey], [][]byte{}}, + {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{}}, } for _, skp := range storeKeysPrefixes { @@ -302,7 +292,7 @@ func TestAppSimulationAfterImport(t *testing.T) { require.NoError(t, os.RemoveAll(newDir)) }() - newApp := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt) + newApp := NewLiquidityApp(logger, newDB, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue, MakeEncodingConfig(), EmptyAppOptions{}, fauxMerkleModeOpt) require.Equal(t, appName, app.Name()) newApp.InitChain(abci.RequestInitChain{ diff --git a/app/test_helpers.go b/app/test_helpers.go index 00d09c099..b425f107e 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -51,7 +51,7 @@ func setup(withGenesis bool, invCheckPeriod uint) (*LiquidityApp, GenesisState) encCdc := MakeEncodingConfig() app := NewLiquidityApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, EmptyAppOptions{}) if withGenesis { - return app, NewDefaultGenesisState() + return app, NewDefaultGenesisState(encCdc.Marshaler) } return app, GenesisState{} } diff --git a/go.mod b/go.mod index 57e8bfd39..dccd0a99d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ module github.com/tendermint/liquidity require ( github.com/cosmos/cosmos-sdk v0.43.0 github.com/gogo/protobuf v1.3.3 + github.com/golang/mock v1.6.0 github.com/golang/protobuf v1.5.2 github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 diff --git a/x/liquidity/client/cli/cli_test.go b/x/liquidity/client/cli/cli_test.go index bfd9db709..aa3e64bbb 100644 --- a/x/liquidity/client/cli/cli_test.go +++ b/x/liquidity/client/cli/cli_test.go @@ -55,9 +55,7 @@ type IntegrationTestSuite struct { func (s *IntegrationTestSuite) SetupTest() { s.T().Log("setting up integration test suite") - db := tmdb.NewMemDB() - - cfg := liquiditytestutil.NewConfig(db) + cfg := liquiditytestutil.NewConfig() cfg.NumValidators = 1 var liquidityGenState liquiditytypes.GenesisState diff --git a/x/liquidity/client/testutil/cli_helpers.go b/x/liquidity/client/testutil/cli_helpers.go index 0fe469b24..8ff7b2b1c 100644 --- a/x/liquidity/client/testutil/cli_helpers.go +++ b/x/liquidity/client/testutil/cli_helpers.go @@ -23,20 +23,20 @@ import ( // NewConfig returns config that defines the necessary testing requirements // used to bootstrap and start an in-process local testing network. -func NewConfig(dbm *dbm.MemDB) network.Config { +func NewConfig() network.Config { encCfg := simapp.MakeTestEncodingConfig() cfg := network.DefaultConfig() - cfg.AppConstructor = NewAppConstructor(encCfg, dbm) // the ABCI application constructor + cfg.AppConstructor = NewAppConstructor(encCfg) // the ABCI application constructor cfg.GenesisState = liquidityapp.ModuleBasics.DefaultGenesis(cfg.Codec) // liquidity genesis state to provide return cfg } // NewAppConstructor returns a new network AppConstructor. -func NewAppConstructor(encodingCfg params.EncodingConfig, db *dbm.MemDB) network.AppConstructor { +func NewAppConstructor(encodingCfg params.EncodingConfig) network.AppConstructor { return func(val network.Validator) servertypes.Application { return liquidityapp.NewLiquidityApp( - val.Ctx.Logger, db, nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0, + val.Ctx.Logger, dbm.NewMemDB(), nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0, liquidityapp.MakeEncodingConfig(), simapp.EmptyAppOptions{}, baseapp.SetPruning(storetypes.NewPruningOptionsFromString(val.AppConfig.Pruning)), diff --git a/x/liquidity/genesis_test.go b/x/liquidity/genesis_test.go index 066c14ded..c5c695ab9 100644 --- a/x/liquidity/genesis_test.go +++ b/x/liquidity/genesis_test.go @@ -50,8 +50,11 @@ func TestGenesisState(t *testing.T) { orderAddrs := addrs[1:2] _, _ = app.TestSwapPool(t, simapp, ctx, offerCoins, orderPrices, orderAddrs, poolID, false) _, _ = app.TestSwapPool(t, simapp, ctx, offerCoins, orderPrices, orderAddrs, poolID, false) - _, _ = app.TestSwapPool(t, simapp, ctx, offerCoins, orderPrices, orderAddrs, poolID, false) _, _ = app.TestSwapPool(t, simapp, ctx, offerCoins, orderPrices, orderAddrs, poolID, true) + liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper) + _, _ = app.TestSwapPool(t, simapp, ctx, offerCoins, orderPrices, orderAddrs, poolID, true) + liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper) + liquidity.EndBlocker(ctx, simapp.LiquidityKeeper) genesisExported := liquidity.ExportGenesis(ctx, simapp.LiquidityKeeper) bankGenesisExported := simapp.BankKeeper.ExportGenesis(ctx) diff --git a/x/liquidity/keeper/batch.go b/x/liquidity/keeper/batch.go index e8f8724c0..948382523 100644 --- a/x/liquidity/keeper/batch.go +++ b/x/liquidity/keeper/batch.go @@ -69,12 +69,11 @@ func (k Keeper) InitNextPoolBatch(ctx sdk.Context, poolBatch types.PoolBatch) er return types.ErrBatchNotExecuted } - poolBatch.Index = k.GetNextPoolBatchIndexWithUpdate(ctx, poolBatch.PoolId) + poolBatch.Index++ poolBatch.BeginHeight = ctx.BlockHeight() poolBatch.Executed = false k.SetPoolBatch(ctx, poolBatch) - return nil } diff --git a/x/liquidity/keeper/batch_test.go b/x/liquidity/keeper/batch_test.go index fba337f1b..9fc754049 100644 --- a/x/liquidity/keeper/batch_test.go +++ b/x/liquidity/keeper/batch_test.go @@ -317,7 +317,7 @@ func TestCreateDepositWithdrawWithinBatch(t *testing.T) { batch, found = simapp.LiquidityKeeper.GetPoolBatch(ctx, batch.PoolId) require.True(t, found) - require.Equal(t, uint64(1), batch.Index) + require.Equal(t, uint64(2), batch.Index) // withdraw withdrawerBalanceX := simapp.BankKeeper.GetBalance(ctx, addrs[1], pools[0].ReserveCoinDenoms[0]) @@ -362,7 +362,7 @@ func TestCreateDepositWithdrawWithinBatch(t *testing.T) { require.True(t, withdrawMsgs[0].Executed) require.True(t, withdrawMsgs[0].Succeeded) require.True(t, withdrawMsgs[0].ToBeDeleted) - require.Equal(t, uint64(1), batch.Index) + require.Equal(t, uint64(2), batch.Index) // next block ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) @@ -376,7 +376,7 @@ func TestCreateDepositWithdrawWithinBatch(t *testing.T) { batch, found = simapp.LiquidityKeeper.GetPoolBatch(ctx, batch.PoolId) require.True(t, found) - require.Equal(t, uint64(2), batch.Index) + require.Equal(t, uint64(3), batch.Index) require.False(t, batch.Executed) } @@ -494,7 +494,7 @@ func TestCreateDepositWithdrawWithinBatch2(t *testing.T) { batch, found = simapp.LiquidityKeeper.GetPoolBatch(ctx, batch.PoolId) require.True(t, found) - require.Equal(t, uint64(1), batch.Index) + require.Equal(t, uint64(2), batch.Index) // withdraw withdrawerBalanceX := simapp.BankKeeper.GetBalance(ctx, addrs[1], pools[0].ReserveCoinDenoms[0]) @@ -539,7 +539,7 @@ func TestCreateDepositWithdrawWithinBatch2(t *testing.T) { require.True(t, withdrawMsgs[0].Executed) require.True(t, withdrawMsgs[0].Succeeded) require.True(t, withdrawMsgs[0].ToBeDeleted) - require.Equal(t, uint64(1), batch.Index) + require.Equal(t, uint64(2), batch.Index) // next block ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) @@ -553,7 +553,7 @@ func TestCreateDepositWithdrawWithinBatch2(t *testing.T) { batch, found = simapp.LiquidityKeeper.GetPoolBatch(ctx, batch.PoolId) require.True(t, found) - require.Equal(t, uint64(2), batch.Index) + require.Equal(t, uint64(3), batch.Index) require.False(t, batch.Executed) } @@ -607,7 +607,7 @@ func TestLiquidityScenario(t *testing.T) { batch, found = simapp.LiquidityKeeper.GetPoolBatch(ctx, batch.PoolId) require.True(t, found) - require.Equal(t, uint64(2), batch.Index) + require.Equal(t, uint64(3), batch.Index) require.False(t, batch.Executed) } @@ -1040,7 +1040,6 @@ func TestInitNextBatch(t *testing.T) { batch := types.NewPoolBatch(pool.Id, 1) simapp.LiquidityKeeper.SetPoolBatch(ctx, batch) - simapp.LiquidityKeeper.SetPoolBatchIndex(ctx, batch.PoolId, batch.Index) err := simapp.LiquidityKeeper.InitNextPoolBatch(ctx, batch) require.ErrorIs(t, err, types.ErrBatchNotExecuted) diff --git a/x/liquidity/keeper/genesis_test.go b/x/liquidity/keeper/genesis_test.go index db49eaa2e..43731052d 100644 --- a/x/liquidity/keeper/genesis_test.go +++ b/x/liquidity/keeper/genesis_test.go @@ -117,4 +117,22 @@ func TestGenesisState(t *testing.T) { app.SaveAccount(simapp2, ctx2, addrs[1], sdk.Coins{poolCoinBalance}) simapp2.LiquidityKeeper.InitGenesis(ctx2, *newGenesis) }) + + simapp3 := app.Setup(false) + ctx3 := simapp3.BaseApp.NewContext(false, tmproto.Header{}).WithBlockHeight(ctx.BlockHeight()) + require.Panics(t, func() { + simapp3.LiquidityKeeper.InitGenesis(ctx3, *newGenesis) + }) + require.Panics(t, func() { + app.SaveAccount(simapp3, ctx, pool.GetReserveAccount(), reserveCoins) + simapp3.LiquidityKeeper.InitGenesis(ctx3, *newGenesis) + }) + require.Panics(t, func() { + app.SaveAccount(simapp3, ctx, addrs[0], sdk.Coins{poolCoinBalanceCreator}) + simapp3.LiquidityKeeper.InitGenesis(ctx3, *newGenesis) + }) + require.Panics(t, func() { + app.SaveAccount(simapp3, ctx3, addrs[1], sdk.Coins{poolCoinBalance}) + simapp3.LiquidityKeeper.InitGenesis(ctx3, *newGenesis) + }) } diff --git a/x/liquidity/keeper/liquidity_pool.go b/x/liquidity/keeper/liquidity_pool.go index eea801870..52a9e1205 100644 --- a/x/liquidity/keeper/liquidity_pool.go +++ b/x/liquidity/keeper/liquidity_pool.go @@ -552,9 +552,8 @@ func (k Keeper) GetPoolRecord(ctx sdk.Context, pool types.Pool) (types.PoolRecor // SetPoolRecord stores liquidity pool states func (k Keeper) SetPoolRecord(ctx sdk.Context, record types.PoolRecord) types.PoolRecord { k.SetPoolAtomic(ctx, record.Pool) - k.GetNextPoolBatchIndexWithUpdate(ctx, record.Pool.Id) if record.PoolBatch.BeginHeight > ctx.BlockHeight() { - record.PoolBatch.BeginHeight = ctx.BlockHeight() + record.PoolBatch.BeginHeight = 0 } k.SetPoolBatch(ctx, record.PoolBatch) k.SetPoolBatchDepositMsgStates(ctx, record.Pool.Id, record.DepositMsgStates) diff --git a/x/liquidity/keeper/store.go b/x/liquidity/keeper/store.go index 643c58977..592e0d140 100644 --- a/x/liquidity/keeper/store.go +++ b/x/liquidity/keeper/store.go @@ -127,25 +127,6 @@ func (k Keeper) SetPoolAtomic(ctx sdk.Context, pool types.Pool) types.Pool { return pool } -// GetPoolBatchIndex returns the pool's latest batch index -func (k Keeper) GetPoolBatchIndex(ctx sdk.Context, poolID uint64) uint64 { - store := ctx.KVStore(k.storeKey) - key := types.GetPoolBatchIndexKey(poolID) - - bz := store.Get(key) - if bz == nil { - return 0 - } - return sdk.BigEndianToUint64(bz) -} - -// SetPoolBatchIndex sets index for pool batch, it should be increase after batch executed -func (k Keeper) SetPoolBatchIndex(ctx sdk.Context, poolID, batchIndex uint64) { - store := ctx.KVStore(k.storeKey) - b := sdk.Uint64ToBigEndian(batchIndex) - store.Set(types.GetPoolBatchIndexKey(poolID), b) -} - // GetPoolBatch returns a specific pool batch func (k Keeper) GetPoolBatch(ctx sdk.Context, poolID uint64) (poolBatch types.PoolBatch, found bool) { store := ctx.KVStore(k.storeKey) @@ -161,14 +142,6 @@ func (k Keeper) GetPoolBatch(ctx sdk.Context, poolID uint64) (poolBatch types.Po return poolBatch, true } -// GetNextPoolBatchIndexWithUpdate returns next batch index, with set index increased -func (k Keeper) GetNextPoolBatchIndexWithUpdate(ctx sdk.Context, poolID uint64) (batchIndex uint64) { - batchIndex = k.GetPoolBatchIndex(ctx, poolID) - batchIndex++ - k.SetPoolBatchIndex(ctx, poolID, batchIndex) - return -} - // GetAllPoolBatches returns all batches of the all existed liquidity pools func (k Keeper) GetAllPoolBatches(ctx sdk.Context) (poolBatches []types.PoolBatch) { k.IterateAllPoolBatches(ctx, func(poolBatch types.PoolBatch) bool { diff --git a/x/liquidity/legacy/v042/keys.go b/x/liquidity/legacy/v042/keys.go index e0858da0f..0e7286c48 100644 --- a/x/liquidity/legacy/v042/keys.go +++ b/x/liquidity/legacy/v042/keys.go @@ -13,6 +13,8 @@ const ( var ( PoolByReserveAccIndexKeyPrefix = []byte{0x12} + + PoolBatchIndexKeyPrefix = []byte{0x21} // Last PoolBatchIndex ) // - PoolByReserveAccIndex: `0x12 | ReserveAcc -> Id` @@ -20,3 +22,11 @@ var ( func GetPoolByReserveAccIndexKey(reserveAcc sdk.AccAddress) []byte { return append(PoolByReserveAccIndexKeyPrefix, reserveAcc.Bytes()...) } + +// GetPoolBatchIndexKey returns kv indexing key of the latest index value of the pool batch +func GetPoolBatchIndexKey(poolID uint64) []byte { + key := make([]byte, 9) + key[0] = PoolBatchIndexKeyPrefix[0] + copy(key[1:9], sdk.Uint64ToBigEndian(poolID)) + return key +} diff --git a/x/liquidity/legacy/v043/helpers.go b/x/liquidity/legacy/v043/helpers.go index 30694bb9b..f9b49461c 100644 --- a/x/liquidity/legacy/v043/helpers.go +++ b/x/liquidity/legacy/v043/helpers.go @@ -22,3 +22,15 @@ func MigratePrefixAddress(store sdk.KVStore, prefixBz []byte) { oldStore.Delete(oldStoreIter.Key()) } } + +// DeleteDeprecatedPrefix is a helper function that deletes all keys which started the prefix +func DeleteDeprecatedPrefix(store sdk.KVStore, prefixBz []byte) { + oldStore := prefix.NewStore(store, prefixBz) + + oldStoreIter := oldStore.Iterator(nil, nil) + defer oldStoreIter.Close() + + for ; oldStoreIter.Valid(); oldStoreIter.Next() { + oldStore.Delete(oldStoreIter.Key()) + } +} diff --git a/x/liquidity/legacy/v043/store.go b/x/liquidity/legacy/v043/store.go index c61348815..2f11ae010 100644 --- a/x/liquidity/legacy/v043/store.go +++ b/x/liquidity/legacy/v043/store.go @@ -15,8 +15,11 @@ func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey) error { // old key format v042: // PoolByReserveAccIndex: `0x12 | ReserveAcc -> ProtocolBuffer(uint64)` + // PoolBatchIndex: `0x21 | PoolId -> ProtocolBuffer(uint64)` // new key format v043: // PoolByReserveAccIndex: `0x12 | ReserveAccLen (1 byte) | ReserveAcc -> ProtocolBuffer(uint64)` + // PoolBatchIndex: deprecated MigratePrefixAddress(store, v042liquidity.PoolByReserveAccIndexKeyPrefix) + DeleteDeprecatedPrefix(store, v042liquidity.PoolBatchIndexKeyPrefix) return nil } diff --git a/x/liquidity/legacy/v043/store_test.go b/x/liquidity/legacy/v043/store_test.go index 083c0d224..447bb77d2 100644 --- a/x/liquidity/legacy/v043/store_test.go +++ b/x/liquidity/legacy/v043/store_test.go @@ -45,6 +45,16 @@ func TestStoreMigration(t *testing.T) { v042liquidity.GetPoolByReserveAccIndexKey(reserveAcc2), types.GetPoolByReserveAccIndexKey(reserveAcc2), }, + { + "poolBatchIndexKeyPrefix1", + v042liquidity.GetPoolBatchIndexKey(1), + nil, + }, + { + "poolBatchIndexKeyPrefix2", + v042liquidity.GetPoolBatchIndexKey(2), + nil, + }, } // Set all the old keys to the store @@ -63,7 +73,9 @@ func TestStoreMigration(t *testing.T) { if !bytes.Equal(tc.oldKey, tc.newKey) { require.Nil(t, store.Get(tc.oldKey)) } - require.Equal(t, value, store.Get(tc.newKey)) + if tc.newKey != nil { + require.Equal(t, value, store.Get(tc.newKey)) + } }) } } diff --git a/x/liquidity/spec/02_state.md b/x/liquidity/spec/02_state.md index e8bef53f4..831d917f4 100644 --- a/x/liquidity/spec/02_state.md +++ b/x/liquidity/spec/02_state.md @@ -114,8 +114,6 @@ type SwapMsgState struct { The parameters of the PoolBatch, DepositMsgState, WithdrawMsgState, and SwapMsgState states are: -- PoolBatchIndex: `0x21 | PoolId -> uint64` - - PoolBatch: `0x22 | PoolId -> ProtocolBuffer(PoolBatch)` - PoolBatchDepositMsgStates: `0x31 | PoolId | MsgIndex -> ProtocolBuffer(DepositMsgState)` diff --git a/x/liquidity/types/keys.go b/x/liquidity/types/keys.go index ab3ebcee2..22798d75e 100644 --- a/x/liquidity/types/keys.go +++ b/x/liquidity/types/keys.go @@ -29,8 +29,7 @@ var ( PoolKeyPrefix = []byte{0x11} PoolByReserveAccIndexKeyPrefix = []byte{0x12} - PoolBatchIndexKeyPrefix = []byte{0x21} // Last PoolBatchIndex - PoolBatchKeyPrefix = []byte{0x22} + PoolBatchKeyPrefix = []byte{0x22} PoolBatchDepositMsgStateIndexKeyPrefix = []byte{0x31} PoolBatchWithdrawMsgStateIndexKeyPrefix = []byte{0x32} @@ -50,14 +49,6 @@ func GetPoolByReserveAccIndexKey(reserveAcc sdk.AccAddress) []byte { return append(PoolByReserveAccIndexKeyPrefix, address.MustLengthPrefix(reserveAcc.Bytes())...) } -// GetPoolBatchIndexKey returns kv indexing key of the latest index value of the pool batch -func GetPoolBatchIndexKey(poolID uint64) []byte { - key := make([]byte, 9) - key[0] = PoolBatchIndexKeyPrefix[0] - copy(key[1:9], sdk.Uint64ToBigEndian(poolID)) - return key -} - // GetPoolBatchKey returns kv indexing key of the pool batch indexed by pool id func GetPoolBatchKey(poolID uint64) []byte { key := make([]byte, 9) diff --git a/x/liquidity/types/keys_test.go b/x/liquidity/types/keys_test.go index ec0d14b6d..7592c28c0 100644 --- a/x/liquidity/types/keys_test.go +++ b/x/liquidity/types/keys_test.go @@ -32,10 +32,6 @@ func (s *keysTestSuite) TestGetLiquidityPoolByReserveAccIndexKey() { s.Require().Equal([]byte{0x12, 0x20, 0x87, 0xec, 0x7d, 0x8f, 0xca, 0xee, 0xb0, 0xaa, 0x2, 0x1d, 0xc7, 0xd0, 0x69, 0xb, 0x1e, 0xb8, 0xfb, 0x3e, 0x8e, 0xb1, 0x22, 0x7f, 0x78, 0xae, 0x6c, 0x5e, 0x8a, 0x96, 0xc6, 0x7, 0xc4, 0x98}, types.GetPoolByReserveAccIndexKey(len32acc)) } -func (s *keysTestSuite) TestGetLiquidityPoolBatchIndexKey() { - s.Require().Equal([]byte{0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, types.GetPoolBatchIndexKey(10)) -} - func (s *keysTestSuite) TestGetLiquidityPoolBatchKey() { s.Require().Equal([]byte{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, types.GetPoolBatchKey(10)) s.Require().Equal([]byte{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, types.GetPoolBatchKey(0)) diff --git a/x/liquidity/types/liquidity_pool_test.go b/x/liquidity/types/liquidity_pool_test.go index 1af2092fd..6cdd507c4 100644 --- a/x/liquidity/types/liquidity_pool_test.go +++ b/x/liquidity/types/liquidity_pool_test.go @@ -96,7 +96,6 @@ func TestLiquidityPoolBatch(t *testing.T) { simapp.LiquidityKeeper.SetPool(ctx, pool) batch := types.NewPoolBatch(pool.Id, 1) simapp.LiquidityKeeper.SetPoolBatch(ctx, batch) - simapp.LiquidityKeeper.SetPoolBatchIndex(ctx, batch.PoolId, batch.Index) batchByte := types.MustMarshalPoolBatch(cdc, batch) require.Equal(t, batch, types.MustUnmarshalPoolBatch(cdc, batchByte)) From 14bee6e318573d2327ad545383fb536a47c894e8 Mon Sep 17 00:00:00 2001 From: dongsam Date: Mon, 30 Aug 2021 22:18:20 +0900 Subject: [PATCH 10/13] fix: remove debugging codes and bug on integration test --- Makefile | 4 +-- app/app_test.go | 4 +-- app/genesis.go | 7 ++-- app/sim_test.go | 38 ---------------------- app/test_helpers.go | 2 +- x/liquidity/client/cli/cli_test.go | 6 ++-- x/liquidity/client/testutil/cli_helpers.go | 8 ++--- 7 files changed, 16 insertions(+), 53 deletions(-) diff --git a/Makefile b/Makefile index 34ef73213..b8bd0f0be 100644 --- a/Makefile +++ b/Makefile @@ -161,11 +161,11 @@ test-sim-nondeterminism: test-sim-import-export: runsim @echo "Running application import/export simulation. This may take several minutes..." - @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 30 5 TestAppImportExport test-sim-after-import: runsim @echo "Running application simulation-after-import. This may take several minutes..." - @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 1 1 TestAppSimulationAfterImport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 30 5 TestAppSimulationAfterImport .PHONY: \ test-sim-nondeterminism \ diff --git a/app/app_test.go b/app/app_test.go index b9d1ad85a..83e3b5f7c 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -50,7 +50,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) { ) } - genesisState := NewDefaultGenesisState(encCfg.Marshaler) + genesisState := NewDefaultGenesisState() stateBytes, err := json.MarshalIndent(genesisState, "", " ") require.NoError(t, err) @@ -247,7 +247,7 @@ func TestUpgradeStateOnGenesis(t *testing.T) { encCfg := MakeEncodingConfig() db := dbm.NewMemDB() app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{}) - genesisState := NewDefaultGenesisState(encCfg.Marshaler) + genesisState := NewDefaultGenesisState() stateBytes, err := json.MarshalIndent(genesisState, "", " ") require.NoError(t, err) diff --git a/app/genesis.go b/app/genesis.go index b5feb14cb..b134ffa29 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -4,8 +4,6 @@ package app import ( "encoding/json" - - "github.com/cosmos/cosmos-sdk/codec" ) // The genesis state of the blockchain is represented here as a map of raw json @@ -18,6 +16,7 @@ import ( type GenesisState map[string]json.RawMessage // NewDefaultGenesisState generates the default state for the application. -func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState { - return ModuleBasics.DefaultGenesis(cdc) +func NewDefaultGenesisState() GenesisState { + encCfg := MakeEncodingConfig() + return ModuleBasics.DefaultGenesis(encCfg.Marshaler) } diff --git a/app/sim_test.go b/app/sim_test.go index 4cc61070c..80a46be98 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -8,7 +8,6 @@ import ( "math/rand" "os" "testing" - "time" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/simapp" @@ -23,7 +22,6 @@ import ( distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" "github.com/cosmos/cosmos-sdk/x/feegrant" - "github.com/cosmos/cosmos-sdk/x/genutil" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" @@ -36,8 +34,6 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" dbm "github.com/tendermint/tm-db" - tmtypes "github.com/tendermint/tendermint/types" - liquiditytypes "github.com/tendermint/liquidity/x/liquidity/types" ) @@ -144,22 +140,6 @@ func TestAppImportExport(t *testing.T) { exported, err := app.ExportAppStateAndValidators(false, []string{}) require.NoError(t, err) - // - // TEST: export genesis file - // - now := time.Now() - genDoc := &tmtypes.GenesisDoc{} - genDoc.ChainID = "simulation-chain" - genDoc.GenesisTime = now - genDoc.Validators = nil - genDoc.AppState = exported.AppState - - err = genutil.ExportGenesisFile(genDoc, "../genesis.json") - if err != nil { - panic(err) - } - // - fmt.Printf("importing genesis...\n") _, newDB, newDir, _, _, err := simapp.SetupSimulation("leveldb-app-sim-2", "Simulation-2") @@ -182,24 +162,6 @@ func TestAppImportExport(t *testing.T) { newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState) newApp.StoreConsensusParams(ctxB, exported.ConsensusParams) - // - // TEST: export genesis file - // - exported2, err := newApp.ExportAppStateAndValidators(false, []string{}) - require.NoError(t, err) - - newgenDoc := &tmtypes.GenesisDoc{} - newgenDoc.ChainID = "simulation-chain" - newgenDoc.GenesisTime = now - newgenDoc.Validators = nil - newgenDoc.AppState = exported2.AppState - - err = genutil.ExportGenesisFile(newgenDoc, "../genesis-new.json") - if err != nil { - panic(err) - } - // - fmt.Printf("comparing stores...\n") storeKeysPrefixes := []StoreKeysPrefixes{ diff --git a/app/test_helpers.go b/app/test_helpers.go index b425f107e..00d09c099 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -51,7 +51,7 @@ func setup(withGenesis bool, invCheckPeriod uint) (*LiquidityApp, GenesisState) encCdc := MakeEncodingConfig() app := NewLiquidityApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, EmptyAppOptions{}) if withGenesis { - return app, NewDefaultGenesisState(encCdc.Marshaler) + return app, NewDefaultGenesisState() } return app, GenesisState{} } diff --git a/x/liquidity/client/cli/cli_test.go b/x/liquidity/client/cli/cli_test.go index aa3e64bbb..cbbf6a095 100644 --- a/x/liquidity/client/cli/cli_test.go +++ b/x/liquidity/client/cli/cli_test.go @@ -55,7 +55,9 @@ type IntegrationTestSuite struct { func (s *IntegrationTestSuite) SetupTest() { s.T().Log("setting up integration test suite") - cfg := liquiditytestutil.NewConfig() + db := tmdb.NewMemDB() + + cfg := liquiditytestutil.NewConfig(db) cfg.NumValidators = 1 var liquidityGenState liquiditytypes.GenesisState @@ -69,7 +71,7 @@ func (s *IntegrationTestSuite) SetupTest() { cfg.StakingTokens = sdk.NewInt(100_000_000_000) // stake denom s.cfg = cfg - s.network = network.New(s.T(), cfg) + s.network = network.New(s.T(), s.cfg) s.db = db _, err = s.network.WaitForHeight(1) diff --git a/x/liquidity/client/testutil/cli_helpers.go b/x/liquidity/client/testutil/cli_helpers.go index 8ff7b2b1c..0fe469b24 100644 --- a/x/liquidity/client/testutil/cli_helpers.go +++ b/x/liquidity/client/testutil/cli_helpers.go @@ -23,20 +23,20 @@ import ( // NewConfig returns config that defines the necessary testing requirements // used to bootstrap and start an in-process local testing network. -func NewConfig() network.Config { +func NewConfig(dbm *dbm.MemDB) network.Config { encCfg := simapp.MakeTestEncodingConfig() cfg := network.DefaultConfig() - cfg.AppConstructor = NewAppConstructor(encCfg) // the ABCI application constructor + cfg.AppConstructor = NewAppConstructor(encCfg, dbm) // the ABCI application constructor cfg.GenesisState = liquidityapp.ModuleBasics.DefaultGenesis(cfg.Codec) // liquidity genesis state to provide return cfg } // NewAppConstructor returns a new network AppConstructor. -func NewAppConstructor(encodingCfg params.EncodingConfig) network.AppConstructor { +func NewAppConstructor(encodingCfg params.EncodingConfig, db *dbm.MemDB) network.AppConstructor { return func(val network.Validator) servertypes.Application { return liquidityapp.NewLiquidityApp( - val.Ctx.Logger, dbm.NewMemDB(), nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0, + val.Ctx.Logger, db, nil, true, make(map[int64]bool), val.Ctx.Config.RootDir, 0, liquidityapp.MakeEncodingConfig(), simapp.EmptyAppOptions{}, baseapp.SetPruning(storetypes.NewPruningOptionsFromString(val.AppConfig.Pruning)), From d94b96a456243495790014f0bb832f5905af3af9 Mon Sep 17 00:00:00 2001 From: dongsam Date: Mon, 30 Aug 2021 23:53:01 +0900 Subject: [PATCH 11/13] chore: reduce simulation count for timeout --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b8bd0f0be..50a5f28df 100644 --- a/Makefile +++ b/Makefile @@ -161,11 +161,11 @@ test-sim-nondeterminism: test-sim-import-export: runsim @echo "Running application import/export simulation. This may take several minutes..." - @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 30 5 TestAppImportExport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 10 5 TestAppImportExport test-sim-after-import: runsim @echo "Running application simulation-after-import. This may take several minutes..." - @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 30 5 TestAppSimulationAfterImport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 10 5 TestAppSimulationAfterImport .PHONY: \ test-sim-nondeterminism \ From c30574b5992a2a5573bb8e90a45d8663e3230fe4 Mon Sep 17 00:00:00 2001 From: dongsam Date: Tue, 31 Aug 2021 01:32:10 +0900 Subject: [PATCH 12/13] docs: update changelog --- CHANGELOG.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beb50ce5c..efa186f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,13 +37,22 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +## [v1.3.0](https://github.com/tendermint/liquidity/releases/tag/v1.3.0) - 2021-08-31 + ### State Machine Breaking +* [\#433](https://github.com/tendermint/liquidity/pull/433) (sdk) Bump SDK version to [v0.43.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0). + * [\#436](https://github.com/tendermint/liquidity/pull/436) Validation `MsgSwapWithinBatch` and `OfferCoinFee` ceiling * When calculating `OfferCoinFee`, the decimal points are rounded up. + - before (v1.2.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate` with Truncate or 0 + - after (v1.3.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate` with Ceil * Fix reserveOfferCoinFee residual Issue due to decimal error -* [\#433](https://github.com/tendermint/liquidity/pull/433) (sdk) Bump SDK version to [v0.43.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0). +* [\#438](https://github.com/tendermint/liquidity/pull/438) Fix PoolBatch index, beginHeight issues and genesis logic + * Remove `PoolBatchIndex` + * Fix `PoolBatch.Index` duplicated bug + * Fix `PoolBatch.BeginHeight` consistency issue on genesis init logic ## [v1.2.9](https://github.com/tendermint/liquidity/releases/tag/v1.2.9) - 2021-06-26 * Liquidity module version 1 for Gravity-DEX From ca1087f5f84708fad26373d15883532c08f7a2a0 Mon Sep 17 00:00:00 2001 From: dongsam Date: Tue, 31 Aug 2021 06:11:07 +0900 Subject: [PATCH 13/13] docs: update docs based sdk 0.43.x --- CHANGELOG.md | 4 +- README.md | 4 +- doc/client.md | 158 +++++++++++++++++++++++++++++++++++--------------- 3 files changed, 115 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efa186f85..c58448b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,8 +45,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#436](https://github.com/tendermint/liquidity/pull/436) Validation `MsgSwapWithinBatch` and `OfferCoinFee` ceiling * When calculating `OfferCoinFee`, the decimal points are rounded up. - - before (v1.2.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate` with Truncate or 0 - - after (v1.3.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate` with Ceil + - before (v1.2.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate/2` with Truncate or 0 + - after (v1.3.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate/2` with Ceil * Fix reserveOfferCoinFee residual Issue due to decimal error * [\#438](https://github.com/tendermint/liquidity/pull/438) Fix PoolBatch index, beginHeight issues and genesis logic diff --git a/README.md b/README.md index dd554a349..65508057b 100644 --- a/README.md +++ b/README.md @@ -168,11 +168,11 @@ $BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from validator -- # An example of generating unsigned tx validator=$($BINARY keys show validator --keyring-backend test -a) -$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from $validator --chain-id testing --generate-only > tx_swap.json +$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from $validator --chain-id testing --generate-only &> tx_swap.json cat tx_swap.json # Sign the unsigned tx -$BINARY tx sign tx_swap.json --from validator --chain-id testing --keyring-backend test -y > tx_swap_signed.json +$BINARY tx sign tx_swap.json --from validator --chain-id testing --keyring-backend test -y &> tx_swap_signed.json cat tx_swap_signed.json # Encode the signed tx diff --git a/doc/client.md b/doc/client.md index be05d976d..23d50c994 100644 --- a/doc/client.md +++ b/doc/client.md @@ -71,7 +71,7 @@ JSON Structure: "body": { "messages": [ { - "@type": "/tendermint.liquidity.MsgCreatePool", + "@type": "/tendermint.liquidity.v1beta1.MsgCreatePool", "pool_creator_address": "cosmos1s6cjfm4djg95jkzsfe490yfc9k6wazx6culyft", "pool_type_id": 1, "deposit_coins": [ @@ -119,6 +119,32 @@ Result "msg_index": 0, "log": "", "events": [ + { + "type": "coin_received", + "attributes": [ + { + "key": "receiver", + "value": "cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl" + }, + { + "key": "amount", + "value": "40000000stake" + } + ] + }, + { + "type": "coin_spent", + "attributes": [ + { + "key": "spender", + "value": "cosmos1s6cjfm4djg95jkzsfe490yfc9k6wazx6culyft" + }, + { + "key": "amount", + "value": "40000000stake" + } + ] + }, { "type": "create_pool", "attributes": [ @@ -153,15 +179,7 @@ Result "attributes": [ { "key": "action", - "value": "create_pool" - }, - { - "key": "sender", - "value": "cosmos1s6cjfm4djg95jkzsfe490yfc9k6wazx6culyft" - }, - { - "key": "sender", - "value": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962" + "value": "/tendermint.liquidity.v1beta1.MsgCreatePool" }, { "key": "sender", @@ -176,22 +194,6 @@ Result { "type": "transfer", "attributes": [ - { - "key": "recipient", - "value": "cosmos1jmhkafh94jpgakr735r70t32sxq9wzkayzs9we" - }, - { - "key": "amount", - "value": "1000000000uatom,50000000000uusd" - }, - { - "key": "recipient", - "value": "cosmos1s6cjfm4djg95jkzsfe490yfc9k6wazx6culyft" - }, - { - "key": "amount", - "value": "1000000pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295" - }, { "key": "recipient", "value": "cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl" @@ -202,7 +204,7 @@ Result }, { "key": "amount", - "value": "100000000stake" + "value": "40000000stake" } ] } @@ -232,7 +234,7 @@ JSON Structure: "body": { "messages": [ { - "@type": "/tendermint.liquidity.MsgDepositWithinBatch", + "@type": "/tendermint.liquidity.v1beta1.MsgDepositWithinBatch", "depositor_address": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3", "pool_id": "1", "deposit_coins": [ @@ -280,6 +282,32 @@ Result: "msg_index": 0, "log": "", "events": [ + { + "type": "coin_received", + "attributes": [ + { + "key": "receiver", + "value": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962" + }, + { + "key": "amount", + "value": "100000000uatom,5000000000uusd" + } + ] + }, + { + "type": "coin_spent", + "attributes": [ + { + "key": "spender", + "value": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3" + }, + { + "key": "amount", + "value": "100000000uatom,5000000000uusd" + } + ] + }, { "type": "deposit_within_batch", "attributes": [ @@ -306,7 +334,7 @@ Result: "attributes": [ { "key": "action", - "value": "deposit_within_batch" + "value": "/tendermint.liquidity.v1beta1.MsgDepositWithinBatch" }, { "key": "sender", @@ -403,12 +431,38 @@ Result: "msg_index": 0, "log": "", "events": [ + { + "type": "coin_received", + "attributes": [ + { + "key": "receiver", + "value": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962" + }, + { + "key": "amount", + "value": "10000pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295" + } + ] + }, + { + "type": "coin_spent", + "attributes": [ + { + "key": "spender", + "value": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3" + }, + { + "key": "amount", + "value": "10000pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295" + } + ] + }, { "type": "message", "attributes": [ { "key": "action", - "value": "withdraw_within_batch" + "value": "/tendermint.liquidity.v1beta1.MsgWithdrawWithinBatch" }, { "key": "sender", @@ -537,16 +591,38 @@ Result: "log": "", "events": [ { - "type": "message", + "type": "coin_received", "attributes": [ { - "key": "action", - "value": "swap_within_batch" + "key": "receiver", + "value": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962" }, { - "key": "sender", + "key": "amount", + "value": "50075000uusd" + } + ] + }, + { + "type": "coin_spent", + "attributes": [ + { + "key": "spender", "value": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3" }, + { + "key": "amount", + "value": "50075000uusd" + } + ] + }, + { + "type": "message", + "attributes": [ + { + "key": "action", + "value": "/tendermint.liquidity.v1beta1.MsgSwapWithinBatch" + }, { "key": "sender", "value": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3" @@ -611,19 +687,7 @@ Result: }, { "key": "amount", - "value": "50000000uusd" - }, - { - "key": "recipient", - "value": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962" - }, - { - "key": "sender", - "value": "cosmos1h6ht09xx0ue0fqmezk7msgqcc9k20a5x5ynvc3" - }, - { - "key": "amount", - "value": "75000uusd" + "value": "50075000uusd" } ] }