-
Notifications
You must be signed in to change notification settings - Fork 211
/
post.go
499 lines (434 loc) · 15.2 KB
/
post.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package activation
import (
"context"
"encoding/hex"
"errors"
"fmt"
"runtime"
"sync"
"github.com/spacemeshos/post/config"
"github.com/spacemeshos/post/initialization"
"github.com/spacemeshos/post/proving"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
)
// PostSetupProvider represent a compute provider for Post setup data creation.
type PostSetupProvider initialization.Provider
// PostConfig is the configuration of the Post protocol, used for data creation, proofs generation and validation.
type PostConfig struct {
MinNumUnits uint32 `mapstructure:"post-min-numunits"`
MaxNumUnits uint32 `mapstructure:"post-max-numunits"`
LabelsPerUnit uint64 `mapstructure:"post-labels-per-unit"`
K1 uint32 `mapstructure:"post-k1"`
K2 uint32 `mapstructure:"post-k2"`
K3 uint32 `mapstructure:"post-k3"`
PowDifficulty PowDifficulty `mapstructure:"post-pow-difficulty"`
// Since when to include the miner ID in the K2 pow.
MinerIDInK2PowSinceEpoch uint32 `mapstructure:"post-minerid-in-k2-pow-since-epoch"`
}
func (c PostConfig) ToConfig() config.Config {
return config.Config{
MinNumUnits: c.MinNumUnits,
MaxNumUnits: c.MaxNumUnits,
LabelsPerUnit: c.LabelsPerUnit,
K1: c.K1,
K2: c.K2,
K3: c.K3,
PowDifficulty: [32]byte(c.PowDifficulty),
}
}
type PowDifficulty [32]byte
func (d PowDifficulty) String() string {
return fmt.Sprintf("%X", d[:])
}
// Set implements pflag.Value.Set.
func (f *PowDifficulty) Set(value string) error {
return f.UnmarshalText([]byte(value))
}
// Type implements pflag.Value.Type.
func (PowDifficulty) Type() string {
return "PowDifficulty"
}
func (d *PowDifficulty) UnmarshalText(text []byte) error {
decodedLen := hex.DecodedLen(len(text))
if decodedLen != 32 {
return fmt.Errorf("expected 32 bytes, got %d", decodedLen)
}
var dst [32]byte
if _, err := hex.Decode(dst[:], text); err != nil {
return err
}
*d = PowDifficulty(dst)
return nil
}
// PostSetupOpts are the options used to initiate a Post setup data creation session,
// either via the public smesher API, or on node launch (via cmd args).
type PostSetupOpts struct {
DataDir string `mapstructure:"smeshing-opts-datadir"`
NumUnits uint32 `mapstructure:"smeshing-opts-numunits"`
MaxFileSize uint64 `mapstructure:"smeshing-opts-maxfilesize"`
ProviderID int `mapstructure:"smeshing-opts-provider"`
Throttle bool `mapstructure:"smeshing-opts-throttle"`
Scrypt config.ScryptParams `mapstructure:"smeshing-opts-scrypt"`
ComputeBatchSize uint64 `mapstructure:"smeshing-opts-compute-batch-size"`
}
// PostProvingOpts are the options controlling POST proving process.
type PostProvingOpts struct {
// Number of threads used in POST proving process.
Threads uint `mapstructure:"smeshing-opts-proving-threads"`
// Number of nonces tried in parallel in POST proving process.
Nonces uint `mapstructure:"smeshing-opts-proving-nonces"`
// Flags used in the PoW computation.
Flags config.PowFlags `mapstructure:"smeshing-opts-proving-powflags"`
}
func DefaultPostProvingOpts() PostProvingOpts {
return PostProvingOpts{
Threads: 1,
Nonces: 16,
Flags: config.DefaultProvingPowFlags(),
}
}
// PostProvingOpts are the options controlling POST proving process.
type PostProofVerifyingOpts struct {
// Number of workers spawned to verify proofs.
Workers int `mapstructure:"smeshing-opts-verifying-workers"`
// Flags used for the PoW verification.
Flags config.PowFlags `mapstructure:"smeshing-opts-verifying-powflags"`
}
func DefaultPostVerifyingOpts() PostProofVerifyingOpts {
workers := runtime.NumCPU() * 3 / 4
if workers < 1 {
workers = 1
}
return PostProofVerifyingOpts{
Workers: workers,
Flags: config.DefaultVerifyingPowFlags(),
}
}
// PostSetupStatus represents a status snapshot of the Post setup.
type PostSetupStatus struct {
State PostSetupState
NumLabelsWritten uint64
LastOpts *PostSetupOpts
}
type PostSetupState int32
const (
PostSetupStateNotStarted PostSetupState = 1 + iota
PostSetupStatePrepared
PostSetupStateInProgress
PostSetupStateStopped
PostSetupStateComplete
PostSetupStateError
)
var (
errNotComplete = errors.New("not complete")
errNotStarted = errors.New("not started")
)
// DefaultPostConfig defines the default configuration for Post.
func DefaultPostConfig() PostConfig {
cfg := config.DefaultConfig()
return PostConfig{
MinNumUnits: cfg.MinNumUnits,
MaxNumUnits: cfg.MaxNumUnits,
LabelsPerUnit: cfg.LabelsPerUnit,
K1: cfg.K1,
K2: cfg.K2,
K3: cfg.K3,
PowDifficulty: PowDifficulty(cfg.PowDifficulty),
}
}
// DefaultPostSetupOpts defines the default options for Post setup.
func DefaultPostSetupOpts() PostSetupOpts {
return (PostSetupOpts)(config.DefaultInitOpts())
}
// PostSetupManager implements the PostProvider interface.
type PostSetupManager struct {
id types.NodeID
commitmentAtxId types.ATXID
cfg PostConfig
logger log.Log
db *datastore.CachedDB
goldenATXID types.ATXID
mu sync.Mutex // mu protects setting the values below.
lastOpts *PostSetupOpts // the last options used to initiate a Post setup session.
state PostSetupState // state is the current state of the Post setup.
init *initialization.Initializer // init is the current initializer instance.
provingOpts PostProvingOpts
}
// NewPostSetupManager creates a new instance of PostSetupManager.
func NewPostSetupManager(id types.NodeID, cfg PostConfig, logger log.Log, db *datastore.CachedDB, goldenATXID types.ATXID, provingOpts PostProvingOpts) (*PostSetupManager, error) {
mgr := &PostSetupManager{
id: id,
cfg: cfg,
logger: logger,
db: db,
goldenATXID: goldenATXID,
state: PostSetupStateNotStarted,
provingOpts: provingOpts,
}
return mgr, nil
}
// Status returns the setup current status.
func (mgr *PostSetupManager) Status() *PostSetupStatus {
mgr.mu.Lock()
defer mgr.mu.Unlock()
switch mgr.state {
case PostSetupStateNotStarted:
return &PostSetupStatus{
State: mgr.state,
}
case PostSetupStateError:
return &PostSetupStatus{
State: mgr.state,
}
default:
return &PostSetupStatus{
State: mgr.state,
NumLabelsWritten: mgr.init.NumLabelsWritten(),
LastOpts: mgr.lastOpts,
}
}
}
// Providers returns a list of available compute providers for Post setup.
func (*PostSetupManager) Providers() ([]PostSetupProvider, error) {
providers, err := initialization.OpenCLProviders()
if err != nil {
return nil, err
}
providersAlias := make([]PostSetupProvider, len(providers))
for i, p := range providers {
providersAlias[i] = PostSetupProvider(p)
}
return providersAlias, nil
}
// BestProvider returns the most performant compute provider based on a short benchmarking session.
func (mgr *PostSetupManager) BestProvider() (*PostSetupProvider, error) {
providers, err := mgr.Providers()
if err != nil {
return nil, fmt.Errorf("fetch best provider: %w", err)
}
var bestProvider PostSetupProvider
var maxHS int
for _, p := range providers {
hs, err := mgr.Benchmark(p)
if err != nil {
return nil, err
}
if hs > maxHS {
maxHS = hs
bestProvider = p
}
}
return &bestProvider, nil
}
// Benchmark runs a short benchmarking session for a given provider to evaluate its performance.
func (mgr *PostSetupManager) Benchmark(p PostSetupProvider) (int, error) {
score, err := initialization.Benchmark(initialization.Provider(p))
if err != nil {
return score, fmt.Errorf("benchmark GPU: %w", err)
}
return score, nil
}
// StartSession starts (or continues) a PoST session. It supports resuming a
// previously started session, and will return an error if a session is already
// in progress. It must be ensured that PrepareInitializer is called once
// before each call to StartSession and that the node is ATX synced.
func (mgr *PostSetupManager) StartSession(ctx context.Context) error {
// Ensure only one goroutine can execute initialization at a time.
err := func() error {
mgr.mu.Lock()
defer mgr.mu.Unlock()
if mgr.state != PostSetupStatePrepared {
return fmt.Errorf("post session not prepared")
}
mgr.state = PostSetupStateInProgress
return nil
}()
if err != nil {
return err
}
mgr.logger.With().Info("post setup session starting",
log.String("node_id", mgr.id.String()),
log.String("commitment_atx", mgr.commitmentAtxId.String()),
log.String("data_dir", mgr.lastOpts.DataDir),
log.String("num_units", fmt.Sprintf("%d", mgr.lastOpts.NumUnits)),
log.String("labels_per_unit", fmt.Sprintf("%d", mgr.cfg.LabelsPerUnit)),
log.String("provider", fmt.Sprintf("%d", mgr.lastOpts.ProviderID)),
)
events.EmitInitStart(mgr.id, mgr.commitmentAtxId)
err = mgr.init.Initialize(ctx)
events.EmitInitComplete(err != nil)
mgr.mu.Lock()
defer mgr.mu.Unlock()
switch {
case errors.Is(err, context.Canceled):
mgr.logger.Info("post setup session was stopped")
mgr.state = PostSetupStateStopped
return err
case err != nil:
mgr.logger.With().Error("post setup session failed", log.Err(err))
mgr.state = PostSetupStateError
return err
}
mgr.logger.With().Info("post setup completed",
log.String("node_id", mgr.id.String()),
log.String("commitment_atx", mgr.commitmentAtxId.String()),
log.String("data_dir", mgr.lastOpts.DataDir),
log.String("num_units", fmt.Sprintf("%d", mgr.lastOpts.NumUnits)),
log.String("labels_per_unit", fmt.Sprintf("%d", mgr.cfg.LabelsPerUnit)),
log.String("provider", fmt.Sprintf("%d", mgr.lastOpts.ProviderID)),
)
mgr.state = PostSetupStateComplete
return nil
}
// PrepareInitializer prepares the initializer to begin the initialization
// process, it needs to be called before each call to StartSession. Having this
// function be separate from StartSession provides a means to understand if the
// post configuration is valid before kicking off a very long running task
// (StartSession can take days to complete). After the first call to this
// method subsequent calls to this method will return an error until
// StartSession has completed execution.
func (mgr *PostSetupManager) PrepareInitializer(ctx context.Context, opts PostSetupOpts) error {
mgr.mu.Lock()
defer mgr.mu.Unlock()
if mgr.state == PostSetupStatePrepared || mgr.state == PostSetupStateInProgress {
return fmt.Errorf("post setup session in progress")
}
if opts.ProviderID == config.BestProviderID {
p, err := mgr.BestProvider()
if err != nil {
return err
}
mgr.logger.Info("found best compute provider: id: %d, model: %v, device type: %v", p.ID, p.Model, p.DeviceType)
opts.ProviderID = int(p.ID)
}
var err error
mgr.commitmentAtxId, err = mgr.commitmentAtx(ctx, opts.DataDir)
if err != nil {
return err
}
newInit, err := initialization.NewInitializer(
initialization.WithNodeId(mgr.id.Bytes()),
initialization.WithCommitmentAtxId(mgr.commitmentAtxId.Bytes()),
initialization.WithConfig(mgr.cfg.ToConfig()),
initialization.WithInitOpts(config.InitOpts(opts)),
initialization.WithLogger(mgr.logger.Zap()),
)
if err != nil {
mgr.state = PostSetupStateError
return fmt.Errorf("new initializer: %w", err)
}
mgr.state = PostSetupStatePrepared
mgr.init = newInit
mgr.lastOpts = &opts
return nil
}
func (mgr *PostSetupManager) CommitmentAtx() (types.ATXID, error) {
mgr.mu.Lock()
defer mgr.mu.Unlock()
if mgr.commitmentAtxId != types.EmptyATXID {
return mgr.commitmentAtxId, nil
}
return types.EmptyATXID, errNotStarted
}
func (mgr *PostSetupManager) commitmentAtx(ctx context.Context, dataDir string) (types.ATXID, error) {
m, err := initialization.LoadMetadata(dataDir)
switch {
case err == nil:
return types.ATXID(types.BytesToHash(m.CommitmentAtxId)), nil
case errors.Is(err, initialization.ErrStateMetadataFileMissing):
// if this node has already published an ATX, get its initial ATX and from it the commitment ATX
atxId, err := atxs.GetFirstIDByNodeID(mgr.db, mgr.id)
if err == nil {
atx, err := atxs.Get(mgr.db, atxId)
if err != nil {
return types.EmptyATXID, err
}
if atx.CommitmentATX == nil {
return types.EmptyATXID, fmt.Errorf("initial ATX %s does not contain a commitment ATX", atxId)
}
return *atx.CommitmentATX, nil
}
// if this node has not published an ATX select the best ATX with `findCommitmentAtx`
return mgr.findCommitmentAtx(ctx)
default:
return types.EmptyATXID, fmt.Errorf("load metadata: %w", err)
}
}
// findCommitmentAtx determines the best commitment ATX to use for the node.
// It will use the ATX with the highest height seen by the node and defaults to the goldenATX,
// when no ATXs have yet been published.
func (mgr *PostSetupManager) findCommitmentAtx(ctx context.Context) (types.ATXID, error) {
atx, err := atxs.GetIDWithMaxHeight(mgr.db, types.EmptyNodeID)
switch {
case errors.Is(err, sql.ErrNotFound):
mgr.logger.With().Info("using golden atx as commitment atx")
return mgr.goldenATXID, nil
case err != nil:
return types.EmptyATXID, fmt.Errorf("get commitment atx: %w", err)
default:
return atx, nil
}
}
// Reset deletes the data file(s).
func (mgr *PostSetupManager) Reset() error {
mgr.mu.Lock()
defer mgr.mu.Unlock()
if err := mgr.init.Reset(); err != nil {
return fmt.Errorf("reset: %w", err)
}
// Reset internal state.
mgr.state = PostSetupStateNotStarted
return nil
}
// GenerateProof generates a new Post.
func (mgr *PostSetupManager) GenerateProof(ctx context.Context, challenge []byte, options ...proving.OptionFunc) (*types.Post, *types.PostMetadata, error) {
mgr.mu.Lock()
if mgr.state != PostSetupStateComplete {
mgr.mu.Unlock()
return nil, nil, errNotComplete
}
mgr.mu.Unlock()
opts := []proving.OptionFunc{
proving.WithDataSource(mgr.cfg.ToConfig(), mgr.id.Bytes(), mgr.commitmentAtxId.Bytes(), mgr.lastOpts.DataDir),
proving.WithNonces(mgr.provingOpts.Nonces),
proving.WithThreads(mgr.provingOpts.Threads),
proving.WithPowFlags(mgr.provingOpts.Flags),
}
opts = append(opts, options...)
proof, proofMetadata, err := proving.Generate(ctx, challenge, mgr.cfg.ToConfig(), mgr.logger.Zap(), opts...)
if err != nil {
return nil, nil, fmt.Errorf("generate proof: %w", err)
}
p := (*types.Post)(proof)
m := &types.PostMetadata{
Challenge: proofMetadata.Challenge,
LabelsPerUnit: proofMetadata.LabelsPerUnit,
}
return p, m, nil
}
// VRFNonce returns the VRF nonce found during initialization.
func (mgr *PostSetupManager) VRFNonce() (*types.VRFPostIndex, error) {
mgr.mu.Lock()
defer mgr.mu.Unlock()
if mgr.state != PostSetupStateComplete {
return nil, errNotComplete
}
return (*types.VRFPostIndex)(mgr.init.Nonce()), nil
}
// LastOpts returns the Post setup last session options.
func (mgr *PostSetupManager) LastOpts() *PostSetupOpts {
mgr.mu.Lock()
defer mgr.mu.Unlock()
return mgr.lastOpts
}
// Config returns the Post protocol config.
func (mgr *PostSetupManager) Config() PostConfig {
mgr.mu.Lock()
defer mgr.mu.Unlock()
return mgr.cfg
}