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

feat: add inactive contract management in wasm #655

Merged
merged 18 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44157,6 +44157,54 @@ paths:
format: boolean
tags:
- Query
'/lbm/wasm/v1/inactive_contracts/{address}':
get:
operationId: InactiveContract
responses:
'200':
description: A successful response.
schema:
type: object
properties:
inactivated:
type: boolean
format: boolean
title: >-
inactivated is the result if the contract is inactive contract
or not
title: >-
QueryInactiveContractsResponse is the response type for the
Query/IsInactiveContract RPC method
default:
description: An unexpected error response
schema:
type: object
properties:
error:
type: string
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
type_url:
type: string
value:
type: string
format: byte
parameters:
- name: address
description: address is the address of the contract
in: path
required: true
type: string
tags:
- Query
securityDefinitions:
kms:
type: basic
Expand Down Expand Up @@ -70472,6 +70520,16 @@ definitions:
description: >-
QueryAllowancesResponse is the response type for the Query/Allowances RPC
method.
lbm.wasm.v1.QueryInactiveContractResponse:
type: object
properties:
inactivated:
type: boolean
format: boolean
title: inactivated is the result if the contract is inactive contract or not
title: >-
QueryInactiveContractsResponse is the response type for the
Query/IsInactiveContract RPC method
lbm.wasm.v1.QueryInactiveContractsResponse:
type: object
properties:
Expand Down
37 changes: 35 additions & 2 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,8 @@
- [DeactivateContractProposal](#lbm.wasm.v1.DeactivateContractProposal)

- [lbm/wasm/v1/query.proto](#lbm/wasm/v1/query.proto)
- [QueryInactiveContractRequest](#lbm.wasm.v1.QueryInactiveContractRequest)
- [QueryInactiveContractResponse](#lbm.wasm.v1.QueryInactiveContractResponse)
- [QueryInactiveContractsRequest](#lbm.wasm.v1.QueryInactiveContractsRequest)
- [QueryInactiveContractsResponse](#lbm.wasm.v1.QueryInactiveContractsResponse)

Expand Down Expand Up @@ -18537,7 +18539,7 @@ EventActivateContractProposal is the event that is emitted when the contract is

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `contractAddress` | [string](#string) | | contractAddress is the smart contract's address |
| `contract` | [string](#string) | | contract is the smart contract's address |



Expand All @@ -18552,7 +18554,7 @@ EventDeactivateContractProposal is the event that is emitted when the contract i

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `contractAddress` | [string](#string) | | contractAddress is the smart contract's address |
| `contract` | [string](#string) | | contract is the smart contract's address |



Expand Down Expand Up @@ -18625,6 +18627,36 @@ DeactivateContractProposal gov proposal content type adds a contract to inactive



<a name="lbm.wasm.v1.QueryInactiveContractRequest"></a>

### QueryInactiveContractRequest
QueryIsInactiveContractRequest is the request type for Query/IsInactiveContract RPC method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | address is the address of the contract |






<a name="lbm.wasm.v1.QueryInactiveContractResponse"></a>

### QueryInactiveContractResponse
QueryInactiveContractsResponse is the response type for the Query/IsInactiveContract RPC method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `inactivated` | [bool](#bool) | | inactivated is the result if the contract is inactive contract or not |






<a name="lbm.wasm.v1.QueryInactiveContractsRequest"></a>

### QueryInactiveContractsRequest
Expand Down Expand Up @@ -18670,6 +18702,7 @@ Query defines the gRPC querier service.
| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `InactiveContracts` | [QueryInactiveContractsRequest](#lbm.wasm.v1.QueryInactiveContractsRequest) | [QueryInactiveContractsResponse](#lbm.wasm.v1.QueryInactiveContractsResponse) | InactiveContracts queries all inactive contracts. | GET|/lbm/wasm/v1/inactive_contracts|
| `InactiveContract` | [QueryInactiveContractRequest](#lbm.wasm.v1.QueryInactiveContractRequest) | [QueryInactiveContractResponse](#lbm.wasm.v1.QueryInactiveContractResponse) | | GET|/lbm/wasm/v1/inactive_contracts/{address}|

<!-- end services -->

Expand Down
2 changes: 1 addition & 1 deletion proto/cosmwasm/wasm/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ message Params {
AccessConfig code_upload_access = 1
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"code_upload_access\""];
AccessType instantiate_default_permission = 2 [(gogoproto.moretags) = "yaml:\"instantiate_default_permission\""];
uint64 gas_multiplier = 3 [(gogoproto.moretags) = "yaml:\"max_gas\""];
uint64 gas_multiplier = 3 [(gogoproto.moretags) = "yaml:\"gas_multiplier\""];
uint64 instance_cost = 4 [(gogoproto.moretags) = "yaml:\"instance_cost\""];
uint64 compile_cost = 5 [(gogoproto.moretags) = "yaml:\"compile_cost\""];
}
Expand Down
8 changes: 4 additions & 4 deletions proto/lbm/wasm/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ option go_package = "github.com/line/lbm-sdk/x/wasm/lbmtypes";

// EventDeactivateContractProposal is the event that is emitted when the contract is deactivate
loloicci marked this conversation as resolved.
Show resolved Hide resolved
message EventDeactivateContractProposal {
// contractAddress is the smart contract's address
string contractAddress = 1;
// contract is the smart contract's address
string contract = 1;
}

// EventActivateContractProposal is the event that is emitted when the contract is activates.
message EventActivateContractProposal {
// contractAddress is the smart contract's address
string contractAddress = 1;
// contract is the smart contract's address
string contract = 1;
}
16 changes: 16 additions & 0 deletions proto/lbm/wasm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ service Query {
rpc InactiveContracts(QueryInactiveContractsRequest) returns (QueryInactiveContractsResponse) {
0Tech marked this conversation as resolved.
Show resolved Hide resolved
option (google.api.http).get = "/lbm/wasm/v1/inactive_contracts";
};

rpc InactiveContract(QueryInactiveContractRequest) returns (QueryInactiveContractResponse) {
option (google.api.http).get = "/lbm/wasm/v1/inactive_contracts/{address}";
}
}

// QueryInactiveContractsRequest is the request type for Query/InactiveContract RPC method
Expand All @@ -31,3 +35,15 @@ message QueryInactiveContractsResponse {
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryIsInactiveContractRequest is the request type for Query/IsInactiveContract RPC method
message QueryInactiveContractRequest {
// address is the address of the contract
string address = 1;
}

// QueryInactiveContractsResponse is the response type for the Query/IsInactiveContract RPC method
message QueryInactiveContractResponse {
// inactivated is the result if the contract is inactive contract or not
bool inactivated = 1;
}
35 changes: 32 additions & 3 deletions x/wasm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func GetQueryCmd() *cobra.Command {
GetCmdGetContractState(),
GetCmdListPinnedCode(),
GetCmdLibVersion(),
GetCmdListInactiveContract(),
GetCmdListInactiveContracts(),
GetCmdIsInactiveContract(),
)
return queryCmd
}
Expand Down Expand Up @@ -540,9 +541,9 @@ func withPageKeyDecoded(flagSet *flag.FlagSet) *flag.FlagSet {
return flagSet
}

func GetCmdListInactiveContract() *cobra.Command {
func GetCmdListInactiveContracts() *cobra.Command {
cmd := &cobra.Command{
Use: "inactive-contract",
Use: "inactive-contracts",
Long: "List all inactive contracts",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -572,3 +573,31 @@ func GetCmdListInactiveContract() *cobra.Command {
flags.AddPaginationFlagsToCmd(cmd, "list of inactive contracts")
return cmd
}

func GetCmdIsInactiveContract() *cobra.Command {
cmd := &cobra.Command{
Use: "is-inactive [bech32_address]",
Long: "check if inactive contract or not",
zemyblue marked this conversation as resolved.
Show resolved Hide resolved
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := lbmtypes.NewQueryClient(clientCtx)
res, err := queryClient.InactiveContract(
context.Background(),
&lbmtypes.QueryInactiveContractRequest{
Address: args[0],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
32 changes: 32 additions & 0 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}", queryContractStateSmartHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET")
r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}", queryContractStateRawHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET")
r.HandleFunc("/wasm/inactive_contract", listInactiveContractsHandlerFn(cliCtx)).Queries("GET")
r.HandleFunc("/wasm/inactive_contract/{contractAddr}", queryInactiveContractHandlerFn(cliCtx)).Queries("GET")
}

func listCodesHandlerFn(cliCtx client.Context) http.HandlerFunc {
Expand Down Expand Up @@ -424,3 +425,34 @@ func listInactiveContractsHandlerFn(cliCtx client.Context) http.HandlerFunc {
rest.PostProcessResponse(w, cliCtx, json.RawMessage(res))
}
}

func queryInactiveContractHandlerFn(cliCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryIsInactiveContract)
addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

data := lbmtypes.QueryInactiveContractRequest{Address: addr.String()}
bs, err := cliCtx.LegacyAmino.MarshalJSON(data)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := cliCtx.QueryWithData(route, bs)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, json.RawMessage(res))
}
}
58 changes: 56 additions & 2 deletions x/wasm/client/testutil/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ func (s *IntegrationTestSuite) TestGetCmdListPinnedCode() {
s.Require().Equal(expcted, &contractInfo)
}

func (s *IntegrationTestSuite) TestGetCmdListInactiveContract() {
func (s *IntegrationTestSuite) TestGetCmdListInactiveContracts() {
val := s.network.Validators[0]

cmd := cli.GetCmdListInactiveContract()
cmd := cli.GetCmdListInactiveContracts()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, s.queryCommonArgs())
s.Require().NoError(err)

Expand All @@ -482,3 +482,57 @@ func (s *IntegrationTestSuite) TestGetCmdListInactiveContract() {
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &resInfo), out.String())
s.Require().Equal(expected, &resInfo)
}

func (s *IntegrationTestSuite) TestGetCmdIsInactiveContract() {
val := s.network.Validators[0]

testCases := map[string]struct {
args []string
valid bool
expected proto.Message
}{
"valid query(inactivate)": {
[]string{
s.inactiveContractAddress,
},
true,
&lbmtypes.QueryInactiveContractResponse{
Inactivated: true,
},
},
"valid query(activate)": {
[]string{
"link1hmayw7vv0p3gzeh3jzwmw9xj8fy8a3kmpqgjrysljdnecqkps02qrq5rvm",
},
true,
&lbmtypes.QueryInactiveContractResponse{
Inactivated: false,
},
},
"wrong bech32_address": {
[]string{
"xxx",
},
false,
nil,
},
}

for name, tc := range testCases {
tc := tc

s.Run(name, func() {
cmd := cli.GetCmdIsInactiveContract()
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, s.queryCommonArgs()...))
if !tc.valid {
s.Require().Error(err)
return
}
s.Require().NoError(err)

var resInfo lbmtypes.QueryInactiveContractResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &resInfo), out.String())
s.Require().Equal(tc.expected, &resInfo)
})
}
}
3 changes: 0 additions & 3 deletions x/wasm/keeper/keeper_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func (k Keeper) activateContract(ctx sdk.Context, contractAddress sdk.AccAddress
if !k.IsInactiveContract(ctx, contractAddress) {
return sdkerrors.Wrapf(wasmtypes.ErrNotFound, "no inactivate contract %s", contractAddress.String())
}
if !k.HasContractInfo(ctx, contractAddress) {
return sdkerrors.Wrapf(wasmtypes.ErrInvalid, "no contract %s", contractAddress)
}

k.deleteInactiveContract(ctx, contractAddress)
k.bank.DeleteFromInactiveAddr(ctx, contractAddress)
Expand Down
Loading