Files
wasmd/x/wasm/keeper/migrate_test.go
vuong 6d67d5b4f7 Get Contracts by Creator Address (#1021)
* add query to query.proto

* add ContractsByCreatorPrefix in keys.go

* add ContractCreatorThirdIndex to keeper.go

* add querier

* cli

* fix test

* linting

* add key test

* no need to change creator when migrate

* add query test

* minor

* add migrate logic

* add more test

* register migration

* minor

* Update x/wasm/client/cli/query.go

Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>

* nits

* remove IterateAllContract

* Update x/wasm/keeper/genesis_test.go

Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>

* nit

* nit: func name

* change key

* improve TestIteratorContractByCreator

* fix test

* use IterateContractInfo in migrate2to3

* minor

* move key

* improve test case

* add pagReq in ContractsByCreator query

* ordering query

* add migrate test

* Make ContractsByCreator plural; formatting and minor updates

* Comment why AbsoluteTxPositionLen makes sense

* Migrate 1 to 2

* Set module version

Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>
Co-authored-by: khanh-notional <50263489+catShaark@users.noreply.github.com>
2022-10-20 19:04:41 +02:00

62 lines
2.4 KiB
Go

package keeper
import (
"bytes"
"encoding/json"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/stretchr/testify/require"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
func TestMigrate1To2(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
wasmKeeper := keepers.WasmKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := sdk.AccAddress(bytes.Repeat([]byte{1}, address.Len))
keepers.Faucet.Fund(ctx, creator, deposit...)
example := StoreHackatomExampleContract(t, ctx, keepers)
initMsg := HackatomExampleInitMsg{
Verifier: RandomAccountAddress(t),
Beneficiary: RandomAccountAddress(t),
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
em := sdk.NewEventManager()
// create with no balance is also legal
gotContractAddr1, _, err := keepers.ContractKeeper.Instantiate(ctx.WithEventManager(em), example.CodeID, creator, nil, initMsgBz, "demo contract 1", nil)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
gotContractAddr2, _, err := keepers.ContractKeeper.Instantiate(ctx.WithEventManager(em), example.CodeID, creator, nil, initMsgBz, "demo contract 1", nil)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
gotContractAddr3, _, err := keepers.ContractKeeper.Instantiate(ctx.WithEventManager(em), example.CodeID, creator, nil, initMsgBz, "demo contract 1", nil)
info1 := wasmKeeper.GetContractInfo(ctx, gotContractAddr1)
info2 := wasmKeeper.GetContractInfo(ctx, gotContractAddr2)
info3 := wasmKeeper.GetContractInfo(ctx, gotContractAddr3)
// remove key
ctx.KVStore(wasmKeeper.storeKey).Delete(types.GetContractByCreatorSecondaryIndexKey(creator, info1.Created.Bytes(), gotContractAddr1))
ctx.KVStore(wasmKeeper.storeKey).Delete(types.GetContractByCreatorSecondaryIndexKey(creator, info2.Created.Bytes(), gotContractAddr2))
ctx.KVStore(wasmKeeper.storeKey).Delete(types.GetContractByCreatorSecondaryIndexKey(creator, info3.Created.Bytes(), gotContractAddr3))
// migrator
migrator := NewMigrator(*wasmKeeper)
migrator.Migrate1to2(ctx)
// check new store
var allContract []string
wasmKeeper.IterateContractsByCreator(ctx, creator, func(addr sdk.AccAddress) bool {
allContract = append(allContract, addr.String())
return false
})
require.Equal(t, []string{gotContractAddr1.String(), gotContractAddr2.String(), gotContractAddr3.String()}, allContract)
}