-
Notifications
You must be signed in to change notification settings - Fork 671
/
post_fork_block.go
177 lines (150 loc) · 4.58 KB
/
post_fork_block.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"context"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
)
var _ PostForkBlock = (*postForkBlock)(nil)
type postForkBlock struct {
block.SignedBlock
postForkCommonComponents
// slot of the proposer that produced this block.
// It is populated in verifyPostDurangoBlockDelay.
// It is used to report metrics during Accept.
slot *uint64
}
// Accept:
// 1) Sets this blocks status to Accepted.
// 2) Persists this block in storage
// 3) Calls Reject() on siblings of this block and their descendants.
func (b *postForkBlock) Accept(ctx context.Context) error {
if err := b.acceptOuterBlk(); err != nil {
return err
}
if err := b.acceptInnerBlk(ctx); err != nil {
return err
}
if b.slot != nil {
b.vm.acceptedBlocksSlotHistogram.Observe(float64(*b.slot))
}
return nil
}
func (b *postForkBlock) acceptOuterBlk() error {
// Update in-memory references
b.status = choices.Accepted
b.vm.lastAcceptedTime = b.Timestamp()
return b.vm.acceptPostForkBlock(b)
}
func (b *postForkBlock) acceptInnerBlk(ctx context.Context) error {
// mark the inner block as accepted and all conflicting inner blocks as
// rejected
return b.vm.Tree.Accept(ctx, b.innerBlk)
}
func (b *postForkBlock) Reject(context.Context) error {
// We do not reject the inner block here because it may be accepted later
delete(b.vm.verifiedBlocks, b.ID())
b.status = choices.Rejected
return nil
}
func (b *postForkBlock) Status() choices.Status {
if b.status == choices.Accepted && b.Height() > b.vm.lastAcceptedHeight {
return choices.Processing
}
return b.status
}
// Return this block's parent, or a *missing.Block if
// we don't have the parent.
func (b *postForkBlock) Parent() ids.ID {
return b.ParentID()
}
// If Verify() returns nil, Accept() or Reject() will eventually be called on
// [b] and [b.innerBlk]
func (b *postForkBlock) Verify(ctx context.Context) error {
parent, err := b.vm.getBlock(ctx, b.ParentID())
if err != nil {
return err
}
return parent.verifyPostForkChild(ctx, b)
}
// Return the two options for the block that follows [b]
func (b *postForkBlock) Options(ctx context.Context) ([2]snowman.Block, error) {
innerOracleBlk, ok := b.innerBlk.(snowman.OracleBlock)
if !ok {
// [b]'s innerBlk isn't an oracle block
return [2]snowman.Block{}, snowman.ErrNotOracle
}
// The inner block's child options
innerOptions, err := innerOracleBlk.Options(ctx)
if err != nil {
return [2]snowman.Block{}, err
}
parentID := b.ID()
outerOptions := [2]snowman.Block{}
for i, innerOption := range innerOptions {
// Wrap the inner block's child option
statelessOuterOption, err := block.BuildOption(
parentID,
innerOption.Bytes(),
)
if err != nil {
return [2]snowman.Block{}, err
}
outerOptions[i] = &postForkOption{
Block: statelessOuterOption,
postForkCommonComponents: postForkCommonComponents{
vm: b.vm,
innerBlk: innerOption,
status: innerOption.Status(),
},
}
}
return outerOptions, nil
}
// A post-fork block can never have a pre-fork child
func (*postForkBlock) verifyPreForkChild(context.Context, *preForkBlock) error {
return errUnsignedChild
}
func (b *postForkBlock) verifyPostForkChild(ctx context.Context, child *postForkBlock) error {
parentTimestamp := b.Timestamp()
parentPChainHeight := b.PChainHeight()
return b.postForkCommonComponents.Verify(
ctx,
parentTimestamp,
parentPChainHeight,
child,
)
}
func (b *postForkBlock) verifyPostForkOption(ctx context.Context, child *postForkOption) error {
if err := verifyIsOracleBlock(ctx, b.innerBlk); err != nil {
return err
}
// Make sure [b]'s inner block is the parent of [child]'s inner block
expectedInnerParentID := b.innerBlk.ID()
innerParentID := child.innerBlk.Parent()
if innerParentID != expectedInnerParentID {
return errInnerParentMismatch
}
return child.vm.verifyAndRecordInnerBlk(ctx, nil, child)
}
// Return the child (a *postForkBlock) of this block
func (b *postForkBlock) buildChild(ctx context.Context) (Block, error) {
return b.postForkCommonComponents.buildChild(
ctx,
b.ID(),
b.Timestamp(),
b.PChainHeight(),
)
}
func (b *postForkBlock) pChainHeight(context.Context) (uint64, error) {
return b.PChainHeight(), nil
}
func (b *postForkBlock) setStatus(status choices.Status) {
b.status = status
}
func (b *postForkBlock) getStatelessBlk() block.Block {
return b.SignedBlock
}