-
Notifications
You must be signed in to change notification settings - Fork 4
/
state_candidate.go
156 lines (124 loc) · 3.31 KB
/
state_candidate.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
package state
import (
"io"
"bytes"
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/formula"
"github.com/MinterTeam/minter-go-node/rlp"
"github.com/tendermint/tendermint/crypto/ed25519"
"math/big"
)
const (
CandidateStatusOffline = 0x01
CandidateStatusOnline = 0x02
)
// stateCandidate represents a candidate which is being modified.
type stateCandidates struct {
data Candidates
db *StateDB
onDirty func() // Callback method to mark a state object newly dirty
}
type Candidates []Candidate
type Stake struct {
Owner types.Address
Coin types.CoinSymbol
Value *big.Int
BipValue *big.Int
}
func (s *Stake) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Owner types.Address `json:"owner"`
Coin types.CoinSymbol `json:"coin"`
Value string `json:"value"`
BipValue string `json:"bip_value"`
}{
Owner: s.Owner,
Coin: s.Coin,
Value: s.Value.String(),
BipValue: s.BipValue.String(),
})
}
func (s *Stake) CalcBipValue(context *StateDB) *big.Int {
if s.Coin.IsBaseCoin() {
return big.NewInt(0).Set(s.Value)
}
if s.Value.Cmp(types.Big0) == 0 {
return big.NewInt(0)
}
if _, has := context.stakeCache[s.Coin]; !has {
totalStaked := big.NewInt(0)
candidates := context.getStateCandidates()
for _, candidate := range candidates.data {
for _, stake := range candidate.Stakes {
if stake.Coin == s.Coin {
totalStaked.Add(totalStaked, stake.Value)
}
}
}
coin := context.getStateCoin(s.Coin)
context.stakeCache[s.Coin] = StakeCache{
TotalValue: totalStaked,
BipValue: formula.CalculateSaleReturn(coin.Volume(), coin.ReserveBalance(), coin.data.Crr, totalStaked),
}
}
data := context.stakeCache[s.Coin]
if data.TotalValue.Cmp(types.Big0) == 0 {
return big.NewInt(0)
}
value := big.NewInt(0).Set(data.BipValue)
value.Mul(value, s.Value)
value.Div(value, data.TotalValue)
return value
}
type Candidate struct {
RewardAddress types.Address
OwnerAddress types.Address
TotalBipStake *big.Int
PubKey types.Pubkey
Commission uint
Stakes []Stake
CreatedAtBlock uint
Status byte
tmAddress *[20]byte
}
func (candidate Candidate) GetStakeOfAddress(addr types.Address, coin types.CoinSymbol) *Stake {
for i, stake := range candidate.Stakes {
if bytes.Equal(stake.Coin.Bytes(), coin.Bytes()) && bytes.Equal(stake.Owner.Bytes(), addr.Bytes()) {
return &(candidate.Stakes[i])
}
}
return nil
}
func (candidate Candidate) String() string {
return fmt.Sprintf("Candidate")
}
func (candidate Candidate) GetAddress() [20]byte {
if candidate.tmAddress != nil {
return *candidate.tmAddress
}
var pubkey ed25519.PubKeyEd25519
copy(pubkey[:], candidate.PubKey)
var address [20]byte
copy(address[:], pubkey.Address().Bytes())
candidate.tmAddress = &address
return address
}
// newCandidate creates a state object.
func newCandidate(db *StateDB, data Candidates, onDirty func()) *stateCandidates {
candidate := &stateCandidates{
db: db,
data: data,
onDirty: onDirty,
}
candidate.onDirty()
return candidate
}
// EncodeRLP implements rlp.Encoder.
func (c *stateCandidates) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, c.data)
}
func (c *stateCandidates) GetData() Candidates {
return c.data
}