2019-07-12 23:52:25 +00:00
|
|
|
package actors
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-10 02:05:24 +00:00
|
|
|
"context"
|
|
|
|
|
2019-08-27 19:54:14 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/build"
|
2019-07-23 13:01:52 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
2019-07-12 20:56:41 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
2019-07-12 20:56:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-09-10 19:58:45 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-12 20:56:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StorageMarketActor struct{}
|
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
type smaMethods struct {
|
|
|
|
Constructor uint64
|
|
|
|
CreateStorageMiner uint64
|
|
|
|
SlashConsensusFault uint64
|
|
|
|
UpdateStorage uint64
|
|
|
|
GetTotalStorage uint64
|
|
|
|
PowerLookup uint64
|
|
|
|
IsMiner uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var SMAMethods = smaMethods{1, 2, 3, 4, 5, 6, 7}
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
func (sma StorageMarketActor) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
2019-07-26 21:42:38 +00:00
|
|
|
//1: sma.StorageMarketConstructor,
|
|
|
|
2: sma.CreateStorageMiner,
|
|
|
|
//3: sma.SlashConsensusFault,
|
|
|
|
4: sma.UpdateStorage,
|
|
|
|
5: sma.GetTotalStorage,
|
|
|
|
6: sma.PowerLookup,
|
|
|
|
7: sma.IsMiner,
|
|
|
|
//8: sma.StorageCollateralForSize,
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageMarketState struct {
|
2019-09-10 02:05:24 +00:00
|
|
|
Miners cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
TotalStorage types.BigInt
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateStorageMinerParams struct {
|
2019-07-16 16:40:25 +00:00
|
|
|
Owner address.Address
|
2019-07-12 20:56:41 +00:00
|
|
|
Worker address.Address
|
|
|
|
SectorSize types.BigInt
|
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) ([]byte, ActorError) {
|
2019-07-12 20:56:41 +00:00
|
|
|
if !SupportedSectorSize(params.SectorSize) {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(1, "Unsupported sector size")
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:12:06 +00:00
|
|
|
encoded, err := CreateExecParams(StorageMinerCodeCid, &StorageMinerConstructorParams{
|
2019-07-16 16:40:25 +00:00
|
|
|
Owner: params.Owner,
|
2019-07-12 23:12:06 +00:00
|
|
|
Worker: params.Worker,
|
|
|
|
SectorSize: params.SectorSize,
|
|
|
|
PeerID: params.PeerID,
|
|
|
|
})
|
2019-07-12 20:56:41 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
ret, err := vmctx.Send(InitActorAddress, IAMethods.Exec, vmctx.Message().Value, encoded)
|
2019-07-12 20:56:41 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:08:54 +00:00
|
|
|
naddr, nerr := address.NewFromBytes(ret)
|
|
|
|
if nerr != nil {
|
2019-07-23 18:19:15 +00:00
|
|
|
return nil, aerrors.Absorb(nerr, 1, "could not read address of new actor")
|
2019-07-17 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
var self StorageMarketState
|
|
|
|
old := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
ncid, err := MinerSetAdd(context.TODO(), vmctx, self.Miners, naddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
self.Miners = ncid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
nroot, err := vmctx.Storage().Put(&self)
|
2019-07-12 20:56:41 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
return naddr.Bytes(), nil
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SupportedSectorSize(ssize types.BigInt) bool {
|
2019-08-27 19:54:14 +00:00
|
|
|
if ssize.Uint64() == build.SectorSize {
|
2019-07-12 20:56:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-07-16 06:07:03 +00:00
|
|
|
|
|
|
|
type UpdateStorageParams struct {
|
|
|
|
Delta types.BigInt
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMarketActor) UpdateStorage(act *types.Actor, vmctx types.VMContext, params *UpdateStorageParams) ([]byte, ActorError) {
|
2019-07-16 06:07:03 +00:00
|
|
|
var self StorageMarketState
|
2019-08-16 20:24:37 +00:00
|
|
|
old := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
has, err := MinerSetHas(context.TODO(), vmctx, self.Miners, vmctx.Message().From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(1, "update storage must only be called by a miner actor")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.TotalStorage = types.BigAdd(self.TotalStorage, params.Delta)
|
2019-08-16 20:24:37 +00:00
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
nroot, err := vmctx.Storage().Put(&self)
|
2019-08-16 20:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMarketActor) GetTotalStorage(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
2019-07-16 06:07:03 +00:00
|
|
|
var self StorageMarketState
|
|
|
|
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
return self.TotalStorage.Bytes(), nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PowerLookupParams struct {
|
|
|
|
Miner address.Address
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMarketActor) PowerLookup(act *types.Actor, vmctx types.VMContext, params *PowerLookupParams) ([]byte, ActorError) {
|
2019-07-16 06:07:03 +00:00
|
|
|
var self StorageMarketState
|
|
|
|
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.Wrap(err, "getting head")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
has, err := MinerSetHas(context.TODO(), vmctx, self.Miners, params.Miner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(1, "miner not registered with storage market")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
ret, err := vmctx.Send(params.Miner, MAMethods.GetPower, types.NewInt(0), nil)
|
2019-07-16 06:07:03 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.Wrap(err, "invoke Miner.GetPower")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
return ret, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
2019-07-17 14:30:55 +00:00
|
|
|
|
|
|
|
type IsMinerParam struct {
|
|
|
|
Addr address.Address
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMarketActor) IsMiner(act *types.Actor, vmctx types.VMContext, param *IsMinerParam) ([]byte, ActorError) {
|
2019-07-17 14:30:55 +00:00
|
|
|
var self StorageMarketState
|
|
|
|
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-17 14:30:55 +00:00
|
|
|
}
|
2019-09-10 02:05:24 +00:00
|
|
|
|
|
|
|
has, err := MinerSetHas(context.TODO(), vmctx, self.Miners, param.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
return cbg.EncodeBool(has), nil
|
2019-07-17 14:30:55 +00:00
|
|
|
}
|
2019-09-10 02:05:24 +00:00
|
|
|
|
|
|
|
func MinerSetHas(ctx context.Context, vmctx types.VMContext, rcid cid.Cid, maddr address.Address) (bool, aerrors.ActorError) {
|
2019-09-11 00:13:46 +00:00
|
|
|
nd, err := hamt.LoadNode(ctx, vmctx.Ipld(), rcid)
|
2019-09-10 02:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, aerrors.Escalate(err, "failed to load miner set")
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:13:46 +00:00
|
|
|
err = nd.Find(ctx, string(maddr.Bytes()), nil)
|
2019-09-10 02:05:24 +00:00
|
|
|
switch err {
|
|
|
|
case hamt.ErrNotFound:
|
|
|
|
return false, nil
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, aerrors.Escalate(err, "failed to do set lookup")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MinerSetAdd(ctx context.Context, vmctx types.VMContext, rcid cid.Cid, maddr address.Address) (cid.Cid, aerrors.ActorError) {
|
|
|
|
nd, err := hamt.LoadNode(ctx, vmctx.Ipld(), rcid)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to load miner set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Set(ctx, string(maddr.Bytes()), uint64(1)); err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "adding miner address to set failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Flush(ctx); err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to flush miner set")
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := vmctx.Ipld().Put(ctx, nd)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to persist miner set to storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|