This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
invocation_context.go
923 lines (789 loc) · 28.4 KB
/
invocation_context.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
package vm
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"reflect"
"runtime/debug"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-state-types/rt"
vm2 "github.com/filecoin-project/specs-actors/v2/support/vm"
"github.com/ipfs/go-cid"
"github.com/minio/blake2b-simd"
mh "github.com/multiformats/go-multihash"
"golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/v6/actors/builtin"
init_ "github.com/filecoin-project/specs-actors/v6/actors/builtin/init"
"github.com/filecoin-project/specs-actors/v6/actors/runtime"
"github.com/filecoin-project/specs-actors/v6/actors/runtime/proof"
"github.com/filecoin-project/specs-actors/v6/actors/states"
"github.com/filecoin-project/specs-actors/v6/actors/util/adt"
"github.com/filecoin-project/specs-actors/v6/support/ipld"
"github.com/filecoin-project/specs-actors/v6/support/testing"
)
var EmptyObjectCid cid.Cid
const defaultGasLimit = 5_000_000_000
// This is set to match the test vector default randomness value. If set
// differently it will cause all generated conformance tests over randomness
// syscalls to fail.
// https://github.com/filecoin-project/test-vectors/blob/master/schema/schema_randomness.go#L76
const RandString = "i_am_random_____i_am_random_____"
// Context for an individual message invocation, including inter-actor sends.
type invocationContext struct {
rt *VM
topLevel *topLevelContext
msg InternalMessage // The message being processed
fromActor *states.Actor // The immediate calling actor
toActor *states.Actor // The actor to which message is addressed
emptyObject cid.Cid
allowSideEffects bool
callerValidated bool
// Maps (references to) loaded state objs to their expected cid.
// Used for detecting modifications to state outside of transactions.
stateUsedObjs map[cbor.Marshaler]cid.Cid
stats *CallStats
}
// Context for a top-level invocation sequence
type topLevelContext struct {
originatorStableAddress address.Address // Stable (public key) address of the top-level message sender.
originatorCallSeq uint64 // Call sequence number of the top-level message.
newActorAddressCount uint64 // Count of calls to NewActorAddress (mutable).
statsSource StatsSource // optional source of external statistics that can be used to profile calls
circSupply abi.TokenAmount // default or externally specified circulating FIL supply
// Gas tracking fields
gasPrices Pricelist
gasUsed int64
gasAvailable int64
// Temporary field to workaround test-vector limitations
// https://github.com/filecoin-project/specs-actors/issues/1454
fakeSyscallsAccessed bool
}
func (tc *topLevelContext) chargeGas(gas GasCharge) {
toUse := gas.Total()
if tc.gasUsed > tc.gasAvailable-toUse {
gasUsed := tc.gasUsed
tc.gasUsed = tc.gasAvailable
panic(
abort{
exitcode.SysErrOutOfGas,
fmt.Sprintf("not enough gas: used=%d, available=%d, attempt to use=%d", gasUsed, tc.gasAvailable, toUse),
},
)
}
tc.gasUsed += toUse
}
func newInvocationContext(rt *VM, topLevel *topLevelContext, msg InternalMessage, fromActor *states.Actor, emptyObject cid.Cid) invocationContext {
// Note: the toActor and stateHandle are loaded during the `invoke()`
return invocationContext{
rt: rt,
topLevel: topLevel,
msg: msg,
fromActor: fromActor,
toActor: nil,
emptyObject: emptyObject,
allowSideEffects: true,
callerValidated: false,
stateUsedObjs: map[cbor.Marshaler]cid.Cid{},
stats: vm2.NewCallStats(topLevel.statsSource),
}
}
var _ runtime.StateHandle = (*invocationContext)(nil)
func (ic *invocationContext) loadState(obj cbor.Unmarshaler) cid.Cid {
// The actor must be loaded from store every time since the state may have changed via a different state handle
// (e.g. in a recursive call).
actr := ic.loadActor()
c := actr.Head
if !c.Defined() {
ic.Abortf(exitcode.SysErrorIllegalActor, "failed to load undefined state, must construct first")
}
found := ic.StoreGet(c, obj)
if !found {
panic(fmt.Errorf("state not found for actor %s, CID %s", ic.msg.to, c))
}
return c
}
func (ic *invocationContext) loadActor() *states.Actor {
actr, found, err := ic.rt.GetActor(ic.msg.to)
if err != nil {
panic(err)
}
if !found {
panic(fmt.Errorf("failed to find actor %s for state", ic.msg.to))
}
return actr
}
func (ic *invocationContext) storeActor(actr *states.Actor) {
err := ic.rt.setActor(ic.rt.ctx, ic.msg.to, actr)
if err != nil {
panic(err)
}
}
/////////////////////////////////////////////
// Runtime methods
/////////////////////////////////////////////
var _ runtime.Runtime = (*invocationContext)(nil)
// Store implements runtime.Runtime.
func (ic *invocationContext) StoreGet(c cid.Cid, o cbor.Unmarshaler) bool {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnIpldGet())
sw := &storeWrapper{s: ic.rt.store, rt: ic.rt}
return sw.StoreGet(c, o)
}
func (ic *invocationContext) StorePut(x cbor.Marshaler) cid.Cid {
// Serialize before putting data to charge gas
// This could be made more efficient by avoiding double serialization
// with a gas charging block store but this is easier for testing
var buf bytes.Buffer
err := x.MarshalCBOR(&buf)
if err != nil {
ic.rt.Abortf(exitcode.ErrIllegalState, "could not put object in store")
}
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnIpldPut(len(buf.Bytes())))
sw := &storeWrapper{s: ic.rt.store, rt: ic.rt}
return sw.StorePut(x)
}
// These methods implement
// ValueReceived implements runtime.Message
func (ic *invocationContext) ValueReceived() abi.TokenAmount {
return ic.msg.ValueReceived()
}
// Caller implements runtime.Message
func (ic *invocationContext) Caller() address.Address {
return ic.msg.Caller()
}
// Receiver implements runtime.Message
func (ic *invocationContext) Receiver() address.Address {
return ic.msg.Receiver()
}
func (ic *invocationContext) StateCreate(obj cbor.Marshaler) {
actr := ic.loadActor()
if actr.Head.Defined() && !ic.emptyObject.Equals(actr.Head) {
ic.Abortf(exitcode.SysErrorIllegalActor, "failed to construct actor state: already initialized")
}
c := ic.StorePut(obj)
actr.Head = c
ic.storeActor(actr)
ic.stateUsedObjs[obj] = c // Track the expected CID of the object.
}
// Readonly is the implementation of the ActorStateHandle interface.
func (ic *invocationContext) StateReadonly(obj cbor.Unmarshaler) {
// Load state to obj.
c := ic.loadState(obj)
ic.stateUsedObjs[obj.(cbor.Marshaler)] = c // Track the expected CID of the object.
}
// Transaction is the implementation of the ActorStateHandle interface.
func (ic *invocationContext) StateTransaction(obj cbor.Er, f func()) {
if obj == nil {
ic.Abortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
}
ic.checkStateObjectsUnmodified()
// Load state to obj.
ic.loadState(obj)
// Call user code allowing mutation but not side-effects
ic.allowSideEffects = false
f()
ic.allowSideEffects = true
c := ic.replace(obj)
ic.stateUsedObjs[obj] = c // Track the expected CID of the object.
}
func (ic *invocationContext) VerifySignature(signature crypto.Signature, signer address.Address, plaintext []byte) error {
charge, err := ic.topLevel.gasPrices.OnVerifySignature(signature.Type, len(plaintext))
if err != nil {
return err
}
ic.topLevel.chargeGas(charge)
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().VerifySignature(signature, signer, plaintext)
}
func (ic *invocationContext) HashBlake2b(data []byte) [32]byte {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnHashing(len(data)))
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().HashBlake2b(data)
}
func (ic *invocationContext) ComputeUnsealedSectorCID(reg abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnComputeUnsealedSectorCid(reg, pieces))
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().ComputeUnsealedSectorCID(reg, pieces)
}
func (ic *invocationContext) VerifySeal(vi proof.SealVerifyInfo) error {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnVerifySeal(vi))
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().VerifySeal(vi)
}
func (ic *invocationContext) BatchVerifySeals(vis map[address.Address][]proof.SealVerifyInfo) (map[address.Address][]bool, error) {
// no explicit gas charged
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().BatchVerifySeals(vis)
}
func (ic *invocationContext) VerifyAggregateSeals(agg proof.AggregateSealVerifyProofAndInfos) error {
ic.topLevel.fakeSyscallsAccessed = true
return ic.Syscalls().VerifyAggregateSeals(agg)
}
func (ic *invocationContext) VerifyPoSt(vi proof.WindowPoStVerifyInfo) error {
ic.topLevel.fakeSyscallsAccessed = true
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnVerifyPost(vi))
return ic.Syscalls().VerifyPoSt(vi)
}
func (ic *invocationContext) VerifyConsensusFault(h1, h2, extra []byte) (*runtime.ConsensusFault, error) {
ic.topLevel.fakeSyscallsAccessed = true
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnVerifyConsensusFault())
return ic.Syscalls().VerifyConsensusFault(h1, h2, extra)
}
func (ic *invocationContext) NetworkVersion() network.Version {
return ic.rt.networkVersion
}
func (ic *invocationContext) CurrEpoch() abi.ChainEpoch {
return ic.rt.currentEpoch
}
func (ic *invocationContext) BaseFee() abi.TokenAmount {
// for now test vm always runs with zero base fee
return big.Zero()
}
func (ic *invocationContext) CurrentBalance() abi.TokenAmount {
// load balance
act, found, err := ic.rt.GetActor(ic.msg.to)
if err != nil {
ic.Abortf(exitcode.ErrIllegalState, "could not load to actor %v: %v", ic.msg.to, err)
} else if !found {
ic.Abortf(exitcode.ErrIllegalState, "could not find to actor %v", ic.msg.to)
}
return act.Balance
}
func (ic *invocationContext) GetActorCodeCID(a address.Address) (cid.Cid, bool) {
entry, found, err := ic.rt.GetActor(a)
if !found {
return cid.Undef, false
}
if err != nil {
panic(err)
}
return entry.Code, true
}
func (ic *invocationContext) GetRandomnessFromBeacon(_ crypto.DomainSeparationTag, _ abi.ChainEpoch, _ []byte) abi.Randomness {
return []byte(RandString)
}
func (ic *invocationContext) GetRandomnessFromTickets(_ crypto.DomainSeparationTag, _ abi.ChainEpoch, _ []byte) abi.Randomness {
return []byte(RandString)
}
func (ic *invocationContext) ValidateImmediateCallerAcceptAny() {
ic.assertf(!ic.callerValidated, "caller has been double validated")
ic.callerValidated = true
}
func (ic *invocationContext) ValidateImmediateCallerIs(addrs ...address.Address) {
ic.assertf(!ic.callerValidated, "caller has been double validated")
ic.callerValidated = true
for _, addr := range addrs {
if ic.msg.from == addr {
return
}
}
ic.Abortf(exitcode.SysErrForbidden, "caller address %v forbidden, allowed: %v", ic.msg.from, addrs)
}
func (ic *invocationContext) ValidateImmediateCallerType(types ...cid.Cid) {
ic.assertf(!ic.callerValidated, "caller has been double validated")
ic.callerValidated = true
for _, t := range types {
if t.Equals(ic.fromActor.Code) {
return
}
}
ic.Abortf(exitcode.SysErrForbidden, "caller type %v forbidden, allowed: %v", ic.fromActor.Code, types)
}
func (ic *invocationContext) Abortf(errExitCode exitcode.ExitCode, msg string, args ...interface{}) {
ic.rt.Abortf(errExitCode, msg, args...)
}
func (ic *invocationContext) assertf(condition bool, msg string, args ...interface{}) {
if !condition {
panic(fmt.Errorf(msg, args...))
}
}
func (ic *invocationContext) ResolveAddress(address address.Address) (address.Address, bool) {
return ic.rt.NormalizeAddress(address)
}
func (ic *invocationContext) NewActorAddress() address.Address {
var buf bytes.Buffer
err := ic.topLevel.originatorStableAddress.MarshalCBOR(&buf)
if err != nil {
panic(err)
}
err = binary.Write(&buf, binary.BigEndian, ic.topLevel.originatorCallSeq)
if err != nil {
panic(err)
}
err = binary.Write(&buf, binary.BigEndian, ic.topLevel.newActorAddressCount)
if err != nil {
panic(err)
}
actorAddress, err := address.NewActorAddress(buf.Bytes())
if err != nil {
panic(err)
}
return actorAddress
}
// Send implements runtime.InvocationContext.
func (ic *invocationContext) Send(toAddr address.Address, methodNum abi.MethodNum, params cbor.Marshaler, value abi.TokenAmount, out cbor.Er) (errcode exitcode.ExitCode) {
// check if side-effects are allowed
if !ic.allowSideEffects {
ic.Abortf(exitcode.SysErrorIllegalActor, "Calling Send() is not allowed during side-effect lock")
}
from := ic.msg.to
fromActor, found, err := ic.rt.GetActor(from)
if err != nil {
ic.Abortf(exitcode.ErrIllegalState, "could not retrieve send actor %v for internal send: %v", from, err)
} else if !found {
ic.Abortf(exitcode.ErrIllegalState, "could not find send actor %v for internal send", from)
}
newMsg := InternalMessage{
from: from,
to: toAddr,
value: value,
method: methodNum,
params: params,
}
newCtx := newInvocationContext(ic.rt, ic.topLevel, newMsg, fromActor, ic.emptyObject)
ret, code := newCtx.invoke()
ic.topLevel.gasUsed = newCtx.topLevel.gasUsed
ic.stats.MergeSubStat(newCtx.toActor.Code, newMsg.method, newCtx.stats)
err = ret.Into(out)
if err != nil {
ic.Abortf(exitcode.ErrSerialization, "failed to serialize send return value into output parameter")
}
return code
}
// CreateActor implements runtime.ExtendedInvocationContext.
func (ic *invocationContext) CreateActor(codeID cid.Cid, addr address.Address) {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnCreateActor())
act, ok := ic.rt.ActorImpls[codeID]
if !ok {
ic.Abortf(exitcode.SysErrorIllegalArgument, "Can only create built-in actors.")
}
if rt.IsSingletonActor(act) {
ic.Abortf(exitcode.SysErrorIllegalArgument, "Can only have one instance of singleton actors.")
}
ic.rt.Log(rt.DEBUG, "creating actor, friendly-name: %s, Exitcode: %s, addr: %s\n", builtin.ActorNameByCode(codeID), codeID, addr)
// Check existing address. If nothing there, create empty actor.
//
// Note: we are storing the actors by ActorID *address*
_, found, err := ic.rt.GetActor(addr)
if err != nil {
panic(err)
}
if found {
ic.Abortf(exitcode.SysErrorIllegalArgument, "Actor address already exists")
}
newActor := &states.Actor{
Head: ic.emptyObject,
Code: codeID,
Balance: abi.NewTokenAmount(0),
}
if err := ic.rt.setActor(ic.rt.ctx, addr, newActor); err != nil {
panic(err)
}
}
// deleteActor implements runtime.ExtendedInvocationContext.
func (ic *invocationContext) DeleteActor(beneficiary address.Address) {
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnDeleteActor())
receiver := ic.msg.to
receiverActor, found, err := ic.rt.GetActor(receiver)
if err != nil {
panic(err)
}
if !found {
ic.Abortf(exitcode.SysErrorIllegalActor, "delete non-existent actor %v", receiverActor)
}
// Transfer any remaining balance to the beneficiary.
// This looks like it could cause a problem with gas refund going to a non-existent actor, but the gas payer
// is always an account actor, which cannot be the receiver of this message.
if receiverActor.Balance.GreaterThan(big.Zero()) {
ic.rt.transfer(receiver, beneficiary, receiverActor.Balance)
}
if err := ic.rt.deleteActor(ic.rt.ctx, receiver); err != nil {
panic(err)
}
}
func (ic *invocationContext) TotalFilCircSupply() abi.TokenAmount {
return ic.topLevel.circSupply
}
func (ic *invocationContext) Context() context.Context {
return ic.rt.ctx
}
func (ic *invocationContext) ChargeGas(name string, compute int64, virtual int64) {
ic.topLevel.chargeGas(GasCharge{
Name: name,
ComputeGas: compute,
VirtualCompute: virtual,
})
}
// Starts a new tracing span. The span must be End()ed explicitly, typically with a deferred invocation.
func (ic *invocationContext) StartSpan(_ string) func() {
return fakeTraceSpanEnd
}
// Provides the system call interface.
func (ic *invocationContext) Syscalls() runtime.Syscalls {
return fakeSyscalls{receiver: ic.msg.to, epoch: ic.rt.currentEpoch}
}
// Note events that may make debugging easier
func (ic *invocationContext) Log(level rt.LogLevel, msg string, args ...interface{}) {
ic.rt.Log(level, msg, args...)
}
type returnWrapper struct {
inner cbor.Marshaler
}
func (r returnWrapper) Into(o cbor.Unmarshaler) error {
if r.inner == nil {
return fmt.Errorf("failed to unmarshal nil return (did you mean abi.Empty?)")
}
b := bytes.Buffer{}
if err := r.inner.MarshalCBOR(&b); err != nil {
return err
}
return o.UnmarshalCBOR(&b)
}
/////////////////////////////////////////////
// Fake syscalls
/////////////////////////////////////////////
type fakeSyscalls struct {
receiver address.Address
epoch abi.ChainEpoch
}
func (s fakeSyscalls) VerifySignature(_ crypto.Signature, _ address.Address, _ []byte) error {
return nil
}
func (s fakeSyscalls) HashBlake2b(b []byte) [32]byte {
return blake2b.Sum256(b)
}
// Prefix for testing unsealed sector CIDs (CommD).
var UnsealedCIDPrefix = cid.Prefix{
Version: 1,
Codec: cid.FilCommitmentUnsealed,
MhType: mh.POSEIDON_BLS12_381_A1_FC1,
MhLength: 32,
}
func (s fakeSyscalls) ComputeUnsealedSectorCID(_ abi.RegisteredSealProof, _ []abi.PieceInfo) (cid.Cid, error) {
return testing.MakeCID("presealedSectorCID", &UnsealedCIDPrefix), nil
}
func (s fakeSyscalls) VerifySeal(_ proof.SealVerifyInfo) error {
return nil
}
func (s fakeSyscalls) BatchVerifySeals(vi map[address.Address][]proof.SealVerifyInfo) (map[address.Address][]bool, error) {
res := map[address.Address][]bool{}
for addr, infos := range vi { //nolint:nomaprange
verified := make([]bool, len(infos))
for i := range infos {
// everyone wins
verified[i] = true
}
res[addr] = verified
}
return res, nil
}
func (s fakeSyscalls) VerifyAggregateSeals(agg proof.AggregateSealVerifyProofAndInfos) error {
return nil
}
func (s fakeSyscalls) VerifyPoSt(_ proof.WindowPoStVerifyInfo) error {
return nil
}
func (s fakeSyscalls) VerifyConsensusFault(_, _, _ []byte) (*runtime.ConsensusFault, error) {
return &runtime.ConsensusFault{
Target: s.receiver,
Epoch: s.epoch - 1,
Type: runtime.ConsensusFaultDoubleForkMining,
}, nil
}
/////////////////////////////////////////////
// Fake trace span
/////////////////////////////////////////////
func fakeTraceSpanEnd() {
}
/////////////////////////////////////////////
// storeWrapper
/////////////////////////////////////////////
type storeWrapper struct {
s adt.Store
rt *VM
}
func (s storeWrapper) StoreGet(c cid.Cid, o cbor.Unmarshaler) bool {
err := s.s.Get(s.rt.ctx, c, o)
// assume all errors are not found errors (bad assumption, but ok for testing)
return err == nil
}
func (s storeWrapper) StorePut(x cbor.Marshaler) cid.Cid {
c, err := s.s.Put(s.rt.ctx, x)
if err != nil {
s.rt.Abortf(exitcode.ErrIllegalState, "could not put object in store")
}
return c
}
/////////////////////////////////////////////
// invocation
/////////////////////////////////////////////
// runtime aborts are trapped by invoke, it will always return an exit code.
func (ic *invocationContext) invoke() (ret returnWrapper, errcode exitcode.ExitCode) {
// Checkpoint state, for restoration on rollback
// Note that changes prior to invocation (sequence number bump and gas prepayment) persist even if invocation fails.
priorRoot, err := ic.rt.checkpoint()
if err != nil {
panic(err)
}
ic.rt.startInvocation(&ic.msg)
// Install handler for abort, which rolls back all state changes from this and any nested invocations.
// This is the only path by which a non-OK exit code may be returned.
defer func() {
ic.stats.Capture()
if r := recover(); r != nil {
if err := ic.rt.rollback(priorRoot); err != nil {
panic(err)
}
switch r := r.(type) {
case abort:
ic.rt.Log(rt.WARN, "Abort during actor execution. errMsg: %v exitCode: %d sender: %v receiver; %v method: %d value %v",
r, r.code, ic.msg.from, ic.msg.to, ic.msg.method, ic.msg.value)
ic.rt.endInvocation(r.code, abi.Empty)
ret = returnWrapper{abi.Empty} // The Empty here should never be used, but slightly safer than zero value.
errcode = r.code
return
default:
// do not trap unknown panics
debug.PrintStack()
panic(r)
}
}
}()
// pre-dispatch
// 1. load target actor
// 2. charge gas for method invoc
// 3. transfer optional funds
// 4. short-circuit _Send_ method
// 5. load target actor code
// 6. create target state handle
// assert from address is an ID address.
if ic.msg.from.Protocol() != address.ID {
panic("bad Exitcode: sender address MUST be an ID address at invocation time")
}
// 2. load target actor
// Note: we replace the "to" address with the normalized version
ic.toActor, ic.msg.to = ic.resolveTarget(ic.msg.to)
// 3. charge gas for method invocation
ic.topLevel.chargeGas(ic.topLevel.gasPrices.OnMethodInvocation(ic.msg.value, ic.msg.method))
// 4. transfer funds carried by the msg
if !ic.msg.value.NilOrZero() {
if ic.msg.value.LessThan(big.Zero()) {
ic.Abortf(exitcode.SysErrForbidden, "attempt to transfer negative value %s from %s to %s",
ic.msg.value, ic.msg.from, ic.msg.to)
}
if ic.fromActor.Balance.LessThan(ic.msg.value) {
ic.Abortf(exitcode.SysErrInsufficientFunds, "sender %s insufficient balance %s to transfer %s to %s",
ic.msg.from, ic.fromActor.Balance, ic.msg.value, ic.msg.to)
}
ic.toActor, ic.fromActor = ic.rt.transfer(ic.msg.from, ic.msg.to, ic.msg.value)
}
// 5. if we are just sending funds, there is nothing else to do.
if ic.msg.method == builtin.MethodSend {
ic.rt.endInvocation(exitcode.Ok, abi.Empty)
return returnWrapper{abi.Empty}, exitcode.Ok
}
// 6. load target actor code
actorImpl := ic.rt.getActorImpl(ic.toActor.Code)
// dispatch
out, err := ic.dispatch(actorImpl, ic.msg.method, ic.msg.params)
if err != nil {
ic.Abortf(exitcode.SysErrInvalidMethod, "could not dispatch method")
}
// assert output implements expected interface
var marsh cbor.Marshaler = abi.Empty
if out != nil {
var ok bool
marsh, ok = out.(cbor.Marshaler)
if !ok {
ic.Abortf(exitcode.SysErrorIllegalActor, "Returned value is not a CBORMarshaler")
}
}
ret = returnWrapper{inner: marsh}
ic.checkStateObjectsUnmodified()
// 3. success!
ic.rt.endInvocation(exitcode.Ok, marsh)
return ret, exitcode.Ok
}
func (ic *invocationContext) dispatch(actor runtime.VMActor, method abi.MethodNum, arg interface{}) (interface{}, error) {
// get method signature
exports := actor.Exports()
// get method entry
methodIdx := (uint64)(method)
if len(exports) <= (int)(methodIdx) {
return nil, fmt.Errorf("method undefined. method: %d, Exitcode: %s", method, actor.Code())
}
entry := exports[methodIdx]
if entry == nil {
return nil, fmt.Errorf("method undefined. method: %d, Exitcode: %s", method, actor.Code())
}
ventry := reflect.ValueOf(entry)
// build args to pass to the method
args := []reflect.Value{
// the ctx will be automatically coerced
reflect.ValueOf(ic),
}
t := ventry.Type().In(1)
if arg == nil {
args = append(args, reflect.New(t).Elem())
} else if raw, ok := arg.([]byte); ok {
obj, err := decodeBytes(t, raw)
if err != nil {
return nil, err
}
args = append(args, reflect.ValueOf(obj))
} else if raw, ok := arg.(builtin.CBORBytes); ok {
obj, err := decodeBytes(t, raw)
if err != nil {
return nil, err
}
args = append(args, reflect.ValueOf(obj))
} else {
args = append(args, reflect.ValueOf(arg))
}
// invoke the method
out := ventry.Call(args)
// Note: we only support single objects being returned
if len(out) > 1 {
return nil, fmt.Errorf("actor method returned more than one object. method: %d, Exitcode: %s", method, actor.Code())
}
// method returns unit
// Note: we need to check for `IsNill()` here because Go doesnt work if you do `== nil` on the interface
if len(out) == 0 || (out[0].Kind() != reflect.Struct && out[0].IsNil()) {
return nil, nil
}
// forward return
return out[0].Interface(), nil
}
// resolveTarget loads and actor and returns its ActorID address.
//
// If the target actor does not exist, and the target address is a pub-key address,
// a new account actor will be created.
// Otherwise, this method will abort execution.
func (ic *invocationContext) resolveTarget(target address.Address) (*states.Actor, address.Address) {
// resolve the target address via the InitActor, and attempt to load state.
initActorEntry, found, err := ic.rt.GetActor(builtin.InitActorAddr)
if err != nil {
panic(err)
}
if !found {
ic.Abortf(exitcode.SysErrSenderInvalid, "init actor not found")
}
if target == builtin.InitActorAddr {
return initActorEntry, target
}
// get a view into the actor state
var state init_.State
if err := ic.rt.store.Get(ic.rt.ctx, initActorEntry.Head, &state); err != nil {
panic(err)
}
// lookup the ActorID based on the address
targetIDAddr, found, err := state.ResolveAddress(ic.rt.store, target)
created := false
if err != nil {
panic(err)
} else if !found {
if target.Protocol() != address.SECP256K1 && target.Protocol() != address.BLS {
// Don't implicitly create an account actor for an address without an associated key.
ic.Abortf(exitcode.SysErrInvalidReceiver, "cannot create account for address type")
}
targetIDAddr, err = state.MapAddressToNewID(ic.rt.store, target)
if err != nil {
panic(err)
}
// store new state
initHead, err := ic.rt.store.Put(ic.rt.ctx, &state)
if err != nil {
panic(err)
}
// update init actor
initActorEntry.Head = initHead
if err := ic.rt.setActor(ic.rt.ctx, builtin.InitActorAddr, initActorEntry); err != nil {
panic(err)
}
ic.CreateActor(builtin.AccountActorCodeID, targetIDAddr)
// call constructor on account
newMsg := InternalMessage{
from: builtin.SystemActorAddr,
to: targetIDAddr,
value: big.Zero(),
method: builtin.MethodsAccount.Constructor,
// use original address as constructor params
// Note: constructor takes a pointer
params: &target,
}
newCtx := newInvocationContext(ic.rt, ic.topLevel, newMsg, nil, ic.emptyObject)
_, code := newCtx.invoke()
ic.topLevel.gasUsed = newCtx.topLevel.gasUsed
ic.stats.MergeSubStat(builtin.InitActorCodeID, builtin.MethodsAccount.Constructor, newCtx.stats)
if code.IsError() {
// we failed to construct an account actor..
ic.Abortf(code, "failed to construct account actor")
}
created = true
}
// load actor
targetActor, found, err := ic.rt.GetActor(targetIDAddr)
if err != nil {
panic(err)
}
if !found && created {
panic(fmt.Errorf("unreachable: actor is supposed to exist but it does not. addr: %s, idAddr: %s", target, targetIDAddr))
}
if !found {
ic.Abortf(exitcode.SysErrInvalidReceiver, "actor at address %s registered but not found", targetIDAddr.String())
}
return targetActor, targetIDAddr
}
func (ic *invocationContext) replace(obj cbor.Marshaler) cid.Cid {
actr, found, err := ic.rt.GetActor(ic.msg.to)
if err != nil {
panic(err)
}
if !found {
ic.rt.Abortf(exitcode.ErrIllegalState, "failed to find actor %s for state", ic.msg.to)
}
c := ic.StorePut(obj)
actr.Head = c
err = ic.rt.setActor(ic.rt.ctx, ic.msg.to, actr)
if err != nil {
ic.rt.Abortf(exitcode.ErrIllegalState, "could not save actor %s", ic.msg.to)
}
return c
}
// Checks that state objects weren't modified outside of transaction.
func (ic *invocationContext) checkStateObjectsUnmodified() {
for obj, expectedKey := range ic.stateUsedObjs { // nolint:nomaprange
// Recompute the CID of the object and check it's the same as was recorded
// when the object was loaded.
finalKey, _, err := ipld.MarshalCBOR(obj)
if err != nil {
ic.Abortf(exitcode.SysErrorIllegalActor, "error marshalling state object for validation: %v", err)
}
if finalKey != expectedKey {
ic.Abortf(exitcode.SysErrorIllegalActor, "State mutated outside of transaction scope")
}
}
}
func decodeBytes(t reflect.Type, argBytes []byte) (interface{}, error) {
// decode arg1 (this is the payload for the actor method)
v := reflect.New(t)
// This would be better fixed in then encoding library.
obj := v.Elem().Interface()
if _, ok := obj.(cbor.Unmarshaler); !ok {
return nil, xerrors.New("method argument cannot be decoded")
}
buf := bytes.NewBuffer(argBytes)
auxv := reflect.New(t.Elem())
obj = auxv.Interface()
unmarsh := obj.(cbor.Unmarshaler)
if err := unmarsh.UnmarshalCBOR(buf); err != nil {
return nil, err
}
return unmarsh, nil
}