-
Notifications
You must be signed in to change notification settings - Fork 211
/
block_builder.go
388 lines (343 loc) · 11.3 KB
/
block_builder.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
// Package miner is responsible for creating valid blocks that contain valid activation transactions and transactions
package miner
import (
"context"
"crypto/md5"
"encoding/binary"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/blocks"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/miner/metrics"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
)
const (
defaultGasLimit = 10
defaultFee = 1
)
// AtxsPerBlockLimit indicates the maximum number of atxs a block can reference.
const AtxsPerBlockLimit = 100
const blockBuildDurationErrorThreshold = 10 * time.Second
type signer interface {
Sign(m []byte) []byte
}
type syncer interface {
IsSynced(context.Context) bool
}
type txPool interface {
GetTxsForBlock(numOfTxs int, getState func(addr types.Address) (nonce, balance uint64, err error)) ([]types.TransactionID, []*types.Transaction, error)
}
type projector interface {
GetProjection(types.Address) (nonce, balance uint64, err error)
}
type blockOracle interface {
BlockEligible(types.LayerID) (types.ATXID, []types.BlockEligibilityProof, []types.ATXID, error)
}
type baseBlockProvider interface {
BaseBlock(context.Context) (types.BlockID, [][]types.BlockID, error)
}
// BlockBuilder is the struct that orchestrates the building of blocks, it is responsible for receiving hare results.
// referencing txs and atxs from mem pool and referencing them in the created block
// it is also responsible for listening to the clock and querying when a block should be created according to the block oracle.
type BlockBuilder struct {
log.Log
signer
minerID types.NodeID
rnd *rand.Rand
beginRoundEvent chan types.LayerID
stopChan chan struct{}
TransactionPool txPool
mu sync.Mutex
publisher pubsub.Publisher
meshProvider meshProvider
baseBlockP baseBlockProvider
blockOracle blockOracle
beaconProvider blocks.BeaconGetter
syncer syncer
eg errgroup.Group
started bool
atxsPerBlock int // number of atxs to select per block
txsPerBlock int // max number of tx to select per block
projector projector
db database.Database
layersPerEpoch uint32
}
// Config is the block builders configuration struct.
type Config struct {
DBPath string
MinerID types.NodeID
AtxsPerBlock int
LayersPerEpoch uint32
TxsPerBlock int
}
// NewBlockBuilder creates a struct of block builder type.
func NewBlockBuilder(
config Config,
sgn signer,
publisher pubsub.Publisher,
beginRoundEvent chan types.LayerID,
msh meshProvider,
bbp baseBlockProvider,
blockOracle blockOracle,
beaconProvider blocks.BeaconGetter,
syncer syncer,
projector projector,
txPool txPool,
logger log.Log,
) *BlockBuilder {
seed := binary.BigEndian.Uint64(md5.New().Sum([]byte(config.MinerID.Key)))
var db *database.LDBDatabase
if len(config.DBPath) == 0 {
db = database.NewMemDatabase()
} else {
var err error
db, err = database.NewLDBDatabase(config.DBPath, 16, 16, logger)
if err != nil {
logger.With().Panic("cannot create block builder database", log.Err(err))
}
}
return &BlockBuilder{
minerID: config.MinerID,
signer: sgn,
Log: logger,
rnd: rand.New(rand.NewSource(int64(seed))),
beginRoundEvent: beginRoundEvent,
stopChan: make(chan struct{}),
mu: sync.Mutex{},
publisher: publisher,
meshProvider: msh,
baseBlockP: bbp,
blockOracle: blockOracle,
beaconProvider: beaconProvider,
syncer: syncer,
started: false,
atxsPerBlock: config.AtxsPerBlock,
txsPerBlock: config.TxsPerBlock,
projector: projector,
TransactionPool: txPool,
db: db,
layersPerEpoch: config.LayersPerEpoch,
}
}
// Start starts the process of creating a block, it listens for txs and atxs received by gossip, and starts querying
// block oracle when it should create a block. This function returns an error if Start was already called once.
func (t *BlockBuilder) Start(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.started {
return fmt.Errorf("already started")
}
t.started = true
t.eg.Go(func() error {
t.createBlockLoop(log.WithNewSessionID(ctx))
return nil
})
return nil
}
// Close stops listeners and stops trying to create block in layers.
func (t *BlockBuilder) Close() error {
t.mu.Lock()
defer t.mu.Unlock()
if !t.started {
return fmt.Errorf("already stopped")
}
t.started = false
close(t.stopChan)
t.eg.Wait()
t.db.Close()
return nil
}
type meshProvider interface {
AddBlockWithTxs(context.Context, *types.Block) error
}
func getEpochKey(ID types.EpochID) []byte {
return []byte(fmt.Sprintf("e_%v", ID))
}
func (t *BlockBuilder) storeRefBlock(epoch types.EpochID, blockID types.BlockID) error {
if err := t.db.Put(getEpochKey(epoch), blockID.Bytes()); err != nil {
return fmt.Errorf("put into DB: %w", err)
}
return nil
}
func (t *BlockBuilder) getRefBlock(epoch types.EpochID) (blockID types.BlockID, err error) {
bts, err := t.db.Get(getEpochKey(epoch))
if err != nil {
return
}
copy(blockID[:], bts)
return
}
// stopped returns if we should stop.
func (t *BlockBuilder) stopped() bool {
select {
case <-t.stopChan:
return true
default:
return false
}
}
func (t *BlockBuilder) createBlock(
ctx context.Context,
id types.LayerID,
atxID types.ATXID,
eligibilityProof types.BlockEligibilityProof,
txids []types.TransactionID,
activeSet []types.ATXID,
beacon []byte,
) (*types.Block, error) {
logger := t.WithContext(ctx)
if !id.After(types.GetEffectiveGenesis()) {
return nil, errors.New("cannot create blockBytes in genesis layer")
}
// get the most up-to-date base block, and a list of diffs versus local opinion
base, diffs, err := t.baseBlockP.BaseBlock(ctx)
if err != nil {
return nil, fmt.Errorf("get base block: %w", err)
}
b := types.MiniBlock{
BlockHeader: types.BlockHeader{
LayerIndex: id,
ATXID: atxID,
EligibilityProof: eligibilityProof,
Data: nil,
BaseBlock: base,
AgainstDiff: diffs[0],
ForDiff: diffs[1],
NeutralDiff: diffs[2],
},
TxIDs: txids,
}
epoch := id.GetEpoch()
refBlock, err := t.getRefBlock(epoch)
if err != nil {
if !errors.Is(err, database.ErrNotFound) {
logger.With().Error("failed to get ref block", log.Err(err))
return nil, fmt.Errorf("get ref block: %w", err)
}
logger.With().Debug("creating block with active set (no reference block for epoch)",
log.Int("active_set_size", len(activeSet)),
log.FieldNamed("ref_block", refBlock),
log.Err(err))
atxs := activeSet
b.ActiveSet = &atxs
b.TortoiseBeacon = beacon
} else {
logger.With().Debug("creating block with reference block (no active set)",
log.Int("active_set_size", len(activeSet)),
log.FieldNamed("ref_block", refBlock))
b.RefBlock = &refBlock
}
blockBytes, err := types.InterfaceToBytes(b)
if err != nil {
logger.With().Panic("failed to serialize block", log.Err(err))
}
bl := &types.Block{MiniBlock: b, Signature: t.signer.Sign(blockBytes)}
bl.Initialize()
if b.ActiveSet != nil {
logger.With().Debug("storing ref block", epoch, bl.ID())
if err := t.storeRefBlock(epoch, bl.ID()); err != nil {
logger.With().Error("cannot store ref block", epoch, log.Err(err))
return nil, fmt.Errorf("store ref block: %w", err)
}
}
logger.Event().Info("block created", bl.Fields()...)
return bl, nil
}
func (t *BlockBuilder) createBlockLoop(ctx context.Context) {
logger := t.WithContext(ctx)
for {
select {
case <-t.stopChan:
return
case layerID := <-t.beginRoundEvent:
logger := logger.WithFields(layerID, layerID.GetEpoch())
logger.Info("builder got layer")
if !t.syncer.IsSynced(ctx) {
logger.Info("not synced yet, not building a block in this round")
continue
}
if layerID.GetEpoch().IsGenesis() {
continue
}
beacon, err := t.beaconProvider.GetBeacon(layerID.GetEpoch())
if err != nil {
logger.With().Info("beacon not available for epoch", log.Err(err))
continue
}
logger.With().Info("miner got beacon to build blocks",
log.String("beacon", types.BytesToHash(beacon).ShortString()))
started := time.Now()
atxID, proofs, activeSet, err := t.blockOracle.BlockEligible(layerID)
if err != nil {
if errors.Is(err, blocks.ErrMinerHasNoATXInPreviousEpoch) {
logger.Info("node has no ATX in previous epoch and is not eligible for blocks")
events.ReportDoneCreatingBlock(false, layerID.Uint32(), "not eligible to produce block")
continue
}
events.ReportDoneCreatingBlock(false, layerID.Uint32(), "failed to check for block eligibility")
logger.With().Error("failed to check for block eligibility", log.Err(err))
continue
}
if len(proofs) == 0 {
events.ReportDoneCreatingBlock(false, layerID.Uint32(), "")
logger.Info("not eligible for blocks in layer")
continue
}
// TODO: include multiple proofs in each block and weigh blocks where applicable
logger.With().Info("eligible for one or more blocks in layer", atxID, log.Int("num_blocks", len(proofs)))
for _, eligibilityProof := range proofs {
txList, _, err := t.TransactionPool.GetTxsForBlock(t.txsPerBlock, t.projector.GetProjection)
if err != nil {
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "failed to get txs for block")
logger.With().Error("failed to get txs for block", log.Err(err))
continue
}
blk, err := t.createBlock(ctx, layerID, atxID, eligibilityProof, txList, activeSet, beacon)
if err != nil {
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "failed to create new block")
logger.With().Error("failed to create new block", log.Err(err))
continue
}
t.saveBlockBuildDurationMetric(ctx, started, layerID)
if t.stopped() {
return
}
if err := t.meshProvider.AddBlockWithTxs(ctx, blk); err != nil {
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "failed to store block")
logger.With().Error("failed to store block", blk.ID(), log.Err(err))
continue
}
t.eg.Go(func() error {
bytes, err := types.InterfaceToBytes(blk)
if err != nil {
logger.With().Error("failed to serialize block", log.Err(err))
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "cannot serialize block")
return nil
}
// generate a new requestID for the new block message
blockCtx := log.WithNewRequestID(ctx, layerID, blk.ID())
if err = t.publisher.Publish(blockCtx, blocks.NewBlockProtocol, bytes); err != nil {
logger.WithContext(blockCtx).With().Error("failed to send block", log.Err(err))
}
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "")
return nil
})
}
}
}
}
func (t *BlockBuilder) saveBlockBuildDurationMetric(ctx context.Context, started time.Time, layerID types.LayerID) {
elapsed := time.Since(started)
if elapsed > blockBuildDurationErrorThreshold {
t.WithContext(ctx).WithFields(layerID, layerID.GetEpoch()).With().
Error("block building took too long ", log.Duration("elapsed", elapsed))
}
metrics.BlockBuildDuration.WithLabelValues().Observe(float64(elapsed / time.Millisecond))
}