Skip to content

Commit

Permalink
Support gaps in query code list
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Aug 6, 2020
1 parent de9ebe8 commit 0e6d724
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
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 {
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"])
}
})
}
}

0 comments on commit 0e6d724

Please sign in to comment.