-
Notifications
You must be signed in to change notification settings - Fork 199
/
peerAccount.go
152 lines (127 loc) · 4.82 KB
/
peerAccount.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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. peerAccountData.proto
package state
import (
"math/big"
"github.com/ElrondNetwork/elrond-go/core"
)
// PeerAccount is the struct used in serialization/deserialization
type peerAccount struct {
*baseAccount
PeerAccountData
}
// NewEmptyPeerAccount returns an empty peerAccount
func NewEmptyPeerAccount() *peerAccount {
return &peerAccount{
baseAccount: &baseAccount{},
PeerAccountData: PeerAccountData{
AccumulatedFees: big.NewInt(0),
UnStakedEpoch: core.DefaultUnstakedEpoch,
},
}
}
// NewPeerAccount creates new simple account wrapper for an PeerAccountContainer (that has just been initialized)
func NewPeerAccount(address []byte) (*peerAccount, error) {
if len(address) == 0 {
return nil, ErrNilAddress
}
return &peerAccount{
baseAccount: &baseAccount{
address: address,
dataTrieTracker: NewTrackableDataTrie(address, nil),
},
PeerAccountData: PeerAccountData{
AccumulatedFees: big.NewInt(0),
UnStakedEpoch: core.DefaultUnstakedEpoch,
},
}, nil
}
// SetBLSPublicKey sets the account's bls public key, saving the old key before changing
func (pa *peerAccount) SetBLSPublicKey(pubKey []byte) error {
if len(pubKey) < 1 {
return ErrNilBLSPublicKey
}
pa.BLSPublicKey = pubKey
return nil
}
// SetRewardAddress sets the account's reward address, saving the old address before changing
func (pa *peerAccount) SetRewardAddress(address []byte) error {
if len(address) < 1 {
return ErrEmptyAddress
}
pa.RewardAddress = address
return nil
}
// AddToAccumulatedFees sets the account's accumulated fees
func (pa *peerAccount) AddToAccumulatedFees(fees *big.Int) {
pa.AccumulatedFees.Add(pa.AccumulatedFees, fees)
}
// IncreaseLeaderSuccessRate increases the account's number of successful signing
func (pa *peerAccount) IncreaseLeaderSuccessRate(value uint32) {
pa.LeaderSuccessRate.NumSuccess += value
}
// DecreaseLeaderSuccessRate increases the account's number of missing signing
func (pa *peerAccount) DecreaseLeaderSuccessRate(value uint32) {
pa.LeaderSuccessRate.NumFailure += value
}
// IncreaseValidatorSuccessRate increases the account's number of successful signing
func (pa *peerAccount) IncreaseValidatorSuccessRate(value uint32) {
pa.ValidatorSuccessRate.NumSuccess += value
}
// DecreaseValidatorSuccessRate increases the account's number of missed signing
func (pa *peerAccount) DecreaseValidatorSuccessRate(value uint32) {
pa.ValidatorSuccessRate.NumFailure += value
}
// IncreaseValidatorIgnoredSignaturesRate increases the account's number of ignored signatures in successful blocks
func (pa *peerAccount) IncreaseValidatorIgnoredSignaturesRate(value uint32) {
pa.ValidatorIgnoredSignaturesRate += value
}
// IncreaseNumSelectedInSuccessBlocks sets the account's NumSelectedInSuccessBlocks
func (pa *peerAccount) IncreaseNumSelectedInSuccessBlocks() {
pa.NumSelectedInSuccessBlocks++
}
// SetRating sets the account's rating id
func (pa *peerAccount) SetRating(rating uint32) {
pa.Rating = rating
}
// SetTempRating sets the account's tempRating
func (pa *peerAccount) SetTempRating(rating uint32) {
pa.TempRating = rating
}
// SetListAndIndex will update the peer's list (eligible, waiting) and the index inside it with journal
func (pa *peerAccount) SetListAndIndex(shardID uint32, list string, index uint32) {
pa.ShardId = shardID
pa.List = list
pa.IndexInList = index
}
// SetUnStakedEpoch updates the unstaked epoch for the validator
func (pa *peerAccount) SetUnStakedEpoch(epoch uint32) {
pa.UnStakedEpoch = epoch
}
// ResetAtNewEpoch will reset a set of values after changing epoch
func (pa *peerAccount) ResetAtNewEpoch() {
pa.AccumulatedFees = big.NewInt(0)
pa.SetRating(pa.GetTempRating())
pa.TotalLeaderSuccessRate.NumFailure += pa.LeaderSuccessRate.NumFailure
pa.TotalLeaderSuccessRate.NumSuccess += pa.LeaderSuccessRate.NumSuccess
pa.TotalValidatorSuccessRate.NumSuccess += pa.ValidatorSuccessRate.NumSuccess
pa.TotalValidatorSuccessRate.NumFailure += pa.ValidatorSuccessRate.NumFailure
pa.TotalValidatorIgnoredSignaturesRate += pa.ValidatorIgnoredSignaturesRate
pa.LeaderSuccessRate.NumFailure = 0
pa.LeaderSuccessRate.NumSuccess = 0
pa.ValidatorSuccessRate.NumSuccess = 0
pa.ValidatorSuccessRate.NumFailure = 0
pa.ValidatorIgnoredSignaturesRate = 0
pa.NumSelectedInSuccessBlocks = 0
}
// SetConsecutiveProposerMisses sets the account's consecutive misses as proposer
func (pa *peerAccount) SetConsecutiveProposerMisses(consecutiveMisses uint32) {
pa.ConsecutiveProposerMisses = consecutiveMisses
}
//IncreaseNonce adds the given value to the current nonce
func (pa *peerAccount) IncreaseNonce(value uint64) {
pa.Nonce = pa.Nonce + value
}
// IsInterfaceNil return if there is no value under the interface
func (pa *peerAccount) IsInterfaceNil() bool {
return pa == nil
}