move actors to separate package
This commit is contained in:
parent
e62816dd15
commit
0ac3545013
@ -1,7 +1,9 @@
|
|||||||
package chain
|
package actors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
@ -10,9 +12,12 @@ import (
|
|||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
hamt "github.com/ipfs/go-hamt-ipld"
|
hamt "github.com/ipfs/go-hamt-ipld"
|
||||||
cbor "github.com/ipfs/go-ipld-cbor"
|
cbor "github.com/ipfs/go-ipld-cbor"
|
||||||
|
logging "github.com/ipfs/go-log"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logging.Logger("actors")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cbor.RegisterCborType(ExecParams{})
|
cbor.RegisterCborType(ExecParams{})
|
||||||
}
|
}
|
||||||
@ -59,19 +64,19 @@ func CreateExecParams(act cid.Cid, obj interface{}) ([]byte, error) {
|
|||||||
return cbor.DumpObject(ep)
|
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()
|
beginState := vmctx.Storage().GetHead()
|
||||||
|
|
||||||
var self InitActorState
|
var self InitActorState
|
||||||
if err := vmctx.Storage().Get(beginState, &self); err != nil {
|
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.
|
// Make sure that only the actors defined in the spec can be launched.
|
||||||
if !IsBuiltinActor(p.Code) {
|
if !IsBuiltinActor(p.Code) {
|
||||||
log.Error("cannot launch actor instance that is not a builtin actor")
|
log.Error("cannot launch actor instance that is not a builtin actor")
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: 1,
|
ReturnCode: 1,
|
||||||
}, nil
|
}, 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?
|
// TODO: do we want to enforce this? If so how should actors be marked as such?
|
||||||
if IsSingletonActor(p.Code) {
|
if IsSingletonActor(p.Code) {
|
||||||
log.Error("cannot launch another actor of this type")
|
log.Error("cannot launch another actor of this type")
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: 1,
|
ReturnCode: 1,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +95,7 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
|
|||||||
nonce := vmctx.Message().Nonce
|
nonce := vmctx.Message().Nonce
|
||||||
addr, err := ComputeActorAddress(creator, nonce)
|
addr, err := ComputeActorAddress(creator, nonce)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the actor itself
|
// 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.
|
// Store the mapping of address to actor ID.
|
||||||
idAddr, err := self.AddActor(vmctx, addr)
|
idAddr, err := self.AddActor(vmctx, addr)
|
||||||
if err != nil {
|
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
|
// 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
|
// FIXME: Had to comment this because state is not in interface
|
||||||
state, err := vmctx.StateTree()
|
state, err := vmctx.StateTree()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := state.SetActor(idAddr, &actor); err != nil {
|
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)
|
c, err := vmctx.Storage().Put(self)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := vmctx.Storage().Commit(beginState, c); err != nil {
|
if err := vmctx.Storage().Commit(beginState, c); err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
result: idAddr.Bytes(),
|
Result: idAddr.Bytes(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,3 +203,18 @@ func (ias *InitActorState) Lookup(cst *hamt.CborIpldStore, addr address.Address)
|
|||||||
type AccountActorState struct {
|
type AccountActorState struct {
|
||||||
Address address.Address
|
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())
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package chain
|
package actors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
@ -77,7 +77,7 @@ type StorageMinerConstructorParams struct {
|
|||||||
PeerID peer.ID
|
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
|
var self StorageMinerActorState
|
||||||
self.Owner = vmctx.Message().From
|
self.Owner = vmctx.Message().From
|
||||||
self.Worker = params.Worker
|
self.Worker = params.Worker
|
||||||
@ -87,12 +87,12 @@ func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMC
|
|||||||
storage := vmctx.Storage()
|
storage := vmctx.Storage()
|
||||||
c, err := storage.Put(self)
|
c, err := storage.Put(self)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := storage.Commit(cid.Undef, c); err != nil {
|
if err := storage.Commit(cid.Undef, c); err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return InvokeRet{}, nil
|
return types.InvokeRet{}, nil
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package chain
|
package actors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
@ -42,11 +42,11 @@ func (p *CreateStorageMinerParams) UnmarshalCBOR(b []byte) (int, error) {
|
|||||||
return len(b), nil
|
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) {
|
if !SupportedSectorSize(params.SectorSize) {
|
||||||
//Fatal("Unsupported sector size")
|
//Fatal("Unsupported sector size")
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: 1,
|
ReturnCode: 1,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,44 +56,44 @@ func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.V
|
|||||||
PeerID: params.PeerID,
|
PeerID: params.PeerID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, exit, err := vmctx.Send(InitActorAddress, 1, vmctx.Message().Value, encoded)
|
ret, exit, err := vmctx.Send(InitActorAddress, 1, vmctx.Message().Value, encoded)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
naddr, err := address.NewFromBytes(ret)
|
naddr, err := address.NewFromBytes(ret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if exit != 0 {
|
if exit != 0 {
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: 2,
|
ReturnCode: 2,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var self StorageMarketState
|
var self StorageMarketState
|
||||||
old := vmctx.Storage().GetHead()
|
old := vmctx.Storage().GetHead()
|
||||||
if err := vmctx.Storage().Get(old, &self); err != nil {
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
self.Miners[naddr] = struct{}{}
|
self.Miners[naddr] = struct{}{}
|
||||||
|
|
||||||
nroot, err := vmctx.Storage().Put(self)
|
nroot, err := vmctx.Storage().Put(self)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
||||||
return InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
result: naddr.Bytes(),
|
Result: naddr.Bytes(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package chain
|
package actors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
@ -47,6 +47,3 @@ func init() {
|
|||||||
MultisigActorCodeCid = mustSum("multisig")
|
MultisigActorCodeCid = mustSum("multisig")
|
||||||
InitActorCodeCid = mustSum("init")
|
InitActorCodeCid = mustSum("init")
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMActor struct {
|
|
||||||
}
|
|
@ -1,9 +1,11 @@
|
|||||||
package chain
|
package actors_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"testing"
|
"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/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
dstore "github.com/ipfs/go-datastore"
|
dstore "github.com/ipfs/go-datastore"
|
||||||
@ -23,7 +25,7 @@ func blsaddr(n uint64) address.Address {
|
|||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
|
func setupVMTestEnv(t *testing.T) (*chain.VM, []address.Address) {
|
||||||
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
|
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
|
||||||
|
|
||||||
from := blsaddr(0)
|
from := blsaddr(0)
|
||||||
@ -33,7 +35,7 @@ func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
|
|||||||
from: types.NewInt(1000000),
|
from: types.NewInt(1000000),
|
||||||
maddr: types.NewInt(0),
|
maddr: types.NewInt(0),
|
||||||
}
|
}
|
||||||
st, err := MakeInitialStateTree(bs, actors)
|
st, err := chain.MakeInitialStateTree(bs, actors)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -43,11 +45,9 @@ func setupVMTestEnv(t *testing.T) (*VM, []address.Address) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cs := &ChainStore{
|
cs := chain.NewChainStore(bs, nil)
|
||||||
bs: bs,
|
|
||||||
}
|
|
||||||
|
|
||||||
vm, err := NewVM(stateroot, 1, maddr, cs)
|
vm, err := chain.NewVM(stateroot, 1, maddr, cs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"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/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"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) {
|
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor, error) {
|
||||||
var ias InitActorState
|
var ias actors.InitActorState
|
||||||
ias.NextID = 100
|
ias.NextID = 100
|
||||||
|
|
||||||
cst := hamt.CSTFromBstore(bs)
|
cst := hamt.CSTFromBstore(bs)
|
||||||
@ -57,7 +58,7 @@ func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor
|
|||||||
}
|
}
|
||||||
|
|
||||||
act := &types.Actor{
|
act := &types.Actor{
|
||||||
Code: InitActorCodeCid,
|
Code: actors.InitActorCodeCid,
|
||||||
Head: statecid,
|
Head: statecid,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ func init() {
|
|||||||
|
|
||||||
var EmptyObjectCid cid.Cid
|
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)
|
cst := hamt.CSTFromBstore(bs)
|
||||||
state, err := NewStateTree(cst)
|
state, err := NewStateTree(cst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -90,7 +91,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
|
|||||||
}
|
}
|
||||||
|
|
||||||
var addrs []address.Address
|
var addrs []address.Address
|
||||||
for a := range actors {
|
for a := range actmap {
|
||||||
addrs = append(addrs, a)
|
addrs = append(addrs, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +100,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := state.SetActor(InitActorAddress, initact); err != nil {
|
if err := state.SetActor(actors.InitActorAddress, initact); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,12 +109,12 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := state.SetActor(StorageMarketAddress, smact); err != nil {
|
if err := state.SetActor(actors.StorageMarketAddress, smact); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = state.SetActor(NetworkAddress, &types.Actor{
|
err = state.SetActor(actors.NetworkAddress, &types.Actor{
|
||||||
Code: AccountActorCodeCid,
|
Code: actors.AccountActorCodeCid,
|
||||||
Balance: types.NewInt(100000000000),
|
Balance: types.NewInt(100000000000),
|
||||||
Head: emptyobject,
|
Head: emptyobject,
|
||||||
})
|
})
|
||||||
@ -121,9 +122,9 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for a, v := range actors {
|
for a, v := range actmap {
|
||||||
err = state.SetActor(a, &types.Actor{
|
err = state.SetActor(a, &types.Actor{
|
||||||
Code: AccountActorCodeCid,
|
Code: actors.AccountActorCodeCid,
|
||||||
Balance: v,
|
Balance: v,
|
||||||
Head: emptyobject,
|
Head: emptyobject,
|
||||||
})
|
})
|
||||||
@ -136,7 +137,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actors map[address.Address]types
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
|
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
|
||||||
sms := &StorageMarketState{
|
sms := &actors.StorageMarketState{
|
||||||
Miners: make(map[address.Address]struct{}),
|
Miners: make(map[address.Address]struct{}),
|
||||||
TotalStorage: types.NewInt(0),
|
TotalStorage: types.NewInt(0),
|
||||||
}
|
}
|
||||||
@ -147,7 +148,7 @@ func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &types.Actor{
|
return &types.Actor{
|
||||||
Code: StorageMarketActorCodeCid,
|
Code: actors.StorageMarketActorCodeCid,
|
||||||
Head: stcid,
|
Head: stcid,
|
||||||
Nonce: 0,
|
Nonce: 0,
|
||||||
Balance: types.NewInt(0),
|
Balance: types.NewInt(0),
|
||||||
@ -184,7 +185,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, w *Wallet) (*GenesisBootstrap, error
|
|||||||
fmt.Println("Empty Genesis root: ", emptyroot)
|
fmt.Println("Empty Genesis root: ", emptyroot)
|
||||||
|
|
||||||
b := &BlockHeader{
|
b := &BlockHeader{
|
||||||
Miner: InitActorAddress,
|
Miner: actors.InitActorAddress,
|
||||||
Tickets: []Ticket{},
|
Tickets: []Ticket{},
|
||||||
ElectionProof: []byte("the Genesis block"),
|
ElectionProof: []byte("the Genesis block"),
|
||||||
Parents: []cid.Cid{},
|
Parents: []cid.Cid{},
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
actors "github.com/filecoin-project/go-lotus/chain/actors"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
)
|
)
|
||||||
@ -13,12 +14,8 @@ type invoker struct {
|
|||||||
builtInCode map[cid.Cid]nativeCode
|
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 nativeCode []invokeFunc
|
||||||
type InvokeRet struct {
|
|
||||||
result []byte
|
|
||||||
returnCode byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func newInvoker() *invoker {
|
func newInvoker() *invoker {
|
||||||
inv := &invoker{
|
inv := &invoker{
|
||||||
@ -26,20 +23,20 @@ func newInvoker() *invoker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add builtInCode using: register(cid, singleton)
|
// add builtInCode using: register(cid, singleton)
|
||||||
inv.register(InitActorCodeCid, InitActor{})
|
inv.register(actors.InitActorCodeCid, actors.InitActor{})
|
||||||
inv.register(StorageMarketActorCodeCid, StorageMarketActor{})
|
inv.register(actors.StorageMarketActorCodeCid, actors.StorageMarketActor{})
|
||||||
|
|
||||||
return inv
|
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]
|
code, ok := inv.builtInCode[act.Code]
|
||||||
if !ok {
|
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 {
|
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)
|
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: " +
|
return nil, newErr("wrong number of outputs should be: " +
|
||||||
"(InvokeRet, error)")
|
"(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")
|
return nil, newErr("first output should be of type InvokeRet")
|
||||||
}
|
}
|
||||||
if !t.Out(1).Implements(tError) {
|
if !t.Out(1).Implements(tError) {
|
||||||
@ -124,7 +121,7 @@ func (*invoker) transform(instance Invokee) (nativeCode, error) {
|
|||||||
_, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes)
|
_, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []reflect.Value{
|
return []reflect.Value{
|
||||||
reflect.ValueOf(InvokeRet{}),
|
reflect.ValueOf(types.InvokeRet{}),
|
||||||
// Below is a hack, fixed in Go 1.13
|
// Below is a hack, fixed in Go 1.13
|
||||||
// https://git.io/fjXU6
|
// https://git.io/fjXU6
|
||||||
reflect.ValueOf(&err).Elem(),
|
reflect.ValueOf(&err).Elem(),
|
||||||
|
@ -43,20 +43,20 @@ func (b basicContract) Exports() []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
|
func (basicContract) InvokeSomething0(act *types.Actor, vmctx types.VMContext,
|
||||||
params *basicParams) (InvokeRet, error) {
|
params *basicParams) (types.InvokeRet, error) {
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: params.b,
|
ReturnCode: params.b,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext,
|
func (basicContract) BadParam(act *types.Actor, vmctx types.VMContext,
|
||||||
params *badParam) (InvokeRet, error) {
|
params *badParam) (types.InvokeRet, error) {
|
||||||
panic("should not execute")
|
panic("should not execute")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
|
func (basicContract) InvokeSomething10(act *types.Actor, vmctx types.VMContext,
|
||||||
params *basicParams) (InvokeRet, error) {
|
params *basicParams) (types.InvokeRet, error) {
|
||||||
return InvokeRet{
|
return types.InvokeRet{
|
||||||
returnCode: params.b + 10,
|
ReturnCode: params.b + 10,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,11 +66,11 @@ func TestInvokerBasic(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
ret, err := code[0](nil, nil, []byte{1})
|
ret, err := code[0](nil, nil, []byte{1})
|
||||||
assert.NoError(t, err)
|
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})
|
ret, err = code[10](nil, &VMContext{}, []byte{2})
|
||||||
assert.NoError(t, err)
|
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})
|
ret, err = code[1](nil, &VMContext{}, []byte{2})
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
sharray "github.com/whyrusleeping/sharray"
|
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/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
bls "github.com/filecoin-project/go-lotus/lib/bls-signatures"
|
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
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
hamt "github.com/ipfs/go-hamt-ipld"
|
hamt "github.com/ipfs/go-hamt-ipld"
|
||||||
cbor "github.com/ipfs/go-ipld-cbor"
|
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/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"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) {
|
func (st *StateTree) lookupID(addr address.Address) (address.Address, error) {
|
||||||
act, err := st.GetActor(InitActorAddress)
|
act, err := st.GetActor(actors.InitActorAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return address.Undef, err
|
return address.Undef, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var ias InitActorState
|
var ias actors.InitActorState
|
||||||
if err := st.store.Get(context.TODO(), act.Head, &ias); err != nil {
|
if err := st.store.Get(context.TODO(), act.Head, &ias); err != nil {
|
||||||
return address.Undef, err
|
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) {
|
func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) (address.Address, error) {
|
||||||
var out address.Address
|
var out address.Address
|
||||||
err := st.MutateActor(InitActorAddress, func(initact *types.Actor) error {
|
err := st.MutateActor(actors.InitActorAddress, func(initact *types.Actor) error {
|
||||||
var ias InitActorState
|
var ias actors.InitActorState
|
||||||
if err := st.store.Get(context.TODO(), initact.Head, &ias); err != nil {
|
if err := st.store.Get(context.TODO(), initact.Head, &ias); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
|
|
||||||
@ -569,7 +570,7 @@ func (syncer *Syncer) ValidateBlock(b *FullBlock) error {
|
|||||||
return err
|
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
|
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) {
|
func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
|
||||||
var acstate AccountActorState
|
var acstate actors.AccountActorState
|
||||||
acstate.Address = addr
|
acstate.Address = addr
|
||||||
|
|
||||||
c, err := st.store.Put(context.TODO(), acstate)
|
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{
|
nact := &types.Actor{
|
||||||
Code: AccountActorCodeCid,
|
Code: actors.AccountActorCodeCid,
|
||||||
Balance: types.NewInt(0),
|
Balance: types.NewInt(0),
|
||||||
Head: c,
|
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) {
|
func NewSecp256k1AccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
|
||||||
nact := &types.Actor{
|
nact := &types.Actor{
|
||||||
Code: AccountActorCodeCid,
|
Code: actors.AccountActorCodeCid,
|
||||||
Balance: types.NewInt(0),
|
Balance: types.NewInt(0),
|
||||||
Head: EmptyObjectCid,
|
Head: EmptyObjectCid,
|
||||||
}
|
}
|
||||||
|
6
chain/types/invokeret.go
Normal file
6
chain/types/invokeret.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
type InvokeRet struct {
|
||||||
|
Result []byte
|
||||||
|
ReturnCode byte
|
||||||
|
}
|
22
chain/vm.go
22
chain/vm.go
@ -1,11 +1,10 @@
|
|||||||
package chain
|
package chain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/types"
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
"github.com/filecoin-project/go-lotus/lib/bufbstore"
|
"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) {
|
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")
|
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 {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
return ret.result, ret.returnCode, nil
|
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())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user