2020-03-10 02:24:02 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
2020-03-23 06:39:42 +00:00
|
|
|
"fmt"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2020-04-03 22:05:59 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-04-03 21:38:11 +00:00
|
|
|
sainit "github.com/filecoin-project/specs-actors/actors/builtin/init"
|
2020-04-16 23:03:38 +00:00
|
|
|
sapower "github.com/filecoin-project/specs-actors/actors/builtin/power"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
vmr "github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"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"
|
2020-03-10 02:24:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runtime struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
vm *VM
|
|
|
|
state *state.StateTree
|
|
|
|
msg *types.Message
|
2020-04-03 21:38:11 +00:00
|
|
|
vmsg vmr.Message
|
2020-03-19 16:27:42 +00:00
|
|
|
height abi.ChainEpoch
|
|
|
|
cst cbor.IpldStore
|
|
|
|
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
|
|
|
|
|
|
|
sys runtime.Syscalls
|
|
|
|
|
|
|
|
// 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-04-08 22:27:31 +00:00
|
|
|
internalExecutions []*types.ExecutionResult
|
2020-03-20 19:43:08 +00:00
|
|
|
numActorsCreated uint64
|
2020-04-24 20:42:26 +00:00
|
|
|
allowInternal bool
|
2020-04-28 02:46:44 +00:00
|
|
|
callerValidated bool
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:19:06 +00:00
|
|
|
func (rt *Runtime) TotalFilCircSupply() abi.TokenAmount {
|
2020-04-16 23:03:38 +00:00
|
|
|
total := types.FromFil(build.TotalFilecoin)
|
|
|
|
|
|
|
|
rew, err := rt.state.GetActor(builtin.RewardActorAddr)
|
|
|
|
if err != nil {
|
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get reward actor for computing total supply: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
burnt, err := rt.state.GetActor(builtin.BurntFundsActorAddr)
|
|
|
|
if err != nil {
|
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get reward actor for computing total supply: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
market, err := rt.state.GetActor(builtin.StorageMarketActorAddr)
|
|
|
|
if err != nil {
|
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get reward actor for computing total supply: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
power, err := rt.state.GetActor(builtin.StoragePowerActorAddr)
|
|
|
|
if err != nil {
|
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get reward actor for computing total supply: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
total = types.BigSub(total, rew.Balance)
|
|
|
|
total = types.BigSub(total, burnt.Balance)
|
|
|
|
total = types.BigSub(total, market.Balance)
|
|
|
|
|
|
|
|
var st sapower.State
|
|
|
|
if err := rt.cst.Get(rt.ctx, power.Head, &st); err != nil {
|
|
|
|
rt.Abortf(exitcode.ErrIllegalState, "failed to get storage power state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.BigSub(total, st.TotalPledgeCollateral)
|
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-04-03 21:38:11 +00:00
|
|
|
if xerrors.Is(err, sainit.ErrAddressNotFound) {
|
|
|
|
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-03-10 02:24:02 +00:00
|
|
|
func (rs *Runtime) Get(c cid.Cid, o vmr.CBORUnmarshaler) bool {
|
|
|
|
if err := rs.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-04-28 13:21:56 +00:00
|
|
|
if xerrors.As(err, new(cbor.SerializationError)) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) Put(x vmr.CBORMarshaler) cid.Cid {
|
|
|
|
c, err := rs.cst.Put(context.TODO(), x)
|
|
|
|
if err != nil {
|
2020-04-28 13:21:56 +00:00
|
|
|
if xerrors.As(err, new(cbor.SerializationError)) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vmr.Runtime = (*Runtime)(nil)
|
|
|
|
|
|
|
|
func (rs *Runtime) shimCall(f func() interface{}) (rval []byte, aerr aerrors.ActorError) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if ar, ok := r.(aerrors.ActorError); ok {
|
2020-03-19 16:27:42 +00:00
|
|
|
log.Errorf("VM.Call failure: %+v", ar)
|
2020-03-10 02:24:02 +00:00
|
|
|
aerr = ar
|
|
|
|
return
|
|
|
|
}
|
2020-03-19 16:27:42 +00:00
|
|
|
log.Errorf("spec actors failure: %s", r)
|
2020-03-10 02:24:02 +00:00
|
|
|
aerr = aerrors.Newf(1, "spec actors failure: %s", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ret := f()
|
2020-04-28 02:46:44 +00:00
|
|
|
|
|
|
|
if !rs.callerValidated {
|
|
|
|
rs.Abortf(exitcode.SysErrorIllegalActor, "Caller MUST be validated during method execution")
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
switch ret := ret.(type) {
|
|
|
|
case []byte:
|
|
|
|
return ret, nil
|
|
|
|
case *adt.EmptyValue:
|
|
|
|
return nil, nil
|
|
|
|
case cbg.CBORMarshaler:
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) Message() vmr.Message {
|
2020-04-03 21:38:11 +00:00
|
|
|
return rs.vmsg
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) ValidateImmediateCallerAcceptAny() {
|
2020-04-28 02:46:44 +00:00
|
|
|
rs.abortIfAlreadyValidated()
|
2020-03-10 02:24:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) CurrentBalance() abi.TokenAmount {
|
|
|
|
b, err := rs.GetBalance(rs.Message().Receiver())
|
|
|
|
if err != nil {
|
|
|
|
rs.Abortf(exitcode.ExitCode(err.RetCode()), "get current balance: %v", err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) GetActorCodeCID(addr address.Address) (ret cid.Cid, ok bool) {
|
|
|
|
act, err := rs.state.GetActor(addr)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) GetRandomness(personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) abi.Randomness {
|
2020-04-08 15:11:42 +00:00
|
|
|
res, err := rt.vm.rand.GetRandomness(rt.ctx, personalization, randEpoch, entropy)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-04-03 21:38:11 +00:00
|
|
|
panic(aerrors.Fatalf("could not get randomness: %s", err))
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) Store() vmr.Store {
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) CreateActor(codeId cid.Cid, address address.Address) {
|
2020-04-30 03:21:56 +00:00
|
|
|
if !builtin.IsBuiltinActor(codeId) {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "Can only create built-in actors.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if builtin.IsSingletonActor(codeId) {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "Can only have one instance of singleton actors.")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rt.state.GetActor(address)
|
|
|
|
if err == nil {
|
|
|
|
rt.Abortf(exitcode.SysErrorIllegalArgument, "Actor address already exists")
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:27:42 +00:00
|
|
|
rt.ChargeGas(rt.Pricelist().OnCreateActor())
|
2020-03-10 02:24:02 +00:00
|
|
|
|
|
|
|
err = rt.state.SetActor(address, &types.Actor{
|
|
|
|
Code: codeId,
|
|
|
|
Head: EmptyObjectCid,
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: big.Zero(),
|
|
|
|
})
|
|
|
|
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-04-10 12:19:06 +00:00
|
|
|
func (rt *Runtime) DeleteActor(addr address.Address) {
|
2020-03-19 16:27:42 +00:00
|
|
|
rt.ChargeGas(rt.Pricelist().OnDeleteActor())
|
2020-03-10 04:38:31 +00:00
|
|
|
act, err := rt.state.GetActor(rt.Message().Receiver())
|
|
|
|
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-04-03 22:05:59 +00:00
|
|
|
rt.vm.transfer(rt.Message().Receiver(), builtin.BurntFundsActorAddr, act.Balance)
|
2020-03-10 04:38:31 +00:00
|
|
|
}
|
2020-04-03 22:05:59 +00:00
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
if err := rt.state.DeleteActor(rt.Message().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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const GasVerifySignature = 50
|
|
|
|
|
|
|
|
func (rs *Runtime) Syscalls() vmr.Syscalls {
|
|
|
|
// TODO: Make sure this is wrapped in something that charges gas for each of the calls
|
|
|
|
return rs.sys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) StartSpan(name string) vmr.TraceSpan {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) ValidateImmediateCallerIs(as ...address.Address) {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.abortIfAlreadyValidated()
|
2020-04-03 22:05:59 +00:00
|
|
|
imm := rt.Message().Caller()
|
2020-03-10 02:24:02 +00:00
|
|
|
|
|
|
|
for _, a := range as {
|
|
|
|
if imm == a {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 20:44:06 +00:00
|
|
|
rt.Abortf(exitcode.SysErrForbidden, "caller %s is not one of %s", rt.Message().Caller(), as)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) Context() context.Context {
|
|
|
|
return rt.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) Abortf(code exitcode.ExitCode, msg string, args ...interface{}) {
|
2020-03-23 06:39:42 +00:00
|
|
|
log.Error("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
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) AbortStateMsg(msg string) {
|
|
|
|
panic(aerrors.NewfSkip(3, 101, msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) ValidateImmediateCallerType(ts ...cid.Cid) {
|
2020-04-28 02:46:44 +00:00
|
|
|
rt.abortIfAlreadyValidated()
|
2020-03-10 02:24:02 +00:00
|
|
|
callerCid, ok := rt.GetActorCodeCID(rt.Message().Caller())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) CurrEpoch() abi.ChainEpoch {
|
|
|
|
return rs.height
|
|
|
|
}
|
|
|
|
|
|
|
|
type dumbWrapperType struct {
|
|
|
|
val []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwt *dumbWrapperType) Into(um vmr.CBORUnmarshaler) error {
|
|
|
|
return um.UnmarshalCBOR(bytes.NewReader(dwt.val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) Send(to address.Address, method abi.MethodNum, m vmr.CBORMarshaler, value abi.TokenAmount) (vmr.SendReturn, exitcode.ExitCode) {
|
2020-04-24 20:42:26 +00:00
|
|
|
if !rs.allowInternal {
|
|
|
|
rs.Abortf(exitcode.SysErrorIllegalActor, "runtime.Send() is currently disallowed")
|
|
|
|
}
|
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 {
|
|
|
|
rs.Abortf(exitcode.SysErrInvalidParameters, "failed to marshal input parameters: %s", err)
|
|
|
|
}
|
|
|
|
params = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
ret, err := rs.internalSend(rs.Message().Receiver(), to, method, types.BigInt(value), params)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.IsFatal() {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Warnf("vmctx send failed: to: %s, method: %d: ret: %d, err: %s", to, method, ret, err)
|
|
|
|
return nil, exitcode.ExitCode(err.RetCode())
|
|
|
|
}
|
|
|
|
return &dumbWrapperType{ret}, 0
|
|
|
|
}
|
|
|
|
|
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-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-03-17 07:31:45 +00:00
|
|
|
ret, errSend, subrt := rt.vm.send(ctx, msg, rt, 0)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := types.MessageReceipt{
|
2020-03-17 07:31:45 +00:00
|
|
|
ExitCode: exitcode.ExitCode(aerrors.RetCode(errSend)),
|
2020-03-10 02:24:02 +00:00
|
|
|
Return: ret,
|
2020-03-18 20:45:37 +00:00
|
|
|
GasUsed: 0,
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 22:27:31 +00:00
|
|
|
er := types.ExecutionResult{
|
2020-03-17 07:31:45 +00:00
|
|
|
Msg: msg,
|
|
|
|
MsgRct: &mr,
|
|
|
|
}
|
|
|
|
|
|
|
|
if errSend != nil {
|
|
|
|
er.Error = errSend.Error()
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 05:58:00 +00:00
|
|
|
if subrt != nil {
|
2020-03-17 07:31:45 +00:00
|
|
|
er.Subcalls = subrt.internalExecutions
|
2020-03-20 19:43:08 +00:00
|
|
|
rt.numActorsCreated = subrt.numActorsCreated
|
2020-03-17 05:58:00 +00:00
|
|
|
}
|
2020-03-10 02:24:02 +00:00
|
|
|
rt.internalExecutions = append(rt.internalExecutions, &er)
|
2020-03-17 07:31:45 +00:00
|
|
|
return ret, errSend
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *Runtime) State() vmr.StateHandle {
|
|
|
|
return &shimStateHandle{rs: rs}
|
|
|
|
}
|
|
|
|
|
|
|
|
type shimStateHandle struct {
|
|
|
|
rs *Runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssh *shimStateHandle) Create(obj vmr.CBORMarshaler) {
|
|
|
|
c := ssh.rs.Put(obj)
|
|
|
|
ssh.rs.stateCommit(EmptyObjectCid, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssh *shimStateHandle) Readonly(obj vmr.CBORUnmarshaler) {
|
|
|
|
act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver())
|
|
|
|
if err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
ssh.rs.Abortf(exitcode.SysErrorIllegalArgument, "failed to get actor for Readonly state: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
ssh.rs.Get(act.Head, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssh *shimStateHandle) Transaction(obj vmr.CBORer, f func() interface{}) interface{} {
|
2020-04-23 16:45:52 +00:00
|
|
|
if obj == nil {
|
|
|
|
ssh.rs.Abortf(exitcode.SysErrorIllegalActor, "Must not pass nil to Transaction()")
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
act, err := ssh.rs.state.GetActor(ssh.rs.Message().Receiver())
|
|
|
|
if err != nil {
|
2020-04-23 16:45:52 +00:00
|
|
|
ssh.rs.Abortf(exitcode.SysErrorIllegalActor, "failed to get actor for Transaction: %s", err)
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
baseState := act.Head
|
|
|
|
ssh.rs.Get(baseState, obj)
|
|
|
|
|
2020-04-24 20:42:26 +00:00
|
|
|
ssh.rs.allowInternal = false
|
2020-03-10 02:24:02 +00:00
|
|
|
out := f()
|
2020-04-24 20:42:26 +00:00
|
|
|
ssh.rs.allowInternal = true
|
2020-03-10 02:24:02 +00:00
|
|
|
|
|
|
|
c := ssh.rs.Put(obj)
|
|
|
|
|
|
|
|
ssh.rs.stateCommit(baseState, c)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
case hamt.ErrNotFound:
|
|
|
|
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...
|
|
|
|
act, err := rt.state.GetActor(rt.Message().Receiver())
|
|
|
|
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
|
|
|
|
|
|
|
|
if err := rt.state.SetActor(rt.Message().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-03-18 20:45:37 +00:00
|
|
|
func (rt *Runtime) ChargeGas(toUse int64) {
|
2020-03-19 16:27:42 +00:00
|
|
|
err := rt.chargeGasSafe(toUse)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *Runtime) chargeGasSafe(toUse int64) aerrors.ActorError {
|
2020-04-01 20:55:10 +00:00
|
|
|
if rt.gasUsed+toUse > rt.gasAvailable {
|
|
|
|
rt.gasUsed = rt.gasAvailable
|
2020-04-02 18:24:38 +00:00
|
|
|
return aerrors.Newf(exitcode.SysErrOutOfGas, "not enough gas: used=%d, available=%d", rt.gasUsed, rt.gasAvailable)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|