-
Notifications
You must be signed in to change notification settings - Fork 655
/
pre_fork_block.go
241 lines (203 loc) · 6.04 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"time"
"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{}
type preForkBlock struct {
snowman.Block
vm *VM
}
func (b *preForkBlock) Accept() error {
if err := b.acceptOuterBlk(); err != nil {
return err
}
return b.acceptInnerBlk()
}
func (b *preForkBlock) acceptOuterBlk() error {
return nil
}
func (b *preForkBlock) acceptInnerBlk() error {
return b.Block.Accept()
}
func (b *preForkBlock) Verify() error {
parent, err := b.vm.getPreForkBlock(b.Block.Parent())
if err != nil {
return err
}
return parent.verifyPreForkChild(b)
}
func (b *preForkBlock) Options() ([2]snowman.Block, error) {
oracleBlk, ok := b.Block.(snowman.OracleBlock)
if !ok {
return [2]snowman.Block{}, snowman.ErrNotOracle
}
options, err := oracleBlk.Options()
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(child *preForkBlock) error {
parentTimestamp := b.Timestamp()
if !parentTimestamp.Before(b.vm.activationTime) {
if err := verifyIsOracleBlock(b.Block); err != nil {
return err
}
if err := b.verifyIsPreForkBlock(); err != nil {
return err
}
b.vm.ctx.Log.Debug("allowing pre-fork block %s after the fork time because the parent is an oracle block",
b.ID())
}
return child.Block.Verify()
}
// This method only returns nil once (during the transition)
func (b *preForkBlock) verifyPostForkChild(child *postForkBlock) error {
if err := verifyIsNotOracleBlock(b.Block); err != nil {
return err
}
if err := b.verifyIsPreForkBlock(); err != nil {
return err
}
childID := child.ID()
childPChainHeight := child.PChainHeight()
currentPChainHeight, err := b.vm.ctx.ValidatorState.GetCurrentHeight()
if err != nil {
b.vm.ctx.Log.Error("couldn't retrieve current P-Chain height while verifying %s: %s", childID, 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(child)
}
func (b *preForkBlock) verifyPostForkOption(child *postForkOption) error {
return errUnexpectedBlockType
}
func (b *preForkBlock) buildChild() (Block, error) {
parentTimestamp := b.Timestamp()
if parentTimestamp.Before(b.vm.activationTime) {
// The chain hasn't forked yet
innerBlock, err := b.vm.ChainVM.BuildBlock()
if err != nil {
return nil, err
}
b.vm.ctx.Log.Info("built block %s - parent timestamp %s",
innerBlock.ID(), 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(b.vm.minimumPChainHeight)
if err != nil {
return nil, err
}
innerBlock, err := b.vm.ChainVM.BuildBlock()
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 %s - parent timestamp %v, block timestamp %v",
blk.ID(), parentTimestamp, newTimestamp)
return blk, nil
}
func (b *preForkBlock) pChainHeight() (uint64, error) {
return 0, nil
}
func (b *preForkBlock) verifyIsPreForkBlock() error {
if status := b.Status(); status == choices.Accepted {
_, err := b.vm.GetLastAccepted()
if err == nil {
// If this block is accepted and it was a preForkBlock, then there
// shouldn't have been an accepted postForkBlock yet. If there was
// an accepted postForkBlock, then this block wasn't a preForkBlock.
return errUnexpectedBlockType
}
if err != database.ErrNotFound {
// If an unexpected error was returned - propagate that that
// error.
return err
}
} else if _, contains := b.vm.Tree.Get(b.Block); contains {
// If this block is a preForkBlock, then it's inner block shouldn't have
// been registered into the inner block tree. If this block was
// registered into the inner block tree, then it wasn't a preForkBlock.
return errUnexpectedBlockType
}
return nil
}