-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
164 lines (141 loc) · 4.44 KB
/
api.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
package equality
import (
"math/big"
"github.com/SecretBlockChain/go-secret/common"
"github.com/SecretBlockChain/go-secret/common/math"
"github.com/SecretBlockChain/go-secret/consensus"
"github.com/SecretBlockChain/go-secret/core/types"
"github.com/SecretBlockChain/go-secret/rpc"
)
type rpcCandidate struct {
Address common.Address `json:"address"`
Staked *math.HexOrDecimal256 `json:"staked"`
BlockNumber *math.HexOrDecimal256 `json:"blockNumber"`
}
type rpcValidator struct {
Address common.Address `json:"address"`
CountMinted *big.Int `json:"countMinted"`
}
type rpcCandidateInfo struct {
Address common.Address `json:"address"`
IsCandidate bool `json:"isCandidate"`
IsValidator bool `json:"isValidator"`
Staked *math.HexOrDecimal256 `json:"staked"`
BlockNumber *math.HexOrDecimal256 `json:"blockNumber"`
}
type rpcCandidatesCount struct {
CandidatesCount int `json:"candidatesCount"`
}
// API is a user facing RPC API to allow controlling the signer and voting
// mechanisms of the proof-of-equality scheme.
type API struct {
chain consensus.ChainHeaderReader
equality *Equality
}
// load a snapshot at specified block
func (api *API) loadSnapshot(number *rpc.BlockNumber) (*Snapshot, HeaderExtra, error) {
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
}
if header == nil {
return nil, HeaderExtra{}, errUnknownBlock
}
headerExtra, err := DecodeHeaderExtra(header)
if err != nil {
return nil, HeaderExtra{}, err
}
snap, err := loadSnapshot(api.equality.db, headerExtra.Root)
return snap, headerExtra, err
}
// GetAddress retrieves the candidate information of the address
func (api *API) GetAddress(address common.Address, number *rpc.BlockNumber) (rpcCandidateInfo, error) {
snap, _, err := api.loadSnapshot(number)
if err != nil {
return rpcCandidateInfo{}, err
}
result := rpcCandidateInfo{Address: address}
candidate, err := snap.GetCandidate(address)
if err != nil {
return rpcCandidateInfo{}, err
}
if candidate != nil {
result.IsCandidate = true
staked := math.HexOrDecimal256(*candidate.Staked)
result.Staked = &staked
blockNumber := math.NewHexOrDecimal256(int64(candidate.BlockNumber))
result.BlockNumber = blockNumber
}
validators, err := snap.GetValidators()
if err != nil {
return rpcCandidateInfo{}, err
}
for _, validator := range validators {
if validator == address {
result.IsValidator = true
break
}
}
return result, nil
}
// GetCandidates retrieves the list of the candidates at specified block
func (api *API) GetCandidates(number *rpc.BlockNumber) ([]rpcCandidate, error) {
snap, _, err := api.loadSnapshot(number)
if err != nil {
return nil, err
}
candidates, err := snap.GetCandidates()
if err != nil {
return nil, err
}
result := make([]rpcCandidate, 0, len(candidates))
for addr, candidate := range candidates {
c := rpcCandidate{Address: addr}
staked := math.HexOrDecimal256(*candidate.Staked)
c.Staked = &staked
blockNumber := math.NewHexOrDecimal256(int64(candidate.BlockNumber))
c.BlockNumber = blockNumber
result = append(result, c)
}
return result, nil
}
// GetCandidatesCount retrieves number of the candidates at specified block
func (api *API) GetCandidatesCount(number *rpc.BlockNumber) (rpcCandidatesCount, error) {
snap, _, err := api.loadSnapshot(number)
if err != nil {
return rpcCandidatesCount{}, err
}
candidates, err := snap.GetCandidates()
if err != nil {
return rpcCandidatesCount{}, err
}
return rpcCandidatesCount{CandidatesCount:len(candidates)}, nil
}
// GetValidators retrieves the list of the validators at specified block
func (api *API) GetValidators(number *rpc.BlockNumber) ([]rpcValidator, error) {
snap, headerExtra, err := api.loadSnapshot(number)
if err != nil {
return nil, err
}
validators, err := snap.GetValidators()
if err != nil {
return nil, err
}
mapper := make(map[common.Address]*big.Int)
addresses, err := snap.CountMinted(headerExtra.Epoch)
if err != nil {
return nil, err
}
for _, address := range addresses {
mapper[address.Address] = address.Weight
}
result := make([]rpcValidator, 0, len(validators))
for _, validator := range validators {
count, _ := mapper[validator]
v := rpcValidator{Address: validator, CountMinted: count}
result = append(result, v)
}
return result, nil
}