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

Update to cosmwasm 0.11 #272

Merged
merged 10 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd
go 1.14

require (
github.com/CosmWasm/go-cosmwasm v0.10.0
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha1
github.com/cosmos/cosmos-sdk v0.39.1-0.20200727135228-9d00f712e334
github.com/golang/mock v1.4.3 // indirect
github.com/google/gofuzz v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQ
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4=
github.com/CosmWasm/go-cosmwasm v0.10.0 h1:3DBOiGtLllevLgf8PQO5+hRCKKqYEQJIw6cgaZzr1Ag=
github.com/CosmWasm/go-cosmwasm v0.10.0/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc=
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha1 h1:5c87JcnA+ncQlSJO/mEK6z9oIi/oS46CCsYduvRgPvw=
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha1/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
}

// emit all events from this contract itself
events := types.ParseEvents(res.Log, contractAddress)
events := types.ParseEvents(res.Attributes, contractAddress)
ctx.EventManager().EmitEvents(events)

err = k.dispatchMessages(ctx, contractAddress, res.Messages)
Expand Down Expand Up @@ -287,7 +287,7 @@ func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller
}

// emit all events from this contract itself
events := types.ParseEvents(res.Log, contractAddress)
events := types.ParseEvents(res.Attributes, contractAddress)
ctx.EventManager().EmitEvents(events)

err = k.dispatchMessages(ctx, contractAddress, res.Messages)
Expand Down Expand Up @@ -340,7 +340,7 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
}

// emit all events from this contract itself
events := types.ParseEvents(res.Log, contractAddress)
events := types.ParseEvents(res.Attributes, contractAddress)
ctx.EventManager().EmitEvents(events)

historyEntry := contractInfo.AddMigration(ctx, newCodeID, msg)
Expand Down
6 changes: 3 additions & 3 deletions x/wasm/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestInstantiate(t *testing.T) {
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractAddr.String())

gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(0x10c43), gasAfter-gasBefore)
require.Equal(t, uint64(0x10c5a), gasAfter-gasBefore)

// ensure it is stored properly
info := keeper.GetContractInfo(ctx, contractAddr)
Expand Down Expand Up @@ -521,7 +521,7 @@ func TestExecute(t *testing.T) {
trialCtx := ctx.WithMultiStore(ctx.MultiStore().CacheWrap().(sdk.MultiStore))
res, err := keeper.Execute(trialCtx, addr, creator, []byte(`{"release":{}}`), nil)
require.Error(t, err)
require.Contains(t, err.Error(), "unauthorized")
require.Contains(t, err.Error(), "Unauthorized")

// verifier can execute, and get proper gas amount
start := time.Now()
Expand All @@ -534,7 +534,7 @@ func TestExecute(t *testing.T) {

// make sure gas is properly deducted from ctx
gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(0x11617), gasAfter-gasBefore)
require.Equal(t, uint64(0x1162c), gasAfter-gasBefore)

// ensure bob now exists and got both payments released
bobAcct = accKeeper.GetAccount(ctx, bob)
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/query_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func sdkToFullDelegation(ctx sdk.Context, keeper staking.Keeper, delegation stak
Validator: delegation.ValidatorAddress.String(),
Amount: convertSdkCoinToWasmCoin(amount),
// TODO: AccumulatedRewards
AccumulatedRewards: wasmTypes.NewCoin(0, bondDenom),
AccumulatedRewards: wasmTypes.Coins{},
// TODO: Determine redelegate
CanRedelegate: wasmTypes.NewCoin(0, bondDenom),
}, nil
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/internal/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.Acc

func TestGasCostOnQuery(t *testing.T) {
const (
GasNoWork uint64 = InstanceCost + 2_756
GasNoWork uint64 = InstanceCost + 2_757
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork50 uint64 = InstanceCost + 8_464 // this is a little shy of 50k gas - to keep an eye on the limit
GasWork50 uint64 = InstanceCost + 8_465 // this is a little shy of 50k gas - to keep an eye on the limit

GasReturnUnhashed uint64 = 647
GasReturnHashed uint64 = 597
GasReturnUnhashed uint64 = 645
GasReturnHashed uint64 = 595
)

cases := map[string]struct {
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork2k uint64 = InstanceCost + 233_379 // we have 6x gas used in cpu than in the instance
// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 603
GasReturnHashed uint64 = 602
)

cases := map[string]struct {
Expand Down
37 changes: 26 additions & 11 deletions x/wasm/internal/keeper/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package keeper

import (
"encoding/json"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"io/ioutil"
"os"
"strings"
Expand All @@ -13,8 +11,10 @@ import (
"github.com/stretchr/testify/require"

wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)
Expand All @@ -37,8 +37,9 @@ type reflectPayload struct {

// MaskQueryMsg is used to encode query messages
type MaskQueryMsg struct {
Owner *struct{} `json:"owner,omitempty"`
ReflectCustom *Text `json:"reflect_custom,omitempty"`
Owner *struct{} `json:"owner,omitempty"`
Capitalized *Text `json:"capitalized,omitempty""`
ethanfrey marked this conversation as resolved.
Show resolved Hide resolved
Chain *wasmTypes.QueryRequest `json:"chain,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

q: were is this used?

Copy link
Member Author

Choose a reason for hiding this comment

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

These 3 are defined in the reflect class. I added the here thinking we may add more tests for them, since I was translating types from rust to go anyway.

}

type Text struct {
Expand Down Expand Up @@ -269,18 +270,18 @@ func TestMaskReflectCustomQuery(t *testing.T) {

// and now making use of the custom querier callbacks
customQuery := MaskQueryMsg{
ReflectCustom: &Text{
Capitalized: &Text{
Text: "all Caps noW",
},
}
customQueryBz, err := json.Marshal(customQuery)
require.NoError(t, err)
custom, err := keeper.QuerySmart(ctx, contractAddr, customQueryBz)
require.NoError(t, err)
var resp customQueryResponse
var resp capitalizedResponse
err = json.Unmarshal(custom, &resp)
require.NoError(t, err)
assert.Equal(t, resp.Msg, "ALL CAPS NOW")
assert.Equal(t, resp.Text, "ALL CAPS NOW")
}

func checkAccount(t *testing.T, ctx sdk.Context, accKeeper auth.AccountKeeper, addr sdk.AccAddress, expected sdk.Coins) {
Expand Down Expand Up @@ -353,14 +354,28 @@ func fromMaskRawMsg(cdc *codec.Codec) CustomEncoder {
}

type maskCustomQuery struct {
Ping *struct{} `json:"ping,omitempty"`
Capital *Text `json:"capital,omitempty"`
Ping *struct{} `json:"ping,omitempty"`
Capitalized *Text `json:"capitalized,omitempty"`
}

// this is from the go code back to the contract (capitalized or ping)
type customQueryResponse struct {
Msg string `json:"msg"`
}

// these are the return values from contract -> go depending on type of query
type ownerResponse struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

q: is this used?

Owner string `json:"owner"`
}

type capitalizedResponse struct {
Text string `json:"text"`
}

type chainResponse struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

q: is this used?

Data []byte `json:"data"`
}

// maskPlugins needs to be registered in test setup to handle custom query callbacks
func maskPlugins() *QueryPlugins {
return &QueryPlugins{
Expand All @@ -374,8 +389,8 @@ func performCustomQuery(_ sdk.Context, request json.RawMessage) ([]byte, error)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if custom.Capital != nil {
msg := strings.ToUpper(custom.Capital.Text)
if custom.Capitalized != nil {
msg := strings.ToUpper(custom.Capitalized.Text)
return json.Marshal(customQueryResponse{Msg: msg})
}
if custom.Ping != nil {
Expand Down
Binary file modified x/wasm/internal/keeper/testdata/burner.wasm
Binary file not shown.
Binary file modified x/wasm/internal/keeper/testdata/contract.wasm
Binary file not shown.
Binary file modified x/wasm/internal/keeper/testdata/contract.wasm.gzip
Binary file not shown.
Binary file modified x/wasm/internal/keeper/testdata/reflect.wasm
Binary file not shown.
Binary file modified x/wasm/internal/keeper/testdata/staking.wasm
Binary file not shown.
16 changes: 11 additions & 5 deletions x/wasm/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,20 @@ func NewEnv(ctx sdk.Context, creator sdk.AccAddress, deposit sdk.Coins, contract
if ctx.BlockHeight() < 0 {
panic("Block height must never be negative")
}
if ctx.BlockTime().Unix() < 0 {
sec := ctx.BlockTime().Unix()
if sec < 0 {
panic("Block (unix) time must never be negative ")
}
nano := ctx.BlockTime().UnixNano() - sec*1e9
ethanfrey marked this conversation as resolved.
Show resolved Hide resolved
if nano < 0 {
panic("Block (unix) nanoseconds must never be negative ")
}
env := wasmTypes.Env{
Block: wasmTypes.BlockInfo{
Height: uint64(ctx.BlockHeight()),
Time: uint64(ctx.BlockTime().Unix()),
ChainID: ctx.ChainID(),
Height: uint64(ctx.BlockHeight()),
Time: uint64(sec),
TimeNanos: uint64(nano),
ChainID: ctx.ChainID(),
},
Message: wasmTypes.MessageInfo{
Sender: creator.String(),
Expand Down Expand Up @@ -229,7 +235,7 @@ const CustomEventType = "wasm"
const AttributeKeyContractAddr = "contract_address"

// ParseEvents converts wasm LogAttributes into an sdk.Events (with 0 or 1 elements)
func ParseEvents(logs []wasmTypes.LogAttribute, contractAddr sdk.AccAddress) sdk.Events {
func ParseEvents(logs []wasmTypes.EventAttribute, contractAddr sdk.AccAddress) sdk.Events {
if len(logs) == 0 {
return nil
}
Expand Down