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/CHANGELOG.md b/CHANGELOG.md index beb50ce5c..c58448b91 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/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 -* [\#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 diff --git a/Makefile b/Makefile index a4797f3fc..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=1 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 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 1 1 TestAppSimulationAfterImport + @$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 10 5 TestAppSimulationAfterImport .PHONY: \ test-sim-nondeterminism \ 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/app/app_test.go b/app/app_test.go new file mode 100644 index 000000000..83e3b5f7c --- /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() + 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() + 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/sim_test.go b/app/sim_test.go index f618082e7..80a46be98 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -16,13 +16,15 @@ 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/feegrant" 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" @@ -148,7 +150,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 @@ -173,10 +175,12 @@ func TestAppImportExport(t *testing.T) { {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[feegrant.StoreKey], newApp.keys[feegrant.StoreKey], [][]byte{}}, {app.keys[liquiditytypes.StoreKey], newApp.keys[liquiditytypes.StoreKey], [][]byte{}}, } @@ -188,6 +192,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)) } } @@ -249,7 +254,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/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" } ] } 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..cbbf6a095 100644 --- a/x/liquidity/client/cli/cli_test.go +++ b/x/liquidity/client/cli/cli_test.go @@ -71,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/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 d6e07ad94..52a9e1205 100644 --- a/x/liquidity/keeper/liquidity_pool.go +++ b/x/liquidity/keeper/liquidity_pool.go @@ -552,8 +552,9 @@ 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) - record.PoolBatch.BeginHeight = ctx.BlockHeight() + if record.PoolBatch.BeginHeight > ctx.BlockHeight() { + record.PoolBatch.BeginHeight = 0 + } 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/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/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) } diff --git a/x/liquidity/simulation/decoder.go b/x/liquidity/simulation/decoder.go index bb2b6aab4..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,19 +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): - var lpA, lpB types.Pool - cdc.MustUnmarshal(kvA.Value, &lpA) - cdc.MustUnmarshal(kvA.Value, &lpB) - return fmt.Sprintf("%v\n%v", lpA, lpB) - - 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) + case bytes.Equal(kvA.Key[:1], types.PoolKeyPrefix): + var poolA, poolB types.Pool + cdc.MustUnmarshal(kvA.Value, &poolA) + cdc.MustUnmarshal(kvB.Value, &poolB) + return fmt.Sprintf("%v\n%v", poolA, poolB) + + 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(kvB.Value, &batchB) + return fmt.Sprintf("%v\n%v", batchA, batchB) + + case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix): + 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 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 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 fc499d880..fa7670e69 100644 --- a/x/liquidity/simulation/decoder_test.go +++ b/x/liquidity/simulation/decoder_test.go @@ -4,26 +4,70 @@ import ( "fmt" "testing" + "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/stretchr/testify/require" "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) - liquidityPool := types.Pool{} - liquidityPool.Id = 1 - liquidityPoolBatch := types.NewPoolBatch(1, 1) + pool := types.Pool{ + Id: uint64(1), + TypeId: uint32(1), + 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)}, + } kvPairs := kv.Pairs{ Pairs: []kv.Pair{ - {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&liquidityPool)}, - {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&liquidityPoolBatch)}, + {Key: types.PoolKeyPrefix, Value: cdc.MustMarshal(&pool)}, + {Key: types.PoolByReserveAccIndexKeyPrefix, Value: reserveAccAddr1.Bytes()}, + {Key: types.PoolBatchKeyPrefix, Value: cdc.MustMarshal(&batch)}, + {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}}, }, } @@ -32,8 +76,12 @@ 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)}, + {"Pool", fmt.Sprintf("%v\n%v", pool, pool)}, + {"PoolByReserveAccIndex", fmt.Sprintf("%v\n%v", reserveAccAddr1, reserveAccAddr1)}, + {"PoolBatchKey", fmt.Sprintf("%v\n%v", batch, batch)}, + {"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 { 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))