-
Notifications
You must be signed in to change notification settings - Fork 663
/
post_fork_option.go
128 lines (104 loc) · 3 KB
/
post_fork_option.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
// 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/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
)
var _ Block = &postForkOption{}
// The parent of a *postForkOption must be a *postForkBlock.
type postForkOption struct {
block.Block
postForkCommonComponents
timestamp time.Time
}
func (b *postForkOption) Timestamp() time.Time {
if b.Status() == choices.Accepted {
return b.vm.lastAcceptedTime
}
return b.timestamp
}
func (b *postForkOption) Accept() error {
blkID := b.ID()
if err := b.vm.State.SetLastAccepted(blkID); err != nil {
return err
}
// Persist this block, its height index, and its status
b.status = choices.Accepted
if err := b.vm.storePostForkBlock(b); err != nil {
return err
}
delete(b.vm.verifiedBlocks, blkID)
// mark the inner block as accepted and all conflicting inner blocks as
// rejected
return b.vm.Tree.Accept(b.innerBlk)
}
func (b *postForkOption) Reject() error {
// we do not reject the inner block here because that block may be contained
// in the proposer block that causing this block to be rejected.
delete(b.vm.verifiedBlocks, b.ID())
// Persist this block and its status
b.status = choices.Rejected
return b.vm.storePostForkBlock(b)
}
func (b *postForkOption) Status() choices.Status { return b.status }
func (b *postForkOption) Parent() ids.ID {
return b.ParentID()
}
// If Verify returns nil, Accept or Reject is eventually called on [b] and
// [b.innerBlk].
func (b *postForkOption) Verify() error {
parent, err := b.vm.getBlock(b.ParentID())
if err != nil {
return err
}
b.timestamp = parent.Timestamp()
return parent.verifyPostForkOption(b)
}
func (b *postForkOption) verifyPreForkChild(child *preForkBlock) error {
// A *preForkBlock's parent must be a *preForkBlock
return errUnsignedChild
}
func (b *postForkOption) verifyPostForkChild(child *postForkBlock) error {
parentTimestamp := b.Timestamp()
parentPChainHeight, err := b.pChainHeight()
if err != nil {
return err
}
return b.postForkCommonComponents.Verify(
parentTimestamp,
parentPChainHeight,
child,
)
}
func (b *postForkOption) verifyPostForkOption(child *postForkOption) error {
// A *postForkOption's parent can't be a *postForkOption
return errUnexpectedBlockType
}
func (b *postForkOption) buildChild() (Block, error) {
parentPChainHeight, err := b.pChainHeight()
if err != nil {
return nil, err
}
return b.postForkCommonComponents.buildChild(
b.ID(),
b.Timestamp(),
parentPChainHeight,
)
}
// This block's P-Chain height is its parent's P-Chain height
func (b *postForkOption) pChainHeight() (uint64, error) {
parent, err := b.vm.getBlock(b.ParentID())
if err != nil {
return 0, err
}
return parent.pChainHeight()
}
func (b *postForkOption) setStatus(status choices.Status) {
b.status = status
}
func (b *postForkOption) getStatelessBlk() block.Block {
return b.Block
}