/
signing_rewards.go
78 lines (64 loc) · 2.24 KB
/
signing_rewards.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
package staking
import (
"fmt"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
func (app *stakingApplication) updateEpochSigning(
ctx *abciAPI.Context,
stakeState *stakingState.MutableState,
signingEntities []signature.PublicKey,
) error {
epochSigning, err := stakeState.EpochSigning(ctx)
if err != nil {
return fmt.Errorf("loading epoch signing info: %w", err)
}
if err := epochSigning.Update(signingEntities); err != nil {
return err
}
if err := stakeState.SetEpochSigning(ctx, epochSigning); err != nil {
return fmt.Errorf("failed to set epoch signing info: %w", err)
}
return nil
}
func (app *stakingApplication) rewardEpochSigning(ctx *abciAPI.Context, time beacon.EpochTime) error {
stakeState := stakingState.NewMutableState(ctx.State())
params, err := stakeState.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("loading consensus parameters: %w", err)
}
if params.SigningRewardThresholdDenominator == 0 {
if err = stakeState.ClearEpochSigning(ctx); err != nil {
return fmt.Errorf("failed to clear epoch signing: %w", err)
}
return nil
}
epochSigning, err := stakeState.EpochSigning(ctx)
if err != nil {
return fmt.Errorf("loading epoch signing info: %w", err)
}
if err = stakeState.ClearEpochSigning(ctx); err != nil {
return fmt.Errorf("failed to clear epoch signing: %w", err)
}
if epochSigning.Total == 0 {
return nil
}
eligibleEntities, err := epochSigning.EligibleEntities(
params.SigningRewardThresholdNumerator,
params.SigningRewardThresholdDenominator,
)
if err != nil {
return fmt.Errorf("determining eligibility: %w", err)
}
var eligibleEntitiesAddrs []staking.Address
for _, entity := range eligibleEntities {
eligibleEntitiesAddrs = append(eligibleEntitiesAddrs, staking.NewAddress(entity))
}
if err := stakeState.AddRewards(ctx, time, ¶ms.RewardFactorEpochSigned, eligibleEntitiesAddrs); err != nil {
return fmt.Errorf("adding rewards: %w", err)
}
return nil
}