-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.go
227 lines (190 loc) · 5.47 KB
/
config.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
package config
import (
"encoding/hex"
"errors"
"fmt"
"math"
"os"
"path/filepath"
"github.com/spacemeshos/post/internal/postrs"
"github.com/spacemeshos/post/shared"
)
const (
DefaultDataDirName = "data"
DefaultComputeBatchSize = 1 << 20
MinBitsPerLabel = 1
MaxBitsPerLabel = 256
BitsPerLabel = 8 * postrs.LabelLength
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
defaultMaxFileSize = 4 * GiB
minFileSize = 1024
)
var DefaultDataDir string
func init() {
home, _ := os.UserHomeDir()
DefaultDataDir = filepath.Join(home, "post", DefaultDataDirName)
}
func BytesPerLabel() int {
return BitsPerLabel / 8
}
type PowFlags = postrs.PowFlags
const (
// Use the full dataset. AKA "Fast mode".
PowFastMode = postrs.PowFastMode
// Allocate memory in large pages.
PowLargePages = postrs.PowLargePages
// Use JIT compilation support.
PowJIT = postrs.PowJIT
// When combined with FLAG_JIT, the JIT pages are never writable and executable at the same time.
PowSecure = postrs.PowSecure
// Use hardware accelerated AES.
PowHardAES = postrs.PowHardAES
// Optimize Argon2 for CPUs with the SSSE3 instruction set.
PowArgon2SSSE3 = postrs.PowArgon2SSSE3
// Optimize Argon2 for CPUs with the SSSE3 instruction set.
PowArgon2AVX2 = postrs.PowArgon2AVX2
// Optimize Argon2 for CPUs without the AVX2 or SSSE3 instruction sets.
PowArgon2 = postrs.PowArgon2
)
func RecommendedPowFlags() PowFlags {
return postrs.GetRecommendedPowFlags()
}
func DefaultProvingPowFlags() PowFlags {
return RecommendedPowFlags() | PowFastMode
}
func DefaultVerifyingPowFlags() PowFlags {
return RecommendedPowFlags()
}
type Config postrs.Config
// MainnetConfig returns the default config for mainnet.
func MainnetConfig() Config {
cfg := Config{
MinNumUnits: 4,
MaxNumUnits: 1048576, // max post size 64 PiB
LabelsPerUnit: 4294967296, // 64GiB units
K1: 26,
K2: 37,
}
_, err := hex.Decode(cfg.PowDifficulty[:],
[]byte("000dfb23b0979b4b000000000000000000000000000000000000000000000000"),
)
if err != nil {
panic(err)
}
return cfg
}
// DefaultConfig returns the default config. These are intended for testing.
func DefaultConfig() Config {
cfg := Config{
MinNumUnits: 1,
MaxNumUnits: 100,
LabelsPerUnit: 512, // 8kB units
K1: 26,
K2: 37,
}
for i := range cfg.PowDifficulty {
cfg.PowDifficulty[i] = 0xFF
}
return cfg
}
func (c *Config) UnitSize() uint64 {
return c.LabelsPerUnit * uint64(BytesPerLabel())
}
type InitOpts struct {
DataDir string
NumUnits uint32
MaxFileSize uint64
ProviderID *uint32
Throttle bool
Scrypt ScryptParams
// ComputeBatchSize must be greater than 0
ComputeBatchSize uint64
// Index of the first file to init (inclusive)
FromFileIdx int
// Index of the last file to init (inclusive). Will init to the end of declared space if not provided.
ToFileIdx *int
}
func (o *InitOpts) MaxFileNumLabels() uint64 {
return o.MaxFileSize / uint64(BytesPerLabel())
}
func (o *InitOpts) TotalLabels(labelsPerUnit uint64) uint64 {
return uint64(o.NumUnits) * labelsPerUnit
}
func (o *InitOpts) TotalFiles(labelsPerUnit uint64) int {
return int(math.Ceil(float64(o.TotalLabels(labelsPerUnit)) / float64(o.MaxFileNumLabels())))
}
type ScryptParams struct {
N, R, P uint
}
func (p *ScryptParams) Validate() error {
if p.N == 0 {
return errors.New("scrypt parameter N cannot be 0")
}
if p.R == 0 {
return errors.New("scrypt parameter r cannot be 0")
}
if p.P == 0 {
return errors.New("scrypt parameter p cannot be 0")
}
return nil
}
func DefaultLabelParams() ScryptParams {
return ScryptParams{
N: 8192,
R: 1,
P: 1,
}
}
// MainnetInitOpts returns the default InitOpts for mainnet.
func MainnetInitOpts() InitOpts {
return InitOpts{
DataDir: DefaultDataDir,
NumUnits: 4,
MaxFileSize: defaultMaxFileSize,
Throttle: false,
Scrypt: DefaultLabelParams(),
ComputeBatchSize: DefaultComputeBatchSize,
}
}
// DefaultInitOpts returns the default InitOpts. These are intended for testing.
func DefaultInitOpts() InitOpts {
return InitOpts{
DataDir: DefaultDataDir,
NumUnits: 2,
MaxFileSize: defaultMaxFileSize,
Throttle: false,
Scrypt: DefaultLabelParams(),
ComputeBatchSize: DefaultComputeBatchSize,
}
}
func Validate(cfg Config, opts InitOpts) error {
if opts.NumUnits < cfg.MinNumUnits {
return fmt.Errorf("invalid `opts.NumUnits`; expected: >= %d, given: %d", cfg.MinNumUnits, opts.NumUnits)
}
if opts.NumUnits > cfg.MaxNumUnits {
return fmt.Errorf("invalid `opts.NumUnits`; expected: <= %d, given: %d", cfg.MaxNumUnits, opts.NumUnits)
}
if opts.MaxFileSize < minFileSize {
return fmt.Errorf("invalid `opts.MaxFileSize`; expected: >= %d, given: %d", minFileSize, opts.MaxFileSize)
}
if opts.ComputeBatchSize == 0 {
return fmt.Errorf("invalid `opts.ComputeBatchSize` expected: > 0, given: %d", opts.ComputeBatchSize)
}
if res := shared.Uint64MulOverflow(cfg.LabelsPerUnit, uint64(opts.NumUnits)); res {
return fmt.Errorf("uint64 overflow: `cfg.LabelsPerUnit` (%v) * `opts.NumUnits` (%v) exceeds uint64",
cfg.LabelsPerUnit, opts.NumUnits,
)
}
numLabels := cfg.LabelsPerUnit * uint64(opts.NumUnits)
if res := shared.Uint64MulOverflow(numLabels, uint64(cfg.K1)); res {
return fmt.Errorf("uint64 overflow: `cfg.LabelsPerUnit` * `opts.NumUnits` (%v) * `cfg.K1` (%v) exceeds uint64",
numLabels, cfg.K1,
)
}
if err := opts.Scrypt.Validate(); err != nil {
return err
}
return nil
}