Complete the refactor
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
877ccb3803
commit
537ff23a63
@ -3,8 +3,10 @@ package actors
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
||||
"github.com/filecoin-project/go-lotus/chain/address"
|
||||
"github.com/filecoin-project/go-lotus/chain/types"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
cid "github.com/ipfs/go-cid"
|
||||
hamt "github.com/ipfs/go-hamt-ipld"
|
||||
@ -106,7 +108,7 @@ func (sma StorageMinerActor) Exports() []interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) (types.InvokeRet, error) {
|
||||
func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) ([]byte, ActorError) {
|
||||
var self StorageMinerActorState
|
||||
self.Owner = params.Owner
|
||||
self.Worker = params.Worker
|
||||
@ -114,9 +116,9 @@ func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx typ
|
||||
self.SectorSize = params.SectorSize
|
||||
|
||||
nd := hamt.NewNode(vmctx.Ipld())
|
||||
sectors, err := vmctx.Ipld().Put(context.TODO(), nd)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
sectors, nerr := vmctx.Ipld().Put(context.TODO(), nd)
|
||||
if nerr != nil {
|
||||
return nil, aerrors.Escalate(nerr, "could not put in storage")
|
||||
}
|
||||
self.Sectors = sectors
|
||||
self.ProvingSet = sectors
|
||||
@ -124,14 +126,14 @@ func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx typ
|
||||
storage := vmctx.Storage()
|
||||
c, err := storage.Put(self)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := storage.Commit(EmptyCBOR, c); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.InvokeRet{}, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type CommitSectorParams struct {
|
||||
@ -142,30 +144,24 @@ type CommitSectorParams struct {
|
||||
Proof []byte
|
||||
}
|
||||
|
||||
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *CommitSectorParams) (types.InvokeRet, error) {
|
||||
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *CommitSectorParams) ([]byte, ActorError) {
|
||||
var self StorageMinerActorState
|
||||
oldstate := vmctx.Storage().GetHead()
|
||||
if err := vmctx.Storage().Get(oldstate, &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ValidatePoRep(self.SectorSize, params) {
|
||||
//Fatal("bad proof!")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 1,
|
||||
}, nil
|
||||
return nil, aerrors.New(1, "bad proof!")
|
||||
}
|
||||
|
||||
// make sure the miner isnt trying to submit a pre-existing sector
|
||||
unique, err := SectorIsUnique(vmctx.Ipld(), self.Sectors, params.SectorId)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
if !unique {
|
||||
//Fatal("sector already committed!")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 2,
|
||||
}, nil
|
||||
return nil, aerrors.New(2, "sector already committed!")
|
||||
}
|
||||
|
||||
// Power of the miner after adding this sector
|
||||
@ -173,18 +169,12 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
|
||||
collateralRequired := CollateralForPower(futurePower)
|
||||
|
||||
if types.BigCmp(collateralRequired, act.Balance) < 0 {
|
||||
//Fatal("not enough collateral")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 3,
|
||||
}, nil
|
||||
return nil, aerrors.New(3, "not enough collateral")
|
||||
}
|
||||
|
||||
// ensure that the miner cannot commit more sectors than can be proved with a single PoSt
|
||||
if self.SectorSetSize >= POST_SECTORS_COUNT {
|
||||
// Fatal("too many sectors")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 4,
|
||||
}, nil
|
||||
return nil, aerrors.New(4, "too many sectors")
|
||||
}
|
||||
|
||||
// Note: There must exist a unique index in the miner's sector set for each
|
||||
@ -192,7 +182,7 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
|
||||
// SubmitPoSt method express indices into this sector set.
|
||||
nssroot, err := AddToSectorSet(self.Sectors, params.SectorId, params.CommR, params.CommD)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
self.Sectors = nssroot
|
||||
|
||||
@ -212,43 +202,41 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
|
||||
|
||||
nstate, err := vmctx.Storage().Put(self)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
if err := vmctx.Storage().Commit(oldstate, nstate); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.InvokeRet{}, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (sma StorageMinerActor) GetPower(act *types.Actor, vmctx types.VMContext, params *struct{}) (types.InvokeRet, error) {
|
||||
func (sma StorageMinerActor) GetPower(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
||||
var self StorageMinerActorState
|
||||
state := vmctx.Storage().GetHead()
|
||||
if err := vmctx.Storage().Get(state, &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
return types.InvokeRet{
|
||||
Result: self.Power.Bytes(),
|
||||
}, nil
|
||||
return self.Power.Bytes(), nil
|
||||
}
|
||||
|
||||
func SectorIsUnique(cst *hamt.CborIpldStore, sroot cid.Cid, sid types.BigInt) (bool, error) {
|
||||
func SectorIsUnique(cst *hamt.CborIpldStore, sroot cid.Cid, sid types.BigInt) (bool, ActorError) {
|
||||
nd, err := hamt.LoadNode(context.TODO(), cst, sroot)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, aerrors.Absorb(err, 1, "could not load node in HAMT")
|
||||
}
|
||||
|
||||
if _, err := nd.Find(context.TODO(), sid.String()); err != nil {
|
||||
if err == hamt.ErrNotFound {
|
||||
if xerrors.Is(err, hamt.ErrNotFound) {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
return false, aerrors.Absorb(err, 1, "could not find node in HAMT")
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func AddToSectorSet(ss cid.Cid, sectorID types.BigInt, commR, commD []byte) (cid.Cid, error) {
|
||||
func AddToSectorSet(ss cid.Cid, sectorID types.BigInt, commR, commD []byte) (cid.Cid, ActorError) {
|
||||
panic("NYI")
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
package actors
|
||||
|
||||
import (
|
||||
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
||||
"github.com/filecoin-project/go-lotus/chain/address"
|
||||
"github.com/filecoin-project/go-lotus/chain/types"
|
||||
|
||||
cbor "github.com/ipfs/go-ipld-cbor"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
const SectorSize = 1024
|
||||
@ -45,12 +45,9 @@ type CreateStorageMinerParams struct {
|
||||
PeerID peer.ID
|
||||
}
|
||||
|
||||
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) (types.InvokeRet, error) {
|
||||
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) ([]byte, ActorError) {
|
||||
if !SupportedSectorSize(params.SectorSize) {
|
||||
//Fatal("Unsupported sector size")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 1,
|
||||
}, nil
|
||||
return nil, aerrors.New(1, "Unsupported sector size")
|
||||
}
|
||||
|
||||
encoded, err := CreateExecParams(StorageMinerCodeCid, &StorageMinerConstructorParams{
|
||||
@ -60,39 +57,37 @@ func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.V
|
||||
PeerID: params.PeerID,
|
||||
})
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ret, err := vmctx.Send(InitActorAddress, 1, vmctx.Message().Value, encoded)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
naddr, nerr := address.NewFromBytes(ret)
|
||||
if nerr != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var self StorageMarketState
|
||||
old := vmctx.Storage().GetHead()
|
||||
if err := vmctx.Storage().Get(old, &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
self.Miners[naddr] = struct{}{}
|
||||
|
||||
nroot, err := vmctx.Storage().Put(self)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.InvokeRet{
|
||||
Result: naddr.Bytes(),
|
||||
}, nil
|
||||
return naddr.Bytes(), nil
|
||||
}
|
||||
|
||||
func SupportedSectorSize(ssize types.BigInt) bool {
|
||||
@ -106,77 +101,65 @@ type UpdateStorageParams struct {
|
||||
Delta types.BigInt
|
||||
}
|
||||
|
||||
func (sma StorageMarketActor) UpdateStorage(act *types.Actor, vmctx types.VMContext, params *UpdateStorageParams) (types.InvokeRet, error) {
|
||||
func (sma StorageMarketActor) UpdateStorage(act *types.Actor, vmctx types.VMContext, params *UpdateStorageParams) ([]byte, ActorError) {
|
||||
var self StorageMarketState
|
||||
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, ok := self.Miners[vmctx.Message().From]
|
||||
if !ok {
|
||||
//Fatal("update storage must only be called by a miner actor")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 1,
|
||||
}, nil
|
||||
return nil, aerrors.New(1, "update storage must only be called by a miner actor")
|
||||
}
|
||||
|
||||
self.TotalStorage = types.BigAdd(self.TotalStorage, params.Delta)
|
||||
return types.InvokeRet{}, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (sma StorageMarketActor) GetTotalStorage(act *types.Actor, vmctx types.VMContext, params *struct{}) (types.InvokeRet, error) {
|
||||
func (sma StorageMarketActor) GetTotalStorage(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
||||
var self StorageMarketState
|
||||
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return types.InvokeRet{
|
||||
Result: self.TotalStorage.Bytes(),
|
||||
}, nil
|
||||
return self.TotalStorage.Bytes(), nil
|
||||
}
|
||||
|
||||
type PowerLookupParams struct {
|
||||
Miner address.Address
|
||||
}
|
||||
|
||||
func (sma StorageMarketActor) PowerLookup(act *types.Actor, vmctx types.VMContext, params *PowerLookupParams) (types.InvokeRet, error) {
|
||||
func (sma StorageMarketActor) PowerLookup(act *types.Actor, vmctx types.VMContext, params *PowerLookupParams) ([]byte, ActorError) {
|
||||
var self StorageMarketState
|
||||
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
||||
return types.InvokeRet{}, xerrors.Errorf("getting head: %w", err)
|
||||
return nil, aerrors.Wrap(err, "getting head")
|
||||
}
|
||||
|
||||
if _, ok := self.Miners[params.Miner]; !ok {
|
||||
//Fatal("miner not registered with storage market")
|
||||
return types.InvokeRet{
|
||||
ReturnCode: 1,
|
||||
}, nil
|
||||
return nil, aerrors.New(1, "miner not registered with storage market")
|
||||
}
|
||||
|
||||
ret, err := vmctx.Send(params.Miner, 9, types.NewInt(0), EmptyStructCBOR)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, xerrors.Errorf("invoke Miner.GetPower: %w", err)
|
||||
return nil, aerrors.Wrap(err, "invoke Miner.GetPower")
|
||||
}
|
||||
|
||||
return types.InvokeRet{
|
||||
Result: ret,
|
||||
}, nil
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
type IsMinerParam struct {
|
||||
Addr address.Address
|
||||
}
|
||||
|
||||
func (sma StorageMarketActor) IsMiner(act *types.Actor, vmctx types.VMContext, param *IsMinerParam) (types.InvokeRet, error) {
|
||||
func (sma StorageMarketActor) IsMiner(act *types.Actor, vmctx types.VMContext, param *IsMinerParam) ([]byte, ActorError) {
|
||||
var self StorageMarketState
|
||||
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
_, ok := self.Miners[param.Addr]
|
||||
out, err := SerializeParams(ok)
|
||||
if err != nil {
|
||||
return types.InvokeRet{}, err
|
||||
return nil, err
|
||||
}
|
||||
return types.InvokeRet{
|
||||
Result: out,
|
||||
}, nil
|
||||
return out, nil
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -67,6 +67,7 @@ require (
|
||||
go4.org v0.0.0-20190313082347-94abd6928b1d // indirect
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
|
||||
google.golang.org/appengine v1.4.0 // indirect
|
||||
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
|
||||
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user