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-09-12 04:12:35 +00:00
|
|
|
"fmt"
|
2019-09-10 02:05:24 +00:00
|
|
|
|
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-09-12 23:48:03 +00:00
|
|
|
xerrors "golang.org/x/xerrors"
|
2019-07-12 20:56:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StorageMarketActor struct{}
|
|
|
|
|
2019-07-26 21:42:38 +00:00
|
|
|
type smaMethods struct {
|
2019-09-12 04:12:35 +00:00
|
|
|
Constructor uint64
|
|
|
|
CreateStorageMiner uint64
|
2019-09-13 01:06:06 +00:00
|
|
|
ArbitrateConsensusFault uint64
|
2019-09-12 04:12:35 +00:00
|
|
|
UpdateStorage uint64
|
|
|
|
GetTotalStorage uint64
|
|
|
|
PowerLookup uint64
|
|
|
|
IsMiner uint64
|
|
|
|
PledgeCollateralForSize uint64
|
2019-07-26 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
var SMAMethods = smaMethods{1, 2, 3, 4, 5, 6, 7, 8}
|
2019-07-26 21:42:38 +00:00
|
|
|
|
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,
|
2019-09-13 01:06:06 +00:00
|
|
|
3: sma.ArbitrateConsensusFault,
|
2019-07-26 21:42:38 +00:00
|
|
|
4: sma.UpdateStorage,
|
|
|
|
5: sma.GetTotalStorage,
|
|
|
|
6: sma.PowerLookup,
|
|
|
|
7: sma.IsMiner,
|
2019-09-12 04:12:35 +00:00
|
|
|
8: sma.PledgeCollateralForSize,
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageMarketState struct {
|
2019-09-12 04:12:35 +00:00
|
|
|
Miners cid.Cid
|
|
|
|
MinerCount uint64
|
|
|
|
|
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-09-12 23:48:03 +00:00
|
|
|
var self StorageMarketState
|
|
|
|
old := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqColl, err := pledgeCollateralForSize(vmctx, types.NewInt(0), self.TotalStorage, self.MinerCount+1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:11:44 +00:00
|
|
|
if vmctx.Message().Value.LessThan(reqColl) {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.Newf(1, "not enough funds passed to cover required miner collateral (needed %s, got %s)", reqColl, vmctx.Message().Value)
|
|
|
|
}
|
|
|
|
|
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-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.Absorb(nerr, 2, "could not read address of new actor")
|
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-09-12 04:12:35 +00:00
|
|
|
self.MinerCount++
|
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
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
type ArbitrateConsensusFaultParams struct {
|
2019-09-12 04:12:35 +00:00
|
|
|
Block1 *types.BlockHeader
|
|
|
|
Block2 *types.BlockHeader
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
func (sma StorageMarketActor) ArbitrateConsensusFault(act *types.Actor, vmctx types.VMContext, params *ArbitrateConsensusFaultParams) ([]byte, ActorError) {
|
2019-09-12 04:12:35 +00:00
|
|
|
if params.Block1.Miner != params.Block2.Miner {
|
|
|
|
return nil, aerrors.New(2, "blocks must be from the same miner")
|
|
|
|
}
|
|
|
|
|
|
|
|
rval, err := vmctx.Send(params.Block1.Miner, MAMethods.GetWorkerAddr, types.NewInt(0), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "failed to get miner worker")
|
|
|
|
}
|
|
|
|
|
|
|
|
worker, oerr := address.NewFromBytes(rval)
|
|
|
|
if oerr != nil {
|
|
|
|
// REVIEW: should this be fatal? i can't think of a real situation that would get us here
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.Absorb(oerr, 3, "response from 'GetWorkerAddr' was not a valid address")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := params.Block1.CheckBlockSignature(worker); err != nil {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.Absorb(err, 4, "block1 did not have valid signature")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := params.Block2.CheckBlockSignature(worker); err != nil {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.Absorb(err, 5, "block2 did not have valid signature")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// see the "Consensus Faults" section of the faults spec (faults.md)
|
|
|
|
// for details on these slashing conditions.
|
|
|
|
if !shouldSlash(params.Block1, params.Block2) {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.New(6, "blocks do not prove a slashable offense")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var self StorageMarketState
|
|
|
|
old := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.BigCmp(self.TotalStorage, types.NewInt(0)) == 0 {
|
|
|
|
return nil, aerrors.Fatal("invalid state, storage market actor has zero total storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
miner := params.Block1.Miner
|
|
|
|
if has, err := MinerSetHas(context.TODO(), vmctx, self.Miners, miner); err != nil {
|
|
|
|
return nil, aerrors.Wrapf(err, "failed to check miner in set")
|
|
|
|
} else if !has {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, aerrors.New(7, "either already slashed or not a miner")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
minerPower, err := powerLookup(context.TODO(), vmctx, &self, miner)
|
2019-09-12 04:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
slashedCollateral, err := pledgeCollateralForSize(vmctx, minerPower, self.TotalStorage, self.MinerCount)
|
2019-09-12 04:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
enc, err := SerializeParams(&MinerSlashConsensusFault{
|
|
|
|
Slasher: vmctx.Message().From,
|
|
|
|
AtHeight: params.Block1.Height,
|
|
|
|
SlashedCollateral: slashedCollateral,
|
|
|
|
})
|
2019-09-12 04:12:35 +00:00
|
|
|
if err != nil {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, err
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
_, err = vmctx.Send(miner, MAMethods.SlashConsensusFault, types.NewInt(0), enc)
|
2019-09-12 04:12:35 +00:00
|
|
|
if err != nil {
|
2019-09-12 23:48:03 +00:00
|
|
|
return nil, err
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the miner from the list of network miners
|
|
|
|
ncid, err := MinerSetRemove(context.TODO(), vmctx, self.Miners, miner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
self.Miners = ncid
|
|
|
|
self.MinerCount--
|
|
|
|
|
|
|
|
self.TotalStorage = types.BigSub(self.TotalStorage, minerPower)
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
nroot, err := vmctx.Storage().Put(&self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
func cidArrContains(a []cid.Cid, b cid.Cid) bool {
|
|
|
|
for _, c := range a {
|
|
|
|
if b == c {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldSlash(block1, block2 *types.BlockHeader) bool {
|
|
|
|
// First slashing condition, blocks have the same ticket round
|
|
|
|
if block1.Height == block2.Height {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second slashing condition requires having access to the parent tipset blocks
|
|
|
|
// This might not always be available, needs some thought on the best way to deal with this
|
|
|
|
|
|
|
|
|
|
|
|
// Second slashing condition, miner ignored own block when mining
|
|
|
|
// Case A: block2 could have been in block1's parent set but is not
|
|
|
|
b1ParentHeight := block1.Height - len(block1.Tickets)
|
|
|
|
|
|
|
|
block1ParentTipSet := block1.Parents
|
|
|
|
if !cidArrContains(block1.Parents, block2.Cid()) &&
|
|
|
|
b1ParentHeight == block2.Height &&
|
|
|
|
block1ParentTipSet.ParentCids == block2.ParentCids {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case B: block1 could have been in block2's parent set but is not
|
|
|
|
block2ParentTipSet := parentOf(block2)
|
|
|
|
if !block2Parent.contains(block1) &&
|
|
|
|
block2ParentTipSet.Height == block1.Height &&
|
|
|
|
block2ParentTipSet.ParentCids == block1.ParentCids {
|
|
|
|
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-12 04:12:35 +00:00
|
|
|
pow, err := powerLookup(context.TODO(), vmctx, &self, params.Miner)
|
2019-09-10 02:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
return pow.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func powerLookup(ctx context.Context, vmctx types.VMContext, self *StorageMarketState, miner address.Address) (types.BigInt, ActorError) {
|
|
|
|
has, err := MinerSetHas(context.TODO(), vmctx, self.Miners, miner)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
if !has {
|
2019-09-12 04:12:35 +00:00
|
|
|
return types.EmptyInt, aerrors.New(1, "miner not registered with storage market")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
ret, err := vmctx.Send(miner, MAMethods.GetPower, types.NewInt(0), nil)
|
2019-07-16 06:07:03 +00:00
|
|
|
if err != nil {
|
2019-09-12 04:12:35 +00:00
|
|
|
return types.EmptyInt, aerrors.Wrap(err, "invoke Miner.GetPower")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
return types.BigFromBytes(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
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
type PledgeCollateralParams struct {
|
|
|
|
Size types.BigInt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMarketActor) PledgeCollateralForSize(act *types.Actor, vmctx types.VMContext, param *PledgeCollateralParams) ([]byte, ActorError) {
|
|
|
|
var self StorageMarketState
|
|
|
|
if err := vmctx.Storage().Get(vmctx.Storage().GetHead(), &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
totalCollateral, err := pledgeCollateralForSize(vmctx, param.Size, self.TotalStorage, self.MinerCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-12 04:12:35 +00:00
|
|
|
|
|
|
|
return totalCollateral.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
func pledgeCollateralForSize(vmctx types.VMContext, size, totalStorage types.BigInt, minerCount uint64) (types.BigInt, aerrors.ActorError) {
|
|
|
|
netBalance, err := vmctx.GetBalance(NetworkAddress)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, err
|
|
|
|
}
|
2019-09-12 04:12:35 +00:00
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
// TODO: the spec says to also grab 'total vested filecoin' and include it as available
|
|
|
|
// If we don't factor that in, we effectively assume all of the locked up filecoin is 'available'
|
|
|
|
// the blocker on that right now is that its hard to tell how much filecoin is unlocked
|
|
|
|
|
|
|
|
availableFilecoin := types.BigSub(
|
|
|
|
types.BigMul(types.NewInt(build.TotalFilecoin), types.NewInt(build.FilecoinPrecision)),
|
|
|
|
netBalance,
|
|
|
|
)
|
|
|
|
|
|
|
|
totalPowerCollateral := types.BigDiv(
|
|
|
|
types.BigMul(
|
|
|
|
availableFilecoin,
|
|
|
|
types.NewInt(build.PowerCollateralProportion),
|
|
|
|
),
|
|
|
|
types.NewInt(build.CollateralPrecision),
|
|
|
|
)
|
|
|
|
|
|
|
|
totalPerCapitaCollateral := types.BigDiv(
|
|
|
|
types.BigMul(
|
|
|
|
availableFilecoin,
|
|
|
|
types.NewInt(build.PerCapitaCollateralProportion),
|
|
|
|
),
|
|
|
|
types.NewInt(build.CollateralPrecision),
|
|
|
|
)
|
|
|
|
|
2019-09-13 01:06:06 +00:00
|
|
|
// REVIEW: for bootstrapping purposes, we skip the power portion of the
|
|
|
|
// collateral if there is no collateral in the network yet
|
|
|
|
powerCollateral := types.NewInt(0)
|
|
|
|
if types.BigCmp(totalStorage, types.NewInt(0)) != 0 {
|
|
|
|
powerCollateral = types.BigDiv(
|
|
|
|
types.BigMul(
|
|
|
|
totalPowerCollateral,
|
|
|
|
size,
|
|
|
|
),
|
|
|
|
totalStorage,
|
|
|
|
)
|
|
|
|
}
|
2019-09-12 23:48:03 +00:00
|
|
|
|
|
|
|
perCapCollateral := types.BigDiv(
|
|
|
|
totalPerCapitaCollateral,
|
|
|
|
types.NewInt(minerCount),
|
|
|
|
)
|
|
|
|
|
|
|
|
return types.BigAdd(powerCollateral, perCapCollateral), nil
|
2019-09-12 04:12:35 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-12 06:45:48 +00:00
|
|
|
func MinerSetList(ctx context.Context, cst *hamt.CborIpldStore, rcid cid.Cid) ([]address.Address, error) {
|
|
|
|
nd, err := hamt.LoadNode(ctx, cst, rcid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load miner set: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []address.Address
|
|
|
|
err = nd.ForEach(ctx, func(k string, val interface{}) error {
|
|
|
|
addr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out = append(out, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
mkey := string(maddr.Bytes())
|
|
|
|
err = nd.Find(ctx, mkey, nil)
|
|
|
|
if err == nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(fmt.Errorf("miner already found"), "miner set add failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !xerrors.Is(err, hamt.ErrNotFound) {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to do miner set check")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Set(ctx, mkey, uint64(1)); err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
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
|
|
|
|
}
|
2019-09-12 04:12:35 +00:00
|
|
|
|
|
|
|
func MinerSetRemove(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")
|
|
|
|
}
|
|
|
|
|
|
|
|
mkey := string(maddr.Bytes())
|
|
|
|
switch nd.Delete(ctx, mkey) {
|
|
|
|
case hamt.ErrNotFound:
|
|
|
|
return cid.Undef, aerrors.New(1, "miner not found in set on delete")
|
|
|
|
default:
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to delete miner from set")
|
|
|
|
case nil:
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|