-
Notifications
You must be signed in to change notification settings - Fork 210
/
generator.go
329 lines (301 loc) · 7.91 KB
/
generator.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
package blocks
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/hare"
heligibility "github.com/spacemeshos/go-spacemesh/hare/eligibility"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql/layers"
dbproposals "github.com/spacemeshos/go-spacemesh/sql/proposals"
"github.com/spacemeshos/go-spacemesh/system"
)
var errInvalidATXID = errors.New("proposal ATXID invalid")
// Generator generates a block from proposals.
type Generator struct {
logger log.Log
cfg Config
once sync.Once
eg errgroup.Group
ctx context.Context
cancel func()
cdb *datastore.CachedDB
msh meshProvider
executor executor
fetcher system.ProposalFetcher
cert certifier
patrol layerPatrol
hareCh chan hare.LayerOutput
optimisticOutput map[types.LayerID]*proposalMetadata
}
// Config is the config for Generator.
type Config struct {
LayerSize uint32
LayersPerEpoch uint32
GenBlockInterval time.Duration
BlockGasLimit uint64
OptFilterThreshold int
}
func defaultConfig() Config {
return Config{
LayerSize: 50,
LayersPerEpoch: 3,
GenBlockInterval: time.Second,
BlockGasLimit: math.MaxUint64,
OptFilterThreshold: 90,
}
}
// GeneratorOpt for configuring Generator.
type GeneratorOpt func(*Generator)
// WithContext modifies default context.
func WithContext(ctx context.Context) GeneratorOpt {
return func(g *Generator) {
g.ctx = ctx
}
}
// WithConfig defines cfg for Generator.
func WithConfig(cfg Config) GeneratorOpt {
return func(g *Generator) {
g.cfg = cfg
}
}
// WithGeneratorLogger defines logger for Generator.
func WithGeneratorLogger(logger log.Log) GeneratorOpt {
return func(g *Generator) {
g.logger = logger
}
}
// WithHareOutputChan sets the chan to listen to hare output.
func WithHareOutputChan(ch chan hare.LayerOutput) GeneratorOpt {
return func(g *Generator) {
g.hareCh = ch
}
}
// NewGenerator creates new block generator.
func NewGenerator(
cdb *datastore.CachedDB,
exec executor,
m meshProvider,
f system.ProposalFetcher,
c certifier,
p layerPatrol,
opts ...GeneratorOpt,
) *Generator {
g := &Generator{
logger: log.NewNop(),
cfg: defaultConfig(),
ctx: context.Background(),
cdb: cdb,
msh: m,
executor: exec,
fetcher: f,
cert: c,
patrol: p,
optimisticOutput: map[types.LayerID]*proposalMetadata{},
}
for _, opt := range opts {
opt(g)
}
g.ctx, g.cancel = context.WithCancel(g.ctx)
return g
}
// Start starts listening to hare output.
func (g *Generator) Start() {
g.once.Do(func() {
g.eg.Go(func() error {
return g.run()
})
})
}
// Stop stops listening to hare output.
func (g *Generator) Stop() {
g.cancel()
err := g.eg.Wait()
if err != nil && !errors.Is(err, context.Canceled) {
g.logger.With().Error("blockGen task failure", log.Err(err))
}
}
func (g *Generator) run() error {
var maxLayer types.LayerID
for {
select {
case <-g.ctx.Done():
return fmt.Errorf("context done: %w", g.ctx.Err())
case out := <-g.hareCh:
g.logger.With().Debug("received hare output",
log.Context(out.Ctx),
out.Layer,
log.Int("num_proposals", len(out.Proposals)),
)
maxLayer = types.MaxLayer(maxLayer, out.Layer)
_, err := g.processHareOutput(out)
if err != nil {
g.logger.With().Error("failed to process hare output",
log.Context(out.Ctx),
out.Layer,
log.Err(err),
)
}
if len(g.optimisticOutput) > 0 {
g.processOptimisticLayers(maxLayer)
}
case <-time.After(g.cfg.GenBlockInterval):
if len(g.optimisticOutput) > 0 {
g.processOptimisticLayers(maxLayer)
}
}
}
}
func (g *Generator) getProposals(pids []types.ProposalID) ([]*types.Proposal, error) {
var (
result = make([]*types.Proposal, 0, len(pids))
p *types.Proposal
err error
)
for _, pid := range pids {
if p, err = dbproposals.Get(g.cdb, pid); err != nil {
return nil, err
}
result = append(result, p)
}
return result, nil
}
func (g *Generator) processHareOutput(out hare.LayerOutput) (*types.Block, error) {
var md *proposalMetadata
if len(out.Proposals) > 0 {
getMetadata := func() error {
// fetch proposals from peers if not locally available
if err := g.fetcher.GetProposals(out.Ctx, out.Proposals); err != nil {
failFetchCnt.Inc()
return fmt.Errorf("preprocess fetch layer %d proposals: %w", out.Layer, err)
}
// now all proposals should be in local DB
props, err := g.getProposals(out.Proposals)
if err != nil {
failErrCnt.Inc()
return fmt.Errorf("preprocess get layer %d proposals: %w", out.Layer, err)
}
md, err = getProposalMetadata(out.Ctx, g.logger, g.cdb, g.cfg, out.Layer, props)
if err != nil {
return err
}
return nil
}
if err := getMetadata(); err != nil {
g.patrol.CompleteHare(out.Layer)
return nil, err
}
}
if md != nil && md.optFilter {
g.optimisticOutput[out.Layer] = md
return nil, nil
}
defer g.patrol.CompleteHare(out.Layer)
var (
block *types.Block
hareOutput types.BlockID
)
if md != nil {
block = &types.Block{
InnerBlock: types.InnerBlock{
LayerIndex: md.lid,
TickHeight: md.tickHeight,
Rewards: md.rewards,
TxIDs: md.tids,
},
}
block.Initialize()
hareOutput = block.ID()
g.logger.With().Info("generated block", out.Layer, block.ID())
}
if err := g.saveAndCertify(out.Ctx, out.Layer, block); err != nil {
return block, err
}
if err := g.msh.ProcessLayerPerHareOutput(out.Ctx, out.Layer, hareOutput, false); err != nil {
return block, err
}
return block, nil
}
func (g *Generator) processOptimisticLayers(max types.LayerID) {
lastApplied, err := layers.GetLastApplied(g.cdb)
if err != nil {
g.logger.Error("failed to get latest applied layer", log.Err(err))
return
}
next := lastApplied.Add(1)
for lid := next; lid <= max; lid++ {
md, ok := g.optimisticOutput[lid]
if !ok {
return
}
delete(g.optimisticOutput, lid)
doit := func() error {
defer g.patrol.CompleteHare(lid)
block, err := g.genBlockOptimistic(md.ctx, md)
if err != nil {
failGenCnt.Inc()
return err
}
g.logger.With().Info("generated block (optimistic)", lid, block.ID())
if err = g.msh.ProcessLayerPerHareOutput(md.ctx, lid, block.ID(), true); err != nil {
return err
}
return nil
}
if err = doit(); err != nil {
g.logger.With().Error("failed to process optimistic layer",
log.Context(md.ctx),
lid,
log.Err(err),
)
return
}
}
}
func (g *Generator) saveAndCertify(ctx context.Context, lid types.LayerID, block *types.Block) error {
hareOutput := types.EmptyBlockID
if block != nil {
if err := g.msh.AddBlockWithTXs(ctx, block); err != nil {
failErrCnt.Inc()
return fmt.Errorf("post process add block: %w", err)
}
blockOkCnt.Inc()
hareOutput = block.ID()
} else {
emptyOutputCnt.Inc()
}
if err := g.cert.RegisterForCert(ctx, lid, hareOutput); err != nil {
g.logger.With().Warning("failed to register hare output for certifying",
log.Context(ctx),
lid,
hareOutput,
log.Err(err),
)
}
if err := g.cert.CertifyIfEligible(ctx, g.logger, lid, hareOutput); err != nil && !errors.Is(err, heligibility.ErrNotActive) {
g.logger.With().Warning("failed to certify block",
log.Context(ctx),
lid,
hareOutput,
log.Err(err),
)
}
return nil
}
func (g *Generator) genBlockOptimistic(ctx context.Context, md *proposalMetadata) (*types.Block, error) {
block, err := g.executor.ExecuteOptimistic(ctx, md.lid, md.tickHeight, md.rewards, md.tids)
if err != nil {
failGenCnt.Inc()
return nil, fmt.Errorf("execute in situ: %w", err)
}
if err = g.saveAndCertify(ctx, md.lid, block); err != nil {
return nil, fmt.Errorf("post-process block (optimistic): %w", err)
}
return block, nil
}