-
Notifications
You must be signed in to change notification settings - Fork 211
/
generator.go
327 lines (300 loc) · 7.87 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
package blocks
import (
"context"
"errors"
"fmt"
"math"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/hare"
"github.com/spacemeshos/go-spacemesh/hare/eligibility"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/sql/proposals"
"github.com/spacemeshos/go-spacemesh/system"
)
// Generator generates a block from proposals.
type Generator struct {
logger log.Log
cfg Config
once sync.Once
eg errgroup.Group
stop func()
db *sql.Database
atxs *atxsdata.Data
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 {
GenBlockInterval time.Duration
BlockGasLimit uint64
OptFilterThreshold int
}
func defaultConfig() Config {
return Config{
GenBlockInterval: time.Second,
BlockGasLimit: math.MaxUint64,
OptFilterThreshold: 90,
}
}
// GeneratorOpt for configuring Generator.
type GeneratorOpt func(*Generator)
// 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(
db *sql.Database,
atxs *atxsdata.Data,
exec executor,
m meshProvider,
f system.ProposalFetcher,
c certifier,
p layerPatrol,
opts ...GeneratorOpt,
) *Generator {
g := &Generator{
logger: log.NewNop(),
cfg: defaultConfig(),
db: db,
atxs: atxs,
msh: m,
executor: exec,
fetcher: f,
cert: c,
patrol: p,
optimisticOutput: map[types.LayerID]*proposalMetadata{},
}
for _, opt := range opts {
opt(g)
}
return g
}
// Start starts listening to hare output.
func (g *Generator) Start(ctx context.Context) {
g.once.Do(func() {
ctx, g.stop = context.WithCancel(ctx)
g.eg.Go(func() error {
return g.run(ctx)
})
})
}
// Stop stops listening to hare output.
func (g *Generator) Stop() {
g.stop()
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(ctx context.Context) error {
var maxLayer types.LayerID
for {
select {
case <-ctx.Done():
return fmt.Errorf("context done: %w", 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 = max(maxLayer, out.Layer)
_, err := g.processHareOutput(out)
if err != nil {
if errors.Is(err, errNodeHasBadMeshHash) {
g.logger.With().Info("node has different mesh hash from majority, will download block instead",
log.Context(out.Ctx),
out.Layer,
log.Err(err),
)
} else {
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 = proposals.Get(g.db, 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.db, g.atxs, 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.db)
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, lid, hareOutput); err != nil && !errors.Is(err, eligibility.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
}