lotus/chain/actors/actor_storagemarket.go

100 lines
2.1 KiB
Go
Raw Normal View History

2019-07-12 23:52:25 +00:00
package actors
import (
"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"
)
2019-07-15 17:48:59 +00:00
const SectorSize = 1024
func init() {
cbor.RegisterCborType(StorageMarketState{})
2019-07-12 21:36:49 +00:00
cbor.RegisterCborType(CreateStorageMinerParams{})
cbor.RegisterCborType(struct{}{})
}
type StorageMarketActor struct{}
func (sma StorageMarketActor) Exports() []interface{} {
return []interface{}{
nil,
sma.CreateStorageMiner,
}
}
type StorageMarketState struct {
Miners map[address.Address]struct{}
TotalStorage types.BigInt
}
type CreateStorageMinerParams struct {
Worker address.Address
SectorSize types.BigInt
PeerID peer.ID
}
2019-07-12 23:52:25 +00:00
func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) (types.InvokeRet, error) {
if !SupportedSectorSize(params.SectorSize) {
//Fatal("Unsupported sector size")
2019-07-12 23:52:25 +00:00
return types.InvokeRet{
ReturnCode: 1,
}, nil
}
2019-07-12 23:12:06 +00:00
encoded, err := CreateExecParams(StorageMinerCodeCid, &StorageMinerConstructorParams{
Worker: params.Worker,
SectorSize: params.SectorSize,
PeerID: params.PeerID,
})
if err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
ret, exit, err := vmctx.Send(InitActorAddress, 1, vmctx.Message().Value, encoded)
if err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
naddr, err := address.NewFromBytes(ret)
if err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
if exit != 0 {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{
ReturnCode: 2,
}, nil
}
var self StorageMarketState
old := vmctx.Storage().GetHead()
if err := vmctx.Storage().Get(old, &self); err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
self.Miners[naddr] = struct{}{}
nroot, err := vmctx.Storage().Put(self)
if err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
if err := vmctx.Storage().Commit(old, nroot); err != nil {
2019-07-12 23:52:25 +00:00
return types.InvokeRet{}, err
}
2019-07-12 23:52:25 +00:00
return types.InvokeRet{
Result: naddr.Bytes(),
}, nil
}
func SupportedSectorSize(ssize types.BigInt) bool {
2019-07-15 17:48:59 +00:00
if ssize.Uint64() == SectorSize {
return true
}
return false
}