-
Notifications
You must be signed in to change notification settings - Fork 662
/
options.go
189 lines (158 loc) · 5.27 KB
/
options.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"errors"
"fmt"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
)
var (
_ block.Visitor = (*options)(nil)
errUnexpectedProposalTxType = errors.New("unexpected proposal transaction type")
errFailedFetchingStakerTx = errors.New("failed fetching staker transaction")
errUnexpectedStakerTxType = errors.New("unexpected staker transaction type")
errFailedFetchingPrimaryStaker = errors.New("failed fetching primary staker")
errFailedFetchingSubnetTransformation = errors.New("failed fetching subnet transformation")
errFailedCalculatingUptime = errors.New("failed calculating uptime")
)
// options supports build new option blocks
type options struct {
// inputs populated before calling this struct's methods:
log logging.Logger
primaryUptimePercentage float64
uptimes uptime.Calculator
state state.Chain
// outputs populated by this struct's methods:
preferredBlock block.Block
alternateBlock block.Block
}
func (*options) BanffAbortBlock(*block.BanffAbortBlock) error {
return snowman.ErrNotOracle
}
func (*options) BanffCommitBlock(*block.BanffCommitBlock) error {
return snowman.ErrNotOracle
}
func (o *options) BanffProposalBlock(b *block.BanffProposalBlock) error {
timestamp := b.Timestamp()
blkID := b.ID()
nextHeight := b.Height() + 1
commitBlock, err := block.NewBanffCommitBlock(timestamp, blkID, nextHeight)
if err != nil {
return fmt.Errorf(
"failed to create commit block: %w",
err,
)
}
abortBlock, err := block.NewBanffAbortBlock(timestamp, blkID, nextHeight)
if err != nil {
return fmt.Errorf(
"failed to create abort block: %w",
err,
)
}
prefersCommit, err := o.prefersCommit(b.Tx)
if err != nil {
o.log.Debug("falling back to prefer commit",
zap.Error(err),
)
// We fall back to commit here to err on the side of over-rewarding
// rather than under-rewarding.
//
// Invariant: We must not return the error here, because the error would
// be treated as fatal. Errors can occur here due to a malicious block
// proposer or even in unusual virtuous cases.
prefersCommit = true
}
if prefersCommit {
o.preferredBlock = commitBlock
o.alternateBlock = abortBlock
} else {
o.preferredBlock = abortBlock
o.alternateBlock = commitBlock
}
return nil
}
func (*options) BanffStandardBlock(*block.BanffStandardBlock) error {
return snowman.ErrNotOracle
}
func (*options) ApricotAbortBlock(*block.ApricotAbortBlock) error {
return snowman.ErrNotOracle
}
func (*options) ApricotCommitBlock(*block.ApricotCommitBlock) error {
return snowman.ErrNotOracle
}
func (o *options) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
blkID := b.ID()
nextHeight := b.Height() + 1
var err error
o.preferredBlock, err = block.NewApricotCommitBlock(blkID, nextHeight)
if err != nil {
return fmt.Errorf(
"failed to create commit block: %w",
err,
)
}
o.alternateBlock, err = block.NewApricotAbortBlock(blkID, nextHeight)
if err != nil {
return fmt.Errorf(
"failed to create abort block: %w",
err,
)
}
return nil
}
func (*options) ApricotStandardBlock(*block.ApricotStandardBlock) error {
return snowman.ErrNotOracle
}
func (*options) ApricotAtomicBlock(*block.ApricotAtomicBlock) error {
return snowman.ErrNotOracle
}
func (o *options) prefersCommit(tx *txs.Tx) (bool, error) {
unsignedTx, ok := tx.Unsigned.(*txs.RewardValidatorTx)
if !ok {
return false, fmt.Errorf("%w: %T", errUnexpectedProposalTxType, tx.Unsigned)
}
stakerTx, _, err := o.state.GetTx(unsignedTx.TxID)
if err != nil {
return false, fmt.Errorf("%w: %w", errFailedFetchingStakerTx, err)
}
staker, ok := stakerTx.Unsigned.(txs.Staker)
if !ok {
return false, fmt.Errorf("%w: %T", errUnexpectedStakerTxType, stakerTx.Unsigned)
}
nodeID := staker.NodeID()
primaryNetworkValidator, err := o.state.GetCurrentValidator(
constants.PrimaryNetworkID,
nodeID,
)
if err != nil {
return false, fmt.Errorf("%w: %w", errFailedFetchingPrimaryStaker, err)
}
expectedUptimePercentage := o.primaryUptimePercentage
if subnetID := staker.SubnetID(); subnetID != constants.PrimaryNetworkID {
transformSubnet, err := executor.GetTransformSubnetTx(o.state, subnetID)
if err != nil {
return false, fmt.Errorf("%w: %w", errFailedFetchingSubnetTransformation, err)
}
expectedUptimePercentage = float64(transformSubnet.UptimeRequirement) / reward.PercentDenominator
}
// TODO: calculate subnet uptimes
uptime, err := o.uptimes.CalculateUptimePercentFrom(
nodeID,
constants.PrimaryNetworkID,
primaryNetworkValidator.StartTime,
)
if err != nil {
return false, fmt.Errorf("%w: %w", errFailedCalculatingUptime, err)
}
return uptime >= expectedUptimePercentage, nil
}