2020-09-12 03:07:52 +00:00
|
|
|
package miner
|
|
|
|
|
|
|
|
import (
|
2020-09-18 04:39:34 +00:00
|
|
|
"bytes"
|
2020-09-15 17:44:44 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-09-14 11:14:06 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/go-bitfield"
|
2020-09-15 04:55:49 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-09-15 13:29:25 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/dline"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2020-09-15 11:04:45 +00:00
|
|
|
v0adt "github.com/filecoin-project/specs-actors/actors/util/adt"
|
2020-09-12 03:07:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-09-15 11:04:45 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
2020-09-12 03:07:52 +00:00
|
|
|
|
2020-09-16 21:20:25 +00:00
|
|
|
v0miner "github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
2020-09-12 03:07:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type v0State struct {
|
2020-09-16 21:20:25 +00:00
|
|
|
v0miner.State
|
2020-09-12 03:07:52 +00:00
|
|
|
store adt.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
type v0Deadline struct {
|
2020-09-16 21:20:25 +00:00
|
|
|
v0miner.Deadline
|
2020-09-12 03:07:52 +00:00
|
|
|
store adt.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
type v0Partition struct {
|
2020-09-16 21:20:25 +00:00
|
|
|
v0miner.Partition
|
2020-09-12 03:07:52 +00:00
|
|
|
store adt.Store
|
|
|
|
}
|
|
|
|
|
2020-09-17 08:17:14 +00:00
|
|
|
func (s *v0State) AvailableBalance(bal abi.TokenAmount) (abi.TokenAmount, error) {
|
|
|
|
return s.GetAvailableBalance(bal), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) VestedFunds(epoch abi.ChainEpoch) (abi.TokenAmount, error) {
|
|
|
|
return s.CheckVestedFunds(s.store, epoch)
|
|
|
|
}
|
|
|
|
|
2020-09-15 04:55:49 +00:00
|
|
|
func (s *v0State) GetSector(num abi.SectorNumber) (*SectorOnChainInfo, error) {
|
|
|
|
info, ok, err := s.State.GetSector(s.store, num)
|
|
|
|
if !ok || err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:44:44 +00:00
|
|
|
func (s *v0State) FindSector(num abi.SectorNumber) (*SectorLocation, error) {
|
|
|
|
dlIdx, partIdx, err := s.State.FindSector(s.store, num)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &SectorLocation{
|
|
|
|
Deadline: dlIdx,
|
|
|
|
Partition: partIdx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSectorExpiration returns the effective expiration of the given sector.
|
|
|
|
//
|
|
|
|
// If the sector isn't found or has already been terminated, this method returns
|
|
|
|
// nil and no error. If the sector does not expire early, the Early expiration
|
|
|
|
// field is 0.
|
2020-09-18 05:12:45 +00:00
|
|
|
func (s *v0State) GetSectorExpiration(num abi.SectorNumber) (*SectorExpiration, error) {
|
2020-09-15 17:44:44 +00:00
|
|
|
dls, err := s.State.LoadDeadlines(s.store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// NOTE: this can be optimized significantly.
|
|
|
|
// 1. If the sector is non-faulty, it will either expire on-time (can be
|
|
|
|
// learned from the sector info), or in the next quantized expiration
|
|
|
|
// epoch (i.e., the first element in the partition's expiration queue.
|
|
|
|
// 2. If it's faulty, it will expire early within the first 14 entries
|
|
|
|
// of the expiration queue.
|
|
|
|
stopErr := errors.New("stop")
|
2020-09-18 05:12:45 +00:00
|
|
|
out := SectorExpiration{}
|
2020-09-16 21:20:25 +00:00
|
|
|
err = dls.ForEach(s.store, func(dlIdx uint64, dl *v0miner.Deadline) error {
|
2020-09-15 17:44:44 +00:00
|
|
|
partitions, err := dl.PartitionsArray(s.store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
quant := s.State.QuantSpecForDeadline(dlIdx)
|
2020-09-16 21:20:25 +00:00
|
|
|
var part v0miner.Partition
|
2020-09-15 17:44:44 +00:00
|
|
|
return partitions.ForEach(&part, func(partIdx int64) error {
|
|
|
|
if found, err := part.Sectors.IsSet(uint64(num)); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if found, err := part.Terminated.IsSet(uint64(num)); err != nil {
|
|
|
|
return err
|
|
|
|
} else if found {
|
|
|
|
// already terminated
|
|
|
|
return stopErr
|
|
|
|
}
|
|
|
|
|
2020-09-18 05:12:45 +00:00
|
|
|
q, err := v0miner.LoadExpirationQueue(s.store, part.ExpirationsEpochs, quant)
|
2020-09-15 17:44:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-16 21:20:25 +00:00
|
|
|
var exp v0miner.ExpirationSet
|
2020-09-15 17:44:44 +00:00
|
|
|
return q.ForEach(&exp, func(epoch int64) error {
|
|
|
|
if early, err := exp.EarlySectors.IsSet(uint64(num)); err != nil {
|
|
|
|
return err
|
|
|
|
} else if early {
|
|
|
|
out.Early = abi.ChainEpoch(epoch)
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-15 19:13:13 +00:00
|
|
|
if onTime, err := exp.OnTimeSectors.IsSet(uint64(num)); err != nil {
|
2020-09-15 17:44:44 +00:00
|
|
|
return err
|
|
|
|
} else if onTime {
|
2020-09-15 19:13:13 +00:00
|
|
|
out.OnTime = abi.ChainEpoch(epoch)
|
2020-09-15 17:44:44 +00:00
|
|
|
return stopErr
|
|
|
|
}
|
2020-09-15 19:13:13 +00:00
|
|
|
return nil
|
2020-09-15 17:44:44 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err == stopErr {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if out.Early == 0 && out.OnTime == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-09-18 05:12:45 +00:00
|
|
|
return &out, nil
|
2020-09-15 17:44:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 04:55:49 +00:00
|
|
|
func (s *v0State) GetPrecommittedSector(num abi.SectorNumber) (*SectorPreCommitOnChainInfo, error) {
|
|
|
|
info, ok, err := s.State.GetPrecommittedSector(s.store, num)
|
|
|
|
if !ok || err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 02:34:13 +00:00
|
|
|
func (s *v0State) LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.Array, error) {
|
2020-09-15 11:04:45 +00:00
|
|
|
a, err := v0adt.AsArray(s.store, s.State.Sectors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var v cbg.Deferred
|
|
|
|
if err := a.ForEach(&v, func(i int64) error {
|
|
|
|
if filter != nil {
|
|
|
|
set, err := filter.IsSet(uint64(i))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("filter check error: %w", err)
|
|
|
|
}
|
|
|
|
if set == filterOut {
|
2020-09-17 02:34:13 +00:00
|
|
|
err = a.Delete(uint64(i))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("filtering error: %w", err)
|
|
|
|
}
|
2020-09-15 11:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-17 02:34:13 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) LoadPreCommittedSectors() (adt.Map, error) {
|
|
|
|
return v0adt.AsMap(s.store, s.State.PreCommittedSectors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) IsAllocated(num abi.SectorNumber) (bool, error) {
|
|
|
|
var allocatedSectors bitfield.BitField
|
|
|
|
if err := s.store.Get(s.store.Context(), s.State.AllocatedSectors, &allocatedSectors); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocatedSectors.IsSet(uint64(num))
|
2020-09-15 11:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (s *v0State) LoadDeadline(idx uint64) (Deadline, error) {
|
|
|
|
dls, err := s.State.LoadDeadlines(s.store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dl, err := dls.LoadDeadline(s.store, idx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v0Deadline{*dl, s.store}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) ForEachDeadline(cb func(uint64, Deadline) error) error {
|
|
|
|
dls, err := s.State.LoadDeadlines(s.store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-16 21:20:25 +00:00
|
|
|
return dls.ForEach(s.store, func(i uint64, dl *v0miner.Deadline) error {
|
2020-09-12 03:07:52 +00:00
|
|
|
return cb(i, &v0Deadline{*dl, s.store})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) NumDeadlines() (uint64, error) {
|
2020-09-16 21:20:25 +00:00
|
|
|
return v0miner.WPoStPeriodDeadlines, nil
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:29:06 +00:00
|
|
|
// Max sectors per PoSt
|
|
|
|
func (s *v0State) MaxAddressedSectors() (uint64, error) {
|
|
|
|
return v0miner.AddressedSectorsMax, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 04:39:34 +00:00
|
|
|
func (s *v0State) DeadlinesChanged(other State) bool {
|
|
|
|
v0other, ok := other.(*v0State)
|
|
|
|
if !ok {
|
|
|
|
// treat an upgrade as a change, always
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.State.Deadlines.Equals(v0other.Deadlines)
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (s *v0State) Info() (MinerInfo, error) {
|
|
|
|
info, err := s.State.GetInfo(s.store)
|
2020-09-14 11:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return MinerInfo{}, err
|
|
|
|
}
|
2020-09-12 03:07:52 +00:00
|
|
|
|
|
|
|
var pid *peer.ID
|
|
|
|
if peerID, err := peer.IDFromBytes(info.PeerId); err == nil {
|
|
|
|
pid = &peerID
|
|
|
|
}
|
|
|
|
|
|
|
|
mi := MinerInfo{
|
|
|
|
Owner: info.Owner,
|
|
|
|
Worker: info.Worker,
|
|
|
|
ControlAddresses: info.ControlAddresses,
|
|
|
|
|
|
|
|
NewWorker: address.Undef,
|
|
|
|
WorkerChangeEpoch: -1,
|
|
|
|
|
|
|
|
PeerId: pid,
|
|
|
|
Multiaddrs: info.Multiaddrs,
|
|
|
|
SealProofType: info.SealProofType,
|
|
|
|
SectorSize: info.SectorSize,
|
|
|
|
WindowPoStPartitionSectors: info.WindowPoStPartitionSectors,
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.PendingWorkerKey != nil {
|
|
|
|
mi.NewWorker = info.PendingWorkerKey.NewWorker
|
|
|
|
mi.WorkerChangeEpoch = info.PendingWorkerKey.EffectiveAt
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:14:06 +00:00
|
|
|
return mi, nil
|
2020-09-12 03:07:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 13:29:25 +00:00
|
|
|
func (s *v0State) DeadlineInfo(epoch abi.ChainEpoch) *dline.Info {
|
|
|
|
return s.State.DeadlineInfo(epoch)
|
|
|
|
}
|
|
|
|
|
2020-09-18 04:39:34 +00:00
|
|
|
func (s *v0State) sectors() (adt.Array, error) {
|
|
|
|
return v0adt.AsArray(s.store, s.Sectors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) decodeSectorOnChainInfo(val *cbg.Deferred) (SectorOnChainInfo, error) {
|
|
|
|
var si v0miner.SectorOnChainInfo
|
|
|
|
err := si.UnmarshalCBOR(bytes.NewReader(val.Raw))
|
|
|
|
return si, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) precommits() (adt.Map, error) {
|
|
|
|
return v0adt.AsMap(s.store, s.PreCommittedSectors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *v0State) decodeSectorPreCommitOnChainInfo(val *cbg.Deferred) (SectorPreCommitOnChainInfo, error) {
|
|
|
|
var sp v0miner.SectorPreCommitOnChainInfo
|
|
|
|
err := sp.UnmarshalCBOR(bytes.NewReader(val.Raw))
|
|
|
|
return sp, err
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (d *v0Deadline) LoadPartition(idx uint64) (Partition, error) {
|
|
|
|
p, err := d.Deadline.LoadPartition(d.store, idx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &v0Partition{*p, d.store}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *v0Deadline) ForEachPartition(cb func(uint64, Partition) error) error {
|
|
|
|
ps, err := d.Deadline.PartitionsArray(d.store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-16 21:20:25 +00:00
|
|
|
var part v0miner.Partition
|
2020-09-12 03:07:52 +00:00
|
|
|
return ps.ForEach(&part, func(i int64) error {
|
|
|
|
return cb(uint64(i), &v0Partition{part, d.store})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-18 05:12:45 +00:00
|
|
|
func (d *v0Deadline) PartitionsChanged(other Deadline) bool {
|
2020-09-18 04:39:34 +00:00
|
|
|
v0other, ok := other.(*v0Deadline)
|
|
|
|
if !ok {
|
|
|
|
// treat an upgrade as a change, always
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-18 05:12:45 +00:00
|
|
|
return d.Deadline.Partitions.Equals(v0other.Deadline.Partitions)
|
2020-09-18 04:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:30:15 +00:00
|
|
|
func (d *v0Deadline) PostSubmissions() (bitfield.BitField, error) {
|
|
|
|
return d.Deadline.PostSubmissions, nil
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
func (p *v0Partition) AllSectors() (bitfield.BitField, error) {
|
|
|
|
return p.Partition.Sectors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v0Partition) FaultySectors() (bitfield.BitField, error) {
|
|
|
|
return p.Partition.Faults, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v0Partition) RecoveringSectors() (bitfield.BitField, error) {
|
|
|
|
return p.Partition.Recoveries, nil
|
|
|
|
}
|