This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
/
message.go
447 lines (384 loc) · 16 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package chain
import (
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/filecoin-project/chain-validation/pkg/state/actors"
"github.com/filecoin-project/chain-validation/pkg/state/actors/initialize"
"github.com/filecoin-project/chain-validation/pkg/state/actors/multsig"
"github.com/filecoin-project/chain-validation/pkg/state/actors/paych"
"github.com/filecoin-project/chain-validation/pkg/state/actors/strgminr"
"github.com/filecoin-project/chain-validation/pkg/state/actors/strgmrkt"
"github.com/filecoin-project/chain-validation/pkg/state/actors/strgpwr"
"github.com/filecoin-project/chain-validation/pkg/state/address"
"github.com/filecoin-project/chain-validation/pkg/state/types"
)
// MethodID identifies a VM actor method.
// The values here are not intended to match the spec's method IDs, though once implementations
// converge on those we could make it so.
// Integrations should map these method ids to the internal method handle representation.
type MethodID int
// An enumeration of all actor methods which a message could invoke.
// Note that some methods are not intended for direct invocation by account actors, but they are still
// listed here so that the behaviour of attempting to invoke them can be exercised.
const (
NoMethod MethodID = iota
InitConstructor
InitExec
InitGetActorIDForAddress
StoragePowerConstructor
StoragePowerCreateStorageMiner
StoragePowerUpdatePower
StoragePowerTotalStorage
StoragePowerPowerLookup
StoragePowerIncrementPower
StoragePowerSuspendMiner
StorageMarketConstructor
StorageMarketWithdrawBalance
StorageMarketAddBalance
StorageMarketPublishStorageDeals
StorageMarketActivateStorageDeals
StorageMarketComputeDataCommitment
StorageMinerUpdatePeerID
StorageMinerGetOwner
StorageMinerGetWorkerAddr
StorageMinerGetPower
StorageMinerGetPeerID
StorageMinerGetSectorSize
MultiSigConstructor
MultiSigPropose
MultiSigApprove
MultiSigCancel
MultiSigClearCompleted
MultiSigAddSigner
MultiSigRemoveSigner
MultiSigSwapSigner
MultiSigChangeRequirement
PaymentChannelConstructor
PaymentChannelUpdate
PaymentChannelClose
PaymentChannelCollect
PaymentChannelGetOwner
PaymentChannelGetToSend
GetSectorSize
CronConstructor
CronTick
// List not yet complete, pending specification.
// Provides a value above which integrations can assign their own method identifiers without
// collision with these "standard" ones.
MethodCount
)
// MessageFactory creates a concrete, but opaque, message object.
// Integrations should implement this to provide a message value that will be accepted by the
// validation engine.
type MessageFactory interface {
MakeMessage(from, to address.Address, method MethodID, nonce uint64, value, gasPrice types.BigInt, gasLimit types.GasUnit,
params []byte) (interface{}, error)
FromSingletonAddress(address actors.SingletonActorID) address.Address
FromActorCodeCid(cod actors.ActorCodeID) cid.Cid
}
// MessageProducer presents a convenient API for scripting the creation of long and complex message sequences.
// The created messages are retained for subsequent export or evaluation in a VM.
// Actual message construction is delegated to a `MessageFactory`, and the message are opaque to the producer.
type MessageProducer struct {
factory MessageFactory
defaults msgOpts // Note non-pointer reference.
messages []interface{}
}
// NewMessageProducer creates a new message producer, delegating message creation to `factory`.
func NewMessageProducer(factory MessageFactory, defaultGasLimit types.GasUnit, defaultGasPrice types.BigInt) *MessageProducer {
return &MessageProducer{
factory: factory,
defaults: msgOpts{
gasLimit: defaultGasLimit,
gasPrice: defaultGasPrice,
},
}
}
// Messages returns a slice containing all messages created by the producer.
func (mp *MessageProducer) Messages() []interface{} {
return mp.messages[:]
}
// msgOpts specifies value and gas parameters for a message, supporting a functional options pattern
// for concise but customizable message construction.
type msgOpts struct {
value types.BigInt
gasLimit types.GasUnit
gasPrice types.BigInt
}
// MsgOpt is an option configuring message value or gas parameters.
type MsgOpt func(*msgOpts)
func Value(value uint64) MsgOpt {
return func(opts *msgOpts) {
opts.value = types.NewInt(value)
}
}
func BigValue(value types.BigInt) MsgOpt {
return func(opts *msgOpts) {
opts.value = value
}
}
func GasLimit(limit uint64) MsgOpt {
return func(opts *msgOpts) {
opts.gasLimit = types.GasUnit(limit)
}
}
func GasPrice(price uint64) MsgOpt {
return func(opts *msgOpts) {
opts.gasPrice = types.NewInt(price)
}
}
// Build creates and returns a single message, using default gas parameters unless modified by `opts`.
func (mp *MessageProducer) Build(from, to address.Address, nonce uint64, method MethodID, params []byte,
opts ...MsgOpt) (interface{}, error) {
values := mp.defaults
for _, opt := range opts {
opt(&values)
}
return mp.BuildFull(from, to, method, nonce, values.value, values.gasLimit, values.gasPrice, params)
}
// BuildFull creates and returns a single message.
func (mp *MessageProducer) BuildFull(from, to address.Address, method MethodID, nonce uint64, value types.BigInt,
gasLimit types.GasUnit, gasPrice types.BigInt, params []byte) (interface{}, error) {
fm, err := mp.factory.MakeMessage(from, to, method, nonce, value, gasPrice, gasLimit, params)
if err != nil {
return nil, err
}
mp.messages = append(mp.messages, fm)
return fm, nil
}
//
// Helper methods until spec defines these
//
func (mp *MessageProducer) SingletonAddress(id actors.SingletonActorID) address.Address {
return mp.factory.FromSingletonAddress(id)
}
func (mp *MessageProducer) ActorCid(c actors.ActorCodeID) cid.Cid {
return mp.factory.FromActorCodeCid(c)
}
//
// Sugar methods for type-checked construction of specific messages.
//
// Transfer builds a simple value transfer message and returns it.
func (mp *MessageProducer) Transfer(from, to address.Address, nonce uint64, value uint64, opts ...MsgOpt) (interface{}, error) {
x := append([]MsgOpt{Value(value)}, opts...)
return mp.Build(from, to, nonce, NoMethod, noParams, x...)
}
// InitExec builds a message invoking InitActor.Exec and returns it.
func (mp *MessageProducer) InitExec(from address.Address, nonce uint64, code actors.ActorCodeID, params []byte, opts ...MsgOpt) (interface{}, error) {
iaAddr := mp.factory.FromSingletonAddress(actors.InitAddress)
initParams, err := types.Serialize(&initialize.ExecParams{
Code: mp.factory.FromActorCodeCid(code),
Params: params,
})
if err != nil {
return nil, err
}
return mp.Build(from, iaAddr, nonce, InitExec, initParams, opts...)
}
//
// Storage Market Actor Methods
//
func (mp *MessageProducer) StorageMarketWithdrawBalance(from address.Address, nonce uint64, balance types.BigInt, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgmrkt.WithdrawBalanceParams{Balance: balance})
if err != nil {
return nil, err
}
smaddr := mp.factory.FromSingletonAddress(actors.StorageMarketAddress)
return mp.Build(from, smaddr, nonce, StorageMarketWithdrawBalance, params, opts...)
}
func (mp *MessageProducer) StorageMarketAddBalance(from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
smaddr := mp.factory.FromSingletonAddress(actors.StorageMarketAddress)
return mp.Build(from, smaddr, nonce, StorageMarketAddBalance, noParams, opts...)
}
func (mp *MessageProducer) StorageMarketPublishStorageDeals(from address.Address, nonce uint64, deals []strgmrkt.StorageDeal, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgmrkt.PublishStorageDealsParams{Deals: deals})
if err != nil {
return nil, err
}
smaddr := mp.factory.FromSingletonAddress(actors.StorageMarketAddress)
return mp.Build(from, smaddr, nonce, StorageMarketPublishStorageDeals, params, opts...)
}
func (mp *MessageProducer) StorageMarketActivateStorageDeals(from address.Address, nonce uint64, dealIDs []uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgmrkt.ActivateStorageDealsParams{Deals: dealIDs})
if err != nil {
return nil, err
}
smaddr := mp.factory.FromSingletonAddress(actors.StorageMarketAddress)
return mp.Build(from, smaddr, nonce, StorageMarketActivateStorageDeals, params, opts...)
}
func (mp *MessageProducer) StorageMarketComputeDataCommitment(from address.Address, nonce uint64, sectorSize uint64, dealIDs []uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgmrkt.ComputeDataCommitmentParams{
DealIDs: dealIDs,
SectorSize: sectorSize,
})
if err != nil {
return nil, err
}
smaddr := mp.factory.FromSingletonAddress(actors.StorageMarketAddress)
return mp.Build(from, smaddr, nonce, StorageMarketComputeDataCommitment, params, opts...)
}
//
// Storage Power Actor Methods
//
// StoragePowerCreateStorageMiner builds a message invoking StoragePowerActor.CreateStorageMiner and returns it.
func (mp *MessageProducer) StoragePowerCreateStorageMiner(from address.Address, nonce uint64,
owner address.Address, worker address.Address, sectorSize uint64, peerID peer.ID,
opts ...MsgOpt) (interface{}, error) {
spaAddr := mp.factory.FromSingletonAddress(actors.StoragePowerAddress)
params, err := types.Serialize(&strgpwr.CreateStorageMinerParams{
Owner: owner,
Worker: worker,
SectorSize: sectorSize,
PeerID: peerID,
})
if err != nil {
return nil, err
}
return mp.Build(from, spaAddr, nonce, StoragePowerCreateStorageMiner, params, opts...)
}
func (mp *MessageProducer) StoragePowerUpdateStorage(from address.Address, nonce uint64, delta types.BigInt, nextppEnd, previousppEnd uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgpwr.UpdateStorageParams{
Delta: delta,
NextProvingPeriodEnd: nextppEnd,
PreviousProvingPeriodEnd: previousppEnd,
})
if err != nil {
return nil, err
}
spaAddr := mp.factory.FromSingletonAddress(actors.StoragePowerAddress)
return mp.Build(from, spaAddr, nonce, StoragePowerUpdatePower, params, opts...)
}
func (mp *MessageProducer) StoragePowerPledgeCollateralForSize(from address.Address, nonce uint64, size types.BigInt, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgpwr.PledgeCollateralParams{Size: size})
if err != nil {
return nil, err
}
spaAddr := mp.factory.FromSingletonAddress(actors.StoragePowerAddress)
return mp.Build(from, spaAddr, nonce, StoragePowerUpdatePower, params, opts...)
}
func (mp *MessageProducer) StoragePowerLookupPower(from address.Address, nonce uint64, miner address.Address, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgpwr.PowerLookupParams{Miner: miner})
if err != nil {
return nil, err
}
spaAddr := mp.factory.FromSingletonAddress(actors.StoragePowerAddress)
return mp.Build(from, spaAddr, nonce, StoragePowerUpdatePower, params, opts...)
}
//
// Storage Miner Actor Methods
//
func (mp *MessageProducer) StorageMinerUpdatePeerID(to, from address.Address, nonce uint64, peerID peer.ID, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&strgminr.UpdatePeerIDParams{PeerID: peerID})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, StorageMinerUpdatePeerID, params, opts...)
}
func (mp *MessageProducer) StorageMinerGetOwner(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, StorageMinerGetOwner, noParams, opts...)
}
func (mp *MessageProducer) StorageMinerGetPower(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, StorageMinerGetPower, noParams, opts...)
}
func (mp *MessageProducer) StorageMinerGetWorkerAddr(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, StorageMinerGetWorkerAddr, noParams, opts...)
}
func (mp *MessageProducer) StorageMinerGetPeerID(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, StorageMinerGetPeerID, noParams, opts...)
}
func (mp *MessageProducer) StorageMinerGetSectorSize(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, StorageMinerGetSectorSize, noParams, opts...)
}
//
// Multi Signature Actor Methods
//
func (mp *MessageProducer) MultiSigPropose(to, from address.Address, nonce uint64, proposeTo address.Address, proposeValue types.BigInt, proposeMethod uint64, proposeParams []byte, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigProposeParams{
To: proposeTo,
Value: proposeValue,
Method: proposeMethod,
Params: proposeParams,
})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigPropose, params, opts...)
}
func (mp *MessageProducer) MultiSigApprove(to, from address.Address, nonce uint64, txID uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigTxID{TxID: txID})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigApprove, params, opts...)
}
func (mp *MessageProducer) MultiSigCancel(to, from address.Address, nonce uint64, txID uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigTxID{TxID: txID})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigCancel, params, opts...)
}
func (mp *MessageProducer) MultiSigAddSigner(to, from address.Address, nonce uint64, signer address.Address, increase bool, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigAddSignerParam{
Signer: signer,
Increase: increase,
})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigAddSigner, params, opts...)
}
func (mp *MessageProducer) MultiSigRemoveSigner(to, from address.Address, nonce uint64, signer address.Address, decrease bool, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigRemoveSignerParam{
Signer: signer,
Decrease: decrease,
})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigRemoveSigner, params, opts...)
}
func (mp *MessageProducer) MultiSigSwapSigner(to, from address.Address, nonce uint64, swapFrom, swapTo address.Address, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigSwapSignerParams{
From: swapFrom,
To: swapTo,
})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigSwapSigner, params, opts...)
}
func (mp *MessageProducer) MultiSigChangeRequirement(to, from address.Address, nonce uint64, req uint64, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&multsig.MultiSigChangeReqParams{Req: req})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, MultiSigChangeRequirement, params, opts...)
}
//
// Payment Channel Actor Methods
//
func (mp *MessageProducer) PaychUpdateChannelState(to, from address.Address, nonce uint64, sv types.SignedVoucher, secret, proof []byte, opts ...MsgOpt) (interface{}, error) {
params, err := types.Serialize(&paych.PaymentChannelUpdateParams{
Sv: sv,
Secret: secret,
Proof: proof,
})
if err != nil {
return nil, err
}
return mp.Build(from, to, nonce, PaymentChannelUpdate, params, opts...)
}
func (mp *MessageProducer) PaychClose(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, PaymentChannelClose, noParams, opts...)
}
func (mp *MessageProducer) PaychCollect(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, PaymentChannelCollect, noParams, opts...)
}
func (mp *MessageProducer) PaychGetOwner(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, PaymentChannelGetOwner, noParams, opts...)
}
func (mp *MessageProducer) PaychGetToSend(to, from address.Address, nonce uint64, opts ...MsgOpt) (interface{}, error) {
return mp.Build(from, to, nonce, PaymentChannelGetToSend, noParams, opts...)
}
var noParams []byte