Skip to content

Commit

Permalink
Add instantiate permission to CodeInfoResponse (#836)
Browse files Browse the repository at this point in the history
* Add instantiate permission to CodeInfoResponse

* add query code info test

* add codes query response test
  • Loading branch information
jhernandezb authored May 5, 2022
1 parent bef8a31 commit cd3c9dd
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 94 deletions.
1 change: 1 addition & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ CodeInfoResponse contains code meta data from CodeInfo
| `code_id` | [uint64](#uint64) | | id for legacy support |
| `creator` | [string](#string) | | |
| `data_hash` | [bytes](#bytes) | | |
| `instantiate_permission` | [AccessConfig](#cosmwasm.wasm.v1.AccessConfig) | | |



Expand Down
1 change: 1 addition & 0 deletions proto/cosmwasm/wasm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ message CodeInfoResponse {
"github.com/tendermint/tendermint/libs/bytes.HexBytes" ];
// Used in v1beta1
reserved 4, 5;
AccessConfig instantiate_permission = 6 [ (gogoproto.nullable) = false ];
}

// QueryCodeResponse is the response type for the Query/Code RPC method
Expand Down
7 changes: 4 additions & 3 deletions x/wasm/keeper/legacy_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ func queryCodeList(ctx sdk.Context, keeper types.ViewKeeper) ([]types.CodeInfoRe
var info []types.CodeInfoResponse
keeper.IterateCodeInfos(ctx, func(i uint64, res types.CodeInfo) bool {
info = append(info, types.CodeInfoResponse{
CodeID: i,
Creator: res.Creator,
DataHash: res.CodeHash,
CodeID: i,
Creator: res.Creator,
DataHash: res.CodeHash,
InstantiatePermission: res.InstantiateConfig,
})
return false
})
Expand Down
14 changes: 8 additions & 6 deletions x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,10 @@ func (q grpcQuerier) Codes(c context.Context, req *types.QueryCodesRequest) (*ty
return false, err
}
r = append(r, types.CodeInfoResponse{
CodeID: binary.BigEndian.Uint64(key),
Creator: c.Creator,
DataHash: c.CodeHash,
CodeID: binary.BigEndian.Uint64(key),
Creator: c.Creator,
DataHash: c.CodeHash,
InstantiatePermission: c.InstantiateConfig,
})
}
return true, nil
Expand Down Expand Up @@ -275,9 +276,10 @@ func queryCode(ctx sdk.Context, codeID uint64, keeper types.ViewKeeper) (*types.
return nil, nil
}
info := types.CodeInfoResponse{
CodeID: codeID,
Creator: res.Creator,
DataHash: res.CodeHash,
CodeID: codeID,
Creator: res.Creator,
DataHash: res.CodeHash,
InstantiatePermission: res.InstantiateConfig,
}

code, err := keeper.GetByteCode(ctx, codeID)
Expand Down
122 changes: 122 additions & 0 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,128 @@ func TestQueryPinnedCodes(t *testing.T) {
}
}

func TestQueryCodeInfo(t *testing.T) {

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

ctx, keepers := CreateTestInput(t, false, SupportedFeatures)
keeper := keepers.WasmKeeper

anyAddress, err := sdk.AccAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz")
require.NoError(t, err)
specs := map[string]struct {
codeId uint64
accessConfig types.AccessConfig
}{
"everybody": {
codeId: 1,
accessConfig: types.AllowEverybody,
},
"nobody": {
codeId: 10,
accessConfig: types.AllowNobody,
},
"with_address": {
codeId: 20,
accessConfig: types.AccessTypeOnlyAddress.With(anyAddress),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode))
codeInfo.InstantiateConfig = spec.accessConfig
require.NoError(t, keeper.importCode(ctx, spec.codeId,
codeInfo,
wasmCode),
)

q := Querier(keeper)
got, err := q.Code(sdk.WrapSDKContext(ctx), &types.QueryCodeRequest{
CodeId: spec.codeId,
})
require.NoError(t, err)
expectedResponse := &types.QueryCodeResponse{
CodeInfoResponse: &types.CodeInfoResponse{
CodeID: spec.codeId,
Creator: codeInfo.Creator,
DataHash: codeInfo.CodeHash,
InstantiatePermission: spec.accessConfig,
},
Data: wasmCode,
}
require.NotNil(t, got.CodeInfoResponse)
require.EqualValues(t, expectedResponse, got)
})
}
}

func TestQueryCodeInfoList(t *testing.T) {

wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)

ctx, keepers := CreateTestInput(t, false, SupportedFeatures)
keeper := keepers.WasmKeeper

anyAddress, err := sdk.AccAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz")
require.NoError(t, err)
codeInfoWithConfig := func(accessConfig types.AccessConfig) types.CodeInfo {
codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode))
codeInfo.InstantiateConfig = accessConfig
return codeInfo
}

codes := []struct {
name string
codeId uint64
codeInfo types.CodeInfo
}{
{
name: "everybody",
codeId: 1,
codeInfo: codeInfoWithConfig(types.AllowEverybody),
},
{
codeId: 10,
name: "nobody",
codeInfo: codeInfoWithConfig(types.AllowNobody),
},
{
name: "with_address",
codeId: 20,
codeInfo: codeInfoWithConfig(types.AccessTypeOnlyAddress.With(anyAddress)),
},
}

allCodesResponse := make([]types.CodeInfoResponse, 0)
for _, code := range codes {
t.Run(fmt.Sprintf("import_%s", code.name), func(t *testing.T) {
require.NoError(t, keeper.importCode(ctx, code.codeId,
code.codeInfo,
wasmCode),
)
})

allCodesResponse = append(allCodesResponse, types.CodeInfoResponse{
CodeID: code.codeId,
Creator: code.codeInfo.Creator,
DataHash: code.codeInfo.CodeHash,
InstantiatePermission: code.codeInfo.InstantiateConfig,
})
}
q := Querier(keeper)
got, err := q.Codes(sdk.WrapSDKContext(ctx), &types.QueryCodesRequest{
Pagination: &query.PageRequest{
Limit: 3,
},
})
require.NoError(t, err)
require.Len(t, got.CodeInfos, 3)
require.EqualValues(t, allCodesResponse, got.CodeInfos)

}

func fromBase64(s string) []byte {
r, err := base64.StdEncoding.DecodeString(s)
if err != nil {
Expand Down
Loading

0 comments on commit cd3c9dd

Please sign in to comment.