/
candidate.go
79 lines (68 loc) 路 2.13 KB
/
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
package api
import (
"github.com/MinterTeam/minter-go-node/core/state"
"github.com/MinterTeam/minter-go-node/core/state/candidates"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/rpc/lib/types"
)
type Stake struct {
Owner string `json:"owner"`
Coin Coin `json:"coin"`
Value string `json:"value"`
BipValue string `json:"bip_value"`
}
type CandidateResponse struct {
RewardAddress string `json:"reward_address"`
OwnerAddress string `json:"owner_address"`
TotalStake string `json:"total_stake"`
PubKey string `json:"pub_key"`
Commission uint32 `json:"commission"`
Stakes []Stake `json:"stakes,omitempty"`
Status byte `json:"status"`
}
func makeResponseCandidate(state *state.CheckState, c candidates.Candidate, includeStakes bool) CandidateResponse {
candidate := CandidateResponse{
RewardAddress: c.RewardAddress.String(),
OwnerAddress: c.OwnerAddress.String(),
TotalStake: state.Candidates().GetTotalStake(c.PubKey).String(),
PubKey: c.PubKey.String(),
Commission: c.Commission,
Status: c.Status,
}
if includeStakes {
stakes := state.Candidates().GetStakes(c.PubKey)
candidate.Stakes = make([]Stake, len(stakes))
for i, stake := range stakes {
candidate.Stakes[i] = Stake{
Owner: stake.Owner.String(),
Coin: Coin{
ID: stake.Coin.Uint32(),
Symbol: state.Coins().GetCoin(stake.Coin).GetFullSymbol(),
},
Value: stake.Value.String(),
BipValue: stake.BipValue.String(),
}
}
}
return candidate
}
func Candidate(pubkey types.Pubkey, height int) (*CandidateResponse, error) {
cState, err := GetStateForHeight(height)
if err != nil {
return nil, err
}
if height != 0 {
cState.Lock()
cState.Candidates().LoadCandidates()
cState.Candidates().LoadStakesOfCandidate(pubkey)
cState.Unlock()
}
cState.RLock()
defer cState.RUnlock()
candidate := cState.Candidates().GetCandidate(pubkey)
if candidate == nil {
return nil, rpctypes.RPCError{Code: 404, Message: "Candidate not found"}
}
response := makeResponseCandidate(cState, *candidate, true)
return &response, nil
}