From 1b88f0d1ac94a340c4501e0fda5e15609613a791 Mon Sep 17 00:00:00 2001 From: 170210 <85928898+170210@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:57:48 +0900 Subject: [PATCH 1/4] fix: fill ContractInfo result's Updated field (#89) * fix: fill Updated in queryContractInfo result Signed-off-by: 170210 * chore: add this PR to CHANGELOG Signed-off-by: 170210 --------- Signed-off-by: 170210 --- CHANGELOG.md | 1 + x/wasm/client/testutil/query.go | 4 +++- x/wasm/keeper/querier.go | 2 -- x/wasm/keeper/querier_test.go | 12 ++++-------- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d74ae50ec1..eb930cf811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ ### Bug Fixes * [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field * [\#52](https://github.com/Finschia/wasmd/pull/52) fix cli_test error of wasmplus and add cli_test ci +* [\#89](https://github.com/Finschia/wasmd/pull/89) fill ContractInfo querier result's Updated field ### Breaking Changes diff --git a/x/wasm/client/testutil/query.go b/x/wasm/client/testutil/query.go index 7953c88cf3..166244e22f 100644 --- a/x/wasm/client/testutil/query.go +++ b/x/wasm/client/testutil/query.go @@ -140,6 +140,8 @@ func (s *IntegrationTestSuite) TestGetCmdGetContractInfo() { codeID, err := strconv.ParseUint(s.codeID, 10, 64) s.Require().NoError(err) + s.Require().True(s.setupHeight > 0) + codeUpdateHeight := uint64(s.setupHeight) testCases := map[string]struct { args []string @@ -158,7 +160,7 @@ func (s *IntegrationTestSuite) TestGetCmdGetContractInfo() { Creator: val.Address.String(), Admin: val.Address.String(), Label: "TestContract", - Created: nil, + Created: &types.AbsoluteTxPosition{BlockHeight: codeUpdateHeight, TxIndex: 0}, IBCPortID: "", Extension: nil, }, diff --git a/x/wasm/keeper/querier.go b/x/wasm/keeper/querier.go index e02d91ca3f..5c84ae297d 100644 --- a/x/wasm/keeper/querier.go +++ b/x/wasm/keeper/querier.go @@ -256,8 +256,6 @@ func queryContractInfo(ctx sdk.Context, addr sdk.AccAddress, keeper types.ViewKe if info == nil { return nil, types.ErrNotFound } - // redact the Created field (just used for sorting, not part of public API) - info.Created = nil return &types.QueryContractInfoResponse{ Address: addr.String(), ContractInfo: *info, diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index f30303ff76..96fb6300a6 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -856,20 +856,16 @@ func TestQueryContractInfo(t *testing.T) { src: &types.QueryContractInfoRequest{Address: contractAddr.String()}, stored: types.ContractInfoFixture(), expRsp: &types.QueryContractInfoResponse{ - Address: contractAddr.String(), - ContractInfo: types.ContractInfoFixture(func(info *types.ContractInfo) { - info.Created = nil // not returned on queries - }), + Address: contractAddr.String(), + ContractInfo: types.ContractInfoFixture(), }, }, "with extension": { src: &types.QueryContractInfoRequest{Address: contractAddr.String()}, stored: types.ContractInfoFixture(myExtension), expRsp: &types.QueryContractInfoResponse{ - Address: contractAddr.String(), - ContractInfo: types.ContractInfoFixture(myExtension, func(info *types.ContractInfo) { - info.Created = nil // not returned on queries - }), + Address: contractAddr.String(), + ContractInfo: types.ContractInfoFixture(myExtension), }, }, "not found": { From 0cecc1ae572f1fa061c6fc61b8e801a3c45a5221 Mon Sep 17 00:00:00 2001 From: 170210 <85928898+170210@users.noreply.github.com> Date: Tue, 22 Aug 2023 14:14:47 +0900 Subject: [PATCH 2/4] fix: delete output in TestQueryContractsByCode (#90) * fix: delete output in TestQueryContractsByCode Signed-off-by: 170210 * chore: add this PR to CHANGELOG Signed-off-by: 170210 --------- Signed-off-by: 170210 --- CHANGELOG.md | 1 + x/wasm/keeper/querier_test.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb930cf811..5b65de8f8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field * [\#52](https://github.com/Finschia/wasmd/pull/52) fix cli_test error of wasmplus and add cli_test ci * [\#89](https://github.com/Finschia/wasmd/pull/89) fill ContractInfo querier result's Updated field +* [\#90](https://github.com/Finschia/wasmd/pull/90) delete output in TestQueryContractsByCode ### Breaking Changes diff --git a/x/wasm/keeper/querier_test.go b/x/wasm/keeper/querier_test.go index 96fb6300a6..ecd40e6dd7 100644 --- a/x/wasm/keeper/querier_test.go +++ b/x/wasm/keeper/querier_test.go @@ -306,7 +306,6 @@ func TestQueryContractsByCode(t *testing.T) { 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 } From 4413ae41f217fdb83bb4788d31adf7642c539a2c Mon Sep 17 00:00:00 2001 From: 170210 <85928898+170210@users.noreply.github.com> Date: Tue, 22 Aug 2023 16:28:44 +0900 Subject: [PATCH 3/4] docs: modify links in x/wasmplus README.md (#92) * docs: modify links in x/wasmplus README.md Signed-off-by: 170210 * chore: add this PR to CHANGELOG Signed-off-by: 170210 --------- Signed-off-by: 170210 --- CHANGELOG.md | 1 + x/wasmplus/README.md | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b65de8f8e..7be97bfe0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ ### Document Updates * [\#54](https://github.com/Finschia/wasmd/pull/54) add documentation about errors (codespace and codes) +* [\#92](https://github.com/Finschia/wasmd/pull/92) modify links in x/wasmplus README.md ## [v0.1.4](https://github.com/Finschia/wasmd/releases/tag/v0.1.4) - 2023.05.22 diff --git a/x/wasmplus/README.md b/x/wasmplus/README.md index ef9fd87a5e..70d625c0a6 100644 --- a/x/wasmplus/README.md +++ b/x/wasmplus/README.md @@ -1,6 +1,6 @@ # wasmplus -Extended module of [finschia/wasmd/x/wasm](https://github.com/Finschia/wasmd/tree/cae21ecd251cea44f56209e0a4586ca2979c6c87/x/wasm) module. +Extended module of [finschia/wasmd/x/wasm](../wasm) module. ## Concepts @@ -26,10 +26,10 @@ Through `ActivateContractProposal`, you can release restrictions on the use of i #### queries ##### InactiveContracts * Query API to query a list of all disabled smart contract addresses with pagination -* [Detailed specification](../../docs/proto/proto-docs.md#activatecontractproposal) +* [Detailed specification](../../docs/proto/proto-docs.md#queryinactivecontractsrequest) ##### InactiveContract * Query API to check if a specific smart contract address is disabled -* [Detailed specification](../../docs/proto/proto-docs.md#deactivatecontractproposal) +* [Detailed specification](../../docs/proto/proto-docs.md#queryinactivecontractrequest) ### Msg/StoreCodeAndInstantiateContract `Msg/StoreCodeAndInstantiateContract` allows `StoreCode` and `InstantiateContract` to be processed as one tx message. From f36b3898b04d22987d05c6393253f863e003bab5 Mon Sep 17 00:00:00 2001 From: kokeshiM0chi <52264064+kokeshiM0chi@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:44:25 +0900 Subject: [PATCH 4/4] test: add test case to QueryInactiveContracts (#82) * test: add test case to `QueryInactiveContracts` * chore: add this pr for changelog.md * fix: Fix address judgment and pagination key * feat: add the function for byte array to use addresses order check * test: add func for make test data for addresses order * fix: Add a function to create an ordered address array Fix to create an ordered address array and test it as an expected value * fix: fix lint error * fix: add the error handling for lint * fix: fix `Error: unreachable: unreachable code (govet)` for `golangci-lint` * fix: fix panic message * refactor: Changed []byte array comparison to string comparison * fix: revert commit for go.mod go.sum * fix: fix lint * fix: fix lint * fix: delete unnecessary code * fix: rename function name and add a contract for order --- CHANGELOG.md | 1 + x/wasmplus/keeper/querier_test.go | 88 ++++++++++++++++++++++++++++--- x/wasmplus/keeper/test_common.go | 14 +++++ 3 files changed, 97 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7be97bfe0b..473fe8fcee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * [\#78](https://github.com/Finschia/wasmd/pull/78) add the check for TestMigrateContract * [\#69](https://github.com/Finschia/wasmd/pull/69) refactor: refactor test cases for Params * [\#71](https://github.com/Finschia/wasmd/pull/71) add test cases in ContractsByCode +* [\#82](https://github.com/Finschia/wasmd/pull/82) add test case to QueryInactiveContracts ### Bug Fixes * [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field diff --git a/x/wasmplus/keeper/querier_test.go b/x/wasmplus/keeper/querier_test.go index 7d445a0bf3..1574c0c7e1 100644 --- a/x/wasmplus/keeper/querier_test.go +++ b/x/wasmplus/keeper/querier_test.go @@ -1,6 +1,9 @@ package keeper import ( + "encoding/base64" + "fmt" + "github.com/Finschia/finschia-sdk/types/query" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" @@ -20,20 +23,85 @@ func TestQueryInactiveContracts(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) example2 := InstantiateHackatomExampleContract(t, ctx, keepers) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) + example3 := InstantiateHackatomExampleContract(t, ctx, keepers) + ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) + + // Address order of contracts is ascending order of byte array whose address is decoded by bech32 + expAddrs := GenerateSortedBech32Address(example1.Contract.Bytes(), example2.Contract.Bytes(), example3.Contract.Bytes()) // set inactive err := keeper.deactivateContract(ctx, example1.Contract) require.NoError(t, err) err = keeper.deactivateContract(ctx, example2.Contract) require.NoError(t, err) + err = keeper.deactivateContract(ctx, example3.Contract) + require.NoError(t, err) q := Querier(keeper) - rq := types.QueryInactiveContractsRequest{} - res, err := q.InactiveContracts(sdk.WrapSDKContext(ctx), &rq) - require.NoError(t, err) - expect := []string{example1.Contract.String(), example2.Contract.String()} - for _, exp := range expect { - assert.Contains(t, res.Addresses, exp) + specs := map[string]struct { + srcQuery *types.QueryInactiveContractsRequest + expAddrs []string + expPaginationTotal uint64 + expErr error + }{ + "req nil": { + srcQuery: nil, + expErr: status.Error(codes.InvalidArgument, "empty request"), + }, + "query all": { + srcQuery: &types.QueryInactiveContractsRequest{}, + expAddrs: expAddrs, + expPaginationTotal: 3, + }, + "with pagination offset": { + srcQuery: &types.QueryInactiveContractsRequest{ + Pagination: &query.PageRequest{ + Offset: 1, + }, + }, + expAddrs: []string{expAddrs[1], expAddrs[2]}, + expPaginationTotal: 3, + }, + "with invalid pagination key": { + srcQuery: &types.QueryInactiveContractsRequest{ + Pagination: &query.PageRequest{ + Offset: 1, + Key: []byte("test"), + }, + }, + expErr: fmt.Errorf("invalid request, either offset or key is expected, got both"), + }, + "with pagination limit": { + srcQuery: &types.QueryInactiveContractsRequest{ + Pagination: &query.PageRequest{ + Limit: 1, + }, + }, + expAddrs: []string{expAddrs[0]}, + expPaginationTotal: 0, + }, + "with pagination next key": { + srcQuery: &types.QueryInactiveContractsRequest{ + Pagination: &query.PageRequest{ + Key: fromBase64("reSl9YA6Q5g1xjY5Wo1kje5XsvyQ2Y3Bf6iHFZtpY4s="), + }, + }, + expAddrs: []string{expAddrs[1], expAddrs[2]}, + expPaginationTotal: 0, + }, + } + + for msg, spec := range specs { + t.Run(msg, func(t *testing.T) { + got, err := q.InactiveContracts(sdk.WrapSDKContext(ctx), spec.srcQuery) + if spec.expErr != nil { + require.Equal(t, spec.expErr, err, "but got %+v", err) + return + } + require.NoError(t, err) + assert.Equal(t, spec.expAddrs, got.Addresses) + assert.EqualValues(t, spec.expPaginationTotal, got.Pagination.Total) + }) } } @@ -88,3 +156,11 @@ func TestQueryInactiveContract(t *testing.T) { }) } } + +func fromBase64(s string) []byte { + r, err := base64.StdEncoding.DecodeString(s) + if err != nil { + panic(err) + } + return r +} diff --git a/x/wasmplus/keeper/test_common.go b/x/wasmplus/keeper/test_common.go index 5e9a5d44a2..875db8485c 100644 --- a/x/wasmplus/keeper/test_common.go +++ b/x/wasmplus/keeper/test_common.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "os" + "sort" "testing" "time" @@ -688,3 +689,16 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) { addr := sdk.AccAddress(pub.Address()) return key, pub, addr } + +func GenerateSortedBech32Address(addrs ...[]byte) []string { + sort.Slice(addrs, func(i, j int) bool { + return string(addrs[i]) < string(addrs[j]) + }) + + expAddrs := make([]string, len(addrs)) + for i, b := range addrs { + expAddrs[i] = sdk.AccAddress(b).String() + } + + return expAddrs +}