-
Notifications
You must be signed in to change notification settings - Fork 211
/
generator.go
295 lines (266 loc) · 7.6 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
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"
"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
hareCh chan hare.LayerOutput
hareOutputs map[types.LayerID]hare.LayerOutput
}
// 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,
opts ...GeneratorOpt,
) *Generator {
g := &Generator{
logger: log.NewNop(),
cfg: defaultConfig(),
ctx: context.Background(),
cdb: cdb,
msh: m,
executor: exec,
fetcher: f,
cert: c,
hareOutputs: map[types.LayerID]hare.LayerOutput{},
}
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 {
for {
select {
case <-g.ctx.Done():
return fmt.Errorf("context done: %w", g.ctx.Err())
case out := <-g.hareCh:
g.logger.WithContext(out.Ctx).With().Info("received hare output",
out.Layer,
log.Int("num_proposals", len(out.Proposals)))
g.hareOutputs[out.Layer] = out
g.tryGenBlock()
case <-time.After(g.cfg.GenBlockInterval):
if len(g.hareOutputs) > 0 {
g.tryGenBlock()
}
}
}
}
func (g *Generator) tryGenBlock() {
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 {
if out, ok := g.hareOutputs[next]; !ok {
break
} else {
g.logger.WithContext(out.Ctx).With().Info("ready to process hare output", next)
_ = g.processHareOutput(out)
delete(g.hareOutputs, next)
next = next.Add(1)
}
}
}
func (g *Generator) getProposals(pids []types.ProposalID) ([]*types.Proposal, error) {
result := make([]*types.Proposal, 0, len(pids))
var (
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) error {
ctx := out.Ctx
logger := g.logger.WithContext(ctx).WithFields(out.Layer)
hareOutput := types.EmptyBlockID
var (
block *types.Block
executed bool
)
if len(out.Proposals) > 0 {
// fetch proposals from peers if not locally available
if err := g.fetcher.GetProposals(ctx, out.Proposals); err != nil {
failFetchCnt.Inc()
logger.With().Error("failed to fetch proposals", log.Err(err))
return err
}
// now all proposals should be in local DB
props, err := g.getProposals(out.Proposals)
if err != nil {
failErrCnt.Inc()
logger.With().Warning("failed to get proposals locally", log.Err(err))
return err
}
block, executed, err = g.generateBlock(ctx, logger, out.Layer, props)
if err != nil {
logger.With().Error("failed to generate block", log.Err(err))
failGenCnt.Inc()
return err
} else if err = g.msh.AddBlockWithTXs(ctx, block); err != nil {
logger.With().Error("failed to add block", log.Err(err))
failErrCnt.Inc()
return err
} else {
blockOkCnt.Inc()
hareOutput = block.ID()
}
} else {
emptyOutputCnt.Inc()
}
if err := g.cert.RegisterForCert(ctx, out.Layer, hareOutput); err != nil {
logger.With().Warning("failed to register hare output for certifying", log.Err(err))
}
if err := g.cert.CertifyIfEligible(ctx, logger.WithFields(hareOutput), out.Layer, hareOutput); err != nil {
logger.With().Warning("failed to certify block", hareOutput, log.Err(err))
}
if err := g.msh.ProcessLayerPerHareOutput(ctx, out.Layer, hareOutput, executed); err != nil {
logger.With().Error("mesh failed to process layer", log.Err(err))
return err
}
return nil
}
// generateBlock combines the transactions in the proposals and put them in a stable order.
// if optimistic filtering is ON, it also executes the transactions in situ.
// else, prune the transactions up to block gas limit allows.
func (g *Generator) generateBlock(ctx context.Context, logger log.Log, lid types.LayerID, proposals []*types.Proposal) (*types.Block, bool, error) {
md, err := getProposalMetadata(logger, g.cdb, g.cfg, lid, proposals)
if err != nil {
return nil, false, fmt.Errorf("collect proposal metadata: %w", err)
}
if md.optFilter {
return g.genBlockOptimistic(ctx, logger, md)
}
var tids []types.TransactionID
if len(md.mtxs) > 0 {
blockSeed := types.CalcProposalsHash32(types.ToProposalIDs(proposals), nil).Bytes()
tids, err = getBlockTXs(logger, md, blockSeed, g.cfg.BlockGasLimit)
if err != nil {
return nil, false, err
}
}
b := &types.Block{
InnerBlock: types.InnerBlock{
LayerIndex: lid,
TickHeight: md.tickHeight,
Rewards: md.rewards,
TxIDs: tids,
},
}
b.Initialize()
logger.With().Info("block generated", log.Inline(b))
return b, false, nil
}
func (g *Generator) genBlockOptimistic(ctx context.Context, logger log.Log, md *proposalMetadata) (*types.Block, bool, error) {
var (
tids []types.TransactionID
err error
)
if len(md.mtxs) > 0 {
blockSeed := types.CalcProposalsHash32(types.ToProposalIDs(md.proposals), nil).Bytes()
tids, err = getBlockTXs(logger, md, blockSeed, 0)
if err != nil {
return nil, false, err
}
}
logger.With().Info("executing txs in situ", log.Int("num_txs", len(tids)))
block, err := g.executor.ExecuteOptimistic(ctx, md.lid, md.tickHeight, md.rewards, tids)
if err != nil {
return nil, false, fmt.Errorf("execute in situ: %w", err)
}
logger.With().Info("block generated and executed", log.Inline(block))
return block, true, nil
}