From 491fec285dd4e28aae9b239aabeeee432f20477c Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 15 Aug 2023 11:56:28 +0900 Subject: [PATCH 1/3] test: add test cases in ContractsByCode Signed-off-by: 170210 --- x/wasm/keeper/querier_test.go | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index 1606c03ecd..80802b2bbd 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -278,6 +278,124 @@ func TestQueryRawContractState(t *testing.T) { } } +func TestQueryContractsByCode(t *testing.T) { + ctx, keepers := CreateTestInput(t, false, AvailableCapabilities) + keeper := keepers.WasmKeeper + q := Querier(keeper) + + // store contract + contract := StoreHackatomExampleContract(t, ctx, keepers) + + createInitMsg := func(ctx sdk.Context, keepers TestKeepers) sdk.AccAddress { + _, _, verifierAddr := keyPubAddr() + fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, verifierAddr, contract.InitialAmount) + + _, _, beneficiaryAddr := keyPubAddr() + initMsgBz := HackatomExampleInitMsg{ + Verifier: verifierAddr, + Beneficiary: beneficiaryAddr, + }.GetBytes(t) + initialAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 100)) + + adminAddr := contract.CreatorAddr + label := "demo contract to query" + contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, contract.CodeID, contract.CreatorAddr, adminAddr, initMsgBz, label, initialAmount) + fmt.Println(contract.CodeID) + require.NoError(t, err) + return contractAddr + } + + // manage some realistic block settings + meter := sdk.NewGasMeter(1000000) + ctx = ctx.WithGasMeter(meter) + ctx = ctx.WithBlockGasMeter(meter) + + // create 2 contracts with real block/gas setup + exampleContractAddr1 := createInitMsg(ctx, keepers).String() + exampleContractAddr2 := createInitMsg(ctx, keepers).String() + + specs := map[string]struct { + req *types.QueryContractsByCodeRequest + expAddr []string + expPaginationTotal uint64 + expErr error + }{ + "with empty request": { + req: nil, + expErr: status.Error(codes.InvalidArgument, "empty request"), + }, + "req.CodeId=0": { + req: &types.QueryContractsByCodeRequest{CodeId: 0}, + expErr: sdkErrors.Wrap(types.ErrInvalid, "code id"), + }, + "not exist codeID": { + req: &types.QueryContractsByCodeRequest{CodeId: 2}, + expAddr: []string{}, + expPaginationTotal: 0, + }, + "query all": { + req: &types.QueryContractsByCodeRequest{ + CodeId: 1, + }, + expAddr: []string{exampleContractAddr1, exampleContractAddr2}, + expPaginationTotal: 2, + }, + "with pagination offset": { + req: &types.QueryContractsByCodeRequest{ + CodeId: 1, + Pagination: &query.PageRequest{ + Offset: 1, + }, + }, + expAddr: []string{exampleContractAddr2}, + expPaginationTotal: 2, + }, + "with invalid pagination key": { + req: &types.QueryContractsByCodeRequest{ + CodeId: 1, + Pagination: &query.PageRequest{ + Offset: 1, + Key: []byte("test"), + }, + }, + expErr: fmt.Errorf("invalid request, either offset or key is expected, got both"), + }, + "with pagination limit": { + req: &types.QueryContractsByCodeRequest{ + CodeId: 1, + Pagination: &query.PageRequest{ + Limit: 1, + }, + }, + expAddr: []string{exampleContractAddr1}, + expPaginationTotal: 0, + }, + "with pagination next key": { + req: &types.QueryContractsByCodeRequest{ + CodeId: 1, + Pagination: &query.PageRequest{ + Key: fromBase64("AAAAAAAS1ocAAAAAAASY0YcuhNIMvyvID4NhhfROlbQNuZ0fl0clmBPoWHtKYazH"), + }, + }, + expAddr: []string{exampleContractAddr2}, + expPaginationTotal: 0, + }, + } + for msg, spec := range specs { + t.Run(msg, func(t *testing.T) { + got, err := q.ContractsByCode(sdk.WrapSDKContext(ctx), spec.req) + + if spec.expErr != nil { + assert.NotNil(t, err) + assert.EqualError(t, err, spec.expErr.Error()) + return + } + assert.EqualValues(t, spec.expPaginationTotal, got.Pagination.Total) + assert.NotNil(t, got) + assert.Equal(t, spec.expAddr, got.Contracts) + }) + } +} func TestQueryContractListByCodeOrdering(t *testing.T) { ctx, keepers := CreateTestInput(t, false, AvailableCapabilities) keeper := keepers.WasmKeeper From 4ba65f07e19df5e5748cbdd1271ebb5561cd14a0 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 15 Aug 2023 11:26:18 +0900 Subject: [PATCH 2/3] chore: add this PR to CHANGELOG Signed-off-by: 170210 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 474dc472ac..2e183366a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [\#65](https://github.com/Finschia/wasmd/pull/65) add test cases for empty request in each function * [\#66](https://github.com/Finschia/wasmd/pull/66) add test cases for invalid pagination key in some functions * [\#64](https://github.com/Finschia/wasmd/pull/64) test: add test cases to confirm output for PinnedCodes +* [\#71](https://github.com/Finschia/wasmd/pull/71) add test cases in ContractsByCode ### Bug Fixes * [\#52](https://github.com/Finschia/wasmd/pull/52) fix cli_test error of wasmplus and add cli_test ci From e5240a663ec455104778e73ed95dcb464f2b1929 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 18 Aug 2023 17:30:54 +0900 Subject: [PATCH 3/3] chore: add an explanation for block setting Signed-off-by: 170210 --- x/wasm/keeper/querier_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index 681e1032ed..f30303ff76 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -311,6 +311,9 @@ func TestQueryContractsByCode(t *testing.T) { return contractAddr } + // related link: https://github.com/CosmWasm/wasmd/issues/1559 + // When not set with a block, the contract will return results based on the order of address rather than instantiation (timestamp). + // Since this function will test results for pagination, it is necessary to ensure that the results are returned in the order of instantiation. // manage some realistic block settings meter := sdk.NewGasMeter(1000000) ctx = ctx.WithGasMeter(meter)