Skip to content

Commit 02644bb

Browse files
jaybxyzdongsam
andauthored
Fix PoolBatch index, beginHeight issues and genesis logic (#438)
* test: debugging diffs of two different genesis files * test: add more store keys in decoder * test: fix decoder to have msg states * fix: context block height * test: debugging proto: wrong wireType = 0 for field Msg * fix comment * test: debugging * chore: debugging pool batch decoding issue * update: deprecate PoolBatchIndexKey and fix genesis logic * fix: remove debugging codes and bug on integration test * chore: reduce simulation count for timeout * docs: update changelog * docs: update docs based sdk 0.43.x Co-authored-by: dongsam <[email protected]>
1 parent a42fed5 commit 02644bb

File tree

27 files changed

+595
-140
lines changed

27 files changed

+595
-140
lines changed

.github/workflows/test.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,41 @@ jobs:
9090
with:
9191
file: ./coverage.txt
9292

93-
test-simulation:
93+
test-sim-nondeterminism:
9494
runs-on: ubuntu-latest
9595
timeout-minutes: 15
9696
steps:
9797
- uses: actions/checkout@v2
9898
- uses: actions/[email protected]
9999
with:
100-
go-version: 1.15
100+
go-version: 1.16
101101
- name: Display go version
102102
run: go version
103103
- name: Testing simulation
104104
run: make test-sim-nondeterminism
105+
106+
test-sim-after-import:
107+
runs-on: ubuntu-latest
108+
timeout-minutes: 15
109+
steps:
110+
- uses: actions/checkout@v2
111+
- uses: actions/[email protected]
112+
with:
113+
go-version: 1.16
114+
- name: Display go version
115+
run: go version
116+
- name: Testing simulation
117+
run: make test-sim-after-import
118+
119+
test-sim-import-export:
120+
runs-on: ubuntu-latest
121+
timeout-minutes: 15
122+
steps:
123+
- uses: actions/checkout@v2
124+
- uses: actions/[email protected]
125+
with:
126+
go-version: 1.16
127+
- name: Display go version
128+
run: go version
129+
- name: Testing simulation
130+
run: make test-sim-import-export

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
## [v1.3.0](https://github.com/tendermint/liquidity/releases/tag/v1.3.0) - 2021-08-31
41+
4042
### State Machine Breaking
4143

44+
* [\#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).
45+
4246
* [\#436](https://github.com/tendermint/liquidity/pull/436) Validation `MsgSwapWithinBatch` and `OfferCoinFee` ceiling
4347
* When calculating `OfferCoinFee`, the decimal points are rounded up.
48+
- before (v1.2.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate/2` with Truncate or 0
49+
- after (v1.3.x): `MsgSwapWithinBatch.OfferCoinFee` should be `OfferCoin` * `params.SwapFeeRate/2` with Ceil
4450
* Fix reserveOfferCoinFee residual Issue due to decimal error
4551

46-
* [\#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).
52+
* [\#438](https://github.com/tendermint/liquidity/pull/438) Fix PoolBatch index, beginHeight issues and genesis logic
53+
* Remove `PoolBatchIndex`
54+
* Fix `PoolBatch.Index` duplicated bug
55+
* Fix `PoolBatch.BeginHeight` consistency issue on genesis init logic
4756

4857
## [v1.2.9](https://github.com/tendermint/liquidity/releases/tag/v1.2.9) - 2021-06-26
4958
* Liquidity module version 1 for Gravity-DEX

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ test-sim-nondeterminism:
161161

162162
test-sim-import-export: runsim
163163
@echo "Running application import/export simulation. This may take several minutes..."
164-
@$(BINDIR)/runsim -Jobs=1 -SimAppPkg=$(SIMAPP) -ExitOnFail 50 5 TestAppImportExport
164+
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 10 5 TestAppImportExport
165165

166166
test-sim-after-import: runsim
167167
@echo "Running application simulation-after-import. This may take several minutes..."
168-
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 1 1 TestAppSimulationAfterImport
168+
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 10 5 TestAppSimulationAfterImport
169169

170170
.PHONY: \
171171
test-sim-nondeterminism \

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ $BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from validator --
168168

169169
# An example of generating unsigned tx
170170
validator=$($BINARY keys show validator --keyring-backend test -a)
171-
$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from $validator --chain-id testing --generate-only > tx_swap.json
171+
$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from $validator --chain-id testing --generate-only &> tx_swap.json
172172
cat tx_swap.json
173173

174174
# Sign the unsigned tx
175-
$BINARY tx sign tx_swap.json --from validator --chain-id testing --keyring-backend test -y > tx_swap_signed.json
175+
$BINARY tx sign tx_swap.json --from validator --chain-id testing --keyring-backend test -y &> tx_swap_signed.json
176176
cat tx_swap_signed.json
177177

178178
# Encode the signed tx

app/app_test.go

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package app
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"testing"
7+
8+
"github.com/golang/mock/gomock"
9+
"github.com/stretchr/testify/require"
10+
abci "github.com/tendermint/tendermint/abci/types"
11+
"github.com/tendermint/tendermint/libs/log"
12+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
13+
dbm "github.com/tendermint/tm-db"
14+
15+
"github.com/cosmos/cosmos-sdk/baseapp"
16+
"github.com/cosmos/cosmos-sdk/tests/mocks"
17+
sdk "github.com/cosmos/cosmos-sdk/types"
18+
"github.com/cosmos/cosmos-sdk/types/module"
19+
"github.com/cosmos/cosmos-sdk/x/auth"
20+
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
21+
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
22+
"github.com/cosmos/cosmos-sdk/x/bank"
23+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
24+
"github.com/cosmos/cosmos-sdk/x/capability"
25+
"github.com/cosmos/cosmos-sdk/x/crisis"
26+
"github.com/cosmos/cosmos-sdk/x/distribution"
27+
"github.com/cosmos/cosmos-sdk/x/evidence"
28+
feegrantmodule "github.com/cosmos/cosmos-sdk/x/feegrant/module"
29+
"github.com/cosmos/cosmos-sdk/x/genutil"
30+
"github.com/cosmos/cosmos-sdk/x/gov"
31+
"github.com/cosmos/cosmos-sdk/x/mint"
32+
"github.com/cosmos/cosmos-sdk/x/params"
33+
"github.com/cosmos/cosmos-sdk/x/slashing"
34+
"github.com/cosmos/cosmos-sdk/x/staking"
35+
"github.com/cosmos/cosmos-sdk/x/upgrade"
36+
37+
"github.com/tendermint/liquidity/x/liquidity"
38+
)
39+
40+
func TestSimAppExportAndBlockedAddrs(t *testing.T) {
41+
encCfg := MakeEncodingConfig()
42+
db := dbm.NewMemDB()
43+
app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
44+
45+
for acc := range maccPerms {
46+
require.True(
47+
t,
48+
app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)),
49+
"ensure that blocked addresses are properly set in bank keeper",
50+
)
51+
}
52+
53+
genesisState := NewDefaultGenesisState()
54+
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
55+
require.NoError(t, err)
56+
57+
// Initialize the chain
58+
app.InitChain(
59+
abci.RequestInitChain{
60+
Validators: []abci.ValidatorUpdate{},
61+
AppStateBytes: stateBytes,
62+
},
63+
)
64+
app.Commit()
65+
66+
// Making a new app object with the db, so that initchain hasn't been called
67+
app2 := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
68+
_, err = app2.ExportAppStateAndValidators(false, []string{})
69+
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
70+
}
71+
72+
func TestGetMaccPerms(t *testing.T) {
73+
dup := GetMaccPerms()
74+
require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions")
75+
}
76+
77+
func TestRunMigrations(t *testing.T) {
78+
db := dbm.NewMemDB()
79+
encCfg := MakeEncodingConfig()
80+
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
81+
app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
82+
83+
// Create a new baseapp and configurator for the purpose of this test.
84+
bApp := baseapp.NewBaseApp(appName, logger, db, encCfg.TxConfig.TxDecoder())
85+
bApp.SetCommitMultiStoreTracer(nil)
86+
bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry)
87+
app.BaseApp = bApp
88+
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
89+
90+
// We register all modules on the Configurator, except x/bank. x/bank will
91+
// serve as the test subject on which we run the migration tests.
92+
//
93+
// The loop below is the same as calling `RegisterServices` on
94+
// ModuleManager, except that we skip x/bank.
95+
for _, module := range app.mm.Modules {
96+
if module.Name() == banktypes.ModuleName {
97+
continue
98+
}
99+
100+
module.RegisterServices(app.configurator)
101+
}
102+
103+
// Initialize the chain
104+
app.InitChain(abci.RequestInitChain{})
105+
app.Commit()
106+
107+
testCases := []struct {
108+
name string
109+
moduleName string
110+
forVersion uint64
111+
expRegErr bool // errors while registering migration
112+
expRegErrMsg string
113+
expRunErr bool // errors while running migration
114+
expRunErrMsg string
115+
expCalled int
116+
}{
117+
{
118+
"cannot register migration for version 0",
119+
"bank", 0,
120+
true, "module migration versions should start at 1: invalid version", false, "", 0,
121+
},
122+
{
123+
"throws error on RunMigrations if no migration registered for bank",
124+
"", 1,
125+
false, "", true, "no migrations found for module bank: not found", 0,
126+
},
127+
{
128+
"can register and run migration handler for x/bank",
129+
"bank", 1,
130+
false, "", false, "", 1,
131+
},
132+
{
133+
"cannot register migration handler for same module & forVersion",
134+
"bank", 1,
135+
true, "another migration for module bank and version 1 already exists: internal logic error", false, "", 0,
136+
},
137+
}
138+
139+
for _, tc := range testCases {
140+
t.Run(tc.name, func(t *testing.T) {
141+
var err error
142+
143+
// Since it's very hard to test actual in-place store migrations in
144+
// tests (due to the difficulty of maintaining multiple versions of a
145+
// module), we're just testing here that the migration logic is
146+
// called.
147+
called := 0
148+
149+
if tc.moduleName != "" {
150+
// Register migration for module from version `forVersion` to `forVersion+1`.
151+
err = app.configurator.RegisterMigration(tc.moduleName, tc.forVersion, func(sdk.Context) error {
152+
called++
153+
154+
return nil
155+
})
156+
157+
if tc.expRegErr {
158+
require.EqualError(t, err, tc.expRegErrMsg)
159+
160+
return
161+
}
162+
}
163+
require.NoError(t, err)
164+
165+
// Run migrations only for bank. That's why we put the initial
166+
// version for bank as 1, and for all other modules, we put as
167+
// their latest ConsensusVersion.
168+
_, err = app.mm.RunMigrations(
169+
app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}), app.configurator,
170+
module.VersionMap{
171+
"bank": 1,
172+
"auth": auth.AppModule{}.ConsensusVersion(),
173+
"authz": authzmodule.AppModule{}.ConsensusVersion(),
174+
"staking": staking.AppModule{}.ConsensusVersion(),
175+
"mint": mint.AppModule{}.ConsensusVersion(),
176+
"distribution": distribution.AppModule{}.ConsensusVersion(),
177+
"slashing": slashing.AppModule{}.ConsensusVersion(),
178+
"gov": gov.AppModule{}.ConsensusVersion(),
179+
"params": params.AppModule{}.ConsensusVersion(),
180+
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
181+
"vesting": vesting.AppModule{}.ConsensusVersion(),
182+
"feegrant": feegrantmodule.AppModule{}.ConsensusVersion(),
183+
"evidence": evidence.AppModule{}.ConsensusVersion(),
184+
"crisis": crisis.AppModule{}.ConsensusVersion(),
185+
"genutil": genutil.AppModule{}.ConsensusVersion(),
186+
"capability": capability.AppModule{}.ConsensusVersion(),
187+
"liquidity": liquidity.AppModule{}.ConsensusVersion(),
188+
},
189+
)
190+
if tc.expRunErr {
191+
require.EqualError(t, err, tc.expRunErrMsg)
192+
} else {
193+
require.NoError(t, err)
194+
// Make sure bank's migration is called.
195+
require.Equal(t, tc.expCalled, called)
196+
}
197+
})
198+
}
199+
}
200+
201+
func TestInitGenesisOnMigration(t *testing.T) {
202+
db := dbm.NewMemDB()
203+
encCfg := MakeEncodingConfig()
204+
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
205+
app := NewLiquidityApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
206+
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
207+
208+
// Create a mock module. This module will serve as the new module we're
209+
// adding during a migration.
210+
mockCtrl := gomock.NewController(t)
211+
t.Cleanup(mockCtrl.Finish)
212+
mockModule := mocks.NewMockAppModule(mockCtrl)
213+
mockDefaultGenesis := json.RawMessage(`{"key": "value"}`)
214+
mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis)
215+
mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil)
216+
mockModule.EXPECT().ConsensusVersion().Times(1).Return(uint64(0))
217+
218+
app.mm.Modules["mock"] = mockModule
219+
220+
// Run migrations only for "mock" module. We exclude it from
221+
// the VersionMap to simulate upgrading with a new module.
222+
_, err := app.mm.RunMigrations(ctx, app.configurator,
223+
module.VersionMap{
224+
"bank": bank.AppModule{}.ConsensusVersion(),
225+
"auth": auth.AppModule{}.ConsensusVersion(),
226+
"authz": authzmodule.AppModule{}.ConsensusVersion(),
227+
"staking": staking.AppModule{}.ConsensusVersion(),
228+
"mint": mint.AppModule{}.ConsensusVersion(),
229+
"distribution": distribution.AppModule{}.ConsensusVersion(),
230+
"slashing": slashing.AppModule{}.ConsensusVersion(),
231+
"gov": gov.AppModule{}.ConsensusVersion(),
232+
"params": params.AppModule{}.ConsensusVersion(),
233+
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
234+
"vesting": vesting.AppModule{}.ConsensusVersion(),
235+
"feegrant": feegrantmodule.AppModule{}.ConsensusVersion(),
236+
"evidence": evidence.AppModule{}.ConsensusVersion(),
237+
"crisis": crisis.AppModule{}.ConsensusVersion(),
238+
"genutil": genutil.AppModule{}.ConsensusVersion(),
239+
"capability": capability.AppModule{}.ConsensusVersion(),
240+
"liquidity": liquidity.AppModule{}.ConsensusVersion(),
241+
},
242+
)
243+
require.NoError(t, err)
244+
}
245+
246+
func TestUpgradeStateOnGenesis(t *testing.T) {
247+
encCfg := MakeEncodingConfig()
248+
db := dbm.NewMemDB()
249+
app := NewLiquidityApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
250+
genesisState := NewDefaultGenesisState()
251+
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
252+
require.NoError(t, err)
253+
254+
// Initialize the chain
255+
app.InitChain(
256+
abci.RequestInitChain{
257+
Validators: []abci.ValidatorUpdate{},
258+
AppStateBytes: stateBytes,
259+
},
260+
)
261+
262+
// make sure the upgrade keeper has version map in state
263+
ctx := app.NewContext(false, tmproto.Header{})
264+
vm := app.UpgradeKeeper.GetModuleVersionMap(ctx)
265+
for v, i := range app.mm.Modules {
266+
require.Equal(t, vm[v], i.ConsensusVersion())
267+
}
268+
}

app/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (app *LiquidityApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAd
145145
counter := int16(0)
146146

147147
for ; iter.Valid(); iter.Next() {
148-
addr := sdk.ValAddress(iter.Key()[1:])
148+
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
149149
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
150150
if !found {
151151
panic("expected validator, not found")

0 commit comments

Comments
 (0)