2020-03-10 02:24:02 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
2020-03-23 06:39:42 +00:00
|
|
|
"fmt"
|
2021-12-10 22:27:44 +00:00
|
|
|
"os"
|
2020-12-01 12:53:30 +00:00
|
|
|
gruntime "runtime"
|
2020-06-11 19:59:39 +00:00
|
|
|
"time"
|
2020-05-27 20:53:20 +00:00
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-14 21:53:57 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/cbor"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2020-09-14 11:45:20 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
rtt "github.com/filecoin-project/go-state-types/rt"
|
2022-05-15 19:26:52 +00:00
|
|
|
rt0 "github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
rt2 "github.com/filecoin-project/specs-actors/v2/actors/runtime"
|
|
|
|
rt3 "github.com/filecoin-project/specs-actors/v3/actors/runtime"
|
|
|
|
rt4 "github.com/filecoin-project/specs-actors/v4/actors/runtime"
|
|
|
|
rt5 "github.com/filecoin-project/specs-actors/v5/actors/runtime"
|
|
|
|
rt6 "github.com/filecoin-project/specs-actors/v6/actors/runtime"
|
2021-11-04 15:59:29 +00:00
|
|
|
rt7 "github.com/filecoin-project/specs-actors/v7/actors/runtime"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-09-14 19:41:55 +00:00
|
|
|
ipldcbor "github.com/ipfs/go-ipld-cbor"
|
2020-03-10 02:24:02 +00:00
|
|
|
"go.opencensus.io/trace"
|
2020-04-03 21:38:11 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-04-16 23:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-04-08 00:03:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
2022-04-08 00:03:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2020-09-24 02:45:53 +00:00
|
|
|
type Message struct {
|
|
|
|
msg types.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Caller() address.Address {
|
|
|
|
if m.msg.From.Protocol() != address.ID {
|
|
|
|
panic("runtime message has a non-ID caller")
|
|
|
|
}
|
|
|
|
return m.msg.From
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Receiver() address.Address {
|
|
|
|
if m.msg.To != address.Undef && m.msg.To.Protocol() != address.ID {
|
|
|
|
panic("runtime message has a non-ID receiver")
|
|
|
|
}
|
|
|
|
return m.msg.To
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) ValueReceived() abi.TokenAmount {
|
|
|
|
return m.msg.Value
|
|
|
|
}
|
|
|
|
|
2022-03-13 21:24:13 +00:00
|
|
|
// EnableDetailedTracing, if true, outputs gas tracing in execution traces.
|
2022-06-01 22:18:49 +00:00
|
|
|
var EnableDetailedTracing = os.Getenv("LOTUS_VM_ENABLE_TRACING") == "1"
|
2020-10-07 14:52:03 +00:00
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
type Runtime struct {
|
2021-11-04 15:59:29 +00:00
|
|
|
rt7.Message
|
|
|
|
rt7.Syscalls
|
2020-09-14 19:41:55 +00:00
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
2022-03-15 23:40:17 +00:00
|
|
|
vm *LegacyVM
|
2020-03-19 16:27:42 +00:00
|
|
|
state *state.StateTree
|
|
|
|
height abi.ChainEpoch
|
2020-09-14 19:41:55 +00:00
|
|
|
cst ipldcbor.IpldStore
|
2020-03-19 16:27:42 +00:00
|
|
|
pricelist Pricelist
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-03-18 20:45:37 +00:00
|
|
|
gasAvailable int64
|
|
|
|
gasUsed int64
|
2020-03-10 02:24:02 +00:00
|
|
|
|
|
|
|
// address that started invoke chain
|
2020-03-17 05:58:00 +00:00
|
|
|
origin address.Address
|
|
|
|
originNonce uint64
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-06-11 19:59:39 +00:00
|
|
|
executionTrace types.ExecutionTrace
|
2020-10-19 17:26:07 +00:00
|
|
|
depth uint64
|
2020-06-11 19:59:39 +00:00
|
|
|
numActorsCreated uint64
|
|
|
|
allowInternal bool
|
|
|
|
callerValidated bool
|
|
|
|
lastGasChargeTime time.Time
|
|
|
|
lastGasCharge *types.GasTrace
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 19:54:31 +00:00
|
|
|
func (rt *Runtime) BaseFee() abi.TokenAmount {
|
|
|
|
return rt.vm.baseFee
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
func (rt *Runtime) NetworkVersion() network.Version {
|
2021-12-18 00:05:59 +00:00
|
|
|
return rt.vm.networkVersion
|
2020-09-07 20:01:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:19:06 +00:00
|
|
|
func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount {
|
2020-08-09 22:49:38 +00:00
|
|
|
cs, err := rt.vm.GetCircSupply(rt.ctx)
|
2020-07-25 22:29:33 +00:00
|
|
|
if err != nil {
|
2020-08-09 22:49:38 +00:00
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get total circ supply: %s", err)
|
2020-07-28 21:36:59 +00:00
|
|
|
}
|
2020-07-25 22:29:33 +00:00
|
|
|
|
2020-08-09 22:49:38 +00:00
|
|
|
return cs
|
2020-04-10 12:19:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 21:38:11 +00:00
|
|
|
func (rt *Runtime) ResolveAddress(addr address.Address) (ret address.Address, ok bool) {
|
|
|
|
r, err := rt.state.LookupID(addr)
|
2020-04-03 22:50:52 +00:00
|
|
|
if err != nil {
|
2020-08-12 02:47:24 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
2020-04-03 21:38:11 +00:00
|
|
|
return address.Undef, false
|
|
|
|
}
|
|
|
|
panic(aerrors.Fatalf("failed to resolve address %s: %s", addr, err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return r, true
|
|
|
|
}
|
|
|
|
|
2020-04-03 21:38:11 +00:00
|
|
|
type notFoundErr interface {
|
|
|
|
IsNotFound() bool
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) StoreGet(c cid.Cid, o cbor.Unmarshaler) bool {
|
2020-06-02 14:29:39 +00:00
|
|
|
if err := rt.cst.Get(context.TODO(), c, o); err != nil {
|
2020-04-03 21:38:11 +00:00
|
|
|
var nfe notFoundErr
|
|
|
|
if xerrors.As(err, &nfe) && nfe.IsNotFound() {
|
2020-09-14 19:41:55 +00:00
|
|
|
if xerrors.As(err, new(ipldcbor.SerializationError)) {
|
2020-04-28 13:21:56 +00:00
|
|
|
panic(aerrors.Newf(exitcode.ErrSerialization, "failed to unmarshal cbor object %s", err))
|
|
|
|
}
|
2020-04-03 21:38:11 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(aerrors.Fatalf("failed to get cbor object %s: %s", c, err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) StorePut(x cbor.Marshaler) cid.Cid {
|
2020-06-02 14:29:39 +00:00
|
|
|
c, err := rt.cst.Put(context.TODO(), x)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-09-14 19:41:55 +00:00
|
|
|
if xerrors.As(err, new(ipldcbor.SerializationError)) {
|
2020-04-28 13:21:56 +00:00
|
|
|
panic(aerrors.Newf(exitcode.ErrSerialization, "failed to marshal cbor object %s", err))
|
|
|
|
}
|
2020-04-03 21:38:11 +00:00
|
|
|
panic(aerrors.Fatalf("failed to put cbor object: %s", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-05-15 19:26:52 +00:00
|
|
|
var _ rt0.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt5.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt2.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt3.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt4.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt5.Runtime = (*Runtime)(nil)
|
|
|
|
var _ rt6.Runtime = (*Runtime)(nil)
|
2021-11-04 15:59:29 +00:00
|
|
|
var _ rt7.Runtime = (*Runtime)(nil)
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) shimCall(f func() interface{}) (rval []byte, aerr aerrors.ActorError) {
|
2020-03-10 02:24:02 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if ar, ok := r.(aerrors.ActorError); ok {
|
2022-03-15 23:40:17 +00:00
|
|
|
log.Warnf("LegacyVM.Call failure in call from: %s to %s: %+v", rt.Caller(), rt.Receiver(), ar)
|
2020-03-10 02:24:02 +00:00
|
|
|
aerr = ar
|
|
|
|
return
|
|
|
|
}
|
2020-07-30 14:23:30 +00:00
|
|
|
//log.Desugar().WithOptions(zap.AddStacktrace(zapcore.ErrorLevel)).
|
|
|
|
//Sugar().Errorf("spec actors failure: %s", r)
|
2020-03-19 16:27:42 +00:00
|
|
|
log.Errorf("spec actors failure: %s", r)
|
2020-10-06 09:34:53 +00:00
|
|
|
if rt.NetworkVersion() <= network.Version3 {
|
|
|
|
aerr = aerrors.Newf(1, "spec actors failure: %s", r)
|
|
|
|
} else {
|
|
|
|
aerr = aerrors.Newf(exitcode.SysErrReserved1, "spec actors failure: %s", r)
|
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ret := f()
|
2020-04-28 02:46:44 +00:00
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
if !rt.callerValidated {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "Caller MUST be validated during method execution")
|
2020-04-28 02:46:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
switch ret := ret.(type) {
|
|
|
|
case []byte:
|
|
|
|
return ret, nil
|
2020-09-14 11:45:20 +00:00
|
|
|
case *abi.EmptyValue:
|
2020-03-10 02:24:02 +00:00
|
|
|
return nil, nil
|
2020-09-14 19:41:55 +00:00
|
|
|
case cbor.Marshaler:
|
2020-03-10 02:24:02 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := ret.MarshalCBOR(buf); err != nil {
|
|
|
|
return nil, aerrors.Absorb(err, 2, "failed to marshal response to cbor")
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
case nil:
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, aerrors.New(3, "could not determine type for response from call")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) ValidateImmediateCallerAcceptAny() {
|
|
|
|
rt.abortIfAlreadyValidated()
|
2020-03-10 02:24:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) CurrentBalance() abi.TokenAmount {
|
2020-09-14 11:45:20 +00:00
|
|
|
b, err := rt.GetBalance(rt.Receiver())
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-06-02 14:29:39 +00:00
|
|
|
rt.Abortf(err.RetCode(), "get current balance: %v", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) GetActorCodeCID(addr address.Address) (ret cid.Cid, ok bool) {
|
|
|
|
act, err := rt.state.GetActor(addr)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-04-03 21:38:11 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
return cid.Undef, false
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(aerrors.Fatalf("failed to get actor: %s", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return act.Code, true
|
|
|
|
}
|
|
|
|
|
2020-08-12 02:47:45 +00:00
|
|
|
func (rt *Runtime) GetRandomnessFromTickets(personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) abi.Randomness {
|
2021-12-17 23:54:17 +00:00
|
|
|
res, err := rt.vm.rand.GetChainRandomness(rt.ctx, personalization, randEpoch, entropy)
|
2021-05-26 23:03:46 +00:00
|
|
|
|
2020-08-12 02:47:45 +00:00
|
|
|
if err != nil {
|
2021-05-26 23:03:46 +00:00
|
|
|
panic(aerrors.Fatalf("could not get ticket randomness: %s", err))
|
2020-08-12 02:47:45 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) GetRandomnessFromBeacon(personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) abi.Randomness {
|
2021-12-17 23:54:17 +00:00
|
|
|
res, err := rt.vm.rand.GetBeaconRandomness(rt.ctx, personalization, randEpoch, entropy)
|
2021-05-26 23:03:46 +00:00
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2021-05-26 23:03:46 +00:00
|
|
|
panic(aerrors.Fatalf("could not get beacon randomness: %s", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) NewActorAddress() address.Address {
|
|
|
|
var b bytes.Buffer
|
2020-03-24 08:43:09 +00:00
|
|
|
oa, _ := ResolveToKeyAddr(rt.vm.cstate, rt.vm.cst, rt.origin)
|
|
|
|
if err := oa.MarshalCBOR(&b); err != nil { // todo: spec says cbor; why not just bytes?
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("writing caller address into a buffer: %v", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 05:58:00 +00:00
|
|
|
if err := binary.Write(&b, binary.BigEndian, rt.originNonce); err != nil {
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("writing nonce address into a buffer: %v", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-03-20 19:43:08 +00:00
|
|
|
if err := binary.Write(&b, binary.BigEndian, rt.numActorsCreated); err != nil { // TODO: expose on vm
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("writing callSeqNum address into a buffer: %v", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
addr, err := address.NewActorAddress(b.Bytes())
|
|
|
|
if err != nil {
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("create actor address: %v", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 19:43:08 +00:00
|
|
|
rt.incrementNumActorsCreated()
|
2020-03-10 02:24:02 +00:00
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2020-11-09 01:48:05 +00:00
|
|
|
func (rt *Runtime) CreateActor(codeID cid.Cid, addr address.Address) {
|
|
|
|
if addr == address.Undef && rt.NetworkVersion() >= network.Version7 {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "CreateActor with Undef address")
|
|
|
|
}
|
2020-09-25 00:51:34 +00:00
|
|
|
act, aerr := rt.vm.areg.Create(codeID, rt)
|
|
|
|
if aerr != nil {
|
|
|
|
rt.Abortf(aerr.RetCode(), aerr.Error())
|
2020-04-30 03:21:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 01:48:05 +00:00
|
|
|
_, err := rt.state.GetActor(addr)
|
2020-04-30 03:21:56 +00:00
|
|
|
if err == nil {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "Actor address already exists")
|
|
|
|
}
|
|
|
|
|
2020-06-17 18:00:30 +00:00
|
|
|
rt.chargeGas(rt.Pricelist().OnCreateActor())
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-11-09 01:48:05 +00:00
|
|
|
err = rt.state.SetActor(addr, act)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("creating actor entry: %v", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-06-16 17:29:33 +00:00
|
|
|
_ = rt.chargeGasSafe(gasOnActorExec)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 22:08:17 +00:00
|
|
|
// DeleteActor deletes the executing actor from the state tree, transferring
|
|
|
|
// any balance to beneficiary.
|
2020-11-09 00:58:10 +00:00
|
|
|
// Aborts if the beneficiary does not exist or is the calling actor.
|
2020-08-04 21:27:27 +00:00
|
|
|
// May only be called by the actor itself.
|
|
|
|
func (rt *Runtime) DeleteActor(beneficiary address.Address) {
|
2020-06-17 18:00:30 +00:00
|
|
|
rt.chargeGas(rt.Pricelist().OnDeleteActor())
|
2020-09-14 11:45:20 +00:00
|
|
|
act, err := rt.state.GetActor(rt.Receiver())
|
2020-03-10 04:38:31 +00:00
|
|
|
if err != nil {
|
2020-04-03 22:05:59 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "failed to load actor in delete actor: %s", err)
|
|
|
|
}
|
|
|
|
panic(aerrors.Fatalf("failed to get actor: %s", err))
|
2020-03-10 04:38:31 +00:00
|
|
|
}
|
|
|
|
if !act.Balance.IsZero() {
|
2020-11-18 06:52:46 +00:00
|
|
|
// TODO: Should be safe to drop the version-check,
|
|
|
|
// since only the paych actor called this pre-version 7, but let's leave it for now
|
|
|
|
if rt.NetworkVersion() >= network.Version7 {
|
|
|
|
beneficiaryId, found := rt.ResolveAddress(beneficiary)
|
|
|
|
if !found {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "beneficiary doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if beneficiaryId == rt.Receiver() {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "benefactor cannot be beneficiary")
|
|
|
|
}
|
2020-11-09 00:58:10 +00:00
|
|
|
}
|
2020-11-18 06:52:46 +00:00
|
|
|
|
2020-08-04 21:27:27 +00:00
|
|
|
// Transfer the executing actor's balance to the beneficiary
|
2021-11-29 22:26:47 +00:00
|
|
|
if err := rt.vm.transfer(rt.Receiver(), beneficiary, act.Balance, rt.NetworkVersion()); err != nil {
|
2020-08-04 21:27:27 +00:00
|
|
|
panic(aerrors.Fatalf("failed to transfer balance to beneficiary actor: %s", err))
|
2020-05-13 21:46:16 +00:00
|
|
|
}
|
2020-03-10 04:38:31 +00:00
|
|
|
}
|
2020-04-03 22:05:59 +00:00
|
|
|
|
2020-08-04 21:27:27 +00:00
|
|
|
// Delete the executing actor
|
2020-09-14 11:45:20 +00:00
|
|
|
if err := rt.state.DeleteActor(rt.Receiver()); err != nil {
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("failed to delete actor: %s", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-06-16 17:29:33 +00:00
|
|
|
_ = rt.chargeGasSafe(gasOnActorExec)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
func (rt *Runtime) StartSpan(name string) func() {
|
2020-03-10 02:24:02 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) ValidateImmediateCallerIs(as ...address.Address) {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.abortIfAlreadyValidated()
|
2020-09-14 11:45:20 +00:00
|
|
|
imm := rt.Caller()
|
2020-03-10 02:24:02 +00:00
|
|
|
|
|
|
|
for _, a := range as {
|
|
|
|
if imm == a {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.Abortf(exitcode.SysErrForbidden, "caller %s is not one of %s", rt.Caller(), as)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) Context() context.Context {
|
|
|
|
return rt.ctx
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) Abortf(code exitcode.ExitCode, msg string, args ...interface{}) {
|
2020-07-01 22:51:21 +00:00
|
|
|
log.Warnf("Abortf: " + fmt.Sprintf(msg, args...))
|
2020-04-02 18:24:38 +00:00
|
|
|
panic(aerrors.NewfSkip(2, code, msg, args...))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) AbortStateMsg(msg string) {
|
2020-03-10 02:24:02 +00:00
|
|
|
panic(aerrors.NewfSkip(3, 101, msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) ValidateImmediateCallerType(ts ...cid.Cid) {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.abortIfAlreadyValidated()
|
2020-09-14 11:45:20 +00:00
|
|
|
callerCid, ok := rt.GetActorCodeCID(rt.Caller())
|
2020-03-10 02:24:02 +00:00
|
|
|
if !ok {
|
2020-04-03 22:05:59 +00:00
|
|
|
panic(aerrors.Fatalf("failed to lookup code cid for caller"))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
for _, t := range ts {
|
|
|
|
if t == callerCid {
|
|
|
|
return
|
|
|
|
}
|
2022-04-08 00:03:07 +00:00
|
|
|
|
|
|
|
// this really only for genesis in tests; nv16 will be running on FVM anyway.
|
|
|
|
if nv := rt.NetworkVersion(); nv >= network.Version16 {
|
|
|
|
av, err := actors.VersionForNetwork(nv)
|
|
|
|
if err != nil {
|
|
|
|
panic(aerrors.Fatalf("failed to get actors version for network version %d", nv))
|
|
|
|
}
|
|
|
|
|
|
|
|
name := actors.CanonicalName(builtin.ActorNameByCode(t))
|
|
|
|
ac, ok := actors.GetActorCodeID(av, name)
|
|
|
|
if ok && ac == callerCid {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-03-23 20:44:06 +00:00
|
|
|
rt.Abortf(exitcode.SysErrForbidden, "caller cid type %q was not one of %v", callerCid, ts)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (rt *Runtime) CurrEpoch() abi.ChainEpoch {
|
|
|
|
return rt.height
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) Send(to address.Address, method abi.MethodNum, m cbor.Marshaler, value abi.TokenAmount, out cbor.Er) exitcode.ExitCode {
|
2020-06-02 14:29:39 +00:00
|
|
|
if !rt.allowInternal {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "runtime.Send() is currently disallowed")
|
2020-04-24 20:42:26 +00:00
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
var params []byte
|
|
|
|
if m != nil {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := m.MarshalCBOR(buf); err != nil {
|
2020-08-20 05:28:42 +00:00
|
|
|
rt.Abortf(exitcode.ErrSerialization, "failed to marshal input parameters: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
params = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
ret, err := rt.internalSend(rt.Receiver(), to, method, value, params)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.IsFatal() {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-28 22:26:09 +00:00
|
|
|
log.Warnf("vmctx send failed: from: %s to: %s, method: %d: err: %s", rt.Receiver(), to, method, err)
|
2020-09-14 11:45:20 +00:00
|
|
|
return err.RetCode()
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-06-16 17:29:33 +00:00
|
|
|
_ = rt.chargeGasSafe(gasOnActorExec)
|
2020-09-14 11:45:20 +00:00
|
|
|
|
|
|
|
if err := out.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
|
2020-09-14 19:41:55 +00:00
|
|
|
rt.Abortf(exitcode.ErrSerialization, "failed to unmarshal return value: %s", err)
|
2020-09-14 11:45:20 +00:00
|
|
|
}
|
|
|
|
return 0
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
func (rt *Runtime) internalSend(from, to address.Address, method abi.MethodNum, value types.BigInt, params []byte) ([]byte, aerrors.ActorError) {
|
2020-07-10 14:43:14 +00:00
|
|
|
start := build.Clock.Now()
|
2020-03-10 02:24:02 +00:00
|
|
|
ctx, span := trace.StartSpan(rt.ctx, "vmc.Send")
|
|
|
|
defer span.End()
|
|
|
|
if span.IsRecordingEvents() {
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("to", to.String()),
|
|
|
|
trace.Int64Attribute("method", int64(method)),
|
|
|
|
trace.StringAttribute("value", value.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
2020-03-25 02:25:20 +00:00
|
|
|
From: from,
|
2020-03-10 02:24:02 +00:00
|
|
|
To: to,
|
|
|
|
Method: method,
|
|
|
|
Value: value,
|
|
|
|
Params: params,
|
|
|
|
GasLimit: rt.gasAvailable,
|
|
|
|
}
|
|
|
|
|
|
|
|
st := rt.state
|
|
|
|
if err := st.Snapshot(ctx); err != nil {
|
|
|
|
return nil, aerrors.Fatalf("snapshot failed: %s", err)
|
|
|
|
}
|
|
|
|
defer st.ClearSnapshot()
|
|
|
|
|
2020-06-12 18:46:04 +00:00
|
|
|
ret, errSend, subrt := rt.vm.send(ctx, msg, rt, nil, start)
|
2020-03-17 07:31:45 +00:00
|
|
|
if errSend != nil {
|
|
|
|
if errRevert := st.Revert(); errRevert != nil {
|
|
|
|
return nil, aerrors.Escalate(errRevert, "failed to revert state tree after failed subcall")
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 05:58:00 +00:00
|
|
|
if subrt != nil {
|
2020-03-20 19:43:08 +00:00
|
|
|
rt.numActorsCreated = subrt.numActorsCreated
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.executionTrace.Subcalls = append(rt.executionTrace.Subcalls, subrt.executionTrace)
|
2020-03-17 05:58:00 +00:00
|
|
|
}
|
2020-03-17 07:31:45 +00:00
|
|
|
return ret, errSend
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) StateCreate(obj cbor.Marshaler) {
|
2020-09-14 11:45:20 +00:00
|
|
|
c := rt.StorePut(obj)
|
|
|
|
err := rt.stateCommit(EmptyObjectCid, c)
|
2020-08-20 04:49:10 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to commit state after creating object: %w", err))
|
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) StateReadonly(obj cbor.Unmarshaler) {
|
2020-09-14 11:45:20 +00:00
|
|
|
act, err := rt.state.GetActor(rt.Receiver())
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "failed to get actor for Readonly state: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.StoreGet(act.Head, obj)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:41:55 +00:00
|
|
|
func (rt *Runtime) StateTransaction(obj cbor.Er, f func()) {
|
2020-04-23 16:45:52 +00:00
|
|
|
if obj == nil {
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
|
2020-04-23 16:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
act, err := rt.state.GetActor(rt.Receiver())
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "failed to get actor for Transaction: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
baseState := act.Head
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.StoreGet(baseState, obj)
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.allowInternal = false
|
2020-07-31 04:22:00 +00:00
|
|
|
f()
|
2020-09-14 11:45:20 +00:00
|
|
|
rt.allowInternal = true
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
c := rt.StorePut(obj)
|
2020-03-10 02:24:02 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
err = rt.stateCommit(baseState, c)
|
2020-08-20 04:49:10 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to commit state after transaction: %w", err))
|
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) GetBalance(a address.Address) (types.BigInt, aerrors.ActorError) {
|
|
|
|
act, err := rt.state.GetActor(a)
|
|
|
|
switch err {
|
|
|
|
default:
|
|
|
|
return types.EmptyInt, aerrors.Escalate(err, "failed to look up actor balance")
|
2020-07-23 00:14:54 +00:00
|
|
|
case types.ErrActorNotFound:
|
2020-03-10 02:24:02 +00:00
|
|
|
return types.NewInt(0), nil
|
|
|
|
case nil:
|
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) stateCommit(oldh, newh cid.Cid) aerrors.ActorError {
|
|
|
|
// TODO: we can make this more efficient in the future...
|
2020-09-14 11:45:20 +00:00
|
|
|
act, err := rt.state.GetActor(rt.Receiver())
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
return aerrors.Escalate(err, "failed to get actor to commit state")
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if act.Head != oldh {
|
2020-04-23 16:45:52 +00:00
|
|
|
return aerrors.Fatal("failed to update, inconsistent base reference")
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act.Head = newh
|
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
if err := rt.state.SetActor(rt.Receiver(), act); err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
return aerrors.Fatalf("failed to set actor in commit state: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 16:49:29 +00:00
|
|
|
func (rt *Runtime) finilizeGasTracing() {
|
2022-03-13 21:24:13 +00:00
|
|
|
if EnableDetailedTracing {
|
2020-09-21 22:46:31 +00:00
|
|
|
if rt.lastGasCharge != nil {
|
|
|
|
rt.lastGasCharge.TimeTaken = time.Since(rt.lastGasChargeTime)
|
|
|
|
}
|
2020-06-12 16:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 18:00:30 +00:00
|
|
|
// ChargeGas is spec actors function
|
|
|
|
func (rt *Runtime) ChargeGas(name string, compute int64, virtual int64) {
|
|
|
|
err := rt.chargeGasInternal(newGasCharge(name, compute, 0).WithVirtual(virtual, 0), 1)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) chargeGas(gas GasCharge) {
|
2020-06-12 16:49:29 +00:00
|
|
|
err := rt.chargeGasInternal(gas, 1)
|
2020-03-19 16:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 16:49:29 +00:00
|
|
|
|
|
|
|
func (rt *Runtime) chargeGasFunc(skip int) func(GasCharge) {
|
|
|
|
return func(gas GasCharge) {
|
|
|
|
err := rt.chargeGasInternal(gas, 1+skip)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-11 19:59:39 +00:00
|
|
|
}
|
2020-06-12 16:49:29 +00:00
|
|
|
|
2020-06-11 19:59:39 +00:00
|
|
|
}
|
2020-03-19 16:27:42 +00:00
|
|
|
|
2020-06-12 16:49:29 +00:00
|
|
|
func (rt *Runtime) chargeGasInternal(gas GasCharge, skip int) aerrors.ActorError {
|
2020-06-11 18:37:14 +00:00
|
|
|
toUse := gas.Total()
|
2022-03-13 21:24:13 +00:00
|
|
|
if EnableDetailedTracing {
|
2020-09-21 22:46:31 +00:00
|
|
|
var callers [10]uintptr
|
2020-06-11 19:59:39 +00:00
|
|
|
|
2020-12-01 12:53:30 +00:00
|
|
|
cout := gruntime.Callers(2+skip, callers[:])
|
2020-06-11 19:59:39 +00:00
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
now := build.Clock.Now()
|
|
|
|
if rt.lastGasCharge != nil {
|
|
|
|
rt.lastGasCharge.TimeTaken = now.Sub(rt.lastGasChargeTime)
|
|
|
|
}
|
2020-06-11 19:59:39 +00:00
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
gasTrace := types.GasTrace{
|
|
|
|
Name: gas.Name,
|
|
|
|
Extra: gas.Extra,
|
2020-06-15 17:50:43 +00:00
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
TotalGas: toUse,
|
|
|
|
ComputeGas: gas.ComputeGas,
|
|
|
|
StorageGas: gas.StorageGas,
|
2020-06-15 17:50:43 +00:00
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
VirtualComputeGas: gas.VirtualCompute,
|
|
|
|
VirtualStorageGas: gas.VirtualStorage,
|
2020-06-15 17:50:43 +00:00
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
Callers: callers[:cout],
|
|
|
|
}
|
2020-11-11 18:07:39 +00:00
|
|
|
if gasTrace.VirtualStorageGas == 0 {
|
|
|
|
gasTrace.VirtualStorageGas = gasTrace.StorageGas
|
|
|
|
}
|
|
|
|
if gasTrace.VirtualComputeGas == 0 {
|
|
|
|
gasTrace.VirtualComputeGas = gasTrace.ComputeGas
|
|
|
|
}
|
2020-11-11 18:18:45 +00:00
|
|
|
gasTrace.TotalVirtualGas = gasTrace.VirtualComputeGas + gasTrace.VirtualStorageGas
|
|
|
|
|
2020-09-21 22:46:31 +00:00
|
|
|
rt.executionTrace.GasCharges = append(rt.executionTrace.GasCharges, &gasTrace)
|
|
|
|
rt.lastGasChargeTime = now
|
|
|
|
rt.lastGasCharge = &gasTrace
|
2020-06-11 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 17:49:55 +00:00
|
|
|
// overflow safe
|
|
|
|
if rt.gasUsed > rt.gasAvailable-toUse {
|
2020-11-03 07:46:35 +00:00
|
|
|
gasUsed := rt.gasUsed
|
2020-04-01 20:55:10 +00:00
|
|
|
rt.gasUsed = rt.gasAvailable
|
2020-11-03 07:46:35 +00:00
|
|
|
return aerrors.Newf(exitcode.SysErrOutOfGas, "not enough gas: used=%d, available=%d, use=%d",
|
|
|
|
gasUsed, rt.gasAvailable, toUse)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-04-01 20:55:10 +00:00
|
|
|
rt.gasUsed += toUse
|
2020-03-19 16:27:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:37:14 +00:00
|
|
|
func (rt *Runtime) chargeGasSafe(gas GasCharge) aerrors.ActorError {
|
2020-06-12 16:49:29 +00:00
|
|
|
return rt.chargeGasInternal(gas, 1)
|
2020-06-11 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
func (rt *Runtime) Pricelist() Pricelist {
|
|
|
|
return rt.pricelist
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
2020-03-20 19:43:08 +00:00
|
|
|
|
|
|
|
func (rt *Runtime) incrementNumActorsCreated() {
|
|
|
|
rt.numActorsCreated++
|
|
|
|
}
|
2020-04-28 02:46:44 +00:00
|
|
|
|
|
|
|
func (rt *Runtime) abortIfAlreadyValidated() {
|
|
|
|
if rt.callerValidated {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalActor, "Method must validate caller identity exactly once")
|
|
|
|
}
|
|
|
|
rt.callerValidated = true
|
|
|
|
}
|
2020-07-10 04:10:15 +00:00
|
|
|
|
2020-09-14 11:45:20 +00:00
|
|
|
func (rt *Runtime) Log(level rtt.LogLevel, msg string, args ...interface{}) {
|
2020-07-10 04:10:15 +00:00
|
|
|
switch level {
|
2020-09-14 11:45:20 +00:00
|
|
|
case rtt.DEBUG:
|
2020-07-23 17:13:16 +00:00
|
|
|
actorLog.Debugf(msg, args...)
|
2020-09-14 11:45:20 +00:00
|
|
|
case rtt.INFO:
|
2020-07-23 17:13:16 +00:00
|
|
|
actorLog.Infof(msg, args...)
|
2020-09-14 11:45:20 +00:00
|
|
|
case rtt.WARN:
|
2020-07-23 17:13:16 +00:00
|
|
|
actorLog.Warnf(msg, args...)
|
2020-09-14 11:45:20 +00:00
|
|
|
case rtt.ERROR:
|
2020-07-23 17:13:16 +00:00
|
|
|
actorLog.Errorf(msg, args...)
|
2020-07-10 04:10:15 +00:00
|
|
|
}
|
2020-07-13 11:35:10 +00:00
|
|
|
}
|