OMG this is killing me
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
218e965869
commit
9da31fb6ad
@ -13,13 +13,22 @@ 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"
|
||||||
logging "github.com/ipfs/go-log"
|
logging "github.com/ipfs/go-log"
|
||||||
|
mh "github.com/multiformats/go-multihash"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.Logger("actors")
|
var log = logging.Logger("actors")
|
||||||
|
|
||||||
|
var EmptyCBOR cid.Cid
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cbor.RegisterCborType(ExecParams{})
|
cbor.RegisterCborType(ExecParams{})
|
||||||
|
cbor.RegisterCborType(struct{}{})
|
||||||
|
n, err := cbor.WrapObject(struct{}{}, mh.SHA2_256, -1)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
EmptyCBOR = n.Cid()
|
||||||
}
|
}
|
||||||
|
|
||||||
type InitActor struct{}
|
type InitActor struct{}
|
||||||
@ -102,9 +111,13 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
|
|||||||
actor := types.Actor{
|
actor := types.Actor{
|
||||||
Code: p.Code,
|
Code: p.Code,
|
||||||
Balance: vmctx.Message().Value,
|
Balance: vmctx.Message().Value,
|
||||||
Head: cid.Undef,
|
Head: EmptyCBOR,
|
||||||
Nonce: 0,
|
Nonce: 0,
|
||||||
}
|
}
|
||||||
|
_, err = vmctx.Storage().Put(struct{}{})
|
||||||
|
if err != nil {
|
||||||
|
return types.InvokeRet{}, err
|
||||||
|
}
|
||||||
|
|
||||||
// The call to the actors constructor will set up the initial state
|
// The call to the actors constructor will set up the initial state
|
||||||
// from the given parameters, setting `actor.Head` to a new value when successful.
|
// from the given parameters, setting `actor.Head` to a new value when successful.
|
||||||
@ -128,6 +141,11 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
|
|||||||
return types.InvokeRet{}, errors.Wrap(err, "inserting new actor into state tree")
|
return types.InvokeRet{}, errors.Wrap(err, "inserting new actor into state tree")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, _, err = vmctx.Send(idAddr, 0, vmctx.Message().Value, p.Params)
|
||||||
|
if err != nil {
|
||||||
|
return types.InvokeRet{}, err
|
||||||
|
}
|
||||||
|
|
||||||
c, err := vmctx.Storage().Put(self)
|
c, err := vmctx.Storage().Put(self)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"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/ipfs/go-cid"
|
|
||||||
cbor "github.com/ipfs/go-ipld-cbor"
|
cbor "github.com/ipfs/go-ipld-cbor"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
)
|
)
|
||||||
@ -56,7 +55,8 @@ type StorageMinerActorState struct {
|
|||||||
//NextDoneSet BitField
|
//NextDoneSet BitField
|
||||||
|
|
||||||
// Deals this miner has been slashed for since the last post submission.
|
// Deals this miner has been slashed for since the last post submission.
|
||||||
ArbitratedDeals map[cid.Cid]struct{}
|
//TODO: unsupported map key type "Cid" (if you want to use struct keys, your atlas needs a transform to string)
|
||||||
|
//ArbitratedDeals map[cid.Cid]struct{}
|
||||||
|
|
||||||
// Amount of power this miner has.
|
// Amount of power this miner has.
|
||||||
Power types.BigInt
|
Power types.BigInt
|
||||||
@ -77,6 +77,12 @@ type StorageMinerConstructorParams struct {
|
|||||||
PeerID peer.ID
|
PeerID peer.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sma StorageMinerActor) Exports() []interface{} {
|
||||||
|
return []interface{}{
|
||||||
|
sma.StorageMinerActor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) (types.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
|
||||||
@ -90,7 +96,7 @@ func (sma StorageMinerActor) StorageMinerActor(act *types.Actor, vmctx types.VMC
|
|||||||
return types.InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := storage.Commit(cid.Undef, c); err != nil {
|
if err := storage.Commit(EmptyCBOR, c); err != nil {
|
||||||
return types.InvokeRet{}, err
|
return types.InvokeRet{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,6 @@ const SectorSize = 1024
|
|||||||
func init() {
|
func init() {
|
||||||
cbor.RegisterCborType(StorageMarketState{})
|
cbor.RegisterCborType(StorageMarketState{})
|
||||||
cbor.RegisterCborType(CreateStorageMinerParams{})
|
cbor.RegisterCborType(CreateStorageMinerParams{})
|
||||||
cbor.RegisterCborType(struct{}{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type StorageMarketActor struct{}
|
type StorageMarketActor struct{}
|
||||||
|
@ -58,9 +58,14 @@ func TestVMInvokeMethod(t *testing.T) {
|
|||||||
vm, addrs := setupVMTestEnv(t)
|
vm, addrs := setupVMTestEnv(t)
|
||||||
from := addrs[0]
|
from := addrs[0]
|
||||||
|
|
||||||
|
cenc, err := cbor.DumpObject(StorageMinerConstructorParams{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
execparams := &ExecParams{
|
execparams := &ExecParams{
|
||||||
Code: StorageMinerCodeCid,
|
Code: StorageMinerCodeCid,
|
||||||
Params: []byte("cats"),
|
Params: cenc,
|
||||||
}
|
}
|
||||||
enc, err := cbor.DumpObject(execparams)
|
enc, err := cbor.DumpObject(execparams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1 +0,0 @@
|
|||||||
package actors
|
|
@ -71,12 +71,12 @@ func (h *Harness) Execute() *chain.StateTree {
|
|||||||
for i, step := range h.Steps {
|
for i, step := range h.Steps {
|
||||||
h.currStep = i
|
h.currStep = i
|
||||||
ret, err := h.vm.ApplyMessage(&step.M)
|
ret, err := h.vm.ApplyMessage(&step.M)
|
||||||
step.Ret(h.t, ret)
|
|
||||||
step.Err(h.t, err)
|
step.Err(h.t, err)
|
||||||
|
step.Ret(h.t, ret)
|
||||||
}
|
}
|
||||||
stateroot, err := h.vm.Flush(context.TODO())
|
stateroot, err := h.vm.Flush(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.t.Fatal(err)
|
h.t.Fatalf("%+v", err)
|
||||||
}
|
}
|
||||||
cst := hamt.CSTFromBstore(h.bs)
|
cst := hamt.CSTFromBstore(h.bs)
|
||||||
state, err := chain.LoadStateTree(cst, stateroot)
|
state, err := chain.LoadStateTree(cst, stateroot)
|
||||||
@ -95,7 +95,7 @@ func (h *Harness) DumpObject(obj interface{}) []byte {
|
|||||||
}
|
}
|
||||||
func (h *Harness) NoError(t *testing.T, err error) {
|
func (h *Harness) NoError(t *testing.T, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no errors from applying message in step %d. Got %s", h.currStep, err)
|
t.Fatalf("Error in step %d: %s", h.currStep, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,13 +111,16 @@ func TestVMInvokeHarness(t *testing.T) {
|
|||||||
Params: h.DumpObject(
|
Params: h.DumpObject(
|
||||||
&ExecParams{
|
&ExecParams{
|
||||||
Code: StorageMinerCodeCid,
|
Code: StorageMinerCodeCid,
|
||||||
Params: []byte("cats"),
|
Params: h.DumpObject(&StorageMinerConstructorParams{}),
|
||||||
}),
|
}),
|
||||||
GasPrice: types.NewInt(1),
|
GasPrice: types.NewInt(1),
|
||||||
GasLimit: types.NewInt(1),
|
GasLimit: types.NewInt(1),
|
||||||
Value: types.NewInt(0),
|
Value: types.NewInt(0),
|
||||||
},
|
},
|
||||||
Ret: func(t *testing.T, ret *types.MessageReceipt) {
|
Ret: func(t *testing.T, ret *types.MessageReceipt) {
|
||||||
|
if ret == nil {
|
||||||
|
t.Fatal("ret is nil")
|
||||||
|
}
|
||||||
if ret.ExitCode != 0 {
|
if ret.ExitCode != 0 {
|
||||||
t.Fatal("invocation failed: ", ret.ExitCode)
|
t.Fatal("invocation failed: ", ret.ExitCode)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package chain
|
package chain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
@ -26,6 +25,7 @@ func newInvoker() *invoker {
|
|||||||
// add builtInCode using: register(cid, singleton)
|
// add builtInCode using: register(cid, singleton)
|
||||||
inv.register(actors.InitActorCodeCid, actors.InitActor{})
|
inv.register(actors.InitActorCodeCid, actors.InitActor{})
|
||||||
inv.register(actors.StorageMarketActorCodeCid, actors.StorageMarketActor{})
|
inv.register(actors.StorageMarketActorCodeCid, actors.StorageMarketActor{})
|
||||||
|
inv.register(actors.StorageMinerCodeCid, actors.StorageMinerActor{})
|
||||||
|
|
||||||
return inv
|
return inv
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ func (inv *invoker) Invoke(act *types.Actor, vmctx *VMContext, method uint64, pa
|
|||||||
|
|
||||||
code, ok := inv.builtInCode[act.Code]
|
code, ok := inv.builtInCode[act.Code]
|
||||||
if !ok {
|
if !ok {
|
||||||
return types.InvokeRet{}, errors.New("no code for actor")
|
return types.InvokeRet{}, fmt.Errorf("no code for actor %s", act.Code)
|
||||||
}
|
}
|
||||||
if method >= uint64(len(code)) || code[method] == nil {
|
if method >= uint64(len(code)) || code[method] == nil {
|
||||||
return types.InvokeRet{}, fmt.Errorf("no method %d on actor", method)
|
return types.InvokeRet{}, fmt.Errorf("no method %d on actor", method)
|
||||||
|
12
chain/vm.go
12
chain/vm.go
@ -8,6 +8,7 @@ import (
|
|||||||
"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"
|
||||||
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
bserv "github.com/ipfs/go-blockservice"
|
bserv "github.com/ipfs/go-blockservice"
|
||||||
cid "github.com/ipfs/go-cid"
|
cid "github.com/ipfs/go-cid"
|
||||||
@ -88,7 +89,12 @@ func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt
|
|||||||
|
|
||||||
nvmctx := vmc.vm.makeVMContext(toAct.Head, msg)
|
nvmctx := vmc.vm.makeVMContext(toAct.Head, msg)
|
||||||
|
|
||||||
return vmc.vm.Invoke(toAct, nvmctx, method, params)
|
res, ret, err := vmc.vm.Invoke(toAct, nvmctx, method, params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
// We need probably copy here the content from sub-vmcontext to this vm-context
|
||||||
|
// I think, @why??
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlockHeight returns the height of the block this message was added to the chain in
|
// BlockHeight returns the height of the block this message was added to the chain in
|
||||||
@ -237,11 +243,11 @@ func (vm *VM) Flush(ctx context.Context) (cid.Cid, error) {
|
|||||||
|
|
||||||
root, err := vm.cstate.Flush()
|
root, err := vm.cstate.Flush()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, err
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ipld.Copy(ctx, from, to, root); err != nil {
|
if err := ipld.Copy(ctx, from, to, root); err != nil {
|
||||||
return cid.Undef, err
|
return cid.Undef, xerrors.Errorf("copying tree: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return root, nil
|
return root, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user