-
Notifications
You must be signed in to change notification settings - Fork 211
/
warmup.go
65 lines (60 loc) · 1.4 KB
/
warmup.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
package atxsdata
import (
"context"
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)
func Warm(db *sql.Database, opts ...Opt) (*Data, error) {
cache := New(opts...)
tx, err := db.Tx(context.Background())
if err != nil {
return nil, err
}
defer tx.Release()
if err := Warmup(tx, cache); err != nil {
return nil, fmt.Errorf("warmup %w", err)
}
return cache, nil
}
func Warmup(db sql.Executor, cache *Data) error {
latest, err := atxs.LatestEpoch(db)
if err != nil {
return err
}
applied, err := layers.GetLastApplied(db)
if err != nil {
return err
}
cache.OnEpoch(applied.GetEpoch())
var ierr error
if err := atxs.IterateAtxs(db, cache.Evicted(), latest, func(vatx *types.VerifiedActivationTx) bool {
nonce, err := atxs.VRFNonce(db, vatx.SmesherID, vatx.TargetEpoch())
if err != nil {
ierr = fmt.Errorf("missing nonce %w", err)
return false
}
malicious, err := identities.IsMalicious(db, vatx.SmesherID)
if err != nil {
ierr = err
return false
}
cache.Add(
vatx.TargetEpoch(),
vatx.SmesherID,
vatx.ID(),
vatx.GetWeight(),
vatx.BaseTickHeight(),
vatx.TickHeight(),
nonce,
malicious,
)
return true
}); err != nil {
return err
}
return ierr
}