diff --git a/go.mod b/go.mod index 7b269b2da8..ac45fac9e2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd go 1.15 require ( - github.com/CosmWasm/wasmvm v0.14.0-beta3 + github.com/CosmWasm/wasmvm v0.14.0-beta4 github.com/cosmos/cosmos-sdk v0.42.4 github.com/cosmos/iavl v0.15.3 github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b @@ -14,7 +14,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/pkg/errors v0.9.1 github.com/rakyll/statik v0.1.7 - github.com/regen-network/cosmos-proto v0.3.1 // indirect + github.com/regen-network/cosmos-proto v0.3.1 github.com/rs/zerolog v1.21.0 github.com/snikch/goodman v0.0.0-20171125024755-10e37e294daa github.com/spf13/cast v1.3.1 @@ -23,12 +23,12 @@ require ( github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.7.0 github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca - github.com/tendermint/tendermint v0.34.9 + github.com/tendermint/tendermint v0.34.10 github.com/tendermint/tm-db v0.6.4 golang.org/x/net v0.0.0-20201209123823-ac852fbbde11 // indirect golang.org/x/text v0.3.4 // indirect google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f - google.golang.org/grpc v1.36.0 + google.golang.org/grpc v1.37.0 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 58cff33596..f7ef9c4e6a 100644 --- a/go.sum +++ b/go.sum @@ -17,8 +17,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= -github.com/CosmWasm/wasmvm v0.14.0-beta3 h1:HN1+HrC2kgO/V4voGdOWrN1sdUoTnSoLuSrBXbDVnbY= -github.com/CosmWasm/wasmvm v0.14.0-beta3/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= +github.com/CosmWasm/wasmvm v0.14.0-beta4 h1:H8zVI/zRc+GAO+dk9jgGlo+5w0AENDJi6KN8Phvd2O0= +github.com/CosmWasm/wasmvm v0.14.0-beta4/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= @@ -570,6 +570,8 @@ github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX github.com/tendermint/tendermint v0.34.0/go.mod h1:Aj3PIipBFSNO21r+Lq3TtzQ+uKESxkbA3yo/INM4QwQ= github.com/tendermint/tendermint v0.34.9 h1:9P2MXDEPOcPW0NBcHQ/HDSfvczZm+q5nUUw7AZ6f1Vc= github.com/tendermint/tendermint v0.34.9/go.mod h1:kl4Z1JwGx1I+u1SXIzMDy7Z3T8LiMeCAOnzNn6AIMT4= +github.com/tendermint/tendermint v0.34.10 h1:wBOc/It8sh/pVH9np2V5fBvRmIyFN/bUrGPx+eAHexs= +github.com/tendermint/tendermint v0.34.10/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0= github.com/tendermint/tm-db v0.5.1/go.mod h1:g92zWjHpCYlEvQXvy9M168Su8V1IBEeawpXVVBaK4f4= github.com/tendermint/tm-db v0.6.2/go.mod h1:GYtQ67SUvATOcoY8/+x6ylk8Qo02BQyLrAs+yAcLvGI= github.com/tendermint/tm-db v0.6.3/go.mod h1:lfA1dL9/Y/Y8wwyPp2NMLyn5P5Ptr/gvDFNWtrCWSf8= diff --git a/x/wasm/keeper/handler_plugin_encoders.go b/x/wasm/keeper/handler_plugin_encoders.go index 1381026518..c79e560002 100644 --- a/x/wasm/keeper/handler_plugin_encoders.go +++ b/x/wasm/keeper/handler_plugin_encoders.go @@ -18,28 +18,31 @@ import ( type BankEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error) type CustomEncoder func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) +type DistributionEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error) type StakingEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error) type StargateEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.StargateMsg) ([]sdk.Msg, error) type WasmEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error) type IBCEncoder func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error) type MessageEncoders struct { - Bank func(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error) - Custom func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) - IBC func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error) - Staking func(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error) - Stargate func(sender sdk.AccAddress, msg *wasmvmtypes.StargateMsg) ([]sdk.Msg, error) - Wasm func(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error) + Bank func(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error) + Custom func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) + Distribution func(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error) + IBC func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error) + Staking func(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error) + Stargate func(sender sdk.AccAddress, msg *wasmvmtypes.StargateMsg) ([]sdk.Msg, error) + Wasm func(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error) } func DefaultEncoders(unpacker codectypes.AnyUnpacker, portSource types.ICS20TransferPortSource) MessageEncoders { return MessageEncoders{ - Bank: EncodeBankMsg, - Custom: NoCustomMsg, - IBC: EncodeIBCMsg(portSource), - Staking: EncodeStakingMsg, - Stargate: EncodeStargateMsg(unpacker), - Wasm: EncodeWasmMsg, + Bank: EncodeBankMsg, + Custom: NoCustomMsg, + Distribution: EncodeDistributionMsg, + IBC: EncodeIBCMsg(portSource), + Staking: EncodeStakingMsg, + Stargate: EncodeStargateMsg(unpacker), + Wasm: EncodeWasmMsg, } } @@ -53,6 +56,9 @@ func (e MessageEncoders) Merge(o *MessageEncoders) MessageEncoders { if o.Custom != nil { e.Custom = o.Custom } + if o.Distribution != nil { + e.Distribution = o.Distribution + } if o.IBC != nil { e.IBC = o.IBC } @@ -74,6 +80,8 @@ func (e MessageEncoders) Encode(ctx sdk.Context, contractAddr sdk.AccAddress, co return e.Bank(contractAddr, msg.Bank) case msg.Custom != nil: return e.Custom(contractAddr, msg.Custom) + case msg.Distribution != nil: + return e.Distribution(contractAddr, msg.Distribution) case msg.IBC != nil: return e.IBC(ctx, contractAddr, contractIBCPortID, msg.IBC) case msg.Staking != nil: @@ -109,6 +117,25 @@ func NoCustomMsg(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) return nil, sdkerrors.Wrap(types.ErrUnknownMsg, "custom variant not supported") } +func EncodeDistributionMsg(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error) { + switch { + case msg.SetWithdrawAddress != nil: + setMsg := distributiontypes.MsgSetWithdrawAddress{ + DelegatorAddress: sender.String(), + WithdrawAddress: msg.SetWithdrawAddress.Address, + } + return []sdk.Msg{&setMsg}, nil + case msg.WithdrawDelegatorReward != nil: + withdrawMsg := distributiontypes.MsgWithdrawDelegatorReward{ + DelegatorAddress: sender.String(), + ValidatorAddress: msg.WithdrawDelegatorReward.Validator, + } + return []sdk.Msg{&withdrawMsg}, nil + default: + return nil, sdkerrors.Wrap(types.ErrUnknownMsg, "unknown variant of Distribution") + } +} + func EncodeStakingMsg(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error) { switch { case msg.Delegate != nil: @@ -146,21 +173,6 @@ func EncodeStakingMsg(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk Amount: coin, } return []sdk.Msg{&sdkMsg}, nil - case msg.Withdraw != nil: - senderAddr := sender.String() - rcpt := senderAddr - if len(msg.Withdraw.Recipient) != 0 { - rcpt = msg.Withdraw.Recipient - } - setMsg := distributiontypes.MsgSetWithdrawAddress{ - DelegatorAddress: senderAddr, - WithdrawAddress: rcpt, - } - withdrawMsg := distributiontypes.MsgWithdrawDelegatorReward{ - DelegatorAddress: senderAddr, - ValidatorAddress: msg.Withdraw.Validator, - } - return []sdk.Msg{&setMsg, &withdrawMsg}, nil default: return nil, sdkerrors.Wrap(types.ErrUnknownMsg, "unknown variant of Staking") } diff --git a/x/wasm/keeper/handler_plugin_encoders_test.go b/x/wasm/keeper/handler_plugin_encoders_test.go index 78af04d47f..af7e343122 100644 --- a/x/wasm/keeper/handler_plugin_encoders_test.go +++ b/x/wasm/keeper/handler_plugin_encoders_test.go @@ -279,33 +279,28 @@ func TestEncoding(t *testing.T) { }, }, }, - "staking withdraw (implicit recipient)": { + "staking withdraw (explicit recipient)": { sender: addr1, srcMsg: wasmvmtypes.CosmosMsg{ - Staking: &wasmvmtypes.StakingMsg{ - Withdraw: &wasmvmtypes.WithdrawMsg{ + Distribution: &wasmvmtypes.DistributionMsg{ + WithdrawDelegatorReward: &wasmvmtypes.WithdrawDelegatorRewardMsg{ Validator: valAddr2.String(), }, }, }, output: []sdk.Msg{ - &distributiontypes.MsgSetWithdrawAddress{ - DelegatorAddress: addr1.String(), - WithdrawAddress: addr1.String(), - }, &distributiontypes.MsgWithdrawDelegatorReward{ DelegatorAddress: addr1.String(), ValidatorAddress: valAddr2.String(), }, }, }, - "staking withdraw (explicit recipient)": { + "staking set withdraw address": { sender: addr1, srcMsg: wasmvmtypes.CosmosMsg{ - Staking: &wasmvmtypes.StakingMsg{ - Withdraw: &wasmvmtypes.WithdrawMsg{ - Validator: valAddr2.String(), - Recipient: addr2.String(), + Distribution: &wasmvmtypes.DistributionMsg{ + SetWithdrawAddress: &wasmvmtypes.SetWithdrawAddressMsg{ + Address: addr2.String(), }, }, }, @@ -314,10 +309,6 @@ func TestEncoding(t *testing.T) { DelegatorAddress: addr1.String(), WithdrawAddress: addr2.String(), }, - &distributiontypes.MsgWithdrawDelegatorReward{ - DelegatorAddress: addr1.String(), - ValidatorAddress: valAddr2.String(), - }, }, }, "stargate encoded bank msg": { diff --git a/x/wasm/keeper/query_plugins.go b/x/wasm/keeper/query_plugins.go index f469fdc266..c37432a18a 100644 --- a/x/wasm/keeper/query_plugins.go +++ b/x/wasm/keeper/query_plugins.go @@ -280,7 +280,7 @@ func StakingQuerier(keeper types.StakingKeeper, distKeeper types.DistributionKee } return json.Marshal(res) } - if request.Validators != nil { + if request.AllValidators != nil { validators := keeper.GetBondedValidatorsByPower(ctx) //validators := keeper.GetAllValidators(ctx) wasmVals := make([]wasmvmtypes.Validator, len(validators)) @@ -292,11 +292,28 @@ func StakingQuerier(keeper types.StakingKeeper, distKeeper types.DistributionKee MaxChangeRate: v.Commission.MaxChangeRate.String(), } } - res := wasmvmtypes.ValidatorsResponse{ + res := wasmvmtypes.AllValidatorsResponse{ Validators: wasmVals, } return json.Marshal(res) } + if request.Validator != nil { + valAddr, err := sdk.ValAddressFromBech32(request.Validator.Address) + if err != nil { + return nil, err + } + v, found := keeper.GetValidator(ctx, valAddr) + res := wasmvmtypes.ValidatorResponse{} + if found { + res.Validator = &wasmvmtypes.Validator{ + Address: v.OperatorAddress, + Commission: v.Commission.Rate.String(), + MaxCommission: v.Commission.MaxRate.String(), + MaxChangeRate: v.Commission.MaxChangeRate.String(), + } + } + return json.Marshal(res) + } if request.AllDelegations != nil { delegator, err := sdk.AccAddressFromBech32(request.AllDelegations.Delegator) if err != nil { diff --git a/x/wasm/keeper/recurse_test.go b/x/wasm/keeper/recurse_test.go index 08cbf31fc9..2b65d3c2da 100644 --- a/x/wasm/keeper/recurse_test.go +++ b/x/wasm/keeper/recurse_test.go @@ -249,8 +249,8 @@ func TestLimitRecursiveQueryGas(t *testing.T) { Work: 2000, }, expectQueriesFromContract: 5, - // FIXME: why +1 ... confused a bit by calculations, seems like rounding issues - expectedGas: GasWork2k + 5*(GasWork2k+GasReturnHashed) + 1, + // FIXME: why -3 ... confused a bit by calculations, seems like rounding issues + expectedGas: GasWork2k + 5*(GasWork2k+GasReturnHashed) - 3, }, // this is where we expect an error... // it has enough gas to run 4 times and die on the 5th (4th time dispatching to sub-contract) diff --git a/x/wasm/keeper/staking_test.go b/x/wasm/keeper/staking_test.go index b90fed75a4..4af1d041f4 100644 --- a/x/wasm/keeper/staking_test.go +++ b/x/wasm/keeper/staking_test.go @@ -470,24 +470,61 @@ func TestQueryStakingInfo(t *testing.T) { assert.Equal(t, "stake", bondedRes.Denom) // now, let's reflect a smart query into the x/wasm handlers and see if we get the same result - reflectValidatorsQuery := ReflectQueryMsg{Chain: &ChainQuery{Request: &wasmvmtypes.QueryRequest{Staking: &wasmvmtypes.StakingQuery{ - Validators: &wasmvmtypes.ValidatorsQuery{}, + reflectAllValidatorsQuery := ReflectQueryMsg{Chain: &ChainQuery{Request: &wasmvmtypes.QueryRequest{Staking: &wasmvmtypes.StakingQuery{ + AllValidators: &wasmvmtypes.AllValidatorsQuery{}, }}}} - reflectValidatorsBin := buildReflectQuery(t, &reflectValidatorsQuery) - res, err = keeper.QuerySmart(ctx, maskAddr, reflectValidatorsBin) + reflectAllValidatorsBin := buildReflectQuery(t, &reflectAllValidatorsQuery) + res, err = keeper.QuerySmart(ctx, maskAddr, reflectAllValidatorsBin) require.NoError(t, err) // first we pull out the data from chain response, before parsing the original response mustParse(t, res, &reflectRes) - var validatorRes wasmvmtypes.ValidatorsResponse + var allValidatorsRes wasmvmtypes.AllValidatorsResponse + mustParse(t, reflectRes.Data, &allValidatorsRes) + require.Len(t, allValidatorsRes.Validators, 1) + valInfo := allValidatorsRes.Validators[0] + // Note: this ValAddress not AccAddress, may change with #264 + require.Equal(t, valAddr.String(), valInfo.Address) + require.Contains(t, valInfo.Commission, "0.100") + require.Contains(t, valInfo.MaxCommission, "0.200") + require.Contains(t, valInfo.MaxChangeRate, "0.010") + + // find a validator + reflectValidatorQuery := ReflectQueryMsg{Chain: &ChainQuery{Request: &wasmvmtypes.QueryRequest{Staking: &wasmvmtypes.StakingQuery{ + Validator: &wasmvmtypes.ValidatorQuery{ + Address: valAddr.String(), + }, + }}}} + reflectValidatorBin := buildReflectQuery(t, &reflectValidatorQuery) + res, err = keeper.QuerySmart(ctx, maskAddr, reflectValidatorBin) + require.NoError(t, err) + // first we pull out the data from chain response, before parsing the original response + mustParse(t, res, &reflectRes) + var validatorRes wasmvmtypes.ValidatorResponse mustParse(t, reflectRes.Data, &validatorRes) - require.Len(t, validatorRes.Validators, 1) - valInfo := validatorRes.Validators[0] + require.NotNil(t, validatorRes.Validator) + valInfo = *validatorRes.Validator // Note: this ValAddress not AccAddress, may change with #264 require.Equal(t, valAddr.String(), valInfo.Address) require.Contains(t, valInfo.Commission, "0.100") require.Contains(t, valInfo.MaxCommission, "0.200") require.Contains(t, valInfo.MaxChangeRate, "0.010") + // missing validator + noVal := sdk.ValAddress(secp256k1.GenPrivKey().PubKey().Address()) + reflectNoValidatorQuery := ReflectQueryMsg{Chain: &ChainQuery{Request: &wasmvmtypes.QueryRequest{Staking: &wasmvmtypes.StakingQuery{ + Validator: &wasmvmtypes.ValidatorQuery{ + Address: noVal.String(), + }, + }}}} + reflectNoValidatorBin := buildReflectQuery(t, &reflectNoValidatorQuery) + res, err = keeper.QuerySmart(ctx, maskAddr, reflectNoValidatorBin) + require.NoError(t, err) + // first we pull out the data from chain response, before parsing the original response + mustParse(t, res, &reflectRes) + var noValidatorRes wasmvmtypes.ValidatorResponse + mustParse(t, reflectRes.Data, &noValidatorRes) + require.Nil(t, noValidatorRes.Validator) + // test to get all my delegations reflectAllDelegationsQuery := ReflectQueryMsg{Chain: &ChainQuery{Request: &wasmvmtypes.QueryRequest{Staking: &wasmvmtypes.StakingQuery{ AllDelegations: &wasmvmtypes.AllDelegationsQuery{ diff --git a/x/wasm/keeper/testdata/burner.wasm b/x/wasm/keeper/testdata/burner.wasm index 356bdcea21..e2a9940735 100644 Binary files a/x/wasm/keeper/testdata/burner.wasm and b/x/wasm/keeper/testdata/burner.wasm differ diff --git a/x/wasm/keeper/testdata/hackatom.wasm b/x/wasm/keeper/testdata/hackatom.wasm index aeecf05af1..1eb955b2ba 100644 Binary files a/x/wasm/keeper/testdata/hackatom.wasm and b/x/wasm/keeper/testdata/hackatom.wasm differ diff --git a/x/wasm/keeper/testdata/hackatom.wasm.gzip b/x/wasm/keeper/testdata/hackatom.wasm.gzip index 116a90fc8b..40a94a970b 100644 Binary files a/x/wasm/keeper/testdata/hackatom.wasm.gzip and b/x/wasm/keeper/testdata/hackatom.wasm.gzip differ diff --git a/x/wasm/keeper/testdata/ibc_reflect.wasm b/x/wasm/keeper/testdata/ibc_reflect.wasm index 3ff47810ab..d110005f49 100644 Binary files a/x/wasm/keeper/testdata/ibc_reflect.wasm and b/x/wasm/keeper/testdata/ibc_reflect.wasm differ diff --git a/x/wasm/keeper/testdata/ibc_reflect_send.wasm b/x/wasm/keeper/testdata/ibc_reflect_send.wasm index b23c7bf648..faf997cb73 100644 Binary files a/x/wasm/keeper/testdata/ibc_reflect_send.wasm and b/x/wasm/keeper/testdata/ibc_reflect_send.wasm differ diff --git a/x/wasm/keeper/testdata/reflect.wasm b/x/wasm/keeper/testdata/reflect.wasm index 3ca95363a0..aa6455c982 100644 Binary files a/x/wasm/keeper/testdata/reflect.wasm and b/x/wasm/keeper/testdata/reflect.wasm differ diff --git a/x/wasm/keeper/testdata/staking.wasm b/x/wasm/keeper/testdata/staking.wasm index dd70d8fff3..ccb77c57e3 100644 Binary files a/x/wasm/keeper/testdata/staking.wasm and b/x/wasm/keeper/testdata/staking.wasm differ