Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gaps in query code list #246

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,16 @@ type ListCodeResponse struct {

func queryCodeList(ctx sdk.Context, keeper Keeper) ([]byte, error) {
var info []ListCodeResponse

var i uint64
for true {
i++
res := keeper.GetCodeInfo(ctx, i)
if res == nil {
break
}
keeper.IterateCodeInfos(ctx, func(i uint64, res types.CodeInfo) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, and the function was already there.

info = append(info, ListCodeResponse{
ID: i,
Creator: res.Creator,
DataHash: res.CodeHash,
Source: res.Source,
Builder: res.Builder,
})
}
return false
})

bz, err := json.MarshalIndent(info, "", " ")
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions x/wasm/internal/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,53 @@ func TestQueryContractHistory(t *testing.T) {
})
}
}

func TestQueryCodeList(t *testing.T) {
wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm")
require.NoError(t, err)

specs := map[string]struct {
codeIDs []uint64
}{
"none": {},
"no gaps": {
codeIDs: []uint64{1, 2, 3},
},
"with gaps": {
codeIDs: []uint64{2, 4, 6},
},
}

for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper

for _, codeID := range spec.codeIDs {
require.NoError(t, keeper.importCode(ctx, codeID,
types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode)),
wasmCode),
)
}
q := NewQuerier(keeper)
// when
query := []string{QueryListCode}
data := abci.RequestQuery{}
resData, err := q(ctx, query, data)

// then
require.NoError(t, err)

var got []map[string]interface{}
err = json.Unmarshal(resData, &got)
require.NoError(t, err)
require.Len(t, got, len(spec.codeIDs))
for i, exp := range spec.codeIDs {
assert.EqualValues(t, exp, got[i]["id"])
}
})
}
}