/
slash.go
177 lines (146 loc) · 4.4 KB
/
slash.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
package stake
import (
"encoding/json"
"fmt"
"github.com/blockservice/echoin/sdk/state"
"github.com/tendermint/go-amino"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/blockservice/echoin/sdk"
"github.com/blockservice/echoin/types"
"github.com/blockservice/echoin/utils"
)
var cdc = amino.NewCodec()
type Absence struct {
Count int16
LastBlockHeight int64
}
func (a *Absence) Accumulate() {
a.Count++
a.LastBlockHeight++
}
func (a Absence) GetCount() int16 {
return a.Count
}
func (a Absence) String() string {
return fmt.Sprintf("[Absence] count: %d, lastBlockHeight: %d\n", a.Count, a.LastBlockHeight)
}
type AbsentValidators struct {
Validators map[string]*Absence
}
func (av AbsentValidators) Add(pk types.PubKey, height int64) {
pkStr := types.PubKeyString(pk)
absence := av.Validators[pkStr]
if absence == nil {
absence = &Absence{Count: 1, LastBlockHeight: height}
} else {
absence.Accumulate()
}
av.Validators[pkStr] = absence
}
func (av AbsentValidators) Remove(pk types.PubKey) {
pkStr := types.PubKeyString(pk)
delete(av.Validators, pkStr)
}
func (av AbsentValidators) Clear(currentBlockHeight int64) {
for k, v := range av.Validators {
if v.LastBlockHeight != currentBlockHeight {
delete(av.Validators, k)
}
}
}
func (av AbsentValidators) Contains(pk types.PubKey) bool {
pkStr := types.PubKeyString(pk)
if _, exists := av.Validators[pkStr]; exists {
return true
}
return false
}
func SlashByzantineValidator(pubKey types.PubKey, blockTime, blockHeight int64) (err error) {
slashRatio := utils.GetParams().SlashRatio
err = slash(pubKey, "Byzantine validator", slashRatio, blockTime, blockHeight, true)
if err != nil {
return err
}
return
}
func SlashAbsentValidator(pubKey types.PubKey, absence *Absence, blockTime, blockHeight int64) (err error) {
slashRatio := utils.GetParams().SlashRatio
maxSlashBlocks := utils.GetParams().MaxSlashBlocks
if absence.GetCount() == maxSlashBlocks {
reason := fmt.Sprintf("Absent for up to %d consecutive blocks", maxSlashBlocks)
err = slash(pubKey, reason, slashRatio, blockTime, blockHeight, utils.GetParams().SlashEnabled)
}
return
}
func SlashBadProposer(pubKey types.PubKey, blockTime, blockHeight int64) (err error) {
slashRatio := utils.GetParams().SlashRatio
err = slash(pubKey, "Bad block proposer", slashRatio, blockTime, blockHeight, true)
if err != nil {
return err
}
return
}
func slash(pubKey types.PubKey, reason string, slashRatio sdk.Rat, blockTime, blockHeight int64, slashEnabled bool) (err error) {
totalDeduction := sdk.NewInt(0)
v := GetCandidateByPubKey(pubKey)
if v == nil {
return ErrBadValidatorAddr()
}
if v.ParseShares().Cmp(big.NewInt(0)) <= 0 {
return nil
}
// Get all of the delegators(includes the validator itself)
delegations := GetDelegationsByCandidate(v.Id, "Y")
slashAmount := sdk.ZeroInt
for _, d := range delegations {
if slashEnabled {
slashAmount = d.Shares().MulRat(slashRatio)
}
slashDelegator(d, common.HexToAddress(v.OwnerAddress), slashAmount)
totalDeduction = totalDeduction.Add(slashAmount)
}
err = RemoveValidator(pubKey)
// Save slash history
slash := &Slash{CandidateId: v.Id, SlashRatio: slashRatio, SlashAmount: totalDeduction, Reason: reason, CreatedAt: blockTime, BlockHeight: blockHeight}
saveSlash(slash)
return
}
func slashDelegator(d *Delegation, validatorAddress common.Address, amount sdk.Int) {
d.AddSlashAmount(amount)
UpdateDelegation(d)
// accumulate shares of the validator
val := GetCandidateByAddress(validatorAddress)
val.AddShares(amount.Neg())
updateCandidate(val)
}
func RemoveValidator(pubKey types.PubKey) (err error) {
v := GetCandidateByPubKey(pubKey)
if v == nil {
return ErrBadValidatorAddr()
}
v.Active = "N"
updateCandidate(v)
return
}
func LoadAbsentValidators(store state.SimpleDB) *AbsentValidators {
blank := &AbsentValidators{Validators: make(map[string]*Absence)}
b := store.Get(utils.AbsentValidatorsKey)
if b == nil {
return blank
}
absentValidators := new(AbsentValidators)
err := json.Unmarshal(b, absentValidators)
if err != nil {
//panic(err) // This error should never occur big problem if does
return blank
}
return absentValidators
}
func SaveAbsentValidators(store state.SimpleDB, absentValidators *AbsentValidators) {
b, err := json.Marshal(AbsentValidators{Validators: absentValidators.Validators})
if err != nil {
panic(err)
}
store.Set(utils.AbsentValidatorsKey, b)
}