2019-07-12 23:52:25 +00:00
|
|
|
package actors
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-13 23:00:11 +00:00
|
|
|
"bytes"
|
2019-09-10 02:05:24 +00:00
|
|
|
"context"
|
2019-11-13 23:35:58 +00:00
|
|
|
"io"
|
2019-09-10 02:05:24 +00:00
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
"github.com/filecoin-project/go-amt-ipld"
|
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-11-14 23:23:07 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-09-12 23:48:03 +00:00
|
|
|
xerrors "golang.org/x/xerrors"
|
2019-11-14 16:14:52 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-11-14 16:14:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-01-30 23:48:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2019-07-12 20:56:41 +00:00
|
|
|
)
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
type StoragePowerActor struct{}
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
type spaMethods 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
|
2019-11-13 23:00:11 +00:00
|
|
|
IsValidMiner uint64
|
2019-09-12 04:12:35 +00:00
|
|
|
PledgeCollateralForSize uint64
|
2019-11-13 22:40:51 +00:00
|
|
|
CheckProofSubmissions uint64
|
2019-07-26 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 22:40:51 +00:00
|
|
|
var SPAMethods = spaMethods{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
2019-07-26 21:42:38 +00:00
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) Exports() []interface{} {
|
2019-07-12 20:56:41 +00:00
|
|
|
return []interface{}{
|
2019-10-11 22:59:36 +00:00
|
|
|
//1: spa.StoragePowerConstructor,
|
|
|
|
2: spa.CreateStorageMiner,
|
|
|
|
3: spa.ArbitrateConsensusFault,
|
|
|
|
4: spa.UpdateStorage,
|
|
|
|
5: spa.GetTotalStorage,
|
|
|
|
6: spa.PowerLookup,
|
2019-11-13 23:00:11 +00:00
|
|
|
7: spa.IsValidMiner,
|
2019-10-11 22:59:36 +00:00
|
|
|
8: spa.PledgeCollateralForSize,
|
2019-11-13 22:40:51 +00:00
|
|
|
9: spa.CheckProofSubmissions,
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
type StoragePowerState struct {
|
2019-11-13 21:16:17 +00:00
|
|
|
Miners cid.Cid
|
|
|
|
ProvingBuckets cid.Cid // amt[ProvingPeriodBucket]hamt[minerAddress]struct{}
|
|
|
|
MinerCount uint64
|
2019-11-14 23:30:31 +00:00
|
|
|
LastMinerCheck uint64
|
2019-09-12 04:12:35 +00:00
|
|
|
|
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
|
2019-10-16 07:07:16 +00:00
|
|
|
SectorSize uint64
|
2019-07-12 20:56:41 +00:00
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) ([]byte, ActorError) {
|
2019-10-16 07:07:16 +00:00
|
|
|
if !build.SupportedSectorSize(params.SectorSize) {
|
2020-01-07 21:51:33 +00:00
|
|
|
return nil, aerrors.Newf(1, "Unsupported sector size: %d", params.SectorSize)
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
var self StoragePowerState
|
2019-09-12 23:48:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:20:56 +00:00
|
|
|
// FORK
|
|
|
|
minerCid := StorageMinerCodeCid
|
2020-01-18 05:35:45 +00:00
|
|
|
if vmctx.BlockHeight() > build.ForkFrigidHeight {
|
2020-01-18 05:20:56 +00:00
|
|
|
minerCid = StorageMiner2CodeCid
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded, err := CreateExecParams(minerCid, &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-10-21 18:12:11 +00:00
|
|
|
ret, err := vmctx.Send(InitAddress, 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
|
|
|
}
|
|
|
|
|
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-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) ArbitrateConsensusFault(act *types.Actor, vmctx types.VMContext, params *ArbitrateConsensusFaultParams) ([]byte, ActorError) {
|
2020-01-14 12:22:41 +00:00
|
|
|
if params == nil || params.Block1 == nil || params.Block2 == nil {
|
|
|
|
return nil, aerrors.New(1, "failed to parse params")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:45:41 +00:00
|
|
|
// FORK
|
2020-01-16 18:41:25 +00:00
|
|
|
if vmctx.BlockHeight() > build.ForkBlizzardHeight {
|
2020-01-17 06:14:00 +00:00
|
|
|
if params.Block1.Height <= build.ForkBlizzardHeight {
|
|
|
|
return nil, aerrors.New(10, "cannot slash miners with blocks from before blizzard")
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Block2.Height <= build.ForkBlizzardHeight {
|
|
|
|
return nil, aerrors.New(11, "cannot slash miners with blocks from before blizzard")
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:41:25 +00:00
|
|
|
if params.Block1.Cid() == params.Block2.Cid() {
|
|
|
|
return nil, aerrors.New(3, "blocks must be different")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
if err := sigs.CheckBlockSignature(params.Block1, vmctx.Context(), 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
|
|
|
}
|
|
|
|
|
2020-01-30 23:48:25 +00:00
|
|
|
if err := sigs.CheckBlockSignature(params.Block2, vmctx.Context(), 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
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
var self StoragePowerState
|
2019-09-12 04:12:35 +00:00
|
|
|
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 {
|
2019-10-11 22:59:36 +00:00
|
|
|
return nil, aerrors.Fatal("invalid state, storage power actor has zero total storage")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
miner := params.Block1.Miner
|
2019-10-13 09:08:34 +00:00
|
|
|
if has, err := MinerSetHas(vmctx, self.Miners, miner); err != nil {
|
2019-09-12 04:12:35 +00:00
|
|
|
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 {
|
2019-12-19 15:50:18 +00:00
|
|
|
Delta types.BigInt
|
|
|
|
NextSlashDeadline uint64
|
|
|
|
PreviousSlashDeadline uint64
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) UpdateStorage(act *types.Actor, vmctx types.VMContext, params *UpdateStorageParams) ([]byte, ActorError) {
|
|
|
|
var self StoragePowerState
|
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-11-13 23:35:58 +00:00
|
|
|
has, err := MinerSetHas(vmctx, self.Miners, vmctx.Message().From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
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-12-19 15:50:18 +00:00
|
|
|
previousBucket := params.PreviousSlashDeadline % build.SlashablePowerDelay
|
|
|
|
nextBucket := params.NextSlashDeadline % build.SlashablePowerDelay
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-12-19 15:50:18 +00:00
|
|
|
if previousBucket == nextBucket && params.PreviousSlashDeadline != 0 {
|
2019-11-13 21:16:17 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil // Nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
buckets, eerr := amt.LoadAMT(types.WrapStorage(vmctx.Storage()), self.ProvingBuckets)
|
|
|
|
if eerr != nil {
|
|
|
|
return nil, aerrors.HandleExternalError(eerr, "loading proving buckets amt")
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:50:18 +00:00
|
|
|
if params.PreviousSlashDeadline != 0 { // delete from previous bucket
|
2019-11-14 23:23:07 +00:00
|
|
|
err := deleteMinerFromBucket(vmctx, buckets, previousBucket)
|
2019-11-13 21:16:17 +00:00
|
|
|
if err != nil {
|
2019-12-19 15:50:18 +00:00
|
|
|
return nil, aerrors.Wrapf(err, "delete from bucket %d, next %d", previousBucket, nextBucket)
|
2019-11-13 21:16:17 +00:00
|
|
|
}
|
2019-11-14 23:23:07 +00:00
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
err = addMinerToBucket(vmctx, buckets, nextBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
self.ProvingBuckets, eerr = buckets.Flush()
|
|
|
|
if eerr != nil {
|
|
|
|
return nil, aerrors.HandleExternalError(eerr, "flushing proving buckets")
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
nroot, err := vmctx.Storage().Put(&self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-13 21:16:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
func deleteMinerFromBucket(vmctx types.VMContext, buckets *amt.Root, previousBucket uint64) aerrors.ActorError {
|
|
|
|
var bucket cid.Cid
|
|
|
|
err := buckets.Get(previousBucket, &bucket)
|
|
|
|
switch err.(type) {
|
|
|
|
case *amt.ErrNotFound:
|
|
|
|
return aerrors.HandleExternalError(err, "proving bucket missing")
|
|
|
|
case nil: // noop
|
|
|
|
default:
|
|
|
|
return aerrors.HandleExternalError(err, "getting proving bucket")
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
bhamt, err := hamt.LoadNode(vmctx.Context(), vmctx.Ipld(), bucket)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "failed to load proving bucket")
|
|
|
|
}
|
|
|
|
err = bhamt.Delete(vmctx.Context(), string(vmctx.Message().From.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "deleting miner from proving bucket")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bhamt.Flush(vmctx.Context())
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "flushing previous proving bucket")
|
|
|
|
}
|
2019-11-13 21:16:17 +00:00
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
bucket, err = vmctx.Ipld().Put(vmctx.Context(), bhamt)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "putting previous proving bucket hamt")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = buckets.Set(previousBucket, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "setting previous proving bucket cid in amt")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMinerToBucket(vmctx types.VMContext, buckets *amt.Root, nextBucket uint64) aerrors.ActorError {
|
|
|
|
var bhamt *hamt.Node
|
|
|
|
var bucket cid.Cid
|
|
|
|
err := buckets.Get(nextBucket, &bucket)
|
|
|
|
switch err.(type) {
|
|
|
|
case *amt.ErrNotFound:
|
|
|
|
bhamt = hamt.NewNode(vmctx.Ipld())
|
|
|
|
case nil:
|
|
|
|
bhamt, err = hamt.LoadNode(vmctx.Context(), vmctx.Ipld(), bucket)
|
2019-11-13 21:16:17 +00:00
|
|
|
if err != nil {
|
2019-11-14 23:23:07 +00:00
|
|
|
return aerrors.HandleExternalError(err, "failed to load proving bucket")
|
2019-11-13 21:16:17 +00:00
|
|
|
}
|
2019-11-14 23:23:07 +00:00
|
|
|
default:
|
|
|
|
return aerrors.HandleExternalError(err, "getting proving bucket")
|
2019-11-13 21:16:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:46:30 +00:00
|
|
|
err = bhamt.Set(vmctx.Context(), string(vmctx.Message().From.Bytes()), CborNull)
|
2019-11-14 23:23:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "setting miner in proving bucket")
|
2019-11-13 21:16:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
err = bhamt.Flush(vmctx.Context())
|
2019-08-16 20:24:37 +00:00
|
|
|
if err != nil {
|
2019-11-14 23:23:07 +00:00
|
|
|
return aerrors.HandleExternalError(err, "flushing previous proving bucket")
|
2019-08-16 20:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
bucket, err = vmctx.Ipld().Put(vmctx.Context(), bhamt)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "putting previous proving bucket hamt")
|
2019-08-16 20:24:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
err = buckets.Set(nextBucket, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "setting previous proving bucket cid in amt")
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) GetTotalStorage(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
|
|
|
var self StoragePowerState
|
2019-07-16 06:07:03 +00:00
|
|
|
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-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) PowerLookup(act *types.Actor, vmctx types.VMContext, params *PowerLookupParams) ([]byte, ActorError) {
|
|
|
|
var self StoragePowerState
|
2019-07-16 06:07:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func powerLookup(ctx context.Context, vmctx types.VMContext, self *StoragePowerState, miner address.Address) (types.BigInt, ActorError) {
|
2019-10-13 09:08:34 +00:00
|
|
|
has, err := MinerSetHas(vmctx, self.Miners, miner)
|
2019-09-12 04:12:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, err
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:05:24 +00:00
|
|
|
if !has {
|
2020-02-06 06:25:30 +00:00
|
|
|
// A miner could be registered with storage power actor, but removed for some reasons, e.g. consensus fault
|
|
|
|
return types.EmptyInt, aerrors.New(1, "miner not registered with storage power actor, or removed already")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 15:55:55 +00:00
|
|
|
// TODO: Use local amt
|
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
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
type IsValidMinerParam struct {
|
2019-07-17 14:30:55 +00:00
|
|
|
Addr address.Address
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
func (spa StoragePowerActor) IsValidMiner(act *types.Actor, vmctx types.VMContext, param *IsValidMinerParam) ([]byte, ActorError) {
|
2019-10-11 22:59:36 +00:00
|
|
|
var self StoragePowerState
|
2019-07-17 14:30:55 +00:00
|
|
|
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
|
|
|
|
2019-10-13 09:08:34 +00:00
|
|
|
has, err := MinerSetHas(vmctx, self.Miners, param.Addr)
|
2019-09-10 02:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-13 23:00:11 +00:00
|
|
|
if !has {
|
2019-11-14 16:14:52 +00:00
|
|
|
log.Warnf("Miner INVALID: not in set: %s", param.Addr)
|
|
|
|
|
2019-11-13 23:00:11 +00:00
|
|
|
return cbg.CborBoolFalse, nil
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
ret, err := vmctx.Send(param.Addr, MAMethods.IsSlashed, types.NewInt(0), nil)
|
2019-11-13 23:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
slashed := bytes.Equal(ret, cbg.CborBoolTrue)
|
|
|
|
|
|
|
|
if slashed {
|
|
|
|
log.Warnf("Miner INVALID: /SLASHED/ : %s", param.Addr)
|
|
|
|
}
|
2019-11-13 23:00:11 +00:00
|
|
|
|
2019-11-14 16:14:52 +00:00
|
|
|
return cbg.EncodeBool(!slashed), 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
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:59:36 +00:00
|
|
|
func (spa StoragePowerActor) PledgeCollateralForSize(act *types.Actor, vmctx types.VMContext, param *PledgeCollateralParams) ([]byte, ActorError) {
|
|
|
|
var self StoragePowerState
|
2019-09-12 04:12:35 +00:00
|
|
|
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-11-13 22:40:51 +00:00
|
|
|
func (spa StoragePowerActor) CheckProofSubmissions(act *types.Actor, vmctx types.VMContext, param *struct{}) ([]byte, ActorError) {
|
2019-11-22 22:51:44 +00:00
|
|
|
if vmctx.Message().From != CronAddress {
|
|
|
|
return nil, aerrors.New(1, "CheckProofSubmissions is only callable from the cron actor")
|
2019-11-13 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var self StoragePowerState
|
|
|
|
old := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(old, &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:30:31 +00:00
|
|
|
for i := self.LastMinerCheck; i < vmctx.BlockHeight(); i++ {
|
|
|
|
height := i + 1
|
|
|
|
|
|
|
|
err := checkProofSubmissionsAtH(vmctx, &self, height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.LastMinerCheck = vmctx.BlockHeight()
|
|
|
|
|
|
|
|
nroot, aerr := vmctx.Storage().Put(&self)
|
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(old, nroot); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkProofSubmissionsAtH(vmctx types.VMContext, self *StoragePowerState, height uint64) aerrors.ActorError {
|
2019-11-29 20:18:34 +00:00
|
|
|
bucketID := height % build.SlashablePowerDelay
|
2019-11-14 23:30:31 +00:00
|
|
|
|
2019-11-13 22:40:51 +00:00
|
|
|
buckets, eerr := amt.LoadAMT(types.WrapStorage(vmctx.Storage()), self.ProvingBuckets)
|
|
|
|
if eerr != nil {
|
2019-11-14 23:30:31 +00:00
|
|
|
return aerrors.HandleExternalError(eerr, "loading proving buckets amt")
|
2019-11-13 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var bucket cid.Cid
|
|
|
|
err := buckets.Get(bucketID, &bucket)
|
|
|
|
switch err.(type) {
|
2019-11-13 23:35:58 +00:00
|
|
|
case *amt.ErrNotFound:
|
2019-11-14 23:30:31 +00:00
|
|
|
return nil // nothing to do
|
2019-11-13 22:40:51 +00:00
|
|
|
case nil:
|
|
|
|
default:
|
2019-11-14 23:30:31 +00:00
|
|
|
return aerrors.HandleExternalError(err, "getting proving bucket")
|
2019-11-13 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bhamt, err := hamt.LoadNode(vmctx.Context(), vmctx.Ipld(), bucket)
|
|
|
|
if err != nil {
|
2019-11-14 23:30:31 +00:00
|
|
|
return aerrors.HandleExternalError(err, "failed to load proving bucket")
|
2019-11-13 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:01:15 +00:00
|
|
|
forRemoval := make([]address.Address, 0)
|
|
|
|
|
2019-11-13 22:40:51 +00:00
|
|
|
err = bhamt.ForEach(vmctx.Context(), func(k string, val interface{}) error {
|
2019-11-14 23:23:07 +00:00
|
|
|
_, span := trace.StartSpan(vmctx.Context(), "StoragePowerActor.CheckProofSubmissions.loop")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-11-13 22:40:51 +00:00
|
|
|
maddr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.Escalate(err, "parsing miner address")
|
|
|
|
}
|
|
|
|
|
2020-01-31 05:05:41 +00:00
|
|
|
if vmctx.BlockHeight() > build.ForkMissingSnowballs {
|
2020-01-30 22:01:15 +00:00
|
|
|
has, aerr := MinerSetHas(vmctx, self.Miners, maddr)
|
|
|
|
if aerr != nil {
|
|
|
|
return aerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
forRemoval = append(forRemoval, maddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:23:07 +00:00
|
|
|
span.AddAttributes(trace.StringAttribute("miner", maddr.String()))
|
|
|
|
|
2019-11-14 11:56:17 +00:00
|
|
|
params, err := SerializeParams(&CheckMinerParams{NetworkPower: self.TotalStorage})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := vmctx.Send(maddr, MAMethods.CheckMiner, types.NewInt(0), params)
|
2019-11-13 22:40:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) == 0 {
|
|
|
|
return nil // miner is fine
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:56:17 +00:00
|
|
|
var power types.BigInt
|
|
|
|
if err := power.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
|
|
|
|
return xerrors.Errorf("unmarshaling CheckMiner response (%x): %w", ret, err)
|
|
|
|
}
|
2019-11-13 22:40:51 +00:00
|
|
|
|
2019-11-14 11:56:17 +00:00
|
|
|
if power.GreaterThan(types.NewInt(0)) {
|
2019-11-14 23:30:31 +00:00
|
|
|
log.Warnf("slashing miner %s for missed PoSt (%s B, H: %d, Bucket: %d)", maddr, power, height, bucketID)
|
2019-11-13 22:40:51 +00:00
|
|
|
|
2019-11-14 11:56:17 +00:00
|
|
|
self.TotalStorage = types.BigSub(self.TotalStorage, power)
|
|
|
|
}
|
2019-11-13 22:40:51 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-11-14 00:02:24 +00:00
|
|
|
|
2019-11-14 23:30:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(err, "iterating miners in proving bucket")
|
2019-11-14 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 05:10:37 +00:00
|
|
|
if vmctx.BlockHeight() > build.ForkMissingSnowballs && len(forRemoval) > 0 {
|
2020-01-30 22:01:15 +00:00
|
|
|
nBucket, err := MinerSetRemove(vmctx.Context(), vmctx, bucket, forRemoval...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.Wrap(err, "could not remove miners from set")
|
|
|
|
}
|
|
|
|
|
|
|
|
eerr := buckets.Set(bucketID, nBucket)
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(eerr, "could not set the bucket")
|
|
|
|
}
|
|
|
|
ncid, eerr := buckets.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return aerrors.HandleExternalError(eerr, "could not flush buckets")
|
|
|
|
}
|
|
|
|
self.ProvingBuckets = ncid
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:30:31 +00:00
|
|
|
return nil
|
2019-11-13 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 09:08:34 +00:00
|
|
|
func MinerSetHas(vmctx types.VMContext, rcid cid.Cid, maddr address.Address) (bool, aerrors.ActorError) {
|
|
|
|
nd, err := hamt.LoadNode(vmctx.Context(), vmctx.Ipld(), rcid)
|
2019-09-10 02:05:24 +00:00
|
|
|
if err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return false, aerrors.HandleExternalError(err, "failed to load miner set")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 09:08:34 +00:00
|
|
|
err = nd.Find(vmctx.Context(), 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:
|
2019-10-15 05:19:37 +00:00
|
|
|
return false, aerrors.HandleExternalError(err, "failed to do set lookup")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to load miner set")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
mkey := string(maddr.Bytes())
|
|
|
|
err = nd.Find(ctx, mkey, nil)
|
|
|
|
if err == nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.New(20, "miner already in set")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !xerrors.Is(err, hamt.ErrNotFound) {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to do miner set check")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Set(ctx, mkey, uint64(1)); err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "adding miner address to set failed")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Flush(ctx); err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to flush miner set")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := vmctx.Ipld().Put(ctx, nd)
|
|
|
|
if err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to persist miner set to storage")
|
2019-09-10 02:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
2019-09-12 04:12:35 +00:00
|
|
|
|
2020-01-30 22:01:15 +00:00
|
|
|
func MinerSetRemove(ctx context.Context, vmctx types.VMContext, rcid cid.Cid, maddrs ...address.Address) (cid.Cid, aerrors.ActorError) {
|
2019-09-12 04:12:35 +00:00
|
|
|
nd, err := hamt.LoadNode(ctx, vmctx.Ipld(), rcid)
|
|
|
|
if err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to load miner set")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 22:01:15 +00:00
|
|
|
for _, maddr := range maddrs {
|
|
|
|
mkey := string(maddr.Bytes())
|
|
|
|
switch nd.Delete(ctx, mkey) {
|
|
|
|
default:
|
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to delete miner from set")
|
|
|
|
case hamt.ErrNotFound:
|
|
|
|
return cid.Undef, aerrors.New(1, "miner not found in set on delete")
|
|
|
|
case nil:
|
|
|
|
}
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := nd.Flush(ctx); err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to flush miner set")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := vmctx.Ipld().Put(ctx, nd)
|
|
|
|
if err != nil {
|
2019-10-15 05:19:37 +00:00
|
|
|
return cid.Undef, aerrors.HandleExternalError(err, "failed to persist miner set to storage")
|
2019-09-12 04:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
2019-11-13 23:35:58 +00:00
|
|
|
|
|
|
|
type cbgNull struct{}
|
|
|
|
|
2019-12-19 18:46:30 +00:00
|
|
|
var CborNull = &cbgNull{}
|
2019-11-13 23:35:58 +00:00
|
|
|
|
|
|
|
func (cbgNull) MarshalCBOR(w io.Writer) error {
|
|
|
|
n, err := w.Write(cbg.CborNull)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return xerrors.New("expected to write 1 byte")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cbgNull) UnmarshalCBOR(r io.Reader) error {
|
|
|
|
b := [1]byte{}
|
|
|
|
n, err := r.Read(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
return xerrors.New("expected 1 byte")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b[:], cbg.CborNull) {
|
|
|
|
return xerrors.New("expected cbor null")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|