-
Notifications
You must be signed in to change notification settings - Fork 662
/
verifier.go
488 lines (419 loc) · 13.3 KB
/
verifier.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
)
var (
_ block.Visitor = (*verifier)(nil)
errApricotBlockIssuedAfterFork = errors.New("apricot block issued after fork")
errBanffProposalBlockWithMultipleTransactions = errors.New("BanffProposalBlock contains multiple transactions")
errBanffStandardBlockWithoutChanges = errors.New("BanffStandardBlock performs no state changes")
errIncorrectBlockHeight = errors.New("incorrect block height")
errChildBlockEarlierThanParent = errors.New("proposed timestamp before current chain time")
errConflictingBatchTxs = errors.New("block contains conflicting transactions")
errConflictingParentTxs = errors.New("block contains a transaction that conflicts with a transaction in a parent block")
errOptionBlockTimestampNotMatchingParent = errors.New("option block proposed timestamp not matching parent block one")
)
// verifier handles the logic for verifying a block.
type verifier struct {
*backend
txExecutorBackend *executor.Backend
}
func (v *verifier) BanffAbortBlock(b *block.BanffAbortBlock) error {
if err := v.banffOptionBlock(b); err != nil {
return err
}
return v.abortBlock(b)
}
func (v *verifier) BanffCommitBlock(b *block.BanffCommitBlock) error {
if err := v.banffOptionBlock(b); err != nil {
return err
}
return v.commitBlock(b)
}
func (v *verifier) BanffProposalBlock(b *block.BanffProposalBlock) error {
if len(b.Transactions) != 0 {
return errBanffProposalBlockWithMultipleTransactions
}
if err := v.banffNonOptionBlock(b); err != nil {
return err
}
parentID := b.Parent()
onCommitState, err := state.NewDiff(parentID, v.backend)
if err != nil {
return err
}
onAbortState, err := state.NewDiff(parentID, v.backend)
if err != nil {
return err
}
// Apply the changes, if any, from advancing the chain time.
nextChainTime := b.Timestamp()
changes, err := executor.AdvanceTimeTo(
v.txExecutorBackend,
onCommitState,
nextChainTime,
)
if err != nil {
return err
}
onCommitState.SetTimestamp(nextChainTime)
changes.Apply(onCommitState)
onAbortState.SetTimestamp(nextChainTime)
changes.Apply(onAbortState)
return v.proposalBlock(&b.ApricotProposalBlock, onCommitState, onAbortState)
}
func (v *verifier) BanffStandardBlock(b *block.BanffStandardBlock) error {
if err := v.banffNonOptionBlock(b); err != nil {
return err
}
parentID := b.Parent()
onAcceptState, err := state.NewDiff(parentID, v.backend)
if err != nil {
return err
}
// Apply the changes, if any, from advancing the chain time.
nextChainTime := b.Timestamp()
changes, err := executor.AdvanceTimeTo(
v.txExecutorBackend,
onAcceptState,
nextChainTime,
)
if err != nil {
return err
}
// If this block doesn't perform any changes, then it should never have been
// issued.
if changes.Len() == 0 && len(b.Transactions) == 0 {
return errBanffStandardBlockWithoutChanges
}
onAcceptState.SetTimestamp(nextChainTime)
changes.Apply(onAcceptState)
return v.standardBlock(&b.ApricotStandardBlock, onAcceptState)
}
func (v *verifier) ApricotAbortBlock(b *block.ApricotAbortBlock) error {
if err := v.apricotCommonBlock(b); err != nil {
return err
}
return v.abortBlock(b)
}
func (v *verifier) ApricotCommitBlock(b *block.ApricotCommitBlock) error {
if err := v.apricotCommonBlock(b); err != nil {
return err
}
return v.commitBlock(b)
}
func (v *verifier) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
if err := v.apricotCommonBlock(b); err != nil {
return err
}
parentID := b.Parent()
onCommitState, err := state.NewDiff(parentID, v.backend)
if err != nil {
return err
}
onAbortState, err := state.NewDiff(parentID, v.backend)
if err != nil {
return err
}
return v.proposalBlock(b, onCommitState, onAbortState)
}
func (v *verifier) ApricotStandardBlock(b *block.ApricotStandardBlock) error {
if err := v.apricotCommonBlock(b); err != nil {
return err
}
parentID := b.Parent()
onAcceptState, err := state.NewDiff(parentID, v)
if err != nil {
return err
}
return v.standardBlock(b, onAcceptState)
}
func (v *verifier) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
// We call [commonBlock] here rather than [apricotCommonBlock] because below
// this check we perform the more strict check that ApricotPhase5 isn't
// activated.
if err := v.commonBlock(b); err != nil {
return err
}
parentID := b.Parent()
currentTimestamp := v.getTimestamp(parentID)
cfg := v.txExecutorBackend.Config
if cfg.IsApricotPhase5Activated(currentTimestamp) {
return fmt.Errorf(
"the chain timestamp (%d) is after the apricot phase 5 time (%d), hence atomic transactions should go through the standard block",
currentTimestamp.Unix(),
cfg.ApricotPhase5Time.Unix(),
)
}
atomicExecutor := executor.AtomicTxExecutor{
Backend: v.txExecutorBackend,
ParentID: parentID,
StateVersions: v,
Tx: b.Tx,
}
if err := b.Tx.Unsigned.Visit(&atomicExecutor); err != nil {
txID := b.Tx.ID()
v.MarkDropped(txID, err) // cache tx as dropped
return fmt.Errorf("tx %s failed semantic verification: %w", txID, err)
}
atomicExecutor.OnAccept.AddTx(b.Tx, status.Committed)
if err := v.verifyUniqueInputs(b, atomicExecutor.Inputs); err != nil {
return err
}
blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
standardBlockState: standardBlockState{
inputs: atomicExecutor.Inputs,
},
statelessBlock: b,
onAcceptState: atomicExecutor.OnAccept,
timestamp: atomicExecutor.OnAccept.GetTimestamp(),
atomicRequests: atomicExecutor.AtomicRequests,
}
v.Mempool.Remove([]*txs.Tx{b.Tx})
return nil
}
func (v *verifier) banffOptionBlock(b block.BanffBlock) error {
if err := v.commonBlock(b); err != nil {
return err
}
// Banff option blocks must be uniquely generated from the
// BanffProposalBlock. This means that the timestamp must be
// standardized to a specific value. Therefore, we require the timestamp to
// be equal to the parents timestamp.
parentID := b.Parent()
parentBlkTime := v.getTimestamp(parentID)
blkTime := b.Timestamp()
if !blkTime.Equal(parentBlkTime) {
return fmt.Errorf(
"%w parent block timestamp (%s) option block timestamp (%s)",
errOptionBlockTimestampNotMatchingParent,
parentBlkTime,
blkTime,
)
}
return nil
}
func (v *verifier) banffNonOptionBlock(b block.BanffBlock) error {
if err := v.commonBlock(b); err != nil {
return err
}
parentID := b.Parent()
parentState, ok := v.GetState(parentID)
if !ok {
return fmt.Errorf("%w: %s", state.ErrMissingParentState, parentID)
}
newChainTime := b.Timestamp()
parentChainTime := parentState.GetTimestamp()
if newChainTime.Before(parentChainTime) {
return fmt.Errorf(
"%w: proposed timestamp (%s), chain time (%s)",
errChildBlockEarlierThanParent,
newChainTime,
parentChainTime,
)
}
nextStakerChangeTime, err := executor.GetNextStakerChangeTime(parentState)
if err != nil {
return fmt.Errorf("could not verify block timestamp: %w", err)
}
now := v.txExecutorBackend.Clk.Time()
return executor.VerifyNewChainTime(
newChainTime,
nextStakerChangeTime,
now,
)
}
func (v *verifier) apricotCommonBlock(b block.Block) error {
// We can use the parent timestamp here, because we are guaranteed that the
// parent was verified. Apricot blocks only update the timestamp with
// AdvanceTimeTxs. This means that this block's timestamp will be equal to
// the parent block's timestamp; unless this is a CommitBlock. In order for
// the timestamp of the CommitBlock to be after the Banff activation,
// the parent ApricotProposalBlock must include an AdvanceTimeTx with a
// timestamp after the Banff timestamp. This is verified not to occur
// during the verification of the ProposalBlock.
parentID := b.Parent()
timestamp := v.getTimestamp(parentID)
if v.txExecutorBackend.Config.IsBanffActivated(timestamp) {
return fmt.Errorf("%w: timestamp = %s", errApricotBlockIssuedAfterFork, timestamp)
}
return v.commonBlock(b)
}
func (v *verifier) commonBlock(b block.Block) error {
parentID := b.Parent()
parent, err := v.GetBlock(parentID)
if err != nil {
return err
}
expectedHeight := parent.Height() + 1
height := b.Height()
if expectedHeight != height {
return fmt.Errorf(
"%w expected %d, but found %d",
errIncorrectBlockHeight,
expectedHeight,
height,
)
}
return nil
}
// abortBlock populates the state of this block if [nil] is returned
func (v *verifier) abortBlock(b block.Block) error {
parentID := b.Parent()
onAcceptState, ok := v.getOnAbortState(parentID)
if !ok {
return fmt.Errorf("%w: %s", state.ErrMissingParentState, parentID)
}
blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
statelessBlock: b,
onAcceptState: onAcceptState,
timestamp: onAcceptState.GetTimestamp(),
}
return nil
}
// commitBlock populates the state of this block if [nil] is returned
func (v *verifier) commitBlock(b block.Block) error {
parentID := b.Parent()
onAcceptState, ok := v.getOnCommitState(parentID)
if !ok {
return fmt.Errorf("%w: %s", state.ErrMissingParentState, parentID)
}
blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
statelessBlock: b,
onAcceptState: onAcceptState,
timestamp: onAcceptState.GetTimestamp(),
}
return nil
}
// proposalBlock populates the state of this block if [nil] is returned
func (v *verifier) proposalBlock(
b *block.ApricotProposalBlock,
onCommitState state.Diff,
onAbortState state.Diff,
) error {
txExecutor := executor.ProposalTxExecutor{
OnCommitState: onCommitState,
OnAbortState: onAbortState,
Backend: v.txExecutorBackend,
Tx: b.Tx,
}
if err := b.Tx.Unsigned.Visit(&txExecutor); err != nil {
txID := b.Tx.ID()
v.MarkDropped(txID, err) // cache tx as dropped
return err
}
onCommitState.AddTx(b.Tx, status.Committed)
onAbortState.AddTx(b.Tx, status.Aborted)
blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
proposalBlockState: proposalBlockState{
onCommitState: onCommitState,
onAbortState: onAbortState,
initiallyPreferCommit: txExecutor.PrefersCommit,
},
statelessBlock: b,
// It is safe to use [b.onAbortState] here because the timestamp will
// never be modified by an Apricot Abort block and the timestamp will
// always be the same as the Banff Proposal Block.
timestamp: onAbortState.GetTimestamp(),
}
v.Mempool.Remove([]*txs.Tx{b.Tx})
return nil
}
// standardBlock populates the state of this block if [nil] is returned
func (v *verifier) standardBlock(
b *block.ApricotStandardBlock,
onAcceptState state.Diff,
) error {
blkState := &blockState{
statelessBlock: b,
onAcceptState: onAcceptState,
timestamp: onAcceptState.GetTimestamp(),
atomicRequests: make(map[ids.ID]*atomic.Requests),
}
// Finally we process the transactions
funcs := make([]func(), 0, len(b.Transactions))
for _, tx := range b.Transactions {
txExecutor := executor.StandardTxExecutor{
Backend: v.txExecutorBackend,
State: onAcceptState,
Tx: tx,
}
if err := tx.Unsigned.Visit(&txExecutor); err != nil {
txID := tx.ID()
v.MarkDropped(txID, err) // cache tx as dropped
return err
}
// ensure it doesn't overlap with current input batch
if blkState.inputs.Overlaps(txExecutor.Inputs) {
return errConflictingBatchTxs
}
// Add UTXOs to batch
blkState.inputs.Union(txExecutor.Inputs)
onAcceptState.AddTx(tx, status.Committed)
if txExecutor.OnAccept != nil {
funcs = append(funcs, txExecutor.OnAccept)
}
for chainID, txRequests := range txExecutor.AtomicRequests {
// Add/merge in the atomic requests represented by [tx]
chainRequests, exists := blkState.atomicRequests[chainID]
if !exists {
blkState.atomicRequests[chainID] = txRequests
continue
}
chainRequests.PutRequests = append(chainRequests.PutRequests, txRequests.PutRequests...)
chainRequests.RemoveRequests = append(chainRequests.RemoveRequests, txRequests.RemoveRequests...)
}
}
if err := v.verifyUniqueInputs(b, blkState.inputs); err != nil {
return err
}
if numFuncs := len(funcs); numFuncs == 1 {
blkState.onAcceptFunc = funcs[0]
} else if numFuncs > 1 {
blkState.onAcceptFunc = func() {
for _, f := range funcs {
f()
}
}
}
blkID := b.ID()
v.blkIDToState[blkID] = blkState
v.Mempool.Remove(b.Transactions)
return nil
}
// verifyUniqueInputs verifies that the inputs of the given block are not
// duplicated in any of the parent blocks pinned in memory.
func (v *verifier) verifyUniqueInputs(block block.Block, inputs set.Set[ids.ID]) error {
if inputs.Len() == 0 {
return nil
}
// Check for conflicts in ancestors.
for {
parentID := block.Parent()
parentState, ok := v.blkIDToState[parentID]
if !ok {
// The parent state isn't pinned in memory.
// This means the parent must be accepted already.
return nil
}
if parentState.inputs.Overlaps(inputs) {
return errConflictingParentTxs
}
block = parentState.statelessBlock
}
}