move actors to separate package

This commit is contained in:
whyrusleeping 2019-07-12 16:52:25 -07:00
parent e62816dd15
commit 0ac3545013
13 changed files with 115 additions and 107 deletions

View File

@ -1,7 +1,9 @@
package chain
package actors
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"github.com/filecoin-project/go-lotus/chain/address"
@ -10,9 +12,12 @@ import (
"github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log"
"github.com/pkg/errors"
)
var log = logging.Logger("actors")
func init() {
cbor.RegisterCborType(ExecParams{})
}
@ -59,19 +64,19 @@ func CreateExecParams(act cid.Cid, obj interface{}) ([]byte, error) {
return cbor.DumpObject(ep)
}
func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams) (InvokeRet, error) {
func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams) (types.InvokeRet, error) {
beginState := vmctx.Storage().GetHead()
var self InitActorState
if err := vmctx.Storage().Get(beginState, &self); err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
// 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")
return InvokeRet{
returnCode: 1,
return types.InvokeRet{
ReturnCode: 1,
}, nil
}
@ -79,8 +84,8 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
// 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")
return InvokeRet{
returnCode: 1,
return types.InvokeRet{
ReturnCode: 1,
}, nil
}
@ -90,7 +95,7 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
nonce := vmctx.Message().Nonce
addr, err := ComputeActorAddress(creator, nonce)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
// Set up the actor itself
@ -109,31 +114,31 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
// Store the mapping of address to actor ID.
idAddr, err := self.AddActor(vmctx, addr)
if err != nil {
return InvokeRet{}, errors.Wrap(err, "adding new actor mapping")
return types.InvokeRet{}, errors.Wrap(err, "adding new actor mapping")
}
// 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
state, err := vmctx.StateTree()
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
if err := state.SetActor(idAddr, &actor); err != nil {
return InvokeRet{}, errors.Wrap(err, "inserting new actor into state tree")
return types.InvokeRet{}, errors.Wrap(err, "inserting new actor into state tree")
}
c, err := vmctx.Storage().Put(self)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
if err := vmctx.Storage().Commit(beginState, c); err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
return InvokeRet{
result: idAddr.Bytes(),
return types.InvokeRet{
Result: idAddr.Bytes(),
}, nil
}
@ -198,3 +203,18 @@ func (ias *InitActorState) Lookup(cst *hamt.CborIpldStore, addr address.Address)
type AccountActorState struct {
Address address.Address
}
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())
}

View File

@ -1,4 +1,4 @@
package chain
package actors
import (
"github.com/filecoin-project/go-lotus/chain/address"
@ -77,7 +77,7 @@ type StorageMinerConstructorParams struct {
PeerID peer.ID
}
func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) (InvokeRet, error) {
func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) (types.InvokeRet, error) {
var self StorageMinerActorState
self.Owner = vmctx.Message().From
self.Worker = params.Worker
@ -87,12 +87,12 @@ func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMC
storage := vmctx.Storage()
c, err := storage.Put(self)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
if err := storage.Commit(cid.Undef, c); err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
return InvokeRet{}, nil
return types.InvokeRet{}, nil
}

View File

@ -1,4 +1,4 @@
package chain
package actors
import (
"github.com/filecoin-project/go-lotus/chain/address"
@ -42,11 +42,11 @@ func (p *CreateStorageMinerParams) UnmarshalCBOR(b []byte) (int, error) {
return len(b), nil
}
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) (InvokeRet, error) {
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) (types.InvokeRet, error) {
if !SupportedSectorSize(params.SectorSize) {
//Fatal("Unsupported sector size")
return InvokeRet{
returnCode: 1,
return types.InvokeRet{
ReturnCode: 1,
}, nil
}
@ -56,44 +56,44 @@ func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.V
PeerID: params.PeerID,
})
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
ret, exit, err := vmctx.Send(InitActorAddress, 1, vmctx.Message().Value, encoded)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
naddr, err := address.NewFromBytes(ret)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
if exit != 0 {
return InvokeRet{
returnCode: 2,
return types.InvokeRet{
ReturnCode: 2,
}, nil
}
var self StorageMarketState
old := vmctx.Storage().GetHead()
if err := vmctx.Storage().Get(old, &self); err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
self.Miners[naddr] = struct{}{}
nroot, err := vmctx.Storage().Put(self)
if err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
if err := vmctx.Storage().Commit(old, nroot); err != nil {
return InvokeRet{}, err
return types.InvokeRet{}, err
}
return InvokeRet{
result: naddr.Bytes(),
return types.InvokeRet{
Result: naddr.Bytes(),
}, nil
}

View File

@ -1,4 +1,4 @@
package chain
package actors
import (
"github.com/filecoin-project/go-lotus/chain/address"
@ -47,6 +47,3 @@ func init() {
MultisigActorCodeCid = mustSum("multisig")
InitActorCodeCid = mustSum("init")
}
type VMActor struct {
}

View File

@ -1,9 +1,11 @@
package chain
package actors_test
import (
"encoding/binary"
"testing"
"github.com/filecoin-project/go-lotus/chain"
. "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
dstore "github.com/ipfs/go-datastore"
@ -23,7 +25,7 @@ func blsaddr(n uint64) address.Address {
return addr
}
func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
func setupVMTestEnv(t *testing.T) (*chain.VM, []address.Address) {
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
from := blsaddr(0)
@ -33,7 +35,7 @@ func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
from: types.NewInt(1000000),
maddr: types.NewInt(0),
}
st, err := MakeInitialStateTree(bs, actors)
st, err := chain.MakeInitialStateTree(bs, actors)
if err != nil {
t.Fatal(err)
}
@ -43,11 +45,9 @@ func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
t.Fatal(err)
}
cs := &ChainStore{
bs: bs,
}
cs := chain.NewChainStore(bs, nil)
vm, err := NewVM(stateroot, 1, maddr, cs)
vm, err := chain.NewVM(stateroot, 1, maddr, cs)
if err != nil {
t.Fatal(err)
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"sync"
actors "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
@ -28,7 +29,7 @@ type GenesisBootstrap struct {
}
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor, error) {
var ias InitActorState
var ias actors.InitActorState
ias.NextID = 100
cst := hamt.CSTFromBstore(bs)
@ -57,7 +58,7 @@ func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor
}
act := &types.Actor{
Code: InitActorCodeCid,
Code: actors.InitActorCodeCid,
Head: statecid,
}
@ -77,7 +78,7 @@ func init() {
var EmptyObjectCid cid.Cid
func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types.BigInt) (*StateTree, error) {
func MakeInitialStateTree(bs bstore.Blockstore, actmap map[address.Address]types.BigInt) (*StateTree, error) {
cst := hamt.CSTFromBstore(bs)
state, err := NewStateTree(cst)
if err != nil {
@ -90,7 +91,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
}
var addrs []address.Address
for a := range actors {
for a := range actmap {
addrs = append(addrs, a)
}
@ -99,7 +100,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
return nil, err
}
if err := state.SetActor(InitActorAddress, initact); err != nil {
if err := state.SetActor(actors.InitActorAddress, initact); err != nil {
return nil, err
}
@ -108,12 +109,12 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
return nil, err
}
if err := state.SetActor(StorageMarketAddress, smact); err != nil {
if err := state.SetActor(actors.StorageMarketAddress, smact); err != nil {
return nil, err
}
err = state.SetActor(NetworkAddress, &types.Actor{
Code: AccountActorCodeCid,
err = state.SetActor(actors.NetworkAddress, &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(100000000000),
Head: emptyobject,
})
@ -121,9 +122,9 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
return nil, err
}
for a, v := range actors {
for a, v := range actmap {
err = state.SetActor(a, &types.Actor{
Code: AccountActorCodeCid,
Code: actors.AccountActorCodeCid,
Balance: v,
Head: emptyobject,
})
@ -136,7 +137,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
}
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
sms := &StorageMarketState{
sms := &actors.StorageMarketState{
Miners: make(map[address.Address]struct{}),
TotalStorage: types.NewInt(0),
}
@ -147,7 +148,7 @@ func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
}
return &types.Actor{
Code: StorageMarketActorCodeCid,
Code: actors.StorageMarketActorCodeCid,
Head: stcid,
Nonce: 0,
Balance: types.NewInt(0),
@ -184,7 +185,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, w *Wallet) (*GenesisBootstrap, error
fmt.Println("Empty Genesis root: ", emptyroot)
b := &BlockHeader{
Miner: InitActorAddress,
Miner: actors.InitActorAddress,
Tickets: []Ticket{},
ElectionProof: []byte("the Genesis block"),
Parents: []cid.Cid{},

View File

@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
actors "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
)
@ -13,12 +14,8 @@ type invoker struct {
builtInCode map[cid.Cid]nativeCode
}
type invokeFunc func(act *types.Actor, vmctx *VMContext, params []byte) (InvokeRet, error)
type invokeFunc func(act *types.Actor, vmctx *VMContext, params []byte) (types.InvokeRet, error)
type nativeCode []invokeFunc
type InvokeRet struct {
result []byte
returnCode byte
}
func newInvoker() *invoker {
inv := &invoker{
@ -26,20 +23,20 @@ func newInvoker() *invoker {
}
// add builtInCode using: register(cid, singleton)
inv.register(InitActorCodeCid, InitActor{})
inv.register(StorageMarketActorCodeCid, StorageMarketActor{})
inv.register(actors.InitActorCodeCid, actors.InitActor{})
inv.register(actors.StorageMarketActorCodeCid, actors.StorageMarketActor{})
return inv
}
func (inv *invoker) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) (InvokeRet, error) {
func (inv *invoker) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) (types.InvokeRet, error) {
code, ok := inv.builtInCode[act.Code]
if !ok {
return InvokeRet{}, errors.New("no code for actor")
return types.InvokeRet{}, errors.New("no code for actor")
}
if method >= uint64(len(code)) || code[method] == nil {
return InvokeRet{}, fmt.Errorf("no method %d on actor", method)
return types.InvokeRet{}, fmt.Errorf("no method %d on actor", method)
}
return code[method](act, vmctx, params)
@ -104,7 +101,7 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
return nil, newErr("wrong number of outputs should be: " +
"(InvokeRet, error)")
}
if t.Out(0) != reflect.TypeOf(InvokeRet{}) {
if t.Out(0) != reflect.TypeOf(types.InvokeRet{}) {
return nil, newErr("first output should be of type InvokeRet")
}
if !t.Out(1).Implements(tError) {
@ -124,7 +121,7 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
_, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes)
if err != nil {
return []reflect.Value{
reflect.ValueOf(InvokeRet{}),
reflect.ValueOf(types.InvokeRet{}),
// Below is a hack, fixed in Go 1.13
// https://git.io/fjXU6
reflect.ValueOf(&err).Elem(),

View File

@ -43,20 +43,20 @@ func (b basicContract) Exports() []interface{} {
}
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b,
params *basicParams) (types.InvokeRet, error) {
return types.InvokeRet{
ReturnCode: params.b,
}, nil
}
func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext,
params *badParam) (InvokeRet, error) {
params *badParam) (types.InvokeRet, error) {
panic("should not execute")
}
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b + 10,
params *basicParams) (types.InvokeRet, error) {
return types.InvokeRet{
ReturnCode: params.b + 10,
}, nil
}
@ -66,11 +66,11 @@ func TestInvokerBasic(t *testing.T) {
assert.NoError(t, err)
ret, err := code[0](nil, nil, []byte{1})
assert.NoError(t, err)
assert.Equal(t, byte(1), ret.returnCode, "return code should be 1")
assert.Equal(t, byte(1), ret.ReturnCode, "return code should be 1")
ret, err = code[10](nil, &VMContext{}, []byte{2})
assert.NoError(t, err)
assert.Equal(t, byte(12), ret.returnCode, "return code should be 1")
assert.Equal(t, byte(12), ret.ReturnCode, "return code should be 1")
ret, err = code[1](nil, &VMContext{}, []byte{2})
assert.Error(t, err)

View File

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
sharray "github.com/whyrusleeping/sharray"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
bls "github.com/filecoin-project/go-lotus/lib/bls-signatures"
@ -32,7 +33,7 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
}
// apply miner reward
if err := vm.TransferFunds(NetworkAddress, miner, miningRewardForBlock(parents)); err != nil {
if err := vm.TransferFunds(actors.NetworkAddress, miner, miningRewardForBlock(parents)); err != nil {
return nil, err
}

View File

@ -8,6 +8,7 @@ import (
hamt "github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
)
@ -64,12 +65,12 @@ func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
}
func (st *StateTree) lookupID(addr address.Address) (address.Address, error) {
act, err := st.GetActor(InitActorAddress)
act, err := st.GetActor(actors.InitActorAddress)
if err != nil {
return address.Undef, err
}
var ias InitActorState
var ias actors.InitActorState
if err := st.store.Get(context.TODO(), act.Head, &ias); err != nil {
return address.Undef, err
}
@ -144,8 +145,8 @@ func (st *StateTree) Snapshot() error {
func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) (address.Address, error) {
var out address.Address
err := st.MutateActor(InitActorAddress, func(initact *types.Actor) error {
var ias InitActorState
err := st.MutateActor(actors.InitActorAddress, func(initact *types.Actor) error {
var ias actors.InitActorState
if err := st.store.Get(context.TODO(), initact.Head, &ias); err != nil {
return err
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"sync"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
@ -569,7 +570,7 @@ func (syncer *Syncer) ValidateBlock(b *FullBlock) error {
return err
}
if err := vm.TransferFunds(NetworkAddress, b.Header.Miner, miningRewardForBlock(baseTs)); err != nil {
if err := vm.TransferFunds(actors.NetworkAddress, b.Header.Miner, miningRewardForBlock(baseTs)); err != nil {
return err
}
@ -648,7 +649,7 @@ func makeActor(st *StateTree, addr address.Address) (*types.Actor, error) {
}
func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
var acstate AccountActorState
var acstate actors.AccountActorState
acstate.Address = addr
c, err := st.store.Put(context.TODO(), acstate)
@ -657,7 +658,7 @@ func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, erro
}
nact := &types.Actor{
Code: AccountActorCodeCid,
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(0),
Head: c,
}
@ -667,7 +668,7 @@ func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, erro
func NewSecp256k1AccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
nact := &types.Actor{
Code: AccountActorCodeCid,
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(0),
Head: EmptyObjectCid,
}

6
chain/types/invokeret.go Normal file
View File

@ -0,0 +1,6 @@
package types
type InvokeRet struct {
Result []byte
ReturnCode byte
}

View File

@ -1,11 +1,10 @@
package chain
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/lib/bufbstore"
@ -102,7 +101,7 @@ func (vmc *VMContext) GasUsed() types.BigInt {
}
func (vmc *VMContext) StateTree() (types.StateTree, error) {
if vmc.msg.To != InitActorAddress {
if vmc.msg.To != actors.InitActorAddress {
return nil, fmt.Errorf("only init actor can access state tree directly")
}
@ -276,20 +275,5 @@ func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params [
if err != nil {
return nil, 0, err
}
return ret.result, ret.returnCode, nil
}
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())
return ret.Result, ret.ReturnCode, nil
}