Make PowerLookup work and test it

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-17 18:00:59 +02:00
parent 81f03a9f68
commit 9597ed8498
5 changed files with 41 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"
)
const SectorSize = 1024
@ -14,6 +15,7 @@ func init() {
cbor.RegisterCborType(StorageMarketState{})
cbor.RegisterCborType(CreateStorageMinerParams{})
cbor.RegisterCborType(IsMinerParam{})
cbor.RegisterCborType(PowerLookupParams{})
}
type StorageMarketActor struct{}
@ -146,7 +148,7 @@ type PowerLookupParams struct {
func (sma StorageMarketActor) PowerLookup(act *types.Actor, vmctx types.VMContext, params *PowerLookupParams) (types.InvokeRet, error) {
var self StorageMarketState
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
return types.InvokeRet{}, err
return types.InvokeRet{}, xerrors.Errorf("getting head: %w", err)
}
if _, ok := self.Miners[params.Miner]; !ok {
@ -156,9 +158,9 @@ func (sma StorageMarketActor) PowerLookup(act *types.Actor, vmctx types.VMContex
}, nil
}
ret, code, err := vmctx.Send(params.Miner, 9, types.NewInt(0), nil)
ret, code, err := vmctx.Send(params.Miner, 9, types.NewInt(0), EmptyStructCBOR)
if err != nil {
return types.InvokeRet{}, err
return types.InvokeRet{}, xerrors.Errorf("invoke Miner.GetPower: %w", err)
}
if code != 0 {

View File

@ -9,6 +9,11 @@ import (
cbor "github.com/ipfs/go-ipld-cbor"
)
func TestDumpEmpyStruct(t *testing.T) {
res, err := SerializeParams(struct{}{})
t.Logf("res: %x, err: %+v", res, err)
}
func TestStorageMarketCreateMiner(t *testing.T) {
h := NewHarness(t)
var sminer address.Address
@ -43,6 +48,7 @@ func TestStorageMarketCreateMiner(t *testing.T) {
t.Fatal("hold up")
}
h.Steps[1].M.Params = h.DumpObject(&IsMinerParam{Addr: sminer})
h.Steps[2].M.Params = h.DumpObject(&PowerLookupParams{Miner: sminer})
},
},
{
@ -71,6 +77,28 @@ func TestStorageMarketCreateMiner(t *testing.T) {
}
},
},
{
M: types.Message{
To: StorageMarketAddress,
From: h.From,
Method: 5,
GasPrice: types.NewInt(1),
GasLimit: types.NewInt(1),
Value: types.NewInt(0),
Nonce: 2,
// Params is sent in previous set
},
Ret: func(t *testing.T, ret *types.MessageReceipt) {
if ret.ExitCode != 0 {
t.Fatal("invokation failed: ", ret.ExitCode)
}
power := types.BigFromBytes(ret.Return)
if types.BigCmp(power, types.NewInt(0)) != 0 {
t.Fatalf("power should be zero, is: %s", power)
}
},
},
}
state := h.Execute()
act, err := state.GetActor(sminer)

View File

@ -4,6 +4,10 @@ import (
cbor "github.com/ipfs/go-ipld-cbor"
)
var (
EmptyStructCBOR = []byte{0xa0}
)
func SerializeParams(i interface{}) ([]byte, error) {
return cbor.DumpObject(i)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"golang.org/x/xerrors"
)
type invoker struct {
@ -34,10 +35,10 @@ func (inv *invoker) Invoke(act *types.Actor, vmctx *VMContext, method uint64, pa
code, ok := inv.builtInCode[act.Code]
if !ok {
return types.InvokeRet{}, fmt.Errorf("no code for actor %s", act.Code)
return types.InvokeRet{}, xerrors.Errorf("no code for actor %s", act.Code)
}
if method >= uint64(len(code)) || code[method] == nil {
return types.InvokeRet{}, fmt.Errorf("no method %d on actor", method)
return types.InvokeRet{}, xerrors.Errorf("no method %d on actor", method)
}
return code[method](act, vmctx, params)

View File

@ -215,7 +215,7 @@ func (vm *VM) ApplyMessage(msg *types.Message) (*types.MessageReceipt, error) {
if msg.Method != 0 {
ret, errcode, err = vm.Invoke(toActor, vmctx, msg.Method, msg.Params)
if err != nil {
return nil, err
return nil, xerrors.Errorf("during invoke: %w", err)
}
if errcode != 0 {