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