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"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
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
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
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-07-12 03:59:55 +00:00
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(ExecParams{})
|
2019-07-15 20:27:45 +00:00
|
|
|
cbor.RegisterCborType(struct{}{})
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ia InitActor) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
nil,
|
|
|
|
ia.Exec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecParams struct {
|
|
|
|
Code cid.Cid
|
|
|
|
Params []byte
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:56:49 +00:00
|
|
|
func (ep *ExecParams) UnmarshalCBOR(b []byte) (int, error) {
|
|
|
|
if err := cbor.DecodeInto(b, ep); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:12:06 +00:00
|
|
|
func CreateExecParams(act cid.Cid, obj interface{}) ([]byte, error) {
|
|
|
|
encparams, err := cbor.DumpObject(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ep ExecParams
|
|
|
|
ep.Code = act
|
|
|
|
ep.Params = encparams
|
|
|
|
|
|
|
|
return cbor.DumpObject(ep)
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams) (types.InvokeRet, error) {
|
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-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, err
|
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) {
|
|
|
|
log.Error("cannot launch actor instance that is not a builtin actor")
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{
|
|
|
|
ReturnCode: 1,
|
2019-07-12 03:59:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that singeltons can be only launched once.
|
|
|
|
// TODO: do we want to enforce this? If so how should actors be marked as such?
|
|
|
|
if IsSingletonActor(p.Code) {
|
|
|
|
log.Error("cannot launch another actor of this type")
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{
|
|
|
|
ReturnCode: 1,
|
2019-07-12 03:59:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the actor itself
|
|
|
|
actor := types.Actor{
|
|
|
|
Code: p.Code,
|
|
|
|
Balance: vmctx.Message().Value,
|
2019-07-15 20:27:45 +00:00
|
|
|
Head: EmptyCBOR,
|
2019-07-12 03:59:55 +00:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
2019-07-15 20:27:45 +00:00
|
|
|
_, err = vmctx.Storage().Put(struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return types.InvokeRet{}, err
|
|
|
|
}
|
2019-07-12 03:59:55 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
idAddr, err := self.AddActor(vmctx, addr)
|
|
|
|
if err != nil {
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, errors.Wrap(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-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, err
|
2019-07-12 16:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := state.SetActor(idAddr, &actor); err != nil {
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, errors.Wrap(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-15 22:19:26 +00:00
|
|
|
fmt.Println("PARAMS: ", string(p.Params))
|
2019-07-15 20:27:45 +00:00
|
|
|
_, _, err = vmctx.Send(idAddr, 0, vmctx.Message().Value, p.Params)
|
|
|
|
if err != nil {
|
|
|
|
return types.InvokeRet{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
c, err := vmctx.Storage().Put(self)
|
|
|
|
if err != nil {
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(beginState, c); err != nil {
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{}, err
|
2019-07-12 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
return types.InvokeRet{
|
|
|
|
Result: idAddr.Bytes(),
|
2019-07-12 03:59:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsBuiltinActor(code cid.Cid) bool {
|
|
|
|
switch code {
|
|
|
|
case StorageMinerCodeCid, StorageMinerCodeCid, AccountActorCodeCid, InitActorCodeCid, MultisigActorCodeCid:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsSingletonActor(code cid.Cid) bool {
|
|
|
|
return code == StorageMarketActorCodeCid || code == InitActorCodeCid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ias *InitActorState) AddActor(vmctx types.VMContext, addr address.Address) (address.Address, error) {
|
|
|
|
nid := ias.NextID
|
|
|
|
ias.NextID++
|
|
|
|
|
|
|
|
amap, err := hamt.LoadNode(context.TODO(), vmctx.Ipld(), ias.AddressMap)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ncid, err := vmctx.Ipld().Put(context.TODO(), amap)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
ias.AddressMap = ncid
|
|
|
|
|
|
|
|
return address.NewIDAddress(nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := amap.Find(context.TODO(), string(addr.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func ComputeActorAddress(creator address.Address, nonce uint64) (address.Address, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err := buf.Write(creator.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return address.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = binary.Write(buf, binary.BigEndian, nonce)
|
|
|
|
if err != nil {
|
|
|
|
return address.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.NewActorAddress(buf.Bytes())
|
|
|
|
}
|