2019-07-12 23:52:25 +00:00
|
|
|
package actors
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-16 06:07:03 +00:00
|
|
|
"context"
|
2019-09-19 13:32:00 +00:00
|
|
|
"fmt"
|
2019-09-10 19:58:45 +00:00
|
|
|
|
2019-09-19 13:32:00 +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-08-07 06:35:57 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
|
2019-08-29 00:01:46 +00:00
|
|
|
|
2019-09-06 22:40:24 +00:00
|
|
|
"github.com/filecoin-project/go-amt-ipld"
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-07-12 20:56:41 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-09-18 15:10:03 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-08-15 14:07:45 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 20:56:41 +00:00
|
|
|
)
|
|
|
|
|
2019-07-16 06:07:03 +00:00
|
|
|
const POST_SECTORS_COUNT = 8192
|
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
type StorageMinerActor struct{}
|
|
|
|
|
|
|
|
type StorageMinerActorState struct {
|
2019-08-06 00:01:49 +00:00
|
|
|
// Contains mostly static info about this miner
|
|
|
|
Info cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
// Collateral that is waiting to be withdrawn.
|
|
|
|
DePledgedCollateral types.BigInt
|
|
|
|
|
|
|
|
// Time at which the depledged collateral may be withdrawn.
|
|
|
|
DePledgeTime types.BigInt
|
|
|
|
|
|
|
|
// All sectors this miner has committed.
|
2019-09-19 20:24:01 +00:00
|
|
|
Sectors cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
// Sectors this miner is currently mining. It is only updated
|
|
|
|
// when a PoSt is submitted (not as each new sector commitment is added).
|
2019-09-19 18:31:25 +00:00
|
|
|
ProvingSet cid.Cid
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-09-17 14:20:08 +00:00
|
|
|
// Faulty sectors reported since last SubmitPost,
|
|
|
|
// up to the current proving period's challenge time.
|
|
|
|
CurrentFaultSet types.BitField
|
|
|
|
|
|
|
|
// Faults submitted after the current proving period's challenge time,
|
|
|
|
// but before the PoSt for that period is submitted.
|
|
|
|
// These become the currentFaultSet when a PoSt is submitted.
|
|
|
|
NextFaultSet types.BitField
|
|
|
|
|
|
|
|
// Sectors reported during the last PoSt submission as being 'done'.
|
|
|
|
// The collateral for them is still being held until
|
|
|
|
// the next PoSt submission in case early sector
|
2019-07-12 20:56:41 +00:00
|
|
|
// removal penalization is needed.
|
2019-09-17 14:20:08 +00:00
|
|
|
NextDoneSet types.BitField
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
// Deals this miner has been slashed for since the last post submission.
|
2019-07-15 20:27:45 +00:00
|
|
|
//TODO: unsupported map key type "Cid" (if you want to use struct keys, your atlas needs a transform to string)
|
|
|
|
//ArbitratedDeals map[cid.Cid]struct{}
|
2019-07-12 20:56:41 +00:00
|
|
|
|
|
|
|
// Amount of power this miner has.
|
|
|
|
Power types.BigInt
|
|
|
|
|
|
|
|
// List of sectors that this miner was slashed for.
|
|
|
|
//SlashedSet SectorSet
|
|
|
|
|
|
|
|
// The height at which this miner was slashed at.
|
|
|
|
SlashedAt types.BigInt
|
|
|
|
|
|
|
|
// The amount of storage collateral that is owed to clients, and cannot be used for collateral anymore.
|
|
|
|
OwedStorageCollateral types.BigInt
|
2019-08-06 00:01:49 +00:00
|
|
|
|
|
|
|
ProvingPeriodEnd uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type MinerInfo struct {
|
|
|
|
// Account that owns this miner.
|
|
|
|
// - Income and returned collateral are paid to this address.
|
|
|
|
// - This address is also allowed to change the worker address for the miner.
|
|
|
|
Owner address.Address
|
|
|
|
|
|
|
|
// Worker account for this miner.
|
|
|
|
// This will be the key that is used to sign blocks created by this miner, and
|
|
|
|
// sign messages sent on behalf of this miner to commit sectors, submit PoSts, and
|
|
|
|
// other day to day miner activities.
|
|
|
|
Worker address.Address
|
|
|
|
|
|
|
|
// Libp2p identity that should be used when connecting to this miner.
|
|
|
|
PeerID peer.ID
|
|
|
|
|
|
|
|
// Amount of space in each sector committed to the network by this miner.
|
|
|
|
SectorSize types.BigInt
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StorageMinerConstructorParams 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-26 21:42:38 +00:00
|
|
|
type maMethods struct {
|
2019-08-15 20:57:14 +00:00
|
|
|
Constructor uint64
|
|
|
|
CommitSector uint64
|
|
|
|
SubmitPoSt uint64
|
|
|
|
SlashStorageFault uint64
|
|
|
|
GetCurrentProvingSet uint64
|
|
|
|
ArbitrateDeal uint64
|
|
|
|
DePledge uint64
|
|
|
|
GetOwner uint64
|
|
|
|
GetWorkerAddr uint64
|
|
|
|
GetPower uint64
|
|
|
|
GetPeerID uint64
|
|
|
|
GetSectorSize uint64
|
|
|
|
UpdatePeerID uint64
|
|
|
|
ChangeWorker uint64
|
|
|
|
IsSlashed uint64
|
|
|
|
IsLate uint64
|
|
|
|
PaymentVerifyInclusion uint64
|
|
|
|
PaymentVerifySector uint64
|
2019-09-18 15:10:03 +00:00
|
|
|
AddFaults uint64
|
2019-09-12 23:48:03 +00:00
|
|
|
SlashConsensusFault uint64
|
2019-07-26 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 23:48:03 +00:00
|
|
|
var MAMethods = maMethods{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
|
2019-07-26 21:42:38 +00:00
|
|
|
|
2019-07-15 20:27:45 +00:00
|
|
|
func (sma StorageMinerActor) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
2019-07-26 21:42:38 +00:00
|
|
|
1: sma.StorageMinerConstructor,
|
|
|
|
2: sma.CommitSector,
|
2019-08-06 00:01:49 +00:00
|
|
|
3: sma.SubmitPoSt,
|
2019-07-26 21:42:38 +00:00
|
|
|
//4: sma.SlashStorageFault,
|
2019-08-06 00:01:49 +00:00
|
|
|
//5: sma.GetCurrentProvingSet,
|
2019-07-26 21:42:38 +00:00
|
|
|
//6: sma.ArbitrateDeal,
|
|
|
|
//7: sma.DePledge,
|
2019-07-30 22:05:27 +00:00
|
|
|
8: sma.GetOwner,
|
2019-07-30 00:46:56 +00:00
|
|
|
9: sma.GetWorkerAddr,
|
2019-07-26 21:42:38 +00:00
|
|
|
10: sma.GetPower,
|
2019-07-30 22:05:27 +00:00
|
|
|
11: sma.GetPeerID,
|
|
|
|
12: sma.GetSectorSize,
|
|
|
|
13: sma.UpdatePeerID,
|
2019-07-26 21:42:38 +00:00
|
|
|
//14: sma.ChangeWorker,
|
2019-08-15 13:52:14 +00:00
|
|
|
//15: sma.IsSlashed,
|
|
|
|
//16: sma.IsLate,
|
2019-08-15 20:57:14 +00:00
|
|
|
17: sma.PaymentVerifyInclusion,
|
|
|
|
18: sma.PaymentVerifySector,
|
2019-09-18 15:10:03 +00:00
|
|
|
19: nil,
|
2019-09-12 23:48:03 +00:00
|
|
|
20: sma.SlashConsensusFault,
|
2019-07-15 20:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 22:05:27 +00:00
|
|
|
func loadState(vmctx types.VMContext) (cid.Cid, *StorageMinerActorState, ActorError) {
|
|
|
|
var self StorageMinerActorState
|
|
|
|
oldstate := vmctx.Storage().GetHead()
|
|
|
|
if err := vmctx.Storage().Get(oldstate, &self); err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldstate, &self, nil
|
|
|
|
}
|
|
|
|
|
2019-08-06 00:01:49 +00:00
|
|
|
func loadMinerInfo(vmctx types.VMContext, m *StorageMinerActorState) (*MinerInfo, ActorError) {
|
|
|
|
var mi MinerInfo
|
|
|
|
if err := vmctx.Storage().Get(m.Info, &mi); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mi, nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) ([]byte, ActorError) {
|
2019-08-06 00:01:49 +00:00
|
|
|
minerInfo := &MinerInfo{
|
|
|
|
Owner: params.Owner,
|
|
|
|
Worker: params.Worker,
|
|
|
|
PeerID: params.PeerID,
|
|
|
|
SectorSize: params.SectorSize,
|
|
|
|
}
|
2019-07-12 20:56:41 +00:00
|
|
|
|
2019-08-06 00:01:49 +00:00
|
|
|
minfocid, err := vmctx.Storage().Put(minerInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var self StorageMinerActorState
|
2019-09-06 22:40:24 +00:00
|
|
|
sectors := amt.NewAMT(types.WrapStorage(vmctx.Storage()))
|
|
|
|
scid, serr := sectors.Flush()
|
|
|
|
if serr != nil {
|
|
|
|
return nil, aerrors.Escalate(serr, "initializing AMT")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
2019-09-06 22:40:24 +00:00
|
|
|
|
|
|
|
self.Sectors = scid
|
|
|
|
self.ProvingSet = scid
|
2019-08-06 04:17:50 +00:00
|
|
|
self.Info = minfocid
|
2019-07-16 06:07:03 +00:00
|
|
|
|
2019-07-12 20:56:41 +00:00
|
|
|
storage := vmctx.Storage()
|
2019-09-10 19:58:45 +00:00
|
|
|
c, err := 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
|
|
|
}
|
|
|
|
|
2019-07-15 20:27:45 +00:00
|
|
|
if err := storage.Commit(EmptyCBOR, c); 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 nil, nil
|
2019-07-12 20:56:41 +00:00
|
|
|
}
|
2019-07-16 06:07:03 +00:00
|
|
|
|
|
|
|
type CommitSectorParams struct {
|
2019-08-29 00:01:46 +00:00
|
|
|
SectorID uint64
|
2019-07-16 06:07:03 +00:00
|
|
|
CommD []byte
|
|
|
|
CommR []byte
|
|
|
|
CommRStar []byte
|
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *CommitSectorParams) ([]byte, ActorError) {
|
2019-08-29 00:01:46 +00:00
|
|
|
ctx := context.TODO()
|
2019-07-30 22:05:27 +00:00
|
|
|
oldstate, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 00:01:49 +00:00
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:35:57 +00:00
|
|
|
// TODO: this needs to get normalized to either the ID address or the actor address
|
|
|
|
maddr := vmctx.Message().To
|
|
|
|
|
|
|
|
if ok, err := ValidatePoRep(maddr, mi.SectorSize, params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !ok {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(1, "bad proof!")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the miner isnt trying to submit a pre-existing sector
|
2019-08-29 00:01:46 +00:00
|
|
|
unique, err := SectorIsUnique(ctx, vmctx.Storage(), self.Sectors, params.SectorID)
|
2019-07-16 06:07:03 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
if !unique {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(2, "sector already committed!")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Power of the miner after adding this sector
|
2019-08-06 00:01:49 +00:00
|
|
|
futurePower := types.BigAdd(self.Power, mi.SectorSize)
|
2019-07-16 06:07:03 +00:00
|
|
|
collateralRequired := CollateralForPower(futurePower)
|
|
|
|
|
2019-09-23 17:11:44 +00:00
|
|
|
if act.Balance.LessThan(collateralRequired) {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, aerrors.New(3, "not enough collateral")
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: There must exist a unique index in the miner's sector set for each
|
|
|
|
// sector ID. The `faults`, `recovered`, and `done` parameters of the
|
|
|
|
// SubmitPoSt method express indices into this sector set.
|
2019-08-29 00:01:46 +00:00
|
|
|
nssroot, err := AddToSectorSet(ctx, vmctx.Storage(), self.Sectors, params.SectorID, params.CommR, params.CommD)
|
2019-07-16 06:07:03 +00:00
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
self.Sectors = nssroot
|
|
|
|
|
|
|
|
// if miner is not mining, start their proving period now
|
|
|
|
// Note: As written here, every miners first PoSt will only be over one sector.
|
|
|
|
// We could set up a 'grace period' for starting mining that would allow miners
|
|
|
|
// to submit several sectors for their first proving period. Alternatively, we
|
|
|
|
// could simply make the 'CommitSector' call take multiple sectors at a time.
|
|
|
|
//
|
|
|
|
// Note: Proving period is a function of sector size; small sectors take less
|
|
|
|
// time to prove than large sectors do. Sector size is selected when pledging.
|
2019-09-19 18:31:25 +00:00
|
|
|
pss, lerr := amt.LoadAMT(types.WrapStorage(vmctx.Storage()), self.ProvingSet)
|
|
|
|
if lerr != nil {
|
|
|
|
return nil, aerrors.Escalate(lerr, "could not load proving set node")
|
|
|
|
}
|
|
|
|
|
|
|
|
if pss.Count == 0 {
|
2019-07-16 06:07:03 +00:00
|
|
|
self.ProvingSet = self.Sectors
|
2019-09-19 13:32:00 +00:00
|
|
|
self.ProvingPeriodEnd = vmctx.BlockHeight() + build.ProvingPeriodDuration
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nstate, err := vmctx.Storage().Put(self)
|
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
if err := vmctx.Storage().Commit(oldstate, nstate); 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 nil, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 00:01:49 +00:00
|
|
|
type SubmitPoStParams struct {
|
2019-09-18 15:10:03 +00:00
|
|
|
Proof []byte
|
|
|
|
DoneSet types.BitField
|
2019-08-06 00:01:49 +00:00
|
|
|
// TODO: once the spec changes finish, we have more work to do here...
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
// TODO: this is a dummy method that allows us to plumb in other parts of the
|
|
|
|
// system for now.
|
2019-09-19 20:24:01 +00:00
|
|
|
func (sma StorageMinerActor) SubmitPoSt(act *types.Actor, vmctx types.VMContext, params *SubmitPoStParams) ([]byte, ActorError) {
|
2019-08-06 00:01:49 +00:00
|
|
|
oldstate, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmctx.Message().From != mi.Worker {
|
|
|
|
return nil, aerrors.New(1, "not authorized to submit post for miner")
|
|
|
|
}
|
|
|
|
|
2019-09-18 15:10:03 +00:00
|
|
|
feesRequired := types.NewInt(0)
|
2019-09-19 13:32:00 +00:00
|
|
|
nextProvingPeriodEnd := self.ProvingPeriodEnd + build.ProvingPeriodDuration
|
2019-09-18 15:10:03 +00:00
|
|
|
if vmctx.BlockHeight() > nextProvingPeriodEnd {
|
|
|
|
return nil, aerrors.New(1, "PoSt submited too late")
|
|
|
|
}
|
2019-08-06 00:01:49 +00:00
|
|
|
|
2019-09-18 15:10:03 +00:00
|
|
|
var lateSubmission bool
|
|
|
|
if vmctx.BlockHeight() > self.ProvingPeriodEnd {
|
|
|
|
//TODO late fee calc
|
|
|
|
lateSubmission = true
|
|
|
|
feesRequired = types.BigAdd(feesRequired, types.NewInt(1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO temporary sector failure fees
|
|
|
|
|
|
|
|
msgVal := vmctx.Message().Value
|
2019-09-23 17:11:44 +00:00
|
|
|
if msgVal.LessThan(feesRequired) {
|
2019-09-18 15:10:03 +00:00
|
|
|
return nil, aerrors.New(2, "not enough funds to pay post submission fees")
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:11:44 +00:00
|
|
|
if msgVal.GreaterThan(feesRequired) {
|
2019-09-18 15:10:03 +00:00
|
|
|
_, err := vmctx.Send(vmctx.Message().From, 0,
|
|
|
|
types.BigSub(msgVal, feesRequired), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "could not refund excess fees")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var seed [sectorbuilder.CommLen]byte
|
2019-09-19 13:32:00 +00:00
|
|
|
{
|
|
|
|
var rand []byte
|
|
|
|
var err ActorError
|
|
|
|
if !lateSubmission {
|
|
|
|
rand, err = vmctx.GetRandomness(self.ProvingPeriodEnd - build.PoSTChallangeTime)
|
|
|
|
} else {
|
|
|
|
rand, err = vmctx.GetRandomness(nextProvingPeriodEnd - build.PoSTChallangeTime)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "could not get randomness for PoST")
|
|
|
|
}
|
|
|
|
if len(rand) < len(seed) {
|
|
|
|
return nil, aerrors.Escalate(fmt.Errorf("randomness too small (%d < %d)",
|
|
|
|
len(rand), len(seed)), "improper randomness")
|
|
|
|
}
|
|
|
|
copy(seed[:], rand)
|
2019-09-18 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pss, lerr := amt.LoadAMT(types.WrapStorage(vmctx.Storage()), self.ProvingSet)
|
|
|
|
if lerr != nil {
|
2019-09-19 18:31:25 +00:00
|
|
|
return nil, aerrors.Escalate(lerr, "could not load proving set node")
|
2019-09-18 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sectorInfos []sectorbuilder.SectorInfo
|
|
|
|
if err := pss.ForEach(func(id uint64, v *cbg.Deferred) error {
|
|
|
|
var comms [][]byte
|
|
|
|
if err := cbor.DecodeInto(v.Raw, &comms); err != nil {
|
|
|
|
return xerrors.New("could not decode comms")
|
|
|
|
}
|
|
|
|
si := sectorbuilder.SectorInfo{
|
|
|
|
SectorID: id,
|
|
|
|
}
|
|
|
|
commR := comms[0]
|
|
|
|
if len(commR) != len(si.CommR) {
|
|
|
|
return xerrors.Errorf("commR length is wrong: %d", len(commR))
|
|
|
|
}
|
|
|
|
copy(si.CommR[:], commR)
|
|
|
|
|
|
|
|
sectorInfos = append(sectorInfos, si)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, aerrors.Absorb(err, 3, "could not decode sectorset")
|
|
|
|
}
|
|
|
|
|
|
|
|
faults := self.CurrentFaultSet.All()
|
|
|
|
|
|
|
|
if ok, lerr := sectorbuilder.VerifyPost(mi.SectorSize.Uint64(),
|
|
|
|
sectorbuilder.NewSortedSectorInfo(sectorInfos), seed, params.Proof,
|
|
|
|
faults); !ok || lerr != nil {
|
|
|
|
if lerr != nil {
|
2019-09-19 13:57:48 +00:00
|
|
|
// TODO: study PoST errors
|
2019-09-18 15:10:03 +00:00
|
|
|
return nil, aerrors.Absorb(lerr, 4, "PoST error")
|
|
|
|
}
|
2019-09-19 13:57:48 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, aerrors.New(4, "PoST invalid")
|
|
|
|
}
|
2019-09-18 15:10:03 +00:00
|
|
|
}
|
|
|
|
self.CurrentFaultSet = self.NextFaultSet
|
|
|
|
self.NextFaultSet = types.NewBitField()
|
|
|
|
|
|
|
|
ss, lerr := amt.LoadAMT(types.WrapStorage(vmctx.Storage()), self.ProvingSet)
|
|
|
|
if lerr != nil {
|
2019-09-19 18:31:25 +00:00
|
|
|
return nil, aerrors.Escalate(lerr, "could not load proving set node")
|
2019-09-18 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 04:56:40 +00:00
|
|
|
if err := ss.BatchDelete(params.DoneSet.All()); err != nil {
|
|
|
|
// TODO: this could fail for system reasons (block not found) or for
|
|
|
|
// bad user input reasons (e.g. bad doneset). The latter should be a
|
|
|
|
// non-fatal error
|
|
|
|
return nil, aerrors.Escalate(err, "failed to delete sectors in done set")
|
|
|
|
}
|
|
|
|
|
2019-09-18 15:10:03 +00:00
|
|
|
self.ProvingSet, lerr = ss.Flush()
|
|
|
|
if lerr != nil {
|
2019-09-19 13:56:44 +00:00
|
|
|
return nil, aerrors.Escalate(lerr, "could not flush AMT")
|
2019-09-18 15:10:03 +00:00
|
|
|
}
|
2019-08-06 00:01:49 +00:00
|
|
|
|
|
|
|
oldPower := self.Power
|
2019-09-19 18:31:25 +00:00
|
|
|
self.Power = types.BigMul(types.NewInt(pss.Count-uint64(len(faults))),
|
2019-09-18 15:10:03 +00:00
|
|
|
mi.SectorSize)
|
2019-08-06 00:01:49 +00:00
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
enc, err := SerializeParams(&UpdateStorageParams{Delta: types.BigSub(self.Power, oldPower)})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = vmctx.Send(StorageMarketAddress, SMAMethods.UpdateStorage, types.NewInt(0), enc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-18 15:10:03 +00:00
|
|
|
self.ProvingSet = self.Sectors
|
2019-09-19 18:38:58 +00:00
|
|
|
self.ProvingPeriodEnd = nextProvingPeriodEnd
|
2019-09-18 15:10:03 +00:00
|
|
|
self.NextDoneSet = params.DoneSet
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
c, err := vmctx.Storage().Put(self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(oldstate, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2019-08-06 00:01:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 13:01:52 +00:00
|
|
|
func (sma StorageMinerActor) GetPower(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
2019-07-30 22:05:27 +00:00
|
|
|
_, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
2019-07-23 13:01:52 +00:00
|
|
|
return nil, err
|
2019-07-17 13:48:20 +00:00
|
|
|
}
|
2019-07-23 13:01:52 +00:00
|
|
|
return self.Power.Bytes(), nil
|
2019-07-17 13:48:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
func SectorIsUnique(ctx context.Context, s types.Storage, sroot cid.Cid, sid uint64) (bool, ActorError) {
|
|
|
|
found, _, _, err := GetFromSectorSet(ctx, s, sroot, sid)
|
2019-07-16 06:07:03 +00:00
|
|
|
if err != nil {
|
2019-08-29 00:01:46 +00:00
|
|
|
return false, err
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
return !found, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
func AddToSectorSet(ctx context.Context, s types.Storage, ss cid.Cid, sectorID uint64, commR, commD []byte) (cid.Cid, ActorError) {
|
|
|
|
ssr, err := amt.LoadAMT(types.WrapStorage(s), ss)
|
2019-08-07 06:35:57 +00:00
|
|
|
if err != nil {
|
2019-08-29 00:01:46 +00:00
|
|
|
return cid.Undef, aerrors.Escalate(err, "could not load sector set node")
|
2019-08-07 06:35:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
if err := ssr.Set(sectorID, [][]byte{commR, commD}); err != nil {
|
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to set commitment in sector set")
|
2019-08-07 06:35:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
ncid, err := ssr.Flush()
|
2019-08-07 06:35:57 +00:00
|
|
|
if err != nil {
|
2019-08-29 00:01:46 +00:00
|
|
|
return cid.Undef, aerrors.Escalate(err, "failed to flush sector set")
|
2019-08-07 06:35:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
return ncid, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
func GetFromSectorSet(ctx context.Context, s types.Storage, ss cid.Cid, sectorID uint64) (bool, []byte, []byte, ActorError) {
|
|
|
|
ssr, err := amt.LoadAMT(types.WrapStorage(s), ss)
|
2019-08-15 13:52:14 +00:00
|
|
|
if err != nil {
|
2019-08-29 00:01:46 +00:00
|
|
|
return false, nil, nil, aerrors.Escalate(err, "could not load sector set node")
|
2019-08-15 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
var comms [][]byte
|
|
|
|
err = ssr.Get(sectorID, &comms)
|
2019-08-15 13:52:14 +00:00
|
|
|
if err != nil {
|
2019-09-09 11:22:09 +00:00
|
|
|
if _, ok := err.(*amt.ErrNotFound); ok {
|
2019-08-29 00:01:46 +00:00
|
|
|
return false, nil, nil, nil
|
|
|
|
}
|
2019-08-15 13:52:14 +00:00
|
|
|
return false, nil, nil, aerrors.Escalate(err, "failed to find sector in sector set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(comms) != 2 {
|
|
|
|
return false, nil, nil, aerrors.Escalate(xerrors.New("sector set entry should only have 2 elements"), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, comms[0], comms[1], nil
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:35:57 +00:00
|
|
|
func ValidatePoRep(maddr address.Address, ssize types.BigInt, params *CommitSectorParams) (bool, ActorError) {
|
2019-08-29 00:01:46 +00:00
|
|
|
ok, err := sectorbuilder.VerifySeal(ssize.Uint64(), params.CommR, params.CommD, params.CommRStar, maddr, params.SectorID, params.Proof)
|
2019-08-07 06:35:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, aerrors.Escalate(err, "verify seal failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok, nil
|
2019-07-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CollateralForPower(power types.BigInt) types.BigInt {
|
|
|
|
return types.BigMul(power, types.NewInt(10))
|
|
|
|
/* TODO: this
|
|
|
|
availableFil = FakeGlobalMethods.GetAvailableFil()
|
|
|
|
totalNetworkPower = StorageMinerActor.GetTotalStorage()
|
|
|
|
numMiners = StorageMarket.GetMinerCount()
|
|
|
|
powerCollateral = availableFil * NetworkConstants.POWER_COLLATERAL_PROPORTION * power / totalNetworkPower
|
|
|
|
perCapitaCollateral = availableFil * NetworkConstants.PER_CAPITA_COLLATERAL_PROPORTION / numMiners
|
|
|
|
collateralRequired = math.Ceil(minerPowerCollateral + minerPerCapitaCollateral)
|
|
|
|
return collateralRequired
|
|
|
|
*/
|
|
|
|
}
|
2019-07-30 00:46:56 +00:00
|
|
|
|
|
|
|
func (sma StorageMinerActor) GetWorkerAddr(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
2019-07-30 22:05:27 +00:00
|
|
|
_, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
2019-07-30 00:46:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-06 04:17:50 +00:00
|
|
|
|
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mi.Worker.Bytes(), nil
|
2019-07-30 00:46:56 +00:00
|
|
|
}
|
2019-07-30 22:05:27 +00:00
|
|
|
|
|
|
|
func (sma StorageMinerActor) GetOwner(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
|
|
|
_, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mi.Owner.Bytes(), nil
|
2019-07-30 22:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMinerActor) GetPeerID(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
|
|
|
_, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(mi.PeerID), nil
|
2019-07-30 22:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdatePeerIDParams struct {
|
|
|
|
PeerID peer.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMinerActor) UpdatePeerID(act *types.Actor, vmctx types.VMContext, params *UpdatePeerIDParams) ([]byte, ActorError) {
|
|
|
|
oldstate, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmctx.Message().From != mi.Worker {
|
2019-07-30 22:05:27 +00:00
|
|
|
return nil, aerrors.New(2, "only the mine worker may update the peer ID")
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
mi.PeerID = params.PeerID
|
|
|
|
|
|
|
|
mic, err := vmctx.Storage().Put(mi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
self.Info = mic
|
2019-07-30 22:05:27 +00:00
|
|
|
|
|
|
|
c, err := vmctx.Storage().Put(self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.Storage().Commit(oldstate, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMinerActor) GetSectorSize(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
|
|
|
|
_, self, err := loadState(vmctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:17:50 +00:00
|
|
|
mi, err := loadMinerInfo(vmctx, self)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mi.SectorSize.Bytes(), nil
|
2019-07-30 22:05:27 +00:00
|
|
|
}
|
2019-08-15 13:52:14 +00:00
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
type PaymentVerifyParams struct {
|
|
|
|
Extra []byte
|
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type PieceInclVoucherData struct { // TODO: Update spec at https://github.com/filecoin-project/specs/blob/master/actors.md#paymentverify
|
2019-08-15 13:52:14 +00:00
|
|
|
CommP []byte
|
2019-08-15 14:07:45 +00:00
|
|
|
PieceSize types.BigInt
|
2019-08-15 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
type InclusionProof struct {
|
2019-08-29 00:01:46 +00:00
|
|
|
Sector uint64 // for CommD, also verifies the sector is in sector set
|
2019-08-15 13:52:14 +00:00
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
func (sma StorageMinerActor) PaymentVerifyInclusion(act *types.Actor, vmctx types.VMContext, params *PaymentVerifyParams) ([]byte, ActorError) {
|
|
|
|
// params.Extra - PieceInclVoucherData
|
|
|
|
// params.Proof - InclusionProof
|
2019-08-15 14:07:45 +00:00
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
_, self, aerr := loadState(vmctx)
|
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
2019-08-15 13:52:14 +00:00
|
|
|
}
|
2019-08-15 20:57:14 +00:00
|
|
|
mi, aerr := loadMinerInfo(vmctx, self)
|
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
2019-08-15 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:57:14 +00:00
|
|
|
var voucherData PieceInclVoucherData
|
2019-08-15 14:07:45 +00:00
|
|
|
if err := cbor.DecodeInto(params.Extra, &voucherData); err != nil {
|
|
|
|
return nil, aerrors.Escalate(err, "failed to decode storage voucher data for verification")
|
|
|
|
}
|
2019-08-15 20:57:14 +00:00
|
|
|
var proof InclusionProof
|
2019-08-15 14:07:45 +00:00
|
|
|
if err := cbor.DecodeInto(params.Proof, &proof); err != nil {
|
|
|
|
return nil, aerrors.Escalate(err, "failed to decode storage payment proof")
|
|
|
|
}
|
2019-08-15 13:52:14 +00:00
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
ok, _, commD, aerr := GetFromSectorSet(context.TODO(), vmctx.Storage(), self.Sectors, proof.Sector)
|
2019-08-15 13:52:14 +00:00
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, aerrors.New(1, "miner does not have required sector")
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:50:52 +00:00
|
|
|
ok, err := sectorbuilder.VerifyPieceInclusionProof(mi.SectorSize.Uint64(), voucherData.PieceSize.Uint64(), voucherData.CommP, commD, proof.Proof)
|
2019-08-15 20:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Escalate(err, "verify piece inclusion proof failed")
|
2019-08-15 13:52:14 +00:00
|
|
|
}
|
2019-08-15 20:57:14 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, aerrors.New(2, "piece inclusion proof was invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMinerActor) PaymentVerifySector(act *types.Actor, vmctx types.VMContext, params *PaymentVerifyParams) ([]byte, ActorError) {
|
|
|
|
// params.Extra - BigInt - sector id
|
|
|
|
// params.Proof - nil
|
|
|
|
|
|
|
|
_, self, aerr := loadState(vmctx)
|
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ensure no sector ID reusability within related deal lifetime
|
|
|
|
sector := types.BigFromBytes(params.Extra)
|
|
|
|
|
|
|
|
if len(params.Proof) > 0 {
|
|
|
|
return nil, aerrors.New(1, "unexpected proof bytes")
|
|
|
|
}
|
|
|
|
|
2019-08-29 00:01:46 +00:00
|
|
|
ok, _, _, aerr := GetFromSectorSet(context.TODO(), vmctx.Storage(), self.Sectors, sector.Uint64())
|
2019-08-15 20:57:14 +00:00
|
|
|
if aerr != nil {
|
|
|
|
return nil, aerr
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, aerrors.New(2, "miner does not have required sector")
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:52:14 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-09-12 23:48:03 +00:00
|
|
|
|
|
|
|
type MinerSlashConsensusFault struct {
|
|
|
|
Slasher address.Address
|
|
|
|
AtHeight uint64
|
|
|
|
SlashedCollateral types.BigInt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma StorageMinerActor) SlashConsensusFault(act *types.Actor, vmctx types.VMContext, params *MinerSlashConsensusFault) ([]byte, ActorError) {
|
|
|
|
if vmctx.Message().From != StorageMarketAddress {
|
|
|
|
return nil, aerrors.New(1, "SlashConsensusFault may only be called by the storage market actor")
|
|
|
|
}
|
|
|
|
|
|
|
|
slashedCollateral := params.SlashedCollateral
|
2019-09-23 17:11:44 +00:00
|
|
|
if slashedCollateral.LessThan(act.Balance) {
|
2019-09-12 23:48:03 +00:00
|
|
|
slashedCollateral = act.Balance
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some of the slashed collateral should be paid to the slasher
|
|
|
|
// GROWTH_RATE determines how fast the slasher share of slashed collateral will increase as block elapses
|
|
|
|
// current GROWTH_RATE results in SLASHER_SHARE reaches 1 after 30 blocks
|
|
|
|
// TODO: define arithmetic precision and rounding for this operation
|
|
|
|
blockElapsed := vmctx.BlockHeight() - params.AtHeight
|
|
|
|
|
2019-09-13 00:12:45 +00:00
|
|
|
slasherShare := slasherShare(params.SlashedCollateral, blockElapsed)
|
2019-09-12 23:48:03 +00:00
|
|
|
|
|
|
|
burnPortion := types.BigSub(slashedCollateral, slasherShare)
|
|
|
|
|
|
|
|
_, err := vmctx.Send(vmctx.Message().From, 0, slasherShare, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "failed to pay slasher")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = vmctx.Send(BurntFundsAddress, 0, burnPortion, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, aerrors.Wrap(err, "failed to burn funds")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this still allows the miner to commit sectors and submit posts,
|
|
|
|
// their users could potentially be unaffected, but the miner will never be
|
|
|
|
// able to mine a block again
|
|
|
|
// One potential issue: the miner will have to pay back the slashed
|
|
|
|
// collateral to continue submitting PoSts, which includes pledge
|
|
|
|
// collateral that they no longer really 'need'
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-09-13 00:12:45 +00:00
|
|
|
|
|
|
|
func slasherShare(total types.BigInt, elapsed uint64) types.BigInt {
|
|
|
|
// [int(pow(1.26, n) * 10) for n in range(30)]
|
|
|
|
fracs := []uint64{10, 12, 15, 20, 25, 31, 40, 50, 63, 80, 100, 127, 160, 201, 254, 320, 403, 508, 640, 807, 1017, 1281, 1614, 2034, 2563, 3230, 4070, 5128, 6462, 8142}
|
|
|
|
const precision = 10000
|
|
|
|
|
|
|
|
var frac uint64
|
|
|
|
if elapsed >= uint64(len(fracs)) {
|
2019-09-20 14:21:00 +00:00
|
|
|
return total
|
2019-09-13 00:12:45 +00:00
|
|
|
} else {
|
|
|
|
frac = fracs[elapsed]
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.BigDiv(
|
|
|
|
types.BigMul(
|
|
|
|
types.NewInt(frac),
|
|
|
|
total,
|
|
|
|
),
|
|
|
|
types.NewInt(precision),
|
|
|
|
)
|
|
|
|
}
|