2019-07-12 23:52:25 +00:00
|
|
|
package actors
|
2019-07-12 03:59:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-12 23:52:25 +00:00
|
|
|
"bytes"
|
2019-07-12 03:59:55 +00:00
|
|
|
"context"
|
2019-07-12 23:52:25 +00:00
|
|
|
"encoding/binary"
|
2019-07-12 03:59:55 +00:00
|
|
|
"fmt"
|
2019-09-10 19:58:45 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-10 19:58:45 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-22 18:17:42 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 03:59:55 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-09-06 22:40:24 +00:00
|
|
|
"github.com/ipfs/go-hamt-ipld"
|
2019-07-12 03:59:55 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-07-12 23:52:25 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-15 20:27:45 +00:00
|
|
|
mh "github.com/multiformats/go-multihash"
|
2019-07-12 03:59:55 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
var log = logging.Logger("actors")
|
|
|
|
|
2019-07-15 20:27:45 +00:00
|
|
|
var EmptyCBOR cid.Cid
|
|
|
|
|
2019-08-21 20:47:13 +00:00
|
|
|
const (
|
|
|
|
GasCreateActor = 100
|
|
|
|
)
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
func init() {
|
2019-07-15 22:19:26 +00:00
|
|
|
|
|
|
|
n, err := cbor.WrapObject(map[string]string{}, mh.SHA2_256, -1)
|
2019-07-15 20:27:45 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-07-15 22:19:26 +00:00
|
|
|
|
2019-07-15 20:27:45 +00:00
|
|
|
EmptyCBOR = n.Cid()
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type InitActor struct{}
|
|
|
|
|
|
|
|
type InitActorState struct {
|
|
|
|
// TODO: this needs to be a HAMT, its a dumb map for now
|
|
|
|
AddressMap cid.Cid
|
|
|
|
|
|
|
|
NextID uint64
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
type iAMethods struct {
|
|
|
|
Exec uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var IAMethods = iAMethods{2}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
func (ia InitActor) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
2019-07-26 21:42:38 +00:00
|
|
|
1: nil,
|
|
|
|
2: ia.Exec,
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecParams struct {
|
|
|
|
Code cid.Cid
|
|
|
|
Params []byte
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
func CreateExecParams(act cid.Cid, obj cbg.CBORMarshaler) ([]byte, aerrors.ActorError) {
|
2019-07-22 16:08:54 +00:00
|
|
|
encparams, err := SerializeParams(obj)
|
2019-07-12 23:12:06 +00:00
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, aerrors.Wrap(err, "creating ExecParams")
|
2019-07-12 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
return SerializeParams(&ExecParams{
|
|
|
|
Code: act,
|
|
|
|
Params: encparams,
|
|
|
|
})
|
2019-07-12 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams) ([]byte, aerrors.ActorError) {
|
2019-07-12 03:59:55 +00:00
|
|
|
beginState := vmctx.Storage().GetHead()
|
|
|
|
|
|
|
|
var self InitActorState
|
|
|
|
if err := vmctx.Storage().Get(beginState, &self); err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:47:13 +00:00
|
|
|
if err := vmctx.ChargeGas(GasCreateActor); err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "run out of gas")
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
// Make sure that only the actors defined in the spec can be launched.
|
|
|
|
if !IsBuiltinActor(p.Code) {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, aerrors.New(1,
|
|
|
|
"cannot launch actor instance that is not a builtin actor")
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 02:39:45 +00:00
|
|
|
// Ensure that singletons can be only launched once.
|
2019-07-12 03:59:55 +00:00
|
|
|
// TODO: do we want to enforce this? If so how should actors be marked as such?
|
|
|
|
if IsSingletonActor(p.Code) {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, aerrors.New(1, "cannot launch another actor of this type")
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This generates a unique address for this actor that is stable across message
|
|
|
|
// reordering
|
|
|
|
creator := vmctx.Message().From
|
|
|
|
nonce := vmctx.Message().Nonce
|
|
|
|
addr, err := ComputeActorAddress(creator, nonce)
|
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the actor itself
|
|
|
|
actor := types.Actor{
|
|
|
|
Code: p.Code,
|
2019-08-15 21:03:47 +00:00
|
|
|
Balance: types.NewInt(0),
|
2019-07-15 20:27:45 +00:00
|
|
|
Head: EmptyCBOR,
|
2019-07-12 03:59:55 +00:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
// The call to the actors constructor will set up the initial state
|
2019-07-12 15:06:16 +00:00
|
|
|
// from the given parameters, setting `actor.Head` to a new value when successful.
|
2019-07-12 03:59:55 +00:00
|
|
|
// TODO: can constructors fail?
|
|
|
|
//actor.Constructor(p.Params)
|
|
|
|
|
|
|
|
// Store the mapping of address to actor ID.
|
2019-07-25 22:15:03 +00:00
|
|
|
idAddr, nerr := self.AddActor(vmctx.Ipld(), addr)
|
2019-07-22 16:08:54 +00:00
|
|
|
if nerr != nil {
|
|
|
|
return nil, aerrors.Escalate(err, "adding new actor mapping")
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: This is a privileged call that only the init actor is allowed to make
|
|
|
|
// FIXME: Had to comment this because state is not in interface
|
2019-07-12 16:40:58 +00:00
|
|
|
state, err := vmctx.StateTree()
|
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-12 16:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := state.SetActor(idAddr, &actor); err != nil {
|
2019-07-22 18:17:42 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
return nil, aerrors.Absorb(err, 1, "SetActor, actor not found")
|
|
|
|
}
|
2019-07-23 13:16:42 +00:00
|
|
|
return nil, aerrors.Escalate(err, "inserting new actor into state tree")
|
2019-07-12 16:40:58 +00:00
|
|
|
}
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
// '1' is reserved for constructor methods
|
|
|
|
_, err = vmctx.Send(idAddr, 1, vmctx.Message().Value, p.Params)
|
2019-07-15 20:27:45 +00:00
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-15 20:27:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
c, err := vmctx.Storage().Put(&self)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(beginState, c); err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return nil, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
return idAddr.Bytes(), nil
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsBuiltinActor(code cid.Cid) bool {
|
|
|
|
switch code {
|
2019-10-21 18:12:11 +00:00
|
|
|
case StorageMarketCodeCid, StoragePowerCodeCid, StorageMinerCodeCid, AccountCodeCid, InitCodeCid, MultisigCodeCid, PaymentChannelCodeCid:
|
2019-07-12 03:59:55 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsSingletonActor(code cid.Cid) bool {
|
2019-10-22 17:34:59 +00:00
|
|
|
return code == StoragePowerCodeCid || code == StorageMarketCodeCid || code == InitCodeCid
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (ias *InitActorState) AddActor(cst *hamt.CborIpldStore, addr address.Address) (address.Address, error) {
|
2019-07-12 03:59:55 +00:00
|
|
|
nid := ias.NextID
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
amap, err := hamt.LoadNode(context.TODO(), cst, ias.AddressMap)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := amap.Set(context.TODO(), string(addr.Bytes()), nid); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := amap.Flush(context.TODO()); err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
ncid, err := cst.Put(context.TODO(), amap)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
ias.AddressMap = ncid
|
2019-10-23 02:47:45 +00:00
|
|
|
ias.NextID++
|
2019-07-12 03:59:55 +00:00
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
return NewIDAddress(nid)
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ias *InitActorState) Lookup(cst *hamt.CborIpldStore, addr address.Address) (address.Address, error) {
|
|
|
|
amap, err := hamt.LoadNode(context.TODO(), cst, ias.AddressMap)
|
|
|
|
if err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return address.Undef, xerrors.Errorf("ias lookup failed loading hamt node: %w", err)
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 22:40:24 +00:00
|
|
|
var val interface{}
|
|
|
|
err = amap.Find(context.TODO(), string(addr.Bytes()), &val)
|
2019-07-12 03:59:55 +00:00
|
|
|
if err != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
return address.Undef, xerrors.Errorf("ias lookup failed to do find: %w", err)
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ival, ok := val.(uint64)
|
|
|
|
if !ok {
|
|
|
|
return address.Undef, fmt.Errorf("invalid value in init actor state, expected uint64, got %T", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.NewIDAddress(ival)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountActorState struct {
|
|
|
|
Address address.Address
|
|
|
|
}
|
2019-07-12 23:52:25 +00:00
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
func ComputeActorAddress(creator address.Address, nonce uint64) (address.Address, ActorError) {
|
2019-07-12 23:52:25 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err := buf.Write(creator.Bytes())
|
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return address.Undef, aerrors.Escalate(err, "could not write address")
|
2019-07-12 23:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = binary.Write(buf, binary.BigEndian, nonce)
|
|
|
|
if err != nil {
|
2019-07-22 16:08:54 +00:00
|
|
|
return address.Undef, aerrors.Escalate(err, "could not write nonce")
|
2019-07-12 23:52:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
addr, err := address.NewActorAddress(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, aerrors.Escalate(err, "could not create address")
|
|
|
|
}
|
|
|
|
return addr, nil
|
2019-07-12 23:52:25 +00:00
|
|
|
}
|