-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
272 lines (235 loc) · 7.87 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package backend
import (
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)
// API is a user facing RPC API to dump Istanbul state
type API struct {
chain consensus.ChainReader
istanbul *backend
}
// BlockSigners is contains who created and who signed a particular block, denoted by its number and hash
type BlockSigners struct {
Number uint64
Hash common.Hash
Author common.Address
Committers []common.Address
}
type Status struct {
SigningStatus map[common.Address]int `json:"sealerActivity"`
NumBlocks uint64 `json:"numBlocks"`
}
// NodeAddress returns the public address that is used to sign block headers in IBFT
func (api *API) NodeAddress() common.Address {
return api.istanbul.Address()
}
// GetSignersFromBlock returns the signers and minter for a given block number, or the
// latest block available if none is specified
func (api *API) GetSignersFromBlock(number *rpc.BlockNumber) (*BlockSigners, error) {
// Retrieve the requested block number (or current if none requested)
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, errUnknownBlock
}
return api.signers(header)
}
// GetSignersFromBlockByHash returns the signers and minter for a given block hash
func (api *API) GetSignersFromBlockByHash(hash common.Hash) (*BlockSigners, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
return api.signers(header)
}
func (api *API) signers(header *types.Header) (*BlockSigners, error) {
author, err := api.istanbul.Author(header)
if err != nil {
return nil, err
}
committers, err := api.istanbul.Signers(header)
if err != nil {
return nil, err
}
return &BlockSigners{
Number: header.Number.Uint64(),
Hash: header.Hash(),
Author: author,
Committers: committers,
}, nil
}
// GetSnapshot retrieves the state snapshot at a given block.
func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
// Retrieve the requested block number (or current if none requested)
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
}
// Ensure we have an actually valid block and return its snapshot
if header == nil {
return nil, errUnknownBlock
}
return api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
}
// GetSnapshotAtHash retrieves the state snapshot at a given block.
func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
return api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
}
// GetValidators retrieves the list of authorized validators at the specified block.
func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error) {
// Retrieve the requested block number (or current if none requested)
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
}
// Ensure we have an actually valid block and return the validators from its snapshot
if header == nil {
return nil, errUnknownBlock
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
if err != nil {
return nil, err
}
return snap.validators(), nil
}
// GetValidatorsAtHash retrieves the state snapshot at a given block.
func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
if err != nil {
return nil, err
}
return snap.validators(), nil
}
// Candidates returns the current candidates the node tries to uphold and vote on.
func (api *API) Candidates() map[common.Address]bool {
api.istanbul.candidatesLock.RLock()
defer api.istanbul.candidatesLock.RUnlock()
proposals := make(map[common.Address]bool)
for address, auth := range api.istanbul.candidates {
proposals[address] = auth
}
return proposals
}
// Propose injects a new authorization candidate that the validator will attempt to
// push through.
func (api *API) Propose(address common.Address, auth bool) {
api.istanbul.candidatesLock.Lock()
defer api.istanbul.candidatesLock.Unlock()
api.istanbul.candidates[address] = auth
}
// Discard drops a currently running candidate, stopping the validator from casting
// further votes (either for or against).
func (api *API) Discard(address common.Address) {
api.istanbul.candidatesLock.Lock()
defer api.istanbul.candidatesLock.Unlock()
delete(api.istanbul.candidates, address)
}
func (api *API) Status(startBlockNum *rpc.BlockNumber, endBlockNum *rpc.BlockNumber) (*Status, error) {
var (
numBlocks uint64
header = api.chain.CurrentHeader()
start uint64
end uint64
blockNumber rpc.BlockNumber
)
if startBlockNum != nil && endBlockNum == nil {
return nil, errors.New("pass the end block number")
}
if startBlockNum == nil && endBlockNum != nil {
return nil, errors.New("pass the start block number")
}
if startBlockNum == nil && endBlockNum == nil {
numBlocks = uint64(64)
header = api.chain.CurrentHeader()
end = header.Number.Uint64()
start = end - numBlocks
blockNumber = rpc.BlockNumber(header.Number.Int64())
} else {
end = uint64(*endBlockNum)
start = uint64(*startBlockNum)
if start > end {
return nil, errors.New("start block number should be less than end block number")
}
if end > api.chain.CurrentHeader().Number.Uint64() {
return nil, errors.New("end block number should be less than or equal to current block height")
}
numBlocks = end - start
header = api.chain.GetHeaderByNumber(end)
blockNumber = rpc.BlockNumber(end)
}
signers, err := api.GetValidators(&blockNumber)
if err != nil {
return nil, err
}
if numBlocks >= end {
start = 1
if end > start {
numBlocks = end - start
} else {
numBlocks = 0
}
}
signStatus := make(map[common.Address]int)
for _, s := range signers {
signStatus[s] = 0
}
for n := start; n < end; n++ {
blockNum := rpc.BlockNumber(int64(n))
s, _ := api.GetSignersFromBlock(&blockNum)
signStatus[s.Author]++
}
return &Status{
SigningStatus: signStatus,
NumBlocks: numBlocks,
}, nil
}
func (api *API) IsValidator(blockNum *rpc.BlockNumber) (bool, error) {
var blockNumber rpc.BlockNumber
if blockNum != nil {
blockNumber = *blockNum
} else {
header := api.chain.CurrentHeader()
blockNumber = rpc.BlockNumber(header.Number.Int64())
}
s, _ := api.GetValidators(&blockNumber)
for _, v := range s {
if v == api.istanbul.address {
return true, nil
}
}
return false, nil
}