This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
sector.go
272 lines (236 loc) · 9.07 KB
/
sector.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package abi
import (
"fmt"
"math"
"strconv"
cid "github.com/ipfs/go-cid"
"github.com/pkg/errors"
big "github.com/filecoin-project/specs-actors/actors/abi/big"
)
// SectorNumber is a numeric identifier for a sector. It is usually relative to a miner.
type SectorNumber uint64
func (s SectorNumber) String() string {
return strconv.FormatUint(uint64(s), 10)
}
// The maximum assignable sector number.
// Raising this would require modifying our AMT implementation.
const MaxSectorNumber = math.MaxInt64
// SectorSize indicates one of a set of possible sizes in the network.
// Ideally, SectorSize would be an enum
// type SectorSize enum {
// 1KiB = 1024
// 1MiB = 1048576
// 1GiB = 1073741824
// 1TiB = 1099511627776
// 1PiB = 1125899906842624
// 1EiB = 1152921504606846976
// max = 18446744073709551615
// }
type SectorSize uint64
// Formats the size as a decimal string.
func (s SectorSize) String() string {
return strconv.FormatUint(uint64(s), 10)
}
// Abbreviates the size as a human-scale number.
// This approximates (truncates) the size unless it is a power of 1024.
func (s SectorSize) ShortString() string {
var biUnits = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
unit := 0
for s >= 1024 && unit < len(biUnits)-1 {
s /= 1024
unit++
}
return fmt.Sprintf("%d%s", s, biUnits[unit])
}
type SectorID struct {
Miner ActorID
Number SectorNumber
}
// The unit of storage power (measured in bytes)
type StoragePower = big.Int
type SectorQuality = big.Int
func NewStoragePower(n int64) StoragePower {
return big.NewInt(n)
}
type RegisteredProof = int64
// This ordering, defines mappings to UInt in a way which MUST never change.
type RegisteredSealProof RegisteredProof
const (
RegisteredSealProof_StackedDrg2KiBV1 = RegisteredSealProof(0)
RegisteredSealProof_StackedDrg8MiBV1 = RegisteredSealProof(1)
RegisteredSealProof_StackedDrg512MiBV1 = RegisteredSealProof(2)
RegisteredSealProof_StackedDrg32GiBV1 = RegisteredSealProof(3)
RegisteredSealProof_StackedDrg64GiBV1 = RegisteredSealProof(4)
)
type RegisteredPoStProof RegisteredProof
const (
RegisteredPoStProof_StackedDrgWinning2KiBV1 = RegisteredPoStProof(0)
RegisteredPoStProof_StackedDrgWinning8MiBV1 = RegisteredPoStProof(1)
RegisteredPoStProof_StackedDrgWinning512MiBV1 = RegisteredPoStProof(2)
RegisteredPoStProof_StackedDrgWinning32GiBV1 = RegisteredPoStProof(3)
RegisteredPoStProof_StackedDrgWinning64GiBV1 = RegisteredPoStProof(4)
RegisteredPoStProof_StackedDrgWindow2KiBV1 = RegisteredPoStProof(5)
RegisteredPoStProof_StackedDrgWindow8MiBV1 = RegisteredPoStProof(6)
RegisteredPoStProof_StackedDrgWindow512MiBV1 = RegisteredPoStProof(7)
RegisteredPoStProof_StackedDrgWindow32GiBV1 = RegisteredPoStProof(8)
RegisteredPoStProof_StackedDrgWindow64GiBV1 = RegisteredPoStProof(9)
)
func (p RegisteredPoStProof) RegisteredSealProof() (RegisteredSealProof, error) {
switch p {
case RegisteredPoStProof_StackedDrgWinning2KiBV1, RegisteredPoStProof_StackedDrgWindow2KiBV1:
return RegisteredSealProof_StackedDrg2KiBV1, nil
case RegisteredPoStProof_StackedDrgWinning8MiBV1, RegisteredPoStProof_StackedDrgWindow8MiBV1:
return RegisteredSealProof_StackedDrg8MiBV1, nil
case RegisteredPoStProof_StackedDrgWinning512MiBV1, RegisteredPoStProof_StackedDrgWindow512MiBV1:
return RegisteredSealProof_StackedDrg512MiBV1, nil
case RegisteredPoStProof_StackedDrgWinning32GiBV1, RegisteredPoStProof_StackedDrgWindow32GiBV1:
return RegisteredSealProof_StackedDrg32GiBV1, nil
case RegisteredPoStProof_StackedDrgWinning64GiBV1, RegisteredPoStProof_StackedDrgWindow64GiBV1:
return RegisteredSealProof_StackedDrg64GiBV1, nil
default:
return 0, errors.Errorf("unsupported PoSt proof type: %v", p)
}
}
func (p RegisteredSealProof) SectorSize() (SectorSize, error) {
switch p {
case RegisteredSealProof_StackedDrg2KiBV1:
return 2 << 10, nil
case RegisteredSealProof_StackedDrg8MiBV1:
return 8 << 20, nil
case RegisteredSealProof_StackedDrg512MiBV1:
return 512 << 20, nil
case RegisteredSealProof_StackedDrg32GiBV1:
return 32 << 30, nil
case RegisteredSealProof_StackedDrg64GiBV1:
return 2 * (32 << 30), nil
default:
return 0, errors.Errorf("unsupported proof type: %v", p)
}
}
func (p RegisteredPoStProof) SectorSize() (SectorSize, error) {
// Resolve to seal proof and then compute size from that.
sp, err := p.RegisteredSealProof()
if err != nil {
return 0, err
}
return sp.SectorSize()
}
// Returns the partition size, in sectors, associated with a proof type.
// The partition size is the number of sectors proved in a single PoSt proof.
func (p RegisteredSealProof) WindowPoStPartitionSectors() (uint64, error) {
// These numbers must match those used by the proofs library.
// See https://github.com/filecoin-project/rust-fil-proofs/blob/master/filecoin-proofs/src/constants.rs#L85
switch p {
case RegisteredSealProof_StackedDrg64GiBV1:
return 2300, nil
case RegisteredSealProof_StackedDrg32GiBV1:
return 2349, nil
case RegisteredSealProof_StackedDrg2KiBV1:
return 2, nil
case RegisteredSealProof_StackedDrg8MiBV1:
return 2, nil
case RegisteredSealProof_StackedDrg512MiBV1:
return 2, nil
default:
return 0, errors.Errorf("unsupported proof type: %v", p)
}
}
// Returns the partition size, in sectors, associated with a proof type.
// The partition size is the number of sectors proved in a single PoSt proof.
func (p RegisteredPoStProof) WindowPoStPartitionSectors() (uint64, error) {
// Resolve to seal proof and then compute size from that.
sp, err := p.RegisteredSealProof()
if err != nil {
return 0, err
}
return sp.WindowPoStPartitionSectors()
}
// RegisteredWinningPoStProof produces the PoSt-specific RegisteredProof corresponding
// to the receiving RegisteredProof.
func (p RegisteredSealProof) RegisteredWinningPoStProof() (RegisteredPoStProof, error) {
switch p {
case RegisteredSealProof_StackedDrg64GiBV1:
return RegisteredPoStProof_StackedDrgWinning64GiBV1, nil
case RegisteredSealProof_StackedDrg32GiBV1:
return RegisteredPoStProof_StackedDrgWinning32GiBV1, nil
case RegisteredSealProof_StackedDrg2KiBV1:
return RegisteredPoStProof_StackedDrgWinning2KiBV1, nil
case RegisteredSealProof_StackedDrg8MiBV1:
return RegisteredPoStProof_StackedDrgWinning8MiBV1, nil
case RegisteredSealProof_StackedDrg512MiBV1:
return RegisteredPoStProof_StackedDrgWinning512MiBV1, nil
default:
return 0, errors.Errorf("unsupported mapping from %+v to PoSt-specific RegisteredProof", p)
}
}
// RegisteredWindowPoStProof produces the PoSt-specific RegisteredProof corresponding
// to the receiving RegisteredProof.
func (p RegisteredSealProof) RegisteredWindowPoStProof() (RegisteredPoStProof, error) {
switch p {
case RegisteredSealProof_StackedDrg64GiBV1:
return RegisteredPoStProof_StackedDrgWindow64GiBV1, nil
case RegisteredSealProof_StackedDrg32GiBV1:
return RegisteredPoStProof_StackedDrgWindow32GiBV1, nil
case RegisteredSealProof_StackedDrg2KiBV1:
return RegisteredPoStProof_StackedDrgWindow2KiBV1, nil
case RegisteredSealProof_StackedDrg8MiBV1:
return RegisteredPoStProof_StackedDrgWindow8MiBV1, nil
case RegisteredSealProof_StackedDrg512MiBV1:
return RegisteredPoStProof_StackedDrgWindow512MiBV1, nil
default:
return 0, errors.Errorf("unsupported mapping from %+v to PoSt-specific RegisteredProof", p)
}
}
// SectorMaximumLifetime is the maximum duration a sector sealed with this proof may exist between activation and expiration
func (p RegisteredSealProof) SectorMaximumLifetime() ChainEpoch {
// For all Stacked DRG sectors, the max is 5 years
epochsPerYear := 1_262_277
fiveYears := 5 * epochsPerYear
return ChainEpoch(fiveYears)
}
///
/// Sealing
///
type SealRandomness Randomness
type InteractiveSealRandomness Randomness
// Information needed to verify a seal proof.
type SealVerifyInfo struct {
SealProof RegisteredSealProof
SectorID
DealIDs []DealID
Randomness SealRandomness
InteractiveRandomness InteractiveSealRandomness
Proof []byte
// Safe because we get those from the miner actor
SealedCID cid.Cid `checked:"true"` // CommR
UnsealedCID cid.Cid `checked:"true"` // CommD
}
///
/// PoSting
///
type PoStRandomness Randomness
// Information about a sector necessary for PoSt verification.
type SectorInfo struct {
SealProof RegisteredSealProof // RegisteredProof used when sealing - needs to be mapped to PoSt registered proof when used to verify a PoSt
SectorNumber SectorNumber
SealedCID cid.Cid // CommR
}
type PoStProof struct {
PoStProof RegisteredPoStProof
ProofBytes []byte
}
// Information needed to verify a Winning PoSt attached to a block header.
// Note: this is not used within the state machine, but by the consensus/election mechanisms.
type WinningPoStVerifyInfo struct {
Randomness PoStRandomness
Proofs []PoStProof
ChallengedSectors []SectorInfo
Prover ActorID // used to derive 32-byte prover ID
}
// Information needed to verify a Window PoSt submitted directly to a miner actor.
type WindowPoStVerifyInfo struct {
Randomness PoStRandomness
Proofs []PoStProof
ChallengedSectors []SectorInfo
Prover ActorID // used to derive 32-byte prover ID
}