-
Notifications
You must be signed in to change notification settings - Fork 664
/
block_builder.go
369 lines (314 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
369
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
)
const (
// syncBound is the synchrony bound used for safe decision making
syncBound = 10 * time.Second
// TargetTxSize is the maximum number of bytes a transaction can use to be
// allowed into the mempool.
TargetTxSize = 64 * units.KiB
// TargetBlockSize is maximum number of transaction bytes to place into a
// StandardBlock
TargetBlockSize = 128 * units.KiB
)
var (
errEndOfTime = errors.New("program time is suspiciously far in the future")
errNoPendingBlocks = errors.New("no pending blocks")
errMempoolReentrancy = errors.New("mempool reentrancy")
errTxTooBig = errors.New("tx too big")
)
// blockBuilder implements a simple blockBuilder to convert txs into valid blocks
type blockBuilder struct {
Mempool
// TODO: factor out VM into separable interfaces
vm *VM
// channel to send messages to the consensus engine
toEngine chan<- common.Message
// This timer goes off when it is time for the next validator to add/leave
// the validator set. When it goes off ResetTimer() is called, potentially
// triggering creation of a new block.
timer *timer.Timer
// Transactions that have not been put into blocks yet
dropIncoming bool
}
// Initialize this builder.
func (m *blockBuilder) Initialize(
vm *VM,
toEngine chan<- common.Message,
registerer prometheus.Registerer,
) error {
m.vm = vm
m.toEngine = toEngine
m.vm.ctx.Log.Verbo("initializing platformVM mempool")
mempool, err := NewMempool("mempool", registerer)
if err != nil {
return err
}
m.Mempool = mempool
m.timer = timer.NewTimer(func() {
m.vm.ctx.Lock.Lock()
defer m.vm.ctx.Lock.Unlock()
m.ResetTimer()
})
go m.vm.ctx.Log.RecoverAndPanic(m.timer.Dispatch)
return nil
}
// AddUnverifiedTx verifies a transaction and attempts to add it to the mempool
func (m *blockBuilder) AddUnverifiedTx(tx *Tx) error {
// Initialize the transaction
if err := tx.Sign(Codec, nil); err != nil {
return err
}
txID := tx.ID()
if m.Has(txID) {
// If the transaction is already in the mempool - then it looks the same
// as if it was successfully added
return nil
}
// Get the preferred block (which we want to build off)
preferred, err := m.vm.Preferred()
if err != nil {
return fmt.Errorf("couldn't get preferred block: %w", err)
}
preferredDecision, ok := preferred.(decision)
if !ok {
// The preferred block should always be a decision block
return errInvalidBlockType
}
preferredState := preferredDecision.onAccept()
if err := tx.UnsignedTx.SemanticVerify(m.vm, preferredState, tx); err != nil {
m.MarkDropped(txID)
return err
}
if err := m.AddVerifiedTx(tx); err != nil {
return err
}
return m.vm.GossipTx(tx)
}
// AddVerifiedTx attempts to add a transaction to the mempool
func (m *blockBuilder) AddVerifiedTx(tx *Tx) error {
if m.dropIncoming {
return errMempoolReentrancy
}
txBytes := tx.Bytes()
if len(txBytes) > TargetTxSize {
return errTxTooBig
}
if err := m.Add(tx); err != nil {
return err
}
m.ResetTimer()
return nil
}
// BuildBlock builds a block to be added to consensus
func (m *blockBuilder) BuildBlock() (snowman.Block, error) {
m.dropIncoming = true
defer func() {
m.dropIncoming = false
m.ResetTimer()
}()
m.vm.ctx.Log.Debug("starting to attempt to build a block")
// Get the block to build on top of and retrieve the new block's context.
preferred, err := m.vm.Preferred()
if err != nil {
return nil, fmt.Errorf("couldn't get preferred block: %w", err)
}
preferredID := preferred.ID()
nextHeight := preferred.Height() + 1
preferredDecision, ok := preferred.(decision)
if !ok {
// The preferred block should always be a decision block
return nil, errInvalidBlockType
}
preferredState := preferredDecision.onAccept()
// Try building a standard block.
if m.HasDecisionTxs() {
txs := m.PopDecisionTxs(TargetBlockSize)
return m.vm.newStandardBlock(preferredID, nextHeight, txs)
}
// Try building a proposal block that rewards a staker.
stakerTxID, shouldReward, err := m.getStakerToReward(preferredState)
if err != nil {
return nil, err
}
if shouldReward {
rewardValidatorTx, err := m.vm.newRewardValidatorTx(stakerTxID)
if err != nil {
return nil, err
}
return m.vm.newProposalBlock(preferredID, nextHeight, *rewardValidatorTx)
}
// Try building a proposal block that advances the chain timestamp.
nextChainTime, shouldAdvanceTime, err := m.getNextChainTime(preferredState)
if err != nil {
return nil, err
}
if shouldAdvanceTime {
advanceTimeTx, err := m.vm.newAdvanceTimeTx(nextChainTime)
if err != nil {
return nil, err
}
return m.vm.newProposalBlock(preferredID, nextHeight, *advanceTimeTx)
}
// Clean out the mempool's transactions with invalid timestamps.
if hasProposalTxs := m.dropTooEarlyMempoolProposalTxs(); !hasProposalTxs {
m.vm.ctx.Log.Debug("no pending blocks to build")
return nil, errNoPendingBlocks
}
// Get the proposal transaction that should be issued.
tx := m.PopProposalTx()
startTime := tx.UnsignedTx.(TimedTx).StartTime()
// If the chain timestamp is too far in the past to issue this transaction
// but according to local time, it's ready to be issued, then attempt to
// advance the timestamp, so it can be issued.
maxChainStartTime := preferredState.GetTimestamp().Add(maxFutureStartTime)
if startTime.After(maxChainStartTime) {
m.AddProposalTx(tx)
advanceTimeTx, err := m.vm.newAdvanceTimeTx(m.vm.clock.Time())
if err != nil {
return nil, err
}
return m.vm.newProposalBlock(preferredID, nextHeight, *advanceTimeTx)
}
return m.vm.newProposalBlock(preferredID, nextHeight, *tx)
}
// ResetTimer Check if there is a block ready to be added to consensus. If so, notify the
// consensus engine.
func (m *blockBuilder) ResetTimer() {
// If there is a pending transaction trigger building of a block with that transaction
if m.HasDecisionTxs() {
m.notifyBlockReady()
return
}
preferred, err := m.vm.Preferred()
if err != nil {
return
}
preferredDecision, ok := preferred.(decision)
if !ok {
// The preferred block should always be a decision block
m.vm.ctx.Log.Error("the preferred block %q should be a decision block but was %T", preferred.ID(), preferred)
return
}
preferredState := preferredDecision.onAccept()
_, shouldReward, err := m.getStakerToReward(preferredState)
if err != nil {
m.vm.ctx.Log.Error("failed to fetch next staker to reward with %s", err)
return
}
if shouldReward {
m.notifyBlockReady()
return
}
_, shouldAdvanceTime, err := m.getNextChainTime(preferredState)
if err != nil {
m.vm.ctx.Log.Error("failed to fetch next chain time with %s", err)
return
}
if shouldAdvanceTime {
// time is at or after the time for the next validator to join/leave
m.notifyBlockReady() // Should issue a proposal to advance timestamp
return
}
if hasProposalTxs := m.dropTooEarlyMempoolProposalTxs(); hasProposalTxs {
m.notifyBlockReady() // Should issue a ProposeAddValidator
return
}
now := m.vm.clock.Time()
nextStakerChangeTime, err := getNextStakerChangeTime(preferredState)
if err != nil {
m.vm.ctx.Log.Error("couldn't get next staker change time: %s", err)
return
}
waitTime := nextStakerChangeTime.Sub(now)
m.vm.ctx.Log.Debug("next scheduled event is at %s (%s in the future)", nextStakerChangeTime, waitTime)
// Wake up when it's time to add/remove the next validator
m.timer.SetTimeoutIn(waitTime)
}
// Shutdown this mempool
func (m *blockBuilder) Shutdown() {
if m.timer == nil {
return
}
// There is a potential deadlock if the timer is about to execute a timeout.
// So, the lock must be released before stopping the timer.
m.vm.ctx.Lock.Unlock()
m.timer.Stop()
m.vm.ctx.Lock.Lock()
}
// getStakerToReward return the staker txID to remove from the primary network
// staking set, if one exists.
func (m *blockBuilder) getStakerToReward(preferredState MutableState) (ids.ID, bool, error) {
currentChainTimestamp := preferredState.GetTimestamp()
if !currentChainTimestamp.Before(mockable.MaxTime) {
return ids.Empty, false, errEndOfTime
}
currentStakers := preferredState.CurrentStakerChainState()
tx, _, err := currentStakers.GetNextStaker()
if err != nil {
return ids.Empty, false, err
}
staker, ok := tx.UnsignedTx.(TimedTx)
if !ok {
return ids.Empty, false, fmt.Errorf("expected staker tx to be TimedTx but got %T", tx.UnsignedTx)
}
return tx.ID(), currentChainTimestamp.Equal(staker.EndTime()), nil
}
// getNextChainTime returns the timestamp for the next chain time and if the
// local time is >= time of the next staker set change.
func (m *blockBuilder) getNextChainTime(preferredState MutableState) (time.Time, bool, error) {
nextStakerChangeTime, err := getNextStakerChangeTime(preferredState)
if err != nil {
return time.Time{}, false, err
}
now := m.vm.clock.Time()
return nextStakerChangeTime, !now.Before(nextStakerChangeTime), nil
}
// dropTooEarlyMempoolProposalTxs drops mempool's validators whose start time is
// too close in the future i.e. within local time plus Delta.
// dropTooEarlyMempoolProposalTxs makes sure that mempool's top proposal tx has
// a valid starting time but does not necessarily remove all txs since
// popped txs are not necessarily ordered by start time.
// Returns true/false if mempool is non-empty/empty following cleanup.
func (m *blockBuilder) dropTooEarlyMempoolProposalTxs() bool {
now := m.vm.clock.Time()
syncTime := now.Add(syncBound)
for m.HasProposalTx() {
tx := m.PopProposalTx()
startTime := tx.UnsignedTx.(TimedTx).StartTime()
if !startTime.Before(syncTime) {
m.AddProposalTx(tx)
return true
}
txID := tx.ID()
errMsg := fmt.Sprintf(
"synchrony bound (%s) is later than staker start time (%s)",
syncTime,
startTime,
)
m.vm.droppedTxCache.Put(txID, errMsg) // cache tx as dropped
m.vm.ctx.Log.Debug("dropping tx %s: %s", txID, errMsg)
}
return false
}
// notifyBlockReady tells the consensus engine that a new block is ready to be
// created
func (m *blockBuilder) notifyBlockReady() {
select {
case m.toEngine <- common.PendingTxs:
default:
m.vm.ctx.Log.Debug("dropping message to consensus engine")
}
}