-
Notifications
You must be signed in to change notification settings - Fork 413
/
handler_plugin.go
289 lines (268 loc) · 8.06 KB
/
handler_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package keeper
import (
"encoding/json"
"fmt"
wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/staking"
)
type MessageHandler struct {
router sdk.Router
encoders MessageEncoders
}
func NewMessageHandler(router sdk.Router, customEncoders *MessageEncoders) MessageHandler {
encoders := DefaultEncoders().Merge(customEncoders)
return MessageHandler{
router: router,
encoders: encoders,
}
}
type BankEncoder func(sender sdk.AccAddress, msg *wasmTypes.BankMsg) ([]sdk.Msg, error)
type CustomEncoder func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error)
type StakingEncoder func(sender sdk.AccAddress, msg *wasmTypes.StakingMsg) ([]sdk.Msg, error)
type WasmEncoder func(sender sdk.AccAddress, msg *wasmTypes.WasmMsg) ([]sdk.Msg, error)
type MessageEncoders struct {
Bank BankEncoder
Custom CustomEncoder
Staking StakingEncoder
Wasm WasmEncoder
}
func DefaultEncoders() MessageEncoders {
return MessageEncoders{
Bank: EncodeBankMsg,
Custom: NoCustomMsg,
Staking: EncodeStakingMsg,
Wasm: EncodeWasmMsg,
}
}
func (e MessageEncoders) Merge(o *MessageEncoders) MessageEncoders {
if o == nil {
return e
}
if o.Bank != nil {
e.Bank = o.Bank
}
if o.Custom != nil {
e.Custom = o.Custom
}
if o.Staking != nil {
e.Staking = o.Staking
}
if o.Wasm != nil {
e.Wasm = o.Wasm
}
return e
}
func (e MessageEncoders) Encode(contractAddr sdk.AccAddress, msg wasmTypes.CosmosMsg) ([]sdk.Msg, error) {
switch {
case msg.Bank != nil:
return e.Bank(contractAddr, msg.Bank)
case msg.Custom != nil:
return e.Custom(contractAddr, msg.Custom)
case msg.Staking != nil:
return e.Staking(contractAddr, msg.Staking)
case msg.Wasm != nil:
return e.Wasm(contractAddr, msg.Wasm)
}
return nil, sdkerrors.Wrap(types.ErrInvalidMsg, "Unknown variant of Wasm")
}
func EncodeBankMsg(sender sdk.AccAddress, msg *wasmTypes.BankMsg) ([]sdk.Msg, error) {
if msg.Send == nil {
return nil, sdkerrors.Wrap(types.ErrInvalidMsg, "Unknown variant of Bank")
}
if len(msg.Send.Amount) == 0 {
return nil, nil
}
fromAddr, stderr := sdk.AccAddressFromBech32(msg.Send.FromAddress)
if stderr != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Send.FromAddress)
}
toAddr, stderr := sdk.AccAddressFromBech32(msg.Send.ToAddress)
if stderr != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Send.ToAddress)
}
toSend, err := convertWasmCoinsToSdkCoins(msg.Send.Amount)
if err != nil {
return nil, err
}
sdkMsg := bank.MsgSend{
FromAddress: fromAddr,
ToAddress: toAddr,
Amount: toSend,
}
return []sdk.Msg{sdkMsg}, nil
}
func NoCustomMsg(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) {
return nil, sdkerrors.Wrap(types.ErrInvalidMsg, "Custom variant not supported")
}
func EncodeStakingMsg(sender sdk.AccAddress, msg *wasmTypes.StakingMsg) ([]sdk.Msg, error) {
if msg.Delegate != nil {
validator, err := sdk.ValAddressFromBech32(msg.Delegate.Validator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Delegate.Validator)
}
coin, err := convertWasmCoinToSdkCoin(msg.Delegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := staking.MsgDelegate{
DelegatorAddress: sender,
ValidatorAddress: validator,
Amount: coin,
}
return []sdk.Msg{sdkMsg}, nil
}
if msg.Redelegate != nil {
src, err := sdk.ValAddressFromBech32(msg.Redelegate.SrcValidator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Redelegate.SrcValidator)
}
dst, err := sdk.ValAddressFromBech32(msg.Redelegate.DstValidator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Redelegate.DstValidator)
}
coin, err := convertWasmCoinToSdkCoin(msg.Redelegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := staking.MsgBeginRedelegate{
DelegatorAddress: sender,
ValidatorSrcAddress: src,
ValidatorDstAddress: dst,
Amount: coin,
}
return []sdk.Msg{sdkMsg}, nil
}
if msg.Undelegate != nil {
validator, err := sdk.ValAddressFromBech32(msg.Undelegate.Validator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Undelegate.Validator)
}
coin, err := convertWasmCoinToSdkCoin(msg.Undelegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := staking.MsgUndelegate{
DelegatorAddress: sender,
ValidatorAddress: validator,
Amount: coin,
}
return []sdk.Msg{sdkMsg}, nil
}
if msg.Withdraw != nil {
var err error
rcpt := sender
if len(msg.Withdraw.Recipient) != 0 {
rcpt, err = sdk.AccAddressFromBech32(msg.Withdraw.Recipient)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Withdraw.Recipient)
}
}
validator, err := sdk.ValAddressFromBech32(msg.Withdraw.Validator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Withdraw.Validator)
}
setMsg := distribution.MsgSetWithdrawAddress{
DelegatorAddress: sender,
WithdrawAddress: rcpt,
}
withdrawMsg := distribution.MsgWithdrawDelegatorReward{
DelegatorAddress: sender,
ValidatorAddress: validator,
}
return []sdk.Msg{setMsg, withdrawMsg}, nil
}
return nil, sdkerrors.Wrap(types.ErrInvalidMsg, "Unknown variant of Staking")
}
func EncodeWasmMsg(sender sdk.AccAddress, msg *wasmTypes.WasmMsg) ([]sdk.Msg, error) {
if msg.Execute != nil {
contractAddr, err := sdk.AccAddressFromBech32(msg.Execute.ContractAddr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Execute.ContractAddr)
}
coins, err := convertWasmCoinsToSdkCoins(msg.Execute.Send)
if err != nil {
return nil, err
}
sdkMsg := types.MsgExecuteContract{
Sender: sender,
Contract: contractAddr,
Msg: msg.Execute.Msg,
SentFunds: coins,
}
return []sdk.Msg{sdkMsg}, nil
}
if msg.Instantiate != nil {
coins, err := convertWasmCoinsToSdkCoins(msg.Instantiate.Send)
if err != nil {
return nil, err
}
sdkMsg := types.MsgInstantiateContract{
Sender: sender,
Code: msg.Instantiate.CodeID,
// TODO: add this to CosmWasm
Label: fmt.Sprintf("Auto-created by %s", sender),
InitMsg: msg.Instantiate.Msg,
InitFunds: coins,
}
return []sdk.Msg{sdkMsg}, nil
}
return nil, sdkerrors.Wrap(types.ErrInvalidMsg, "Unknown variant of Wasm")
}
func (h MessageHandler) Dispatch(ctx sdk.Context, contractAddr sdk.AccAddress, msg wasmTypes.CosmosMsg) error {
sdkMsgs, err := h.encoders.Encode(contractAddr, msg)
if err != nil {
return err
}
for _, sdkMsg := range sdkMsgs {
if err := h.handleSdkMessage(ctx, contractAddr, sdkMsg); err != nil {
return err
}
}
return nil
}
func (h MessageHandler) handleSdkMessage(ctx sdk.Context, contractAddr sdk.Address, msg sdk.Msg) error {
// make sure this account can send it
for _, acct := range msg.GetSigners() {
if !acct.Equals(contractAddr) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "contract doesn't have permission")
}
}
// find the handler and execute it
handler := h.router.Route(ctx, msg.Route())
if handler == nil {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, msg.Route())
}
res, err := handler(ctx, msg)
if err != nil {
return err
}
// redispatch all events, (type sdk.EventTypeMessage will be filtered out in the handler)
ctx.EventManager().EmitEvents(res.Events)
return nil
}
func convertWasmCoinsToSdkCoins(coins []wasmTypes.Coin) (sdk.Coins, error) {
var toSend sdk.Coins
for _, coin := range coins {
c, err := convertWasmCoinToSdkCoin(coin)
if err != nil {
return nil, err
}
toSend = append(toSend, c)
}
return toSend, nil
}
func convertWasmCoinToSdkCoin(coin wasmTypes.Coin) (sdk.Coin, error) {
amount, ok := sdk.NewIntFromString(coin.Amount)
if !ok {
return sdk.Coin{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, coin.Amount+coin.Denom)
}
return sdk.Coin{
Denom: coin.Denom,
Amount: amount,
}, nil
}