-
Notifications
You must be signed in to change notification settings - Fork 211
/
block_builder.go
368 lines (324 loc) · 10.4 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
// 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"
"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"
)
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
hdist uint32
beginRoundEvent chan types.LayerID
stopChan chan struct{}
TransactionPool txPool
mu sync.Mutex
network p2p.Service
meshProvider meshProvider
baseBlockP baseBlockProvider
blockOracle blockOracle
syncer syncer
wg sync.WaitGroup
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
Hdist uint32
AtxsPerBlock int
LayersPerEpoch uint32
TxsPerBlock int
}
// NewBlockBuilder creates a struct of block builder type.
func NewBlockBuilder(
config Config,
sgn signer,
net p2p.Service,
beginRoundEvent chan types.LayerID,
orph meshProvider,
bbp baseBlockProvider,
blockOracle blockOracle,
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,
hdist: config.Hdist,
Log: logger,
rnd: rand.New(rand.NewSource(int64(seed))),
beginRoundEvent: beginRoundEvent,
stopChan: make(chan struct{}),
mu: sync.Mutex{},
network: net,
meshProvider: orph,
baseBlockP: bbp,
blockOracle: blockOracle,
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.wg.Add(1)
go func() {
t.createBlockLoop(log.WithNewSessionID(ctx))
t.wg.Done()
}()
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()
t.db.Close()
if !t.started {
return fmt.Errorf("already stopped")
}
t.started = false
close(t.stopChan)
t.wg.Wait()
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 {
return t.db.Put(getEpochKey(epoch), blockID.Bytes())
}
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,
) (*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, 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 {
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
} 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 {
return nil, 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))
// todo: panic?
}
}
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
}
started := time.Now()
atxID, proofs, atxs, err := t.blockOracle.BlockEligible(layerID)
if err != nil {
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "failed to check for block eligibility")
logger.With().Error("failed to check for block eligibility", layerID, log.Err(err))
continue
}
if len(proofs) == 0 {
events.ReportDoneCreatingBlock(false, layerID.Uint32(), "")
logger.With().Info("not eligible for blocks in layer", layerID, layerID.GetEpoch())
continue
}
// TODO: include multiple proofs in each block and weigh blocks where applicable
logger.With().Info("eligible for one or more blocks in layer", log.Int("count", len(proofs)))
for _, eligibilityProof := range proofs {
txList, _, err := t.TransactionPool.GetTxsForBlock(t.txsPerBlock, t.projector.GetProjection)
if err != nil {
events.ReportDoneCreatingBlock(false, layerID.Uint32(), "failed to get txs for block")
logger.With().Error("failed to get txs for block", layerID, log.Err(err))
continue
}
blk, err := t.createBlock(ctx, layerID, atxID, eligibilityProof, txList, atxs)
if err != nil {
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "cannot create new block")
logger.With().Error("failed to create new block", log.Err(err))
continue
}
t.saveBlockBuildDurationMetric(ctx, started, layerID, blk.ID())
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
}
go func() {
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
}
// generate a new requestID for the new block message
blockCtx := log.WithNewRequestID(ctx, layerID, blk.ID())
if err = t.network.Broadcast(blockCtx, blocks.NewBlockProtocol, bytes); err != nil {
logger.WithContext(blockCtx).With().Error("failed to send block", log.Err(err))
}
events.ReportDoneCreatingBlock(true, layerID.Uint32(), "")
}()
}
}
}
}
func (t *BlockBuilder) saveBlockBuildDurationMetric(ctx context.Context, started time.Time, layerID types.LayerID, blockID types.BlockID) {
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.
With(metrics.LayerIDLabel, layerID.String()).
With(metrics.BlockIDLabel, blockID.String()).
Observe(float64(elapsed / time.Millisecond))
}