/
staking.go
321 lines (275 loc) · 10.5 KB
/
staking.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Package staking implements the staking application.
package staking
import (
"fmt"
"github.com/tendermint/tendermint/abci/types"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
governanceApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/governance/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
"github.com/oasisprotocol/oasis-core/go/roothash/api/message"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
var _ api.Application = (*stakingApplication)(nil)
type stakingApplication struct {
state api.ApplicationState
}
func (app *stakingApplication) Name() string {
return AppName
}
func (app *stakingApplication) ID() uint8 {
return AppID
}
func (app *stakingApplication) Methods() []transaction.MethodName {
return staking.Methods
}
func (app *stakingApplication) Blessed() bool {
return false
}
func (app *stakingApplication) Dependencies() []string {
return nil
}
func (app *stakingApplication) OnRegister(state api.ApplicationState, md api.MessageDispatcher) {
app.state = state
// Subscribe to messages emitted by other apps.
md.Subscribe(roothashApi.RuntimeMessageStaking, app)
md.Subscribe(governanceApi.MessageChangeParameters, app)
md.Subscribe(governanceApi.MessageValidateParameterChanges, app)
}
func (app *stakingApplication) OnCleanup() {
}
func (app *stakingApplication) BeginBlock(ctx *api.Context, request types.RequestBeginBlock) error {
regState := registryState.NewMutableState(ctx.State())
stakeState := stakingState.NewMutableState(ctx.State())
// Look up the proposer's entity.
proposingEntity, err := app.resolveEntityIDFromProposer(ctx, regState, request)
if err != nil {
return fmt.Errorf("failed to resolve proposer entity ID: %w", err)
}
// Go through all voters of the previous block and resolve entities.
// numEligibleValidators is how many total validators are in the validator set, while
// votingEntities is from the validators which actually voted.
numEligibleValidators := len(request.GetLastCommitInfo().Votes)
votingEntities, err := app.resolveEntityIDsFromVotes(ctx, regState, request.GetLastCommitInfo())
if err != nil {
return fmt.Errorf("failed to resolve entity IDs from votes: %w", err)
}
// Disburse fees from previous block.
if err = app.disburseFeesVQ(ctx, stakeState, proposingEntity, numEligibleValidators, votingEntities); err != nil {
return fmt.Errorf("disburse fees voters and next proposer: %w", err)
}
// Save block proposer for fee disbursements.
stakingState.SetBlockProposer(ctx, proposingEntity)
// Add rewards for proposer.
if err = app.rewardBlockProposing(ctx, stakeState, proposingEntity, numEligibleValidators, len(votingEntities)); err != nil {
return fmt.Errorf("staking: block proposing reward: %w", err)
}
// Track signing for rewards.
if err = app.updateEpochSigning(ctx, stakeState, votingEntities); err != nil {
return fmt.Errorf("staking: failed to update epoch signing info: %w", err)
}
// Iterate over any submitted evidence of a validator misbehaving. Note that
// the actual evidence has already been verified by Tendermint to be valid.
for _, evidence := range request.ByzantineValidators {
var reason staking.SlashReason
switch evidence.Type {
case types.EvidenceType_DUPLICATE_VOTE:
reason = staking.SlashConsensusEquivocation
case types.EvidenceType_LIGHT_CLIENT_ATTACK:
reason = staking.SlashConsensusLightClientAttack
default:
ctx.Logger().Debug("ignoring unknown evidence type",
"evidence_type", evidence.Type,
)
continue
}
if err = onEvidenceByzantineConsensus(ctx, reason, evidence.Validator.Address, evidence.Height, evidence.Time, evidence.Validator.Power); err != nil {
return err
}
}
return nil
}
func (app *stakingApplication) ExecuteMessage(ctx *api.Context, kind, msg interface{}) (interface{}, error) {
switch kind {
case roothashApi.RuntimeMessageStaking:
state := stakingState.NewMutableState(ctx.State())
m := msg.(*message.StakingMessage)
switch {
case m.Transfer != nil:
return app.transfer(ctx, state, m.Transfer)
case m.Withdraw != nil:
return app.withdraw(ctx, state, m.Withdraw)
case m.AddEscrow != nil:
return app.addEscrow(ctx, state, m.AddEscrow)
case m.ReclaimEscrow != nil:
return app.reclaimEscrow(ctx, state, m.ReclaimEscrow)
default:
return nil, staking.ErrInvalidArgument
}
case governanceApi.MessageValidateParameterChanges:
// A change parameters proposal is about to be submitted. Validate changes.
return app.changeParameters(ctx, msg, false)
case governanceApi.MessageChangeParameters:
// A change parameters proposal has just been accepted and closed. Validate and apply
// changes.
return app.changeParameters(ctx, msg, true)
default:
return nil, staking.ErrInvalidArgument
}
}
func (app *stakingApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
state := stakingState.NewMutableState(ctx.State())
ctx.SetPriority(AppPriority)
switch tx.Method {
case staking.MethodTransfer:
var xfer staking.Transfer
if err := cbor.Unmarshal(tx.Body, &xfer); err != nil {
return err
}
_, err := app.transfer(ctx, state, &xfer)
return err
case staking.MethodBurn:
var burn staking.Burn
if err := cbor.Unmarshal(tx.Body, &burn); err != nil {
return err
}
return app.burn(ctx, state, &burn)
case staking.MethodAddEscrow:
var escrow staking.Escrow
if err := cbor.Unmarshal(tx.Body, &escrow); err != nil {
return err
}
_, err := app.addEscrow(ctx, state, &escrow)
return err
case staking.MethodReclaimEscrow:
var reclaim staking.ReclaimEscrow
if err := cbor.Unmarshal(tx.Body, &reclaim); err != nil {
return err
}
_, err := app.reclaimEscrow(ctx, state, &reclaim)
return err
case staking.MethodAmendCommissionSchedule:
var amend staking.AmendCommissionSchedule
if err := cbor.Unmarshal(tx.Body, &amend); err != nil {
return err
}
return app.amendCommissionSchedule(ctx, state, &amend)
case staking.MethodAllow:
var allow staking.Allow
if err := cbor.Unmarshal(tx.Body, &allow); err != nil {
return err
}
return app.allow(ctx, state, &allow)
case staking.MethodWithdraw:
var withdraw staking.Withdraw
if err := cbor.Unmarshal(tx.Body, &withdraw); err != nil {
return err
}
_, err := app.withdraw(ctx, state, &withdraw)
return err
default:
return staking.ErrInvalidArgument
}
}
func (app *stakingApplication) EndBlock(ctx *api.Context, request types.RequestEndBlock) (types.ResponseEndBlock, error) {
fees := stakingState.BlockFees(ctx)
if err := app.disburseFeesP(ctx, stakingState.NewMutableState(ctx.State()), stakingState.BlockProposer(ctx), &fees); err != nil {
return types.ResponseEndBlock{}, fmt.Errorf("disburse fees proposer: %w", err)
}
if changed, epoch := app.state.EpochChanged(ctx); changed {
return types.ResponseEndBlock{}, app.onEpochChange(ctx, epoch)
}
return types.ResponseEndBlock{}, nil
}
func (app *stakingApplication) onEpochChange(ctx *api.Context, epoch beacon.EpochTime) error {
state := stakingState.NewMutableState(ctx.State())
// Delegation unbonding after debonding period elapses.
expiredDebondingQueue, err := state.ExpiredDebondingQueue(ctx, epoch)
if err != nil {
return fmt.Errorf("failed to query expired debonding queue: %w", err)
}
for _, e := range expiredDebondingQueue {
deb := e.Delegation
shareAmount := deb.Shares.Clone()
delegator, err := state.Account(ctx, e.DelegatorAddr)
if err != nil {
return fmt.Errorf("failed to query delegator account: %w", err)
}
// NOTE: Could be the same account, so make sure to not have two duplicate
// copies of it and overwrite it later.
var escrow *staking.Account
if e.DelegatorAddr.Equal(e.EscrowAddr) {
escrow = delegator
} else {
escrow, err = state.Account(ctx, e.EscrowAddr)
if err != nil {
return fmt.Errorf("failed to query escrow account: %w", err)
}
}
var baseUnits quantity.Quantity
if err = escrow.Escrow.Debonding.Withdraw(&baseUnits, &deb.Shares, shareAmount); err != nil {
ctx.Logger().Error("failed to redeem debonding shares",
"err", err,
"escrow_addr", e.EscrowAddr,
"delegator_addr", e.DelegatorAddr,
"shares", deb.Shares,
)
return fmt.Errorf("staking/tendermint: failed to redeem debonding shares: %w", err)
}
stakeAmount := baseUnits.Clone()
if err = quantity.Move(&delegator.General.Balance, &baseUnits, stakeAmount); err != nil {
ctx.Logger().Error("failed to move debonded stake",
"err", err,
"escrow_addr", e.EscrowAddr,
"delegator_addr", e.DelegatorAddr,
"shares", deb.Shares,
"base_units", stakeAmount,
)
return fmt.Errorf("staking/tendermint: failed to redeem debonding shares: %w", err)
}
// Update state.
if err = state.RemoveFromDebondingQueue(ctx, e.Epoch, e.DelegatorAddr, e.EscrowAddr); err != nil {
return fmt.Errorf("failed to remove from debonding queue: %w", err)
}
if err = state.SetDebondingDelegation(ctx, e.DelegatorAddr, e.EscrowAddr, e.Delegation.DebondEndTime, nil); err != nil {
return fmt.Errorf("failed to set debonding delegation: %w", err)
}
if err = state.SetAccount(ctx, e.DelegatorAddr, delegator); err != nil {
return fmt.Errorf("failed to set delegator (%s) account: %w", e.DelegatorAddr, err)
}
if !e.DelegatorAddr.Equal(e.EscrowAddr) {
if err = state.SetAccount(ctx, e.EscrowAddr, escrow); err != nil {
return fmt.Errorf("failed to set escrow (%s) account: %w", e.EscrowAddr, err)
}
}
ctx.Logger().Debug("released stake",
"escrow_addr", e.EscrowAddr,
"delegator_addr", e.DelegatorAddr,
"base_units", stakeAmount,
"num_shares", shareAmount,
)
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&staking.ReclaimEscrowEvent{
Owner: e.DelegatorAddr,
Escrow: e.EscrowAddr,
Amount: *stakeAmount,
Shares: *shareAmount,
}))
}
// Add signing rewards.
if err := app.rewardEpochSigning(ctx, epoch); err != nil {
ctx.Logger().Error("failed to add signing rewards",
"err", err,
)
return fmt.Errorf("staking/tendermint: failed to add signing rewards: %w", err)
}
return nil
}
// New constructs a new staking application instance.
func New() api.Application {
return &stakingApplication{}
}