-
Notifications
You must be signed in to change notification settings - Fork 459
/
vmcontext.go
743 lines (634 loc) · 21.9 KB
/
vmcontext.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
package vmcontext
import (
"context"
"fmt"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/venus/pkg/constants"
cbor "github.com/ipfs/go-ipld-cbor"
"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/exitcode"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/pkg/errors"
rt5 "github.com/filecoin-project/specs-actors/v5/actors/runtime"
"github.com/filecoin-project/venus/pkg/state/tree"
"github.com/filecoin-project/venus/pkg/vm/dispatch"
"github.com/filecoin-project/venus/pkg/vm/gas"
"github.com/filecoin-project/venus/pkg/vm/runtime"
"github.com/filecoin-project/venus/venus-shared/actors/adt"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
initActor "github.com/filecoin-project/venus/venus-shared/actors/builtin/init"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/reward"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
)
const MaxCallDepth = 4096
var vmlog = logging.Logger("vm.context")
// LegacyVM holds the stateView and executes messages over the stateView.
type LegacyVM struct {
context context.Context
actorImpls ActorImplLookup
bsstore *blockstoreutil.BufferedBS
store cbor.IpldStore
currentEpoch abi.ChainEpoch
pricelist gas.Pricelist
debugger *VMDebugMsg // nolint
vmOption VmOption
baseCircSupply abi.TokenAmount
State tree.Tree
}
func (vm *LegacyVM) ApplyImplicitMessage(ctx context.Context, msg types.ChainMsg) (*Ret, error) {
unsignedMsg := msg.VMMessage()
imsg := VmMessage{
From: unsignedMsg.From,
To: unsignedMsg.To,
Value: unsignedMsg.Value,
Method: unsignedMsg.Method,
Params: unsignedMsg.Params,
}
return vm.applyImplicitMessage(imsg)
}
// ActorImplLookup provides access To upgradeable actor code.
type ActorImplLookup interface {
GetActorImpl(code cid.Cid, rt runtime.Runtime) (dispatch.Dispatcher, *dispatch.ExcuteError)
}
// implement VMInterpreter for LegacyVM
var _ VMInterpreter = (*LegacyVM)(nil)
var _ Interface = (*LegacyVM)(nil)
// NewLegacyVM creates a new runtime for executing messages.
// Dragons: change To take a root and the store, build the tree internally
func NewLegacyVM(ctx context.Context, actorImpls ActorImplLookup, vmOption VmOption) (*LegacyVM, error) {
buf := blockstoreutil.NewBufferedBstore(vmOption.Bsstore)
cst := cbor.NewCborStore(buf)
var st tree.Tree
var err error
if vmOption.PRoot == cid.Undef {
// just for chain gen
st, err = tree.NewState(cst, tree.StateTreeVersion1)
if err != nil {
panic(fmt.Errorf("create state error, should never come here"))
}
} else {
st, err = tree.LoadState(context.Background(), cst, vmOption.PRoot)
if err != nil {
return nil, err
}
}
baseCirc, err := vmOption.CircSupplyCalculator(ctx, vmOption.Epoch, st)
if err != nil {
return nil, err
}
return &LegacyVM{
context: ctx,
actorImpls: actorImpls,
bsstore: buf,
store: cst,
State: st,
vmOption: vmOption,
baseCircSupply: baseCirc,
pricelist: vmOption.GasPriceSchedule.PricelistByEpoch(vmOption.Epoch),
currentEpoch: vmOption.Epoch,
}, nil
}
// nolint
func (vm *LegacyVM) setDebugger() {
vm.debugger = NewVMDebugMsg()
}
// ApplyGenesisMessage forces the execution of a message in the vm actor.
//
// This Method is intended To be used in the generation of the genesis block only.
func (vm *LegacyVM) ApplyGenesisMessage(from address.Address, to address.Address, method abi.MethodNum, value abi.TokenAmount, params interface{}) (*Ret, error) {
// normalize From addr
var ok bool
if from, ok = vm.normalizeAddress(from); !ok {
runtime.Abort(exitcode.SysErrSenderInvalid)
}
// build internal message
imsg := VmMessage{
From: from,
To: to,
Value: value,
Method: method,
Params: params,
}
ret, err := vm.applyImplicitMessage(imsg)
if err != nil {
return ret, err
}
// commit
if _, err := vm.Flush(vm.context); err != nil {
return nil, err
}
return ret, nil
}
// ContextStore provides access To specs-actors adt library.
//
// This type of store is used To access some internal actor stateView.
func (vm *LegacyVM) ContextStore() adt.Store {
return adt.WrapStore(vm.context, vm.store)
}
func (vm *LegacyVM) normalizeAddress(addr address.Address) (address.Address, bool) {
// short-circuit if the address is already an ID address
if addr.Protocol() == address.ID {
return addr, true
}
// resolve the target address via the InitActor, and attempt To load stateView.
initActorEntry, found, err := vm.State.GetActor(vm.context, initActor.Address)
if err != nil {
panic(errors.Wrapf(err, "failed To load init actor"))
}
if !found {
panic(errors.Wrapf(err, "no init actor"))
}
// get a view into the actor stateView
initActorState, err := initActor.Load(adt.WrapStore(vm.context, vm.store), initActorEntry)
if err != nil {
panic(err)
}
idAddr, found, err := initActorState.ResolveAddress(addr)
if !found {
return address.Undef, false
}
if err != nil {
panic(err)
}
return idAddr, true
}
// applyImplicitMessage applies messages automatically generated by the vm itself.
//
// This messages do not consume client gas and must not fail.
func (vm *LegacyVM) applyImplicitMessage(imsg VmMessage) (*Ret, error) {
// implicit messages gas is tracked separatly and not paid by the miner
gasTank := gas.NewGasTracker(constants.BlockGasLimit * 10000)
// the execution of the implicit messages is simpler than full external/actor-actor messages
// execution:
// 1. load From actor
// 2. build new context
// 3. invoke message
// 1. load From actor
fromActor, found, err := vm.State.GetActor(vm.context, imsg.From)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("implicit message `From` field actor not found, addr: %s", imsg.From)
}
// 2. build context
topLevel := topLevelContext{
originatorStableAddress: imsg.From,
originatorCallSeq: fromActor.Nonce, // Implied Nonce is that of the actor before incrementing.
newActorAddressCount: 0,
}
gasBsstore := &GasChargeBlockStore{
inner: vm.bsstore,
pricelist: vm.pricelist,
gasTank: gasTank,
}
cst := cbor.NewCborStore(gasBsstore)
ctx := newInvocationContext(vm, cst, &topLevel, imsg, gasTank, vm.vmOption.Rnd, nil)
// 3. invoke message
ret, code := ctx.invoke()
if code.IsError() {
return nil, fmt.Errorf("invalid exit code %d during implicit message execution: From %s, To %s, Method %d, Value %s, Params %v",
code, imsg.From, imsg.To, imsg.Method, imsg.Value, imsg.Params)
}
return &Ret{
GasTracker: gasTank,
OutPuts: gas.GasOutputs{},
Receipt: types.MessageReceipt{
ExitCode: code,
Return: ret,
GasUsed: 0,
},
}, nil
}
// Get the buffered blockstore associated with the LegacyVM. This includes any temporary blocks produced
// during this LegacyVM's execution.
func (vm *LegacyVM) ActorStore(ctx context.Context) adt.Store {
return adt.WrapStore(ctx, vm.store)
}
// todo estimate gasLimit
func (vm *LegacyVM) ApplyMessage(ctx context.Context, msg types.ChainMsg) (*Ret, error) {
return vm.applyMessage(msg.VMMessage(), msg.ChainLength())
}
// applyMessage applies the message To the current stateView.
func (vm *LegacyVM) applyMessage(msg *types.Message, onChainMsgSize int) (*Ret, error) {
// This Method does not actually execute the message itself,
// but rather deals with the pre/post processing of a message.
// (see: `invocationContext.invoke()` for the dispatch and execution)
// initiate gas tracking
gasTank := gas.NewGasTracker(msg.GasLimit)
// pre-send
// 1. charge for message existence
// 2. load sender actor
// 3. check message seq number
// 4. check sender gas fee is enough
// 5. increment message seq number
// 6. withheld maximum gas From _sender_
// 7. snapshot stateView
// 1. charge for bytes used in chain
msgGasCost := vm.pricelist.OnChainMessage(onChainMsgSize) // todo get price list by height
ok := gasTank.TryCharge(msgGasCost)
if !ok {
gasOutputs := gas.ZeroGasOutputs()
gasOutputs.MinerPenalty = big.Mul(vm.vmOption.BaseFee, big.NewInt(msgGasCost.Total()))
// Invalid message; insufficient gas limit To pay for the on-chain message size.
// Note: the miner needs To pay the full msg cost, not what might have been partially consumed
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: Failure(exitcode.SysErrOutOfGas, 0),
}, nil
}
minerPenaltyAmount := big.Mul(vm.vmOption.BaseFee, big.NewInt(msg.GasLimit))
// 2. load sender actor and check send whether to be an account
fromActor, found, err := vm.State.GetActor(vm.context, msg.From)
if err != nil {
return nil, err
}
if !found {
// Execution error; sender does not exist at time of message execution.
gasOutputs := gas.ZeroGasOutputs()
gasOutputs.MinerPenalty = minerPenaltyAmount
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: Failure(exitcode.SysErrSenderInvalid, 0),
}, nil
}
if !builtin.IsAccountActor(fromActor.Code) /*!fromActor.Code.Equals(builtin.AccountActorCodeID)*/ {
// Execution error; sender is not an account.
gasOutputs := gas.ZeroGasOutputs()
gasOutputs.MinerPenalty = minerPenaltyAmount
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: Failure(exitcode.SysErrSenderInvalid, 0),
}, nil
}
// 3. make sure this is the right message order for fromActor
if msg.Nonce != fromActor.Nonce {
// Execution error; invalid seq number.
gasOutputs := gas.ZeroGasOutputs()
gasOutputs.MinerPenalty = minerPenaltyAmount
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: Failure(exitcode.SysErrSenderStateInvalid, 0),
}, nil
}
// 4. Check sender gas fee is enough
gasLimitCost := big.Mul(big.NewIntUnsigned(uint64(msg.GasLimit)), msg.GasFeeCap)
if fromActor.Balance.LessThan(gasLimitCost) {
// Execution error; sender does not have sufficient funds To pay for the gas limit.
gasOutputs := gas.ZeroGasOutputs()
gasOutputs.MinerPenalty = minerPenaltyAmount
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: Failure(exitcode.SysErrSenderStateInvalid, 0),
}, nil
}
gasHolder := &types.Actor{Balance: big.NewInt(0)}
if err := vm.transferToGasHolder(msg.From, gasHolder, gasLimitCost); err != nil {
return nil, fmt.Errorf("failed To withdraw gas funds: %w", err)
}
// 5. increment sender Nonce
if err = vm.State.MutateActor(msg.From, func(msgFromActor *types.Actor) error {
msgFromActor.IncrementSeqNum()
return nil
}); err != nil {
return nil, err
}
// 7. snapshot stateView
// Even if the message fails, the following accumulated changes will be applied:
// - CallSeqNumber increment
// - sender balance withheld
err = vm.snapshot()
if err != nil {
return nil, err
}
defer vm.clearSnapshot()
// send
// 1. build internal message
// 2. build invocation context
// 3. process the msg
topLevel := topLevelContext{
originatorStableAddress: msg.From,
originatorCallSeq: msg.Nonce,
newActorAddressCount: 0,
}
// 1. build internal msg
imsg := VmMessage{
From: msg.From,
To: msg.To,
Value: msg.Value,
Method: msg.Method,
Params: msg.Params,
}
// 2. build invocation context
gasBsstore := &GasChargeBlockStore{
inner: vm.bsstore,
pricelist: vm.pricelist,
gasTank: gasTank,
}
cst := cbor.NewCborStore(gasBsstore)
// cst.Atlas = vm.store.Atlas // associate the atlas. //todo
// Note replace from and to address here
ctx := newInvocationContext(vm, cst, &topLevel, imsg, gasTank, vm.vmOption.Rnd, nil)
// 3. invoke
ret, code := ctx.invoke()
// post-send
// 1. charge gas for putting the return Value on the chain
// 2. settle gas money around (unused_gas -> sender)
// 3. success!
// 1. charge for the space used by the return Value
// Note: the GasUsed in the message receipt does not
ok = gasTank.TryCharge(vm.pricelist.OnChainReturnValue(len(ret)))
if !ok {
// Insufficient gas remaining To cover the on-chain return Value; proceed as in the case
// of Method execution failure.
code = exitcode.SysErrOutOfGas
ret = []byte{}
}
// Roll back all stateView if the receipt's exit code is not ok.
// This is required in addition To revert within the invocation context since top level messages can fail for
// more reasons than internal ones. Invocation context still needs its own revert so actors can recover and
// proceed From a nested call failure.
if code != exitcode.Ok {
if err := vm.revert(); err != nil {
return nil, err
}
}
// 2. settle gas money around (unused_gas -> sender)
gasUsed := gasTank.GasUsed
if gasUsed < 0 {
gasUsed = 0
}
burn, err := vm.shouldBurn(vm.context, msg, code)
if err != nil {
return nil, fmt.Errorf("deciding whether should burn failed: %w", err)
}
gasOutputs := gas.ComputeGasOutputs(gasUsed, msg.GasLimit, vm.vmOption.BaseFee, msg.GasFeeCap, msg.GasPremium, burn)
if err := vm.transferFromGasHolder(builtin.BurntFundsActorAddr, gasHolder, gasOutputs.BaseFeeBurn); err != nil {
return nil, fmt.Errorf("failed To burn base fee: %w", err)
}
if err := vm.transferFromGasHolder(reward.Address, gasHolder, gasOutputs.MinerTip); err != nil {
return nil, fmt.Errorf("failed To give miner gas reward: %w", err)
}
if err := vm.transferFromGasHolder(builtin.BurntFundsActorAddr, gasHolder, gasOutputs.OverEstimationBurn); err != nil {
return nil, fmt.Errorf("failed To burn overestimation fee: %w", err)
}
// refund unused gas
if err := vm.transferFromGasHolder(msg.From, gasHolder, gasOutputs.Refund); err != nil {
return nil, fmt.Errorf("failed To refund gas: %w", err)
}
if big.Cmp(big.NewInt(0), gasHolder.Balance) != 0 {
return nil, fmt.Errorf("gas handling math is wrong")
}
// 3. Success!
return &Ret{
GasTracker: gasTank,
OutPuts: gasOutputs,
Receipt: types.MessageReceipt{
ExitCode: code,
Return: ret,
GasUsed: gasUsed,
},
}, nil
}
func (vm *LegacyVM) shouldBurn(ctx context.Context, msg *types.Message, errcode exitcode.ExitCode) (bool, error) {
if vm.NetworkVersion() <= network.Version12 {
// Check to see if we should burn funds. We avoid burning on successful
// window post. This won't catch _indirect_ window post calls, but this
// is the best we can get for now.
if vm.currentEpoch > vm.vmOption.Fork.GetForkUpgrade().UpgradeClausHeight && errcode == exitcode.Ok && msg.Method == builtintypes.MethodsMiner.SubmitWindowedPoSt {
// Ok, we've checked the _method_, but we still need to check
// the target actor. It would be nice if we could just look at
// the trace, but I'm not sure if that's safe?
if toActor, _, err := vm.State.GetActor(vm.context, msg.To); err != nil {
// If the actor wasn't found, we probably deleted it or something. Move on.
if !errors.Is(err, types.ErrActorNotFound) {
// Otherwise, this should never fail and something is very wrong.
return false, fmt.Errorf("failed to lookup target actor: %w", err)
}
} else if builtin.IsStorageMinerActor(toActor.Code) {
// Ok, this is a storage miner and we've processed a window post. Remove the burn.
return false, nil
}
}
return true, nil
}
// Any "don't burn" rules from Network v13 onwards go here, for now we always return true
return true, nil
}
// transfer debits money From one account and credits it To another.
// avoid calling this Method with a zero amount else it will perform unnecessary actor loading.
//
// WARNING: this Method will panic if the the amount is negative, accounts dont exist, or have inssuficient funds.
//
// Note: this is not idiomatic, it follows the Spec expectations for this Method.
func (vm *LegacyVM) transfer(from address.Address, to address.Address, amount abi.TokenAmount, networkVersion network.Version) {
var fromActor *types.Actor
var fromID, toID address.Address
var err error
var found bool
// switching the order around so that transactions for more than the balance sent to self fail
if networkVersion >= network.Version15 {
if amount.LessThan(big.Zero()) {
runtime.Abortf(exitcode.SysErrForbidden, "attempt To transfer negative Value %s From %s To %s", amount, from, to)
}
fromID, err = vm.State.LookupID(from)
if err != nil {
panic(fmt.Errorf("transfer failed when resolving sender address: %s", err))
}
// retrieve sender account
fromActor, found, err = vm.State.GetActor(vm.context, fromID)
if err != nil {
panic(err)
}
if !found {
panic(fmt.Errorf("unreachable: sender account not found. %s", err))
}
// check that account has enough balance for transfer
if fromActor.Balance.LessThan(amount) {
runtime.Abortf(exitcode.SysErrInsufficientFunds, "sender %s insufficient balance %s To transfer %s To %s", amount, fromActor.Balance, from, to)
}
if from == to {
vmlog.Infow("sending to same address: noop", "from/to addr", from)
return
}
toID, err = vm.State.LookupID(to)
if err != nil {
panic(fmt.Errorf("transfer failed when resolving receiver address: %s", err))
}
if fromID == toID {
vmlog.Infow("sending to same actor ID: noop", "from/to actor", fromID)
return
}
} else {
if from == to {
return
}
fromID, err = vm.State.LookupID(from)
if err != nil {
panic(fmt.Errorf("transfer failed when resolving sender address: %s", err))
}
toID, err = vm.State.LookupID(to)
if err != nil {
panic(fmt.Errorf("transfer failed when resolving receiver address: %s", err))
}
if fromID == toID {
return
}
if amount.LessThan(types.NewInt(0)) {
runtime.Abortf(exitcode.SysErrForbidden, "attempt To transfer negative Value %s From %s To %s", amount, from, to)
}
// retrieve sender account
fromActor, found, err = vm.State.GetActor(vm.context, fromID)
if err != nil {
panic(err)
}
if !found {
panic(fmt.Errorf("unreachable: sender account not found. %s", err))
}
}
// retrieve receiver account
toActor, found, err := vm.State.GetActor(vm.context, toID)
if err != nil {
panic(err)
}
if !found {
panic(fmt.Errorf("unreachable: credit account not found. %s", err))
}
// check that account has enough balance for transfer
if fromActor.Balance.LessThan(amount) {
runtime.Abortf(exitcode.SysErrInsufficientFunds, "sender %s insufficient balance %s To transfer %s To %s", amount, fromActor.Balance, from, to)
}
// deduct funds
fromActor.Balance = big.Sub(fromActor.Balance, amount)
if err := vm.State.SetActor(vm.context, from, fromActor); err != nil {
panic(err)
}
// deposit funds
toActor.Balance = big.Add(toActor.Balance, amount)
if err := vm.State.SetActor(vm.context, to, toActor); err != nil {
panic(err)
}
}
func (vm *LegacyVM) getActorImpl(code cid.Cid, runtime2 runtime.Runtime) dispatch.Dispatcher {
actorImpl, err := vm.actorImpls.GetActorImpl(code, runtime2)
if err != nil {
runtime.Abort(exitcode.SysErrInvalidReceiver)
}
return actorImpl
}
//
// implement runtime.Runtime for LegacyVM
//
var _ runtime.Runtime = (*LegacyVM)(nil)
// CurrentEpoch implements runtime.Runtime.
func (vm *LegacyVM) CurrentEpoch() abi.ChainEpoch {
return vm.currentEpoch
}
func (vm *LegacyVM) NetworkVersion() network.Version {
return vm.vmOption.NetworkVersion
}
func (vm *LegacyVM) transferToGasHolder(addr address.Address, gasHolder *types.Actor, amt abi.TokenAmount) error {
if amt.LessThan(big.NewInt(0)) {
return fmt.Errorf("attempted To transfer negative Value To gas holder")
}
return vm.State.MutateActor(addr, func(a *types.Actor) error {
if err := deductFunds(a, amt); err != nil {
return err
}
depositFunds(gasHolder, amt)
return nil
})
}
func (vm *LegacyVM) transferFromGasHolder(addr address.Address, gasHolder *types.Actor, amt abi.TokenAmount) error {
if amt.LessThan(big.NewInt(0)) {
return fmt.Errorf("attempted To transfer negative Value From gas holder")
}
if amt.Equals(big.NewInt(0)) {
return nil
}
return vm.State.MutateActor(addr, func(a *types.Actor) error {
if err := deductFunds(gasHolder, amt); err != nil {
return err
}
depositFunds(a, amt)
return nil
})
}
func (vm *LegacyVM) StateTree() tree.Tree {
return vm.State
}
func (vm *LegacyVM) GetCircSupply(ctx context.Context) (abi.TokenAmount, error) {
// Before v15, this was recalculated on each invocation as the state tree was mutated
if vm.vmOption.NetworkVersion <= network.Version14 {
return vm.vmOption.CircSupplyCalculator(ctx, vm.currentEpoch, vm.State)
}
return vm.baseCircSupply, nil
}
func deductFunds(act *types.Actor, amt abi.TokenAmount) error {
if act.Balance.LessThan(amt) {
return fmt.Errorf("not enough funds")
}
act.Balance = big.Sub(act.Balance, amt)
return nil
}
func depositFunds(act *types.Actor, amt abi.TokenAmount) {
act.Balance = big.Add(act.Balance, amt)
}
//
// implement runtime.MessageInfo for VmMessage
//
var _ rt5.Message = (*VmMessage)(nil)
type VmMessage struct { //nolint
From address.Address
To address.Address
Value abi.TokenAmount
Method abi.MethodNum
Params interface{}
}
// ValueReceived implements runtime.MessageInfo.
func (msg VmMessage) ValueReceived() abi.TokenAmount {
return msg.Value
}
// Caller implements runtime.MessageInfo.
func (msg VmMessage) Caller() address.Address {
return msg.From
}
// Receiver implements runtime.MessageInfo.
func (msg VmMessage) Receiver() address.Address {
return msg.To
}
func (vm *LegacyVM) revert() error {
return vm.State.Revert()
}
func (vm *LegacyVM) snapshot() error {
err := vm.State.Snapshot(vm.context)
if err != nil {
return err
}
return nil
}
func (vm *LegacyVM) clearSnapshot() {
vm.State.ClearSnapshot()
}
// nolint
func (vm *LegacyVM) Flush(ctx context.Context) (tree.Root, error) {
// Flush all blocks out of the store
if root, err := vm.State.Flush(vm.context); err != nil {
return cid.Undef, err
} else {
if err := blockstoreutil.CopyBlockstore(context.TODO(), vm.bsstore.Write(), vm.bsstore.Read()); err != nil {
return cid.Undef, fmt.Errorf("copying tree: %w", err)
}
return root, nil
}
}