github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/builtin/miner/termination.go (about)

     1  package miner
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/filecoin-project/go-bitfield"
     7  	"github.com/filecoin-project/go-state-types/abi"
     8  )
     9  
    10  type TerminationResult struct {
    11  	// Sectors maps epochs at which sectors expired, to bitfields of sector
    12  	// numbers.
    13  	Sectors map[abi.ChainEpoch]bitfield.BitField
    14  	// Counts the number of partitions & sectors processed.
    15  	PartitionsProcessed, SectorsProcessed uint64
    16  }
    17  
    18  func (t *TerminationResult) Add(newResult TerminationResult) error {
    19  	if t.Sectors == nil {
    20  		t.Sectors = make(map[abi.ChainEpoch]bitfield.BitField, len(newResult.Sectors))
    21  	}
    22  	t.PartitionsProcessed += newResult.PartitionsProcessed
    23  	t.SectorsProcessed += newResult.SectorsProcessed
    24  	for epoch, newSectors := range newResult.Sectors { //nolint:nomaprange
    25  		if oldSectors, ok := t.Sectors[epoch]; !ok {
    26  			t.Sectors[epoch] = newSectors
    27  		} else {
    28  			var err error
    29  			t.Sectors[epoch], err = bitfield.MergeBitFields(oldSectors, newSectors)
    30  			if err != nil {
    31  				return err
    32  			}
    33  		}
    34  	}
    35  	return nil
    36  }
    37  
    38  // Returns true if we're below the partition/sector limit. Returns false if
    39  // we're at (or above) the limit.
    40  func (t *TerminationResult) BelowLimit(maxPartitions, maxSectors uint64) bool {
    41  	return t.PartitionsProcessed < maxPartitions && t.SectorsProcessed < maxSectors
    42  }
    43  
    44  func (t *TerminationResult) IsEmpty() bool {
    45  	return t.SectorsProcessed == 0
    46  }
    47  
    48  func (t *TerminationResult) ForEach(cb func(epoch abi.ChainEpoch, sectors bitfield.BitField) error) error {
    49  	// We're sorting here, so iterating over the map is fine.
    50  	epochs := make([]abi.ChainEpoch, 0, len(t.Sectors))
    51  	for epoch := range t.Sectors { //nolint:nomaprange
    52  		epochs = append(epochs, epoch)
    53  	}
    54  	sort.Slice(epochs, func(i, j int) bool {
    55  		return epochs[i] < epochs[j]
    56  	})
    57  	for _, epoch := range epochs {
    58  		err := cb(epoch, t.Sectors[epoch])
    59  		if err != nil {
    60  			return err
    61  		}
    62  	}
    63  	return nil
    64  }