/
staking_command.go
188 lines (161 loc) · 6.12 KB
/
staking_command.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
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package poll
import (
"context"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/state"
)
type stakingCommand struct {
addr address.Address
stakingV1 Protocol
stakingV2 Protocol
}
// NewStakingCommand creates a staking command center to manage staking committee and new native staking
func NewStakingCommand(stkV1 Protocol, stkV2 Protocol) (Protocol, error) {
if stkV1 == nil && stkV2 == nil {
return nil, errors.New("empty staking protocol")
}
h := hash.Hash160b([]byte(_protocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
return nil, err
}
return &stakingCommand{
addr: addr,
stakingV1: stkV1,
stakingV2: stkV2,
}, nil
}
func (sc *stakingCommand) CreateGenesisStates(ctx context.Context, sm protocol.StateManager) error {
// if v1 exists, bootstrap from v1 only
if sc.stakingV1 != nil {
return sc.stakingV1.CreateGenesisStates(ctx, sm)
}
return sc.stakingV2.CreateGenesisStates(ctx, sm)
}
func (sc *stakingCommand) Start(ctx context.Context, sr protocol.StateReader) (interface{}, error) {
if sc.stakingV1 != nil {
if starter, ok := sc.stakingV1.(protocol.Starter); ok {
if _, err := starter.Start(ctx, sr); err != nil {
return nil, err
}
}
}
if sc.stakingV2 != nil {
if starter, ok := sc.stakingV2.(protocol.Starter); ok {
return starter.Start(ctx, sr)
}
}
return nil, nil
}
func (sc *stakingCommand) CreatePreStates(ctx context.Context, sm protocol.StateManager) error {
if sc.useV2(ctx, sm) {
if p, ok := sc.stakingV2.(protocol.PreStatesCreator); ok {
return p.CreatePreStates(ctx, sm)
}
}
if p, ok := sc.stakingV1.(protocol.PreStatesCreator); ok {
return p.CreatePreStates(ctx, sm)
}
return nil
}
func (sc *stakingCommand) CreatePostSystemActions(ctx context.Context, sr protocol.StateReader) ([]action.Envelope, error) {
// no height here, v1 v2 has the same createPostSystemActions method, so directly use common one
return createPostSystemActions(ctx, sr, sc)
}
func (sc *stakingCommand) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
if sc.useV2(ctx, sm) {
return sc.stakingV2.Handle(ctx, act, sm)
}
return sc.stakingV1.Handle(ctx, act, sm)
}
func (sc *stakingCommand) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error {
// no height here, v1 v2 has the same validate method, so directly use common one
return validate(ctx, sr, sc, act)
}
func (sc *stakingCommand) CalculateCandidatesByHeight(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error) {
if sc.useV2ByHeight(ctx, height) {
return sc.stakingV2.CalculateCandidatesByHeight(ctx, sr, height)
}
return sc.stakingV1.CalculateCandidatesByHeight(ctx, sr, height)
}
func (sc *stakingCommand) CalculateUnproductiveDelegates(
ctx context.Context,
sr protocol.StateReader,
) ([]string, error) {
if sc.useV2(ctx, sr) && protocol.MustGetFeatureCtx(ctx).FixUnproductiveDelegates {
return sc.stakingV2.CalculateUnproductiveDelegates(ctx, sr)
}
return sc.stakingV1.CalculateUnproductiveDelegates(ctx, sr)
}
// Delegates returns exact number of delegates of current epoch
func (sc *stakingCommand) Delegates(ctx context.Context, sr protocol.StateReader) (state.CandidateList, error) {
if sc.useV2(ctx, sr) {
return sc.stakingV2.Delegates(ctx, sr)
}
return sc.stakingV1.Delegates(ctx, sr)
}
// NextDelegates returns exact number of delegates of next epoch
func (sc *stakingCommand) NextDelegates(ctx context.Context, sr protocol.StateReader) (state.CandidateList, error) {
if sc.useV2(ctx, sr) {
return sc.stakingV2.NextDelegates(ctx, sr)
}
return sc.stakingV1.NextDelegates(ctx, sr)
}
// Candidates returns candidate list from state factory of current epoch
func (sc *stakingCommand) Candidates(ctx context.Context, sr protocol.StateReader) (state.CandidateList, error) {
if sc.useV2(ctx, sr) {
return sc.stakingV2.Candidates(ctx, sr)
}
return sc.stakingV1.Candidates(ctx, sr)
}
// NextCandidates returns candidate list from state factory of next epoch
func (sc *stakingCommand) NextCandidates(ctx context.Context, sr protocol.StateReader) (state.CandidateList, error) {
if sc.useV2(ctx, sr) {
return sc.stakingV2.NextCandidates(ctx, sr)
}
return sc.stakingV1.NextCandidates(ctx, sr)
}
func (sc *stakingCommand) ReadState(ctx context.Context, sr protocol.StateReader, method []byte, args ...[]byte) ([]byte, uint64, error) {
if sc.useV2(ctx, sr) {
res, height, err := sc.stakingV2.ReadState(ctx, sr, method, args...)
if err != nil && sc.stakingV1 != nil {
// check if reading from v1 only method
return sc.stakingV1.ReadState(ctx, sr, method, args...)
}
return res, height, nil
}
return sc.stakingV1.ReadState(ctx, sr, method, args...)
}
// Register registers the protocol with a unique ID
func (sc *stakingCommand) Register(r *protocol.Registry) error {
return r.Register(_protocolID, sc)
}
// ForceRegister registers the protocol with a unique ID and force replacing the previous protocol if it exists
func (sc *stakingCommand) ForceRegister(r *protocol.Registry) error {
return r.ForceRegister(_protocolID, sc)
}
func (sc *stakingCommand) Name() string {
return _protocolID
}
func (sc *stakingCommand) useV2(ctx context.Context, sr protocol.StateReader) bool {
height, err := sr.Height()
if err != nil {
panic("failed to return out height from state reader")
}
return sc.useV2ByHeight(ctx, height)
}
func (sc *stakingCommand) useV2ByHeight(ctx context.Context, height uint64) bool {
featureCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
if sc.stakingV1 == nil || featureCtx.UseV2Staking(height) {
return true
}
return false
}