/
sector_map.go
150 lines (132 loc) · 4.59 KB
/
sector_map.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package miner
import (
"math"
"sort"
"github.com/chenjianmei111/go-bitfield"
"golang.org/x/xerrors"
)
// Maps deadlines to partition maps.
type DeadlineSectorMap map[uint64]PartitionSectorMap
// Maps partitions to sector bitfields.
type PartitionSectorMap map[uint64]bitfield.BitField
// Check validates all bitfields and counts the number of partitions & sectors
// contained within the map, and returns an error if they exceed the given
// maximums.
func (dm DeadlineSectorMap) Check(maxPartitions, maxSectors uint64) error {
partitionCount, sectorCount, err := dm.Count()
if err != nil {
return xerrors.Errorf("failed to count sectors: %w", err)
}
if partitionCount > maxPartitions {
return xerrors.Errorf("too many partitions %d, max %d", partitionCount, maxPartitions)
}
if sectorCount > maxSectors {
return xerrors.Errorf("too many sectors %d, max %d", sectorCount, maxSectors)
}
return nil
}
// Count counts the number of partitions & sectors within the map.
func (dm DeadlineSectorMap) Count() (partitions, sectors uint64, err error) {
for dlIdx, pm := range dm { //nolint:nomaprange
partCount, sectorCount, err := pm.Count()
if err != nil {
return 0, 0, xerrors.Errorf("when counting deadline %d: %w", dlIdx, err)
}
if partCount > math.MaxUint64-partitions {
return 0, 0, xerrors.Errorf("uint64 overflow when counting partitions")
}
if sectorCount > math.MaxUint64-sectors {
return 0, 0, xerrors.Errorf("uint64 overflow when counting sectors")
}
sectors += sectorCount
partitions += partCount
}
return partitions, sectors, nil
}
// Add records the given sector bitfield at the given deadline/partition index.
func (dm DeadlineSectorMap) Add(dlIdx, partIdx uint64, sectorNos bitfield.BitField) error {
if dlIdx >= WPoStPeriodDeadlines {
return xerrors.Errorf("invalid deadline %d", dlIdx)
}
dl, ok := dm[dlIdx]
if !ok {
dl = make(PartitionSectorMap)
dm[dlIdx] = dl
}
return dl.Add(partIdx, sectorNos)
}
// AddValues records the given sectors at the given deadline/partition index.
func (dm DeadlineSectorMap) AddValues(dlIdx, partIdx uint64, sectorNos ...uint64) error {
return dm.Add(dlIdx, partIdx, bitfield.NewFromSet(sectorNos))
}
// Deadlines returns a sorted slice of deadlines in the map.
func (dm DeadlineSectorMap) Deadlines() []uint64 {
deadlines := make([]uint64, 0, len(dm))
for dlIdx := range dm { //nolint:nomaprange
deadlines = append(deadlines, dlIdx)
}
sort.Slice(deadlines, func(i, j int) bool {
return deadlines[i] < deadlines[j]
})
return deadlines
}
// ForEach walks the deadlines in deadline order.
func (dm DeadlineSectorMap) ForEach(cb func(dlIdx uint64, pm PartitionSectorMap) error) error {
for _, dlIdx := range dm.Deadlines() {
if err := cb(dlIdx, dm[dlIdx]); err != nil {
return err
}
}
return nil
}
// AddValues records the given sectors at the given partition.
func (pm PartitionSectorMap) AddValues(partIdx uint64, sectorNos ...uint64) error {
return pm.Add(partIdx, bitfield.NewFromSet(sectorNos))
}
// Add records the given sector bitfield at the given partition index, merging
// it with any existing bitfields if necessary.
func (pm PartitionSectorMap) Add(partIdx uint64, sectorNos bitfield.BitField) error {
if oldSectorNos, ok := pm[partIdx]; ok {
var err error
sectorNos, err = bitfield.MergeBitFields(sectorNos, oldSectorNos)
if err != nil {
return xerrors.Errorf("failed to merge sector bitfields: %w", err)
}
}
pm[partIdx] = sectorNos
return nil
}
// Count counts the number of partitions & sectors within the map.
func (pm PartitionSectorMap) Count() (partitions, sectors uint64, err error) {
for partIdx, bf := range pm { //nolint:nomaprange
count, err := bf.Count()
if err != nil {
return 0, 0, xerrors.Errorf("failed to parse bitmap for partition %d: %w", partIdx, err)
}
if count > math.MaxUint64-sectors {
return 0, 0, xerrors.Errorf("uint64 overflow when counting sectors")
}
sectors += count
}
return uint64(len(pm)), sectors, nil
}
// Partitions returns a sorted slice of partitions in the map.
func (pm PartitionSectorMap) Partitions() []uint64 {
partitions := make([]uint64, 0, len(pm))
for partIdx := range pm { //nolint:nomaprange
partitions = append(partitions, partIdx)
}
sort.Slice(partitions, func(i, j int) bool {
return partitions[i] < partitions[j]
})
return partitions
}
// ForEach walks the partitions in the map, in order of increasing index.
func (pm PartitionSectorMap) ForEach(cb func(partIdx uint64, sectorNos bitfield.BitField) error) error {
for _, partIdx := range pm.Partitions() {
if err := cb(partIdx, pm[partIdx]); err != nil {
return err
}
}
return nil
}