Skip to content

Commit

Permalink
feat(wasm): porting our modifications after 0.12.0-0.1.0 (#139)
Browse files Browse the repository at this point in the history
* feat(wasm): add pagination for rest api

The original is the PR #85.
cherry-pick from v1/develop by Jiyong Ha<[email protected]>

* feat(wasm): make hardcoded params to gov params

The original is the PR #86.
with excessive gas consumption issue needs further modification.

cherry-pick from v1/develop by Jiyong Ha<[email protected]>

* test(wasm): adjust gas by gov params

Limit values are greatly eased due to the known issue of gas consumption.

* chore: go fmt

* chore: fix typo

Co-authored-by: shiki <[email protected]>
  • Loading branch information
brew0722 and shiki-tak authored Apr 29, 2021
1 parent c612d35 commit 2a9f3c7
Show file tree
Hide file tree
Showing 26 changed files with 605 additions and 225 deletions.
6 changes: 2 additions & 4 deletions client/docs/statik/statik.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func ReadPageRequest(flagSet *pflag.FlagSet) (*query.PageRequest, error) {
countTotal, _ := flagSet.GetBool(flags.FlagCountTotal)
page, _ := flagSet.GetUint64(flags.FlagPage)

return NewPageRequest(pageKey, offset, limit, page, countTotal)
}

func NewPageRequest(pageKey string, offset, limit, page uint64, countTotal bool) (*query.PageRequest, error) {
if page > 1 && offset > 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "page and offset cannot be used together")
}
Expand Down
4 changes: 2 additions & 2 deletions x/staking/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ func (s *IntegrationTestSuite) TestNewCmdRedelegate() {
{
"with wrong source validator address",
[]string{
`linkvaloper120yvjfy7m2gnu9mvusrs40cxxhpt8nr3jr36d2`, // src-validator-addr
`linkvaloper120yvjfy7m2gnu9mvusrs40cxxhpt8nr3jr36d2`, // src-validator-addr
val2.ValAddress.String(), // dst-validator-addr
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(150)).String(), // amount
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
Expand All @@ -1156,7 +1156,7 @@ func (s *IntegrationTestSuite) TestNewCmdRedelegate() {
"with wrong destination validator address",
[]string{
val.ValAddress.String(), // dst-validator-addr
`linkvaloper120yvjfy7m2gnu9mvusrs40cxxhpt8nr3jr36d2`, // src-validator-addr
`linkvaloper120yvjfy7m2gnu9mvusrs40cxxhpt8nr3jr36d2`, // src-validator-addr
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(150)).String(), // amount
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
Expand Down
1 change: 0 additions & 1 deletion x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const (
ProposalTypeMigrateContract = types.ProposalTypeMigrateContract
ProposalTypeUpdateAdmin = types.ProposalTypeUpdateAdmin
ProposalTypeClearAdmin = types.ProposalTypeClearAdmin
GasMultiplier = keeper.GasMultiplier
MaxGas = keeper.MaxGas
QueryListContractByCode = keeper.QueryListContractByCode
QueryGetContract = keeper.QueryGetContract
Expand Down
121 changes: 118 additions & 3 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -35,7 +36,28 @@ func listCodesHandlerFn(cliCtx client.Context) http.HandlerFunc {
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryListCode)
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := parseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := client.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

data := &types.QueryCodesRequest{
Pagination: pageReq,
}
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
Expand Down Expand Up @@ -87,7 +109,29 @@ func listContractsByCodeHandlerFn(cliCtx client.Context) http.HandlerFunc {
}

route := fmt.Sprintf("custom/%s/%s/%d", types.QuerierRoute, keeper.QueryListContractByCode, codeID)
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := parseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := client.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

data := &types.QueryContractsByCodeRequest{
CodeId: codeID,
Pagination: pageReq,
}
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
Expand Down Expand Up @@ -237,7 +281,29 @@ func queryContractHistoryFn(cliCtx client.Context) http.HandlerFunc {
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := parseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := client.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

data := &types.QueryContractHistoryRequest{
Address: addr.String(),
Pagination: pageReq,
}
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
Expand Down Expand Up @@ -267,3 +333,52 @@ func (a *argumentDecoder) DecodeString(s string) ([]byte, error) {
return a.dec(s)
}
}

func parseHTTPArgs(r *http.Request) (pageKey string, offset, limit, page uint64, countTotal bool, err error) {
pageKey = r.FormValue("page-key")

offsetStr := r.FormValue("offset")
if offsetStr != "" {
offset, err = strconv.ParseUint(offsetStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
if offset <= 0 {
return pageKey, offset, limit, page, countTotal, errors.New("offset must greater than 0")
}
}

pageStr := r.FormValue("page")
if pageStr == "" {
page = rest.DefaultPage
} else {
page, err = strconv.ParseUint(pageStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if page <= 0 {
return pageKey, offset, limit, page, countTotal, errors.New("page must greater than 0")
}
}

limitStr := r.FormValue("limit")
if limitStr == "" {
limit = rest.DefaultLimit
} else {
limit, err = strconv.ParseUint(limitStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if limit <= 0 {
return pageKey, offset, limit, page, countTotal, errors.New("limit must greater than 0")
}
}

countTotalStr := r.FormValue("count-total")
if countTotalStr != "" {
countTotal, err = strconv.ParseBool(countTotalStr)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
}

return pageKey, offset, limit, page, countTotal, nil
}
30 changes: 18 additions & 12 deletions x/wasm/internal/keeper/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ import (
sdk "github.com/line/lbm-sdk/v2/types"
)

var (
CostHumanize = 5 * GasMultiplier
CostCanonical = 4 * GasMultiplier
)
type cosmwasmAPIImpl struct {
gasMultiplier uint64
}

func humanAddress(canon []byte) (string, uint64, error) {
func (a cosmwasmAPIImpl) humanAddress(canon []byte) (string, uint64, error) {
gas := 5 * a.gasMultiplier
if len(canon) != sdk.AddrLen {
//nolint:stylecheck
return "", CostHumanize, fmt.Errorf("Expected %d byte address", sdk.AddrLen)
return "", gas, fmt.Errorf("expected %d byte address", sdk.AddrLen)
}
return sdk.AccAddress(canon).String(), CostHumanize, nil

return sdk.AccAddress(canon).String(), gas, nil
}

func canonicalAddress(human string) ([]byte, uint64, error) {
func (a cosmwasmAPIImpl) canonicalAddress(human string) ([]byte, uint64, error) {
bz, err := sdk.AccAddressFromBech32(human)
return bz, CostCanonical, err
return bz, 4 * a.gasMultiplier, err
}

var cosmwasmAPI = wasmvm.GoAPI{
HumanAddress: humanAddress,
CanonicalAddress: canonicalAddress,
func (k Keeper) cosmwasmAPI(ctx sdk.Context) wasmvm.GoAPI {
x := cosmwasmAPIImpl{
gasMultiplier: k.getGasMultiplier(ctx),
}
return wasmvm.GoAPI{
HumanAddress: x.humanAddress,
CanonicalAddress: x.canonicalAddress,
}
}
6 changes: 3 additions & 3 deletions x/wasm/internal/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package keeper

import (
"github.com/line/lbm-sdk/v2/x/wasm/internal/types"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb/opt"
dbm "github.com/line/tm-db/v2"
"github.com/line/tm-db/v2/memdb"
"github.com/line/tm-db/v2/goleveldb"
"github.com/line/tm-db/v2/memdb"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb/opt"
"testing"
)

Expand Down
5 changes: 4 additions & 1 deletion x/wasm/internal/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ func TestImportContractWithCodeHistoryReset(t *testing.T) {
"permission": "Everybody"
},
"instantiate_default_permission": "Everybody",
"max_wasm_code_size": 500000
"max_wasm_code_size": 500000,
"gas_multiplier": 100,
"instance_cost": 40000,
"compile_cost": 2
},
"codes": [
{
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/internal/keeper/handler_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package keeper

import (
"encoding/json"
"github.com/line/lbm-sdk/v2/x/wasm/internal/keeper/wasmtesting"
"github.com/golang/protobuf/proto"
codectypes "github.com/line/lbm-sdk/v2/codec/types"
capabilitytypes "github.com/line/lbm-sdk/v2/x/capability/types"
govtypes "github.com/line/lbm-sdk/v2/x/gov/types"
ibctransfertypes "github.com/line/lbm-sdk/v2/x/ibc/applications/transfer/types"
clienttypes "github.com/line/lbm-sdk/v2/x/ibc/core/02-client/types"
channeltypes "github.com/line/lbm-sdk/v2/x/ibc/core/04-channel/types"
ibcexported "github.com/line/lbm-sdk/v2/x/ibc/core/exported"
"github.com/golang/protobuf/proto"
"github.com/line/lbm-sdk/v2/x/wasm/internal/keeper/wasmtesting"
"testing"

"github.com/line/lbm-sdk/v2/x/wasm/internal/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/line/lbm-sdk/v2/types"
banktypes "github.com/line/lbm-sdk/v2/x/bank/types"
distributiontypes "github.com/line/lbm-sdk/v2/x/distribution/types"
stakingtypes "github.com/line/lbm-sdk/v2/x/staking/types"
"github.com/line/lbm-sdk/v2/x/wasm/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
Loading

0 comments on commit 2a9f3c7

Please sign in to comment.