2019-07-26 04:54:22 +00:00
|
|
|
package vm
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-22 18:17:42 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
2019-07-08 12:51:45 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
2019-07-12 10:23:05 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-08 20:29:01 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/bufbstore"
|
2019-07-26 19:01:02 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
bserv "github.com/ipfs/go-blockservice"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2019-07-26 19:01:02 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-05 14:29:17 +00:00
|
|
|
dag "github.com/ipfs/go-merkledag"
|
2019-07-16 16:07:14 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 19:01:02 +00:00
|
|
|
var log = logging.Logger("vm")
|
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
const (
|
|
|
|
gasFundTransfer = 10
|
|
|
|
)
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
type VMContext struct {
|
2019-07-26 19:01:02 +00:00
|
|
|
ctx context.Context
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
vm *VM
|
2019-07-25 22:15:03 +00:00
|
|
|
state *state.StateTree
|
2019-07-12 10:43:15 +00:00
|
|
|
msg *types.Message
|
2019-07-05 14:29:17 +00:00
|
|
|
height uint64
|
|
|
|
cst *hamt.CborIpldStore
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-08-16 12:41:43 +00:00
|
|
|
gasAvailable types.BigInt
|
2019-08-15 16:23:28 +00:00
|
|
|
gasUsed types.BigInt
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
// root cid of the state of the actor this invocation will be on
|
|
|
|
sroot cid.Cid
|
|
|
|
|
2019-07-16 16:40:25 +00:00
|
|
|
// address that started invokation chain
|
|
|
|
origin address.Address
|
|
|
|
|
2019-07-17 14:30:55 +00:00
|
|
|
storage *storage
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Message is the message that kicked off the current invocation
|
2019-07-12 10:43:15 +00:00
|
|
|
func (vmc *VMContext) Message() *types.Message {
|
2019-07-05 14:29:17 +00:00
|
|
|
return vmc.msg
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
type storage struct {
|
|
|
|
// would be great to stop depending on this crap everywhere
|
|
|
|
// I am my own worst enemy
|
|
|
|
cst *hamt.CborIpldStore
|
|
|
|
head cid.Cid
|
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
func (s *storage) Put(i interface{}) (cid.Cid, aerrors.ActorError) {
|
|
|
|
c, err := s.cst.Put(context.TODO(), i)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "putting cid")
|
|
|
|
}
|
|
|
|
return c, nil
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
func (s *storage) Get(c cid.Cid, out interface{}) aerrors.ActorError {
|
|
|
|
return aerrors.Escalate(s.cst.Get(context.TODO(), c, out), "getting cid")
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storage) GetHead() cid.Cid {
|
|
|
|
return s.head
|
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
func (s *storage) Commit(oldh, newh cid.Cid) aerrors.ActorError {
|
2019-07-12 03:59:55 +00:00
|
|
|
if s.head != oldh {
|
2019-07-22 18:17:42 +00:00
|
|
|
return aerrors.New(1, "failed to update, inconsistent base reference")
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.head = newh
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
// Storage provides access to the VM storage layer
|
2019-07-12 03:59:55 +00:00
|
|
|
func (vmc *VMContext) Storage() types.Storage {
|
|
|
|
return vmc.storage
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vmc *VMContext) Ipld() *hamt.CborIpldStore {
|
|
|
|
return vmc.cst
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:40:25 +00:00
|
|
|
func (vmc *VMContext) Origin() address.Address {
|
|
|
|
return vmc.origin
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
// Send allows the current execution context to invoke methods on other actors in the system
|
2019-07-22 18:17:42 +00:00
|
|
|
func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt, params []byte) ([]byte, aerrors.ActorError) {
|
2019-08-15 16:23:28 +00:00
|
|
|
ctx, span := trace.StartSpan(vmc.ctx, "vmc.Send")
|
2019-07-26 19:01:02 +00:00
|
|
|
defer span.End()
|
|
|
|
if span.IsRecordingEvents() {
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("to", to.String()),
|
|
|
|
trace.Int64Attribute("method", int64(method)),
|
|
|
|
trace.StringAttribute("value", value.String()),
|
|
|
|
)
|
|
|
|
}
|
2019-07-16 11:35:56 +00:00
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
msg := &types.Message{
|
2019-08-15 16:23:28 +00:00
|
|
|
From: vmc.msg.To,
|
|
|
|
To: to,
|
|
|
|
Method: method,
|
|
|
|
Value: value,
|
|
|
|
Params: params,
|
2019-08-16 12:41:43 +00:00
|
|
|
GasLimit: vmc.gasAvailable,
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
2019-07-15 22:19:26 +00:00
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
ret, err, _ := vmc.vm.send(ctx, msg, vmc)
|
2019-08-03 11:53:18 +00:00
|
|
|
return ret, err
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockHeight returns the height of the block this message was added to the chain in
|
|
|
|
func (vmc *VMContext) BlockHeight() uint64 {
|
|
|
|
return vmc.height
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (vmc *VMContext) GasUsed() types.BigInt {
|
2019-08-15 16:23:28 +00:00
|
|
|
return vmc.gasUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vmc *VMContext) ChargeGas(amount uint64) aerrors.ActorError {
|
|
|
|
toUse := types.NewInt(amount)
|
|
|
|
vmc.gasUsed = types.BigAdd(vmc.gasUsed, toUse)
|
2019-08-16 12:41:43 +00:00
|
|
|
if types.BigCmp(vmc.gasUsed, vmc.gasAvailable) > 0 {
|
|
|
|
return aerrors.Newf(254, "not enough gas: used=%s, available=%s", vmc.gasUsed, vmc.gasAvailable)
|
2019-08-15 16:23:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
func (vmc *VMContext) StateTree() (types.StateTree, aerrors.ActorError) {
|
2019-07-12 23:52:25 +00:00
|
|
|
if vmc.msg.To != actors.InitActorAddress {
|
2019-07-22 18:17:42 +00:00
|
|
|
return nil, aerrors.Escalate(fmt.Errorf("only init actor can access state tree directly"), "invalid use of StateTree")
|
2019-07-12 16:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return vmc.state, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (vmctx *VMContext) VerifySignature(sig *types.Signature, act address.Address, data []byte) aerrors.ActorError {
|
|
|
|
if act.Protocol() == address.ID {
|
|
|
|
kaddr, err := vmctx.resolveToKeyAddr(act)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.Wrap(err, "failed to resolve address to key address")
|
|
|
|
}
|
|
|
|
act = kaddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sig.Verify(act, data); err != nil {
|
|
|
|
return aerrors.New(2, "signature verification failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vmctx *VMContext) resolveToKeyAddr(addr address.Address) (address.Address, aerrors.ActorError) {
|
|
|
|
if addr.Protocol() == address.BLS || addr.Protocol() == address.SECP256K1 {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := vmctx.state.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, aerrors.Newf(1, "failed to find actor: %s", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if act.Code != actors.AccountActorCodeCid {
|
|
|
|
return address.Undef, aerrors.New(1, "address was not for an account actor")
|
|
|
|
}
|
|
|
|
|
|
|
|
var aast actors.AccountActorState
|
|
|
|
if err := vmctx.cst.Get(context.TODO(), act.Head, &aast); err != nil {
|
|
|
|
return address.Undef, aerrors.Escalate(err, fmt.Sprintf("failed to get account actor state for %s", addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return aast.Address, nil
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
func (vm *VM) makeVMContext(ctx context.Context, sroot cid.Cid, msg *types.Message, origin address.Address, usedGas types.BigInt) *VMContext {
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return &VMContext{
|
2019-07-26 19:01:02 +00:00
|
|
|
ctx: ctx,
|
2019-07-12 20:56:41 +00:00
|
|
|
vm: vm,
|
|
|
|
state: vm.cstate,
|
2019-07-12 03:59:55 +00:00
|
|
|
sroot: sroot,
|
2019-07-05 14:29:17 +00:00
|
|
|
msg: msg,
|
2019-07-16 16:40:25 +00:00
|
|
|
origin: origin,
|
2019-07-12 20:56:41 +00:00
|
|
|
height: vm.blockHeight,
|
2019-07-26 04:54:22 +00:00
|
|
|
cst: vm.cst,
|
2019-07-12 03:59:55 +00:00
|
|
|
storage: &storage{
|
2019-07-26 04:54:22 +00:00
|
|
|
cst: vm.cst,
|
2019-07-12 03:59:55 +00:00
|
|
|
head: sroot,
|
|
|
|
},
|
2019-08-15 16:23:28 +00:00
|
|
|
|
|
|
|
gasUsed: usedGas,
|
2019-08-16 12:41:43 +00:00
|
|
|
gasAvailable: msg.GasLimit,
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type VM struct {
|
2019-07-25 22:15:03 +00:00
|
|
|
cstate *state.StateTree
|
2019-07-05 14:29:17 +00:00
|
|
|
base cid.Cid
|
2019-07-26 04:54:22 +00:00
|
|
|
cs *store.ChainStore
|
|
|
|
cst *hamt.CborIpldStore
|
2019-07-08 20:29:01 +00:00
|
|
|
buf *bufbstore.BufferedBS
|
2019-07-05 14:29:17 +00:00
|
|
|
blockHeight uint64
|
|
|
|
blockMiner address.Address
|
2019-07-11 15:38:37 +00:00
|
|
|
inv *invoker
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *store.ChainStore) (*VM, error) {
|
|
|
|
buf := bufbstore.NewBufferedBstore(cs.Blockstore())
|
2019-07-05 14:29:17 +00:00
|
|
|
cst := hamt.CSTFromBstore(buf)
|
2019-07-25 22:15:03 +00:00
|
|
|
state, err := state.LoadStateTree(cst, base)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &VM{
|
|
|
|
cstate: state,
|
|
|
|
base: base,
|
|
|
|
cs: cs,
|
2019-07-26 04:54:22 +00:00
|
|
|
cst: cst,
|
2019-07-05 14:29:17 +00:00
|
|
|
buf: buf,
|
|
|
|
blockHeight: height,
|
|
|
|
blockMiner: maddr,
|
2019-07-11 15:47:05 +00:00
|
|
|
inv: newInvoker(),
|
2019-07-05 14:29:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:37:00 +00:00
|
|
|
type ApplyRet struct {
|
|
|
|
types.MessageReceipt
|
|
|
|
ActorErr aerrors.ActorError
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
func (vm *VM) send(ctx context.Context, msg *types.Message, parent *VMContext) ([]byte, aerrors.ActorError, *VMContext) {
|
|
|
|
|
2019-08-03 11:26:35 +00:00
|
|
|
st := vm.cstate
|
|
|
|
fromActor, err := st.GetActor(msg.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Absorb(err, 1, "could not find source actor"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
toActor, err := st.GetActor(msg.To)
|
|
|
|
if err != nil {
|
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
a, err := TryCreateAccountActor(st, msg.To)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Absorb(err, 1, "could not create account"), nil
|
|
|
|
}
|
|
|
|
toActor = a
|
|
|
|
} else {
|
|
|
|
return nil, aerrors.Escalate(err, "getting actor"), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
gasUsed := types.NewInt(0)
|
|
|
|
origin := msg.From
|
|
|
|
if parent != nil {
|
|
|
|
gasUsed = parent.gasUsed
|
|
|
|
origin = parent.origin
|
2019-08-03 11:26:35 +00:00
|
|
|
}
|
2019-08-15 16:23:28 +00:00
|
|
|
vmctx := vm.makeVMContext(ctx, toActor.Head, msg, origin, gasUsed)
|
|
|
|
if parent != nil {
|
|
|
|
defer func() {
|
|
|
|
parent.gasUsed = vmctx.gasUsed
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.BigCmp(msg.Value, types.NewInt(0)) != 0 {
|
|
|
|
if aerr := vmctx.ChargeGas(gasFundTransfer); aerr != nil {
|
|
|
|
return nil, aerrors.Wrap(aerr, "sending funds"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DeductFunds(fromActor, msg.Value); err != nil {
|
|
|
|
return nil, aerrors.Absorb(err, 1, "failed to deduct funds"), nil
|
|
|
|
}
|
|
|
|
DepositFunds(toActor, msg.Value)
|
|
|
|
}
|
|
|
|
|
2019-08-03 11:26:35 +00:00
|
|
|
if msg.Method != 0 {
|
|
|
|
ret, err := vm.Invoke(toActor, vmctx, msg.Method, msg.Params)
|
|
|
|
toActor.Head = vmctx.Storage().GetHead()
|
|
|
|
return ret, err, vmctx
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, vmctx
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func checkMessage(msg *types.Message) error {
|
|
|
|
if msg.GasLimit == types.EmptyInt {
|
|
|
|
return xerrors.Errorf("message gas no gas limit set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.GasPrice == types.EmptyInt {
|
|
|
|
return xerrors.Errorf("message gas no gas price set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Value == types.EmptyInt {
|
2019-08-15 16:23:28 +00:00
|
|
|
return xerrors.Errorf("message no value set")
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:37:00 +00:00
|
|
|
func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet, error) {
|
2019-07-26 19:01:02 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "vm.ApplyMessage")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
if err := checkMessage(msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
st := vm.cstate
|
2019-07-28 05:35:32 +00:00
|
|
|
if err := st.Snapshot(); err != nil {
|
|
|
|
return nil, xerrors.Errorf("snapshot failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
fromActor, err := st.GetActor(msg.From)
|
|
|
|
if err != nil {
|
2019-07-16 16:07:14 +00:00
|
|
|
return nil, xerrors.Errorf("from actor not found: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 11:53:18 +00:00
|
|
|
gascost := types.BigMul(msg.GasLimit, msg.GasPrice)
|
|
|
|
totalCost := types.BigAdd(gascost, msg.Value)
|
|
|
|
if types.BigCmp(fromActor.Balance, totalCost) < 0 {
|
2019-08-04 12:11:06 +00:00
|
|
|
return nil, xerrors.Errorf("not enough funds (%s < %s)", fromActor.Balance, totalCost)
|
2019-08-03 11:53:18 +00:00
|
|
|
}
|
|
|
|
if err := DeductFunds(fromActor, gascost); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to deduct funds: %w", err)
|
|
|
|
}
|
2019-07-22 18:17:42 +00:00
|
|
|
|
2019-08-03 11:53:18 +00:00
|
|
|
if msg.Nonce != fromActor.Nonce {
|
|
|
|
return nil, xerrors.Errorf("invalid nonce (got %d, expected %d)", msg.Nonce, fromActor.Nonce)
|
|
|
|
}
|
|
|
|
fromActor.Nonce++
|
2019-08-15 16:23:28 +00:00
|
|
|
|
|
|
|
ret, actorErr, vmctx := vm.send(ctx, msg, nil)
|
2019-08-03 11:53:18 +00:00
|
|
|
|
|
|
|
var errcode uint8
|
2019-08-15 16:23:28 +00:00
|
|
|
var gasUsed types.BigInt
|
2019-08-03 11:53:18 +00:00
|
|
|
if errcode = aerrors.RetCode(actorErr); errcode != 0 {
|
2019-08-15 16:23:28 +00:00
|
|
|
gasUsed = msg.GasLimit
|
2019-08-03 11:53:18 +00:00
|
|
|
// revert all state changes since snapshot
|
|
|
|
if err := st.Revert(); err != nil {
|
|
|
|
return nil, xerrors.Errorf("revert state failed: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-08-03 11:26:35 +00:00
|
|
|
} else {
|
2019-08-03 11:53:18 +00:00
|
|
|
// refund unused gas
|
2019-08-15 16:23:28 +00:00
|
|
|
gasUsed = vmctx.GasUsed()
|
|
|
|
refund := types.BigMul(types.BigSub(msg.GasLimit, gasUsed), msg.GasPrice)
|
2019-08-03 11:53:18 +00:00
|
|
|
DepositFunds(fromActor, refund)
|
|
|
|
}
|
2019-08-03 11:26:35 +00:00
|
|
|
|
2019-08-03 11:53:18 +00:00
|
|
|
miner, err := st.GetActor(vm.blockMiner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting block miner actor (%s) failed: %w", vm.blockMiner, err)
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-08-15 16:23:28 +00:00
|
|
|
gasReward := types.BigMul(msg.GasPrice, gasUsed)
|
2019-08-03 11:53:18 +00:00
|
|
|
DepositFunds(miner, gasReward)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-08-03 11:53:18 +00:00
|
|
|
return &ApplyRet{
|
|
|
|
MessageReceipt: types.MessageReceipt{
|
|
|
|
ExitCode: errcode,
|
|
|
|
Return: ret,
|
2019-08-15 16:23:28 +00:00
|
|
|
GasUsed: gasUsed,
|
2019-08-03 11:53:18 +00:00
|
|
|
},
|
|
|
|
ActorErr: actorErr,
|
|
|
|
}, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (vm *VM) ActorBalance(addr address.Address) (types.BigInt, aerrors.ActorError) {
|
|
|
|
act, err := vm.cstate.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, aerrors.Absorb(err, 1, "failed to find actor")
|
|
|
|
}
|
|
|
|
|
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
func (vm *VM) Flush(ctx context.Context) (cid.Cid, error) {
|
|
|
|
from := dag.NewDAGService(bserv.New(vm.buf, nil))
|
2019-07-08 20:29:01 +00:00
|
|
|
to := dag.NewDAGService(bserv.New(vm.buf.Read(), nil))
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
root, err := vm.cstate.Flush()
|
|
|
|
if err != nil {
|
2019-07-15 20:27:45 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 22:19:26 +00:00
|
|
|
if err := Copy(ctx, from, to, root); err != nil {
|
2019-07-15 20:27:45 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("copying tree: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2019-07-15 22:19:26 +00:00
|
|
|
func Copy(ctx context.Context, from, to ipld.DAGService, root cid.Cid) error {
|
|
|
|
if root.Prefix().MhType == 0 {
|
|
|
|
// identity cid, skip
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
node, err := from.Get(ctx, root)
|
|
|
|
if err != nil {
|
2019-07-16 16:07:14 +00:00
|
|
|
return xerrors.Errorf("get %s failed: %w", root, err)
|
2019-07-15 22:19:26 +00:00
|
|
|
}
|
|
|
|
links := node.Links()
|
|
|
|
for _, link := range links {
|
2019-07-25 22:15:33 +00:00
|
|
|
if link.Cid.Prefix().MhType == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-15 22:19:26 +00:00
|
|
|
_, err := to.Get(ctx, link.Cid)
|
|
|
|
switch err {
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
case nil:
|
|
|
|
continue
|
|
|
|
case ipld.ErrNotFound:
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
if err := Copy(ctx, from, to, link.Cid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = to.Add(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (vm *VM) TransferFunds(from, to address.Address, amt types.BigInt) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
if from == to {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAct, err := vm.cstate.GetActor(from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
toAct, err := vm.cstate.GetActor(from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DeductFunds(fromAct, amt); err != nil {
|
2019-07-16 16:07:14 +00:00
|
|
|
return xerrors.Errorf("failed to deduct funds: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
DepositFunds(toAct, amt)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (vm *VM) SetBlockHeight(h uint64) {
|
|
|
|
vm.blockHeight = h
|
|
|
|
}
|
|
|
|
|
2019-07-22 18:17:42 +00:00
|
|
|
func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) ([]byte, aerrors.ActorError) {
|
2019-07-26 19:01:02 +00:00
|
|
|
ctx, span := trace.StartSpan(vmctx.ctx, "vm.Invoke")
|
|
|
|
defer span.End()
|
|
|
|
var oldCtx context.Context
|
|
|
|
oldCtx, vmctx.ctx = vmctx.ctx, ctx
|
|
|
|
defer func() {
|
|
|
|
vmctx.ctx = oldCtx
|
|
|
|
}()
|
2019-07-11 15:38:37 +00:00
|
|
|
ret, err := vm.inv.Invoke(act, vmctx, method, params)
|
|
|
|
if err != nil {
|
2019-07-22 18:17:42 +00:00
|
|
|
return nil, err
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|
2019-07-22 18:17:42 +00:00
|
|
|
return ret, nil
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
2019-07-26 04:54:22 +00:00
|
|
|
|
|
|
|
func DeductFunds(act *types.Actor, amt types.BigInt) error {
|
|
|
|
if types.BigCmp(act.Balance, amt) < 0 {
|
|
|
|
return fmt.Errorf("not enough funds")
|
|
|
|
}
|
|
|
|
|
|
|
|
act.Balance = types.BigSub(act.Balance, amt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DepositFunds(act *types.Actor, amt types.BigInt) {
|
|
|
|
act.Balance = types.BigAdd(act.Balance, amt)
|
|
|
|
}
|
2019-07-25 22:15:33 +00:00
|
|
|
|
|
|
|
func MiningRewardForBlock(base *types.TipSet) types.BigInt {
|
|
|
|
return types.NewInt(10000)
|
|
|
|
}
|