-
Notifications
You must be signed in to change notification settings - Fork 466
/
message.go
284 lines (249 loc) · 8.27 KB
/
message.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
package cmd
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
fbig "github.com/filecoin-project/go-state-types/big"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
"github.com/ipfs/go-cid"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/pkg/errors"
cbg "github.com/whyrusleeping/cbor-gen"
"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/pkg/vm"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/filecoin-project/venus/venus-shared/utils"
)
var (
feecapOption = cmds.StringOption("gas-feecap", "Price (FIL e.g. 0.00013) to pay for each GasUnit consumed mining this message")
premiumOption = cmds.StringOption("gas-premium", "Price (FIL e.g. 0.00013) to pay for each GasUnit consumed mining this message")
limitOption = cmds.Int64Option("gas-limit", "Maximum GasUnits this message is allowed to consume")
)
func parseGasOptions(req *cmds.Request) (fbig.Int, fbig.Int, int64, error) {
var (
feecap = types.FIL{Int: types.NewInt(0).Int}
premium = types.FIL{Int: types.NewInt(0).Int}
ok = false
gasLimitInt = int64(0)
)
var err error
feecapOption := req.Options["gas-feecap"]
if feecapOption != nil {
feecap, err = types.ParseFIL(feecapOption.(string))
if err != nil {
return types.ZeroFIL, types.ZeroFIL, 0, errors.New("invalid gas price (specify FIL as a decimal number)")
}
}
premiumOption := req.Options["gas-premium"]
if premiumOption != nil {
premium, err = types.ParseFIL(premiumOption.(string))
if err != nil {
return types.ZeroFIL, types.ZeroFIL, 0, errors.New("invalid gas price (specify FIL as a decimal number)")
}
}
limitOption := req.Options["gas-limit"]
if limitOption != nil {
gasLimitInt, ok = limitOption.(int64)
if !ok {
msg := fmt.Sprintf("invalid gas limit: %s", limitOption)
return types.ZeroFIL, types.ZeroFIL, 0, errors.New(msg)
}
}
return fbig.Int{Int: feecap.Int}, fbig.Int{Int: premium.Int}, gasLimitInt, nil
}
// MessageSendResult is the return type for message send command
type MessageSendResult struct {
Cid cid.Cid
GasUsed int64
Preview bool
}
var msgSendCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Send a message", // This feels too generic...
},
Arguments: []cmds.Argument{
cmds.StringArg("target", true, false, "address of the actor to send the message to"),
cmds.StringArg("value", true, false, "amount of FIL"),
},
Options: []cmds.Option{
cmds.StringOption("value", "Value to send with message in FIL"),
cmds.StringOption("from", "address to send message from"),
cmds.StringOption("from-eth-addr", "optionally specify the eth addr to send funds from"),
feecapOption,
premiumOption,
limitOption,
cmds.Uint64Option("nonce", "specify the nonce to use"),
cmds.StringOption("params-json", "specify invocation parameters in json"),
cmds.StringOption("params-hex", "specify invocation parameters in hex"),
cmds.Uint64Option("method", "The method to invoke on the target actor"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context
is0xRecipient := false
toAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
// could be an ETH address
ea, err := types.ParseEthAddress(req.Arguments[0])
if err != nil {
return err
}
is0xRecipient = true
// this will be either "f410f..." or "f0..."
toAddr, err = ea.ToFilecoinAddress()
if err != nil {
return err
}
// ideally, this should never happen
if !(toAddr.Protocol() == address.ID || toAddr.Protocol() == address.Delegated) {
return err
}
}
v := req.Arguments[1]
val, err := types.ParseFIL(v)
if err != nil {
return fmt.Errorf("mal-formed value: %v", err)
}
var fromAddr address.Address
if addrStr, _ := req.Options["from-eth-addr"].(string); len(addrStr) != 0 {
fromAddr, err = address.NewFromString(addrStr)
if err != nil {
return err
}
} else {
fromAddr, err = fromAddrOrDefault(req, env)
if err != nil {
return err
}
}
var params []byte
if rawPH := req.Options["params-hex"]; rawPH != nil {
decparams, err := hex.DecodeString(rawPH.(string))
if err != nil {
return fmt.Errorf("failed to decode hex params: %w", err)
}
params = decparams
}
methodID := builtin.MethodSend
method := req.Options["method"]
if types.IsEthAddress(fromAddr) || is0xRecipient {
// Method numbers don't make sense from eth accounts.
if method != nil {
return fmt.Errorf("messages from f410f addresses may not specify a method number")
}
// Now, figure out the correct method number from the recipient.
if toAddr == builtintypes.EthereumAddressManagerActorAddr {
methodID = builtintypes.MethodsEAM.CreateExternal
} else {
methodID = builtintypes.MethodsEVM.InvokeContract
}
if req.Options["params-json"] != nil {
return fmt.Errorf("may not call with json parameters from an eth account")
}
// And format the parameters, if present.
if len(params) > 0 {
var buf bytes.Buffer
if err := cbg.WriteByteArray(&buf, params); err != nil {
return fmt.Errorf("failed to marshal EVM parameters")
}
params = buf.Bytes()
}
// We can only send to an f410f or f0 address.
if !(toAddr.Protocol() == address.ID || toAddr.Protocol() == address.Delegated) {
// Resolve id addr if possible.
toAddr, err = env.(*node.Env).ChainAPI.StateLookupID(ctx, toAddr, types.EmptyTSK)
if err != nil {
return fmt.Errorf("addresses starting with f410f can only send to other addresses starting with f410f, or id addresses. could not find id address for %s", toAddr.String())
}
}
} else if method != nil {
methodID = abi.MethodNum(method.(uint64))
}
if methodID == builtin.MethodSend && fromAddr.String() == toAddr.String() {
return errors.New("self-transfer is not allowed")
}
feecap, premium, gasLimit, err := parseGasOptions(req)
if err != nil {
return err
}
if err := utils.LoadBuiltinActors(ctx, env.(*node.Env).ChainAPI); err != nil {
return err
}
rawPJ := req.Options["params-json"]
if rawPJ != nil {
if params != nil {
return fmt.Errorf("can only specify one of 'params-json' and 'params-hex'")
}
decparams, err := decodeTypedParams(ctx, env.(*node.Env), toAddr, methodID, rawPJ.(string))
if err != nil {
return fmt.Errorf("failed to decode json params: %s", err)
}
params = decparams
}
msg := &types.Message{
From: fromAddr,
To: toAddr,
Value: abi.TokenAmount{Int: val.Int},
GasPremium: premium,
GasFeeCap: feecap,
GasLimit: gasLimit,
Method: methodID,
Params: params,
}
nonceOption := req.Options["nonce"]
var c cid.Cid
if nonceOption != nil {
nonce, ok := nonceOption.(uint64)
if !ok {
return fmt.Errorf("invalid nonce option: %v", nonceOption)
}
msg.Nonce = nonce
sm, err := env.(*node.Env).WalletAPI.WalletSignMessage(ctx, msg.From, msg)
if err != nil {
return err
}
_, err = env.(*node.Env).MessagePoolAPI.MpoolPush(ctx, sm)
if err != nil {
return err
}
c = sm.Cid()
} else {
sm, err := env.(*node.Env).MessagePoolAPI.MpoolPushMessage(ctx, msg, nil)
if err != nil {
return err
}
c = sm.Cid()
}
return re.Emit(c.String())
},
}
func decodeTypedParams(ctx context.Context, fapi *node.Env, to address.Address, method abi.MethodNum, paramstr string) ([]byte, error) {
act, err := fapi.ChainAPI.StateGetActor(ctx, to, types.EmptyTSK)
if err != nil {
return nil, err
}
methodMeta, found := utils.MethodsMap[act.Code][method]
if !found {
return nil, fmt.Errorf("method %d not found on actor %s", method, act.Code)
}
p := reflect.New(methodMeta.Params.Elem()).Interface().(cbg.CBORMarshaler)
if err := json.Unmarshal([]byte(paramstr), p); err != nil {
return nil, fmt.Errorf("unmarshaling input into params type: %s", err)
}
buf := new(bytes.Buffer)
if err := p.MarshalCBOR(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// WaitResult is the result of a message wait call.
type WaitResult struct {
Message *types.Message
Receipt *types.MessageReceipt
Signature vm.ActorMethodSignature
}