more progress

This commit is contained in:
Steven Allen 2020-09-15 10:44:44 -07:00
parent 9dad38c9a5
commit 00c6397ec9
4 changed files with 109 additions and 93 deletions

View File

@ -473,19 +473,6 @@ type MinerSectors struct {
Active uint64
}
type SectorExpiration struct {
OnTime abi.ChainEpoch
// non-zero if sector is faulty, epoch at which it will be permanently
// removed if it doesn't recover
Early abi.ChainEpoch
}
type SectorLocation struct {
Deadline uint64
Partition uint64
}
type ImportRes struct {
Root cid.Cid
ImportID multistore.StoreID

View File

@ -35,6 +35,8 @@ type State interface {
cbor.Marshaler
GetSector(abi.SectorNumber) (*SectorOnChainInfo, error)
FindSector(abi.SectorNumber) (*SectorLocation, error)
GetSectorExpiration(abi.SectorNumber) (*SectorExpiration, error)
GetPrecommittedSector(abi.SectorNumber) (*SectorPreCommitOnChainInfo, error)
LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) ([]*ChainSectorInfo, error)
@ -80,3 +82,16 @@ type ChainSectorInfo struct {
Info SectorOnChainInfo
ID abi.SectorNumber
}
type SectorExpiration struct {
OnTime abi.ChainEpoch
// non-zero if sector is faulty, epoch at which it will be permanently
// removed if it doesn't recover
Early abi.ChainEpoch
}
type SectorLocation struct {
Deadline uint64
Partition uint64
}

View File

@ -1,6 +1,8 @@
package miner
import (
"errors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
@ -39,6 +41,87 @@ func (s *v0State) GetSector(num abi.SectorNumber) (*SectorOnChainInfo, error) {
return info, nil
}
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.
func (s *v0State) GetSectorExpiration(num abi.SectorNumber) (out *SectorExpiration, err error) {
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")
err := dls.ForEach(s.store, func(dlIdx uint64, dl *miner.Deadline) error {
partitions, err := dl.PartitionsArray(s.store)
if err != nil {
return err
}
quant := s.State.QuantSpecForDeadline(dlIdx)
var part miner.Partition
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
}
q, err := miner.LoadExpirationQueue(s.store, part.EarlyTerminated, quant)
if err != nil {
return err
}
var exp miner.ExpirationSet
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
}
if onTime, err := exp.OnTime.IsSet(uint64(num)); err != nil {
return err
} else if onTime {
out.OnTime = epoch
return stopErr
}
})
})
})
if err == stopErr {
err = nil
}
if err != nil {
return nil, err
}
if out.Early == 0 && out.OnTime == 0 {
return nil, nil
}
return out, nil
}
func (s *v0State) GetPrecommittedSector(num abi.SectorNumber) (*SectorPreCommitOnChainInfo, error) {
info, ok, err := s.State.GetPrecommittedSector(s.store, num)
if !ok || err != nil {

View File

@ -698,97 +698,28 @@ func (a *StateAPI) StateSectorGetInfo(ctx context.Context, maddr address.Address
return stmgr.MinerSectorInfo(ctx, a.StateManager, maddr, n, ts)
}
type sectorPartitionCb func(store adt.Store, mas *miner.State, di uint64, pi uint64, part *miner.Partition) error
func (a *StateAPI) sectorPartition(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tsk types.TipSetKey, cb sectorPartitionCb) error {
return a.StateManager.WithParentStateTsk(tsk,
a.StateManager.WithActor(maddr,
a.StateManager.WithActorState(ctx, func(store adt.Store, mas *miner.State) error {
return a.StateManager.WithDeadlines(func(store adt.Store, deadlines *miner.Deadlines) error {
err := a.StateManager.WithEachDeadline(func(store adt.Store, di uint64, deadline *miner.Deadline) error {
return a.StateManager.WithEachPartition(func(store adt.Store, pi uint64, partition *miner.Partition) error {
set, err := partition.Sectors.IsSet(uint64(sectorNumber))
if err != nil {
return xerrors.Errorf("is set: %w", err)
}
if set {
if err := cb(store, mas, di, pi, partition); err != nil {
return err
}
return errBreakForeach
}
return nil
})(store, di, deadline)
})(store, deadlines)
if err == errBreakForeach {
err = nil
}
return err
})(store, mas)
})))
}
func (a *StateAPI) StateSectorExpiration(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tsk types.TipSetKey) (*api.SectorExpiration, error) {
var onTimeEpoch, earlyEpoch abi.ChainEpoch
err := a.sectorPartition(ctx, maddr, sectorNumber, tsk, func(store adt.Store, mas *miner.State, di uint64, pi uint64, part *miner.Partition) error {
quant := mas.QuantSpecForDeadline(di)
expirations, err := miner.LoadExpirationQueue(store, part.ExpirationsEpochs, quant)
if err != nil {
return xerrors.Errorf("loading expiration queue: %w", err)
}
var eset miner.ExpirationSet
return expirations.Array.ForEach(&eset, func(epoch int64) error {
set, err := eset.OnTimeSectors.IsSet(uint64(sectorNumber))
if err != nil {
return xerrors.Errorf("checking if sector is in onTime set: %w", err)
}
if set {
onTimeEpoch = abi.ChainEpoch(epoch)
}
set, err = eset.EarlySectors.IsSet(uint64(sectorNumber))
if err != nil {
return xerrors.Errorf("checking if sector is in early set: %w", err)
}
if set {
earlyEpoch = abi.ChainEpoch(epoch)
}
return nil
})
})
act, err := a.StateManager.LoadActorTsk(ctx, maddr, tsk)
if err != nil {
return nil, err
}
if onTimeEpoch == 0 {
return nil, xerrors.Errorf("expiration for sector %d not found", sectorNumber)
mas, err := miner.Load(a.StateManager.ChainStore().Store(ctx), act)
if err != nil {
return nil, err
}
return &api.SectorExpiration{
OnTime: onTimeEpoch,
Early: earlyEpoch,
}, nil
return mas.GetSectorExpiration(sectorNumber)
}
func (a *StateAPI) StateSectorPartition(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tsk types.TipSetKey) (*api.SectorLocation, error) {
var found *api.SectorLocation
err := a.sectorPartition(ctx, maddr, sectorNumber, tsk, func(store adt.Store, mas *miner.State, di, pi uint64, partition *miner.Partition) error {
found = &api.SectorLocation{
Deadline: di,
Partition: pi,
}
return errBreakForeach
})
act, err := a.StateManager.LoadActorTsk(ctx, maddr, tsk)
if err != nil {
return nil, err
}
return found, nil
mas, err := miner.Load(a.StateManager.ChainStore().Store(ctx), act)
if err != nil {
return nil, err
}
return mas.FindSector(sectorNumber)
}
func (a *StateAPI) StateListMessages(ctx context.Context, match *types.Message, tsk types.TipSetKey, toheight abi.ChainEpoch) ([]cid.Cid, error) {