-
Notifications
You must be signed in to change notification settings - Fork 671
/
pre_fork_block.go
253 lines (215 loc) · 6.33 KB
/
pre_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
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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"context"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
)
var _ Block = (*preForkBlock)(nil)
type preForkBlock struct {
snowman.Block
vm *VM
}
func (b *preForkBlock) Accept(ctx context.Context) error {
if err := b.acceptOuterBlk(); err != nil {
return err
}
return b.acceptInnerBlk(ctx)
}
func (*preForkBlock) acceptOuterBlk() error {
return nil
}
func (b *preForkBlock) acceptInnerBlk(ctx context.Context) error {
return b.Block.Accept(ctx)
}
func (b *preForkBlock) Status() choices.Status {
forkHeight, err := b.vm.getForkHeight()
if err == database.ErrNotFound {
return b.Block.Status()
}
if err != nil {
// TODO: Once `Status()` can return an error, we should return the error
// here.
b.vm.ctx.Log.Error("unexpected error looking up fork height",
zap.Error(err),
)
return b.Block.Status()
}
// The fork has occurred earlier than this block, so preForkBlocks are all
// invalid.
if b.Height() >= forkHeight {
return choices.Rejected
}
return b.Block.Status()
}
func (b *preForkBlock) Verify(ctx context.Context) error {
parent, err := b.vm.getPreForkBlock(ctx, b.Block.Parent())
if err != nil {
return err
}
return parent.verifyPreForkChild(ctx, b)
}
func (b *preForkBlock) Options(ctx context.Context) ([2]snowman.Block, error) {
oracleBlk, ok := b.Block.(snowman.OracleBlock)
if !ok {
return [2]snowman.Block{}, snowman.ErrNotOracle
}
options, err := oracleBlk.Options(ctx)
if err != nil {
return [2]snowman.Block{}, err
}
// A pre-fork block's child options are always pre-fork blocks
return [2]snowman.Block{
&preForkBlock{
Block: options[0],
vm: b.vm,
},
&preForkBlock{
Block: options[1],
vm: b.vm,
},
}, nil
}
func (b *preForkBlock) getInnerBlk() snowman.Block {
return b.Block
}
func (b *preForkBlock) verifyPreForkChild(ctx context.Context, child *preForkBlock) error {
parentTimestamp := b.Timestamp()
if !parentTimestamp.Before(b.vm.activationTime) {
if err := verifyIsOracleBlock(ctx, b.Block); err != nil {
return err
}
b.vm.ctx.Log.Debug("allowing pre-fork block after the fork time",
zap.String("reason", "parent is an oracle block"),
zap.Stringer("blkID", b.ID()),
)
}
return child.Block.Verify(ctx)
}
// This method only returns nil once (during the transition)
func (b *preForkBlock) verifyPostForkChild(ctx context.Context, child *postForkBlock) error {
if err := verifyIsNotOracleBlock(ctx, b.Block); err != nil {
return err
}
childID := child.ID()
childPChainHeight := child.PChainHeight()
currentPChainHeight, err := b.vm.ctx.ValidatorState.GetCurrentHeight(ctx)
if err != nil {
b.vm.ctx.Log.Error("block verification failed",
zap.String("reason", "failed to get current P-Chain height"),
zap.Stringer("blkID", childID),
zap.Error(err),
)
return err
}
if childPChainHeight > currentPChainHeight {
return errPChainHeightNotReached
}
if childPChainHeight < b.vm.minimumPChainHeight {
return errPChainHeightTooLow
}
// Make sure [b] is the parent of [child]'s inner block
expectedInnerParentID := b.ID()
innerParentID := child.innerBlk.Parent()
if innerParentID != expectedInnerParentID {
return errInnerParentMismatch
}
// A *preForkBlock can only have a *postForkBlock child
// if the *preForkBlock is the last *preForkBlock before activation takes effect
// (its timestamp is at or after the activation time)
parentTimestamp := b.Timestamp()
if parentTimestamp.Before(b.vm.activationTime) {
return errProposersNotActivated
}
// Child's timestamp must be at or after its parent's timestamp
childTimestamp := child.Timestamp()
if childTimestamp.Before(parentTimestamp) {
return errTimeNotMonotonic
}
// Child timestamp can't be too far in the future
maxTimestamp := b.vm.Time().Add(maxSkew)
if childTimestamp.After(maxTimestamp) {
return errTimeTooAdvanced
}
// Verify the lack of signature on the node
if err := child.SignedBlock.Verify(false, b.vm.ctx.ChainID); err != nil {
return err
}
// Verify the inner block and track it as verified
return b.vm.verifyAndRecordInnerBlk(ctx, nil, child)
}
func (*preForkBlock) verifyPostForkOption(context.Context, *postForkOption) error {
return errUnexpectedBlockType
}
func (b *preForkBlock) buildChild(ctx context.Context) (Block, error) {
parentTimestamp := b.Timestamp()
if parentTimestamp.Before(b.vm.activationTime) {
// The chain hasn't forked yet
innerBlock, err := b.vm.ChainVM.BuildBlock(ctx)
if err != nil {
return nil, err
}
b.vm.ctx.Log.Info("built block",
zap.Stringer("blkID", innerBlock.ID()),
zap.Uint64("height", innerBlock.Height()),
zap.Time("parentTimestamp", parentTimestamp),
)
return &preForkBlock{
Block: innerBlock,
vm: b.vm,
}, nil
}
// The chain is currently forking
parentID := b.ID()
newTimestamp := b.vm.Time().Truncate(time.Second)
if newTimestamp.Before(parentTimestamp) {
newTimestamp = parentTimestamp
}
// The child's P-Chain height is proposed as the optimal P-Chain height that
// is at least the minimum height
pChainHeight, err := b.vm.optimalPChainHeight(ctx, b.vm.minimumPChainHeight)
if err != nil {
b.vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to calculate optimal P-chain height"),
zap.Stringer("parentID", parentID),
zap.Error(err),
)
return nil, err
}
innerBlock, err := b.vm.ChainVM.BuildBlock(ctx)
if err != nil {
return nil, err
}
statelessBlock, err := block.BuildUnsigned(
parentID,
newTimestamp,
pChainHeight,
innerBlock.Bytes(),
)
if err != nil {
return nil, err
}
blk := &postForkBlock{
SignedBlock: statelessBlock,
postForkCommonComponents: postForkCommonComponents{
vm: b.vm,
innerBlk: innerBlock,
status: choices.Processing,
},
}
b.vm.ctx.Log.Info("built block",
zap.Stringer("blkID", blk.ID()),
zap.Stringer("innerBlkID", innerBlock.ID()),
zap.Uint64("height", blk.Height()),
zap.Time("parentTimestamp", parentTimestamp),
zap.Time("blockTimestamp", newTimestamp))
return blk, nil
}
func (*preForkBlock) pChainHeight(context.Context) (uint64, error) {
return 0, nil
}