-
Notifications
You must be signed in to change notification settings - Fork 189
/
api.go
560 lines (482 loc) · 17.8 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Modifications Copyright 2018 The klaytn Authors
// 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/>.
//
// This file is derived from quorum/consensus/istanbul/backend/api.go (2018/06/04).
// Modified and improved for the klaytn development.
package backend
import (
"errors"
"fmt"
"math/big"
"reflect"
klaytnApi "github.com/klaytn/klaytn/api"
"github.com/klaytn/klaytn/blockchain"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/consensus"
"github.com/klaytn/klaytn/consensus/istanbul"
"github.com/klaytn/klaytn/networks/rpc"
)
// API is a user facing RPC API to dump Istanbul state
type API struct {
chain consensus.ChainReader
istanbul *backend
}
// 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)
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}
return api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil, false)
}
// 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, false)
}
// GetValidators retrieves the list of authorized validators with the given block number.
func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return snap.validators(), nil
}
// GetValidatorsAtHash retrieves the list of authorized validators with the given block hash.
func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, errInternalError
}
return snap.validators(), nil
}
// GetDemotedValidators retrieves the list of authorized, but demoted validators with the given block number.
func (api *API) GetDemoteValidators(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
} else {
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
}
}
// GetDemotedValidatorsAtHash retrieves the list of authorized, but demoted validators with the given block hash.
func (api *API) GetDemotedValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
header := api.chain.GetHeaderByHash(hash)
if header == nil {
return nil, errUnknownBlock
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return snap.demotedValidators(), nil
} else {
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return snap.demotedValidators(), 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)
}
// API extended by Klaytn developers
type APIExtension struct {
chain consensus.ChainReader
istanbul *backend
}
var (
errPendingNotAllowed = errors.New("pending is not allowed")
errInternalError = errors.New("internal error")
errStartNotPositive = errors.New("start block number should be positive")
errEndLargetThanLatest = errors.New("end block number should be smaller than the latest block number")
errStartLargerThanEnd = errors.New("start should be smaller than end")
errRequestedBlocksTooLarge = errors.New("number of requested blocks should be smaller than 50")
errRangeNil = errors.New("range values should not be nil")
errExtractIstanbulExtra = errors.New("extract Istanbul Extra from block header of the given block number")
errNoBlockExist = errors.New("block with the given block number is not existed")
errNoBlockNumber = errors.New("block number is not assigned")
)
// GetCouncil retrieves the list of authorized validators at the specified block.
func (api *APIExtension) GetCouncil(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return nil, err
}
return append(snap.validators(), snap.demotedValidators()...), nil
}
func (api *APIExtension) GetCouncilSize(number *rpc.BlockNumber) (int, error) {
council, err := api.GetCouncil(number)
if err == nil {
return len(council), nil
} else {
return -1, err
}
}
func (api *APIExtension) GetCommittee(number *rpc.BlockNumber) ([]common.Address, error) {
header, err := headerByRpcNumber(api.chain, number)
if err != nil {
return nil, err
}
blockNumber := header.Number.Uint64()
if blockNumber == 0 {
// The committee of genesis block can not be calculated because it requires a previous block.
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return nil, errExtractIstanbulExtra
}
return istanbulExtra.Validators, nil
}
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64()-1, header.ParentHash, nil, false)
if err != nil {
return nil, err
}
round := header.Round()
view := &istanbul.View{
Sequence: new(big.Int).SetUint64(blockNumber),
Round: new(big.Int).SetUint64(uint64(round)),
}
// get the proposer of this block.
proposer, err := ecrecover(header)
if err != nil {
return nil, err
}
parentHash := header.ParentHash
// get the committee list of this block at the view (blockNumber, round)
committee := snap.ValSet.SubListWithProposer(parentHash, proposer, view)
addresses := make([]common.Address, len(committee))
for i, v := range committee {
addresses[i] = v.Address()
}
return addresses, nil
}
func (api *APIExtension) GetCommitteeSize(number *rpc.BlockNumber) (int, error) {
committee, err := api.GetCommittee(number)
if err == nil {
return len(committee), nil
} else {
return -1, err
}
}
type ConsensusInfo struct {
proposer common.Address
originProposer common.Address // the proposal of 0 round at the same block number
committee []common.Address
round byte
}
func (api *APIExtension) getConsensusInfo(block *types.Block) (ConsensusInfo, error) {
blockNumber := block.NumberU64()
if blockNumber == 0 {
return ConsensusInfo{}, nil
}
round := block.Header().Round()
view := &istanbul.View{
Sequence: new(big.Int).Set(block.Number()),
Round: new(big.Int).SetInt64(int64(round)),
}
// get the proposer of this block.
proposer, err := ecrecover(block.Header())
if err != nil {
return ConsensusInfo{}, err
}
// get the snapshot of the previous block.
parentHash := block.ParentHash()
snap, err := api.istanbul.snapshot(api.chain, blockNumber-1, parentHash, nil, false)
if err != nil {
logger.Error("Failed to get snapshot.", "hash", snap.Hash, "err", err)
return ConsensusInfo{}, errInternalError
}
// get origin proposer at 0 round.
originProposer := common.Address{}
lastProposer := api.istanbul.GetProposer(blockNumber - 1)
newValSet := snap.ValSet.Copy()
newValSet.CalcProposer(lastProposer, 0)
originProposer = newValSet.GetProposer().Address()
// get the committee list of this block at the view (blockNumber, round)
committee := snap.ValSet.SubListWithProposer(parentHash, proposer, view)
committeeAddrs := make([]common.Address, len(committee))
for i, v := range committee {
committeeAddrs[i] = v.Address()
}
// verify the committee list of the block using istanbul
//proposalSeal := istanbulCore.PrepareCommittedSeal(block.Hash())
//extra, err := types.ExtractIstanbulExtra(block.Header())
//istanbulAddrs := make([]common.Address, len(committeeAddrs))
//for i, seal := range extra.CommittedSeal {
// addr, err := istanbul.GetSignatureAddress(proposalSeal, seal)
// istanbulAddrs[i] = addr
// if err != nil {
// return proposer, []common.Address{}, err
// }
//
// var found bool = false
// for _, v := range committeeAddrs {
// if addr == v {
// found = true
// break
// }
// }
// if found == false {
// logger.Trace("validator is different!", "snap", committeeAddrs, "istanbul", istanbulAddrs)
// return proposer, committeeAddrs, errors.New("validator set is different from Istanbul engine!!")
// }
//}
cInfo := ConsensusInfo{
proposer: proposer,
originProposer: originProposer,
committee: committeeAddrs,
round: round,
}
return cInfo, nil
}
func (api *APIExtension) makeRPCBlockOutput(b *types.Block,
cInfo ConsensusInfo, transactions types.Transactions, receipts types.Receipts,
) map[string]interface{} {
head := b.Header() // copies the header once
hash := head.Hash()
td := big.NewInt(0)
if bc, ok := api.chain.(*blockchain.BlockChain); ok {
td = bc.GetTd(hash, b.NumberU64())
}
r, err := klaytnApi.RpcOutputBlock(b, td, false, false, api.chain.Config().IsEthTxTypeForkEnabled(b.Header().Number))
if err != nil {
logger.Error("failed to RpcOutputBlock", "err", err)
return nil
}
// make transactions
numTxs := len(transactions)
rpcTransactions := make([]map[string]interface{}, numTxs)
for i, tx := range transactions {
rpcTransactions[i] = klaytnApi.RpcOutputReceipt(head, tx, hash, head.Number.Uint64(), uint64(i), receipts[i])
}
r["committee"] = cInfo.committee
r["proposer"] = cInfo.proposer
r["round"] = cInfo.round
r["originProposer"] = cInfo.originProposer
r["transactions"] = rpcTransactions
return r
}
// TODO-Klaytn: This API functions should be managed with API functions with namespace "klay"
func (api *APIExtension) GetBlockWithConsensusInfoByNumber(number *rpc.BlockNumber) (map[string]interface{}, error) {
b, ok := api.chain.(*blockchain.BlockChain)
if !ok {
logger.Error("chain is not a type of blockchain.BlockChain", "type", reflect.TypeOf(api.chain))
return nil, errInternalError
}
var block *types.Block
var blockNumber uint64
if number == nil {
logger.Trace("block number is not assigned")
return nil, errNoBlockNumber
}
if *number == rpc.PendingBlockNumber {
logger.Trace("Cannot get consensus information of the PendingBlock.")
return nil, errPendingNotAllowed
}
if *number == rpc.LatestBlockNumber {
block = b.CurrentBlock()
blockNumber = block.NumberU64()
} else {
// rpc.EarliestBlockNumber == 0, no need to treat it as a special case.
blockNumber = uint64(number.Int64())
block = b.GetBlockByNumber(blockNumber)
}
if block == nil {
logger.Trace("Finding a block by number failed.", "blockNum", blockNumber)
return nil, fmt.Errorf("the block does not exist (block number: %d)", blockNumber)
}
blockHash := block.Hash()
cInfo, err := api.getConsensusInfo(block)
if err != nil {
logger.Error("Getting the proposer and validators failed.", "blockHash", blockHash, "err", err)
return nil, errInternalError
}
receipts := b.GetBlockReceiptsInCache(blockHash)
if receipts == nil {
receipts = b.GetReceiptsByBlockHash(blockHash)
}
return api.makeRPCBlockOutput(block, cInfo, block.Transactions(), receipts), nil
}
func (api *APIExtension) GetBlockWithConsensusInfoByNumberRange(start *rpc.BlockNumber, end *rpc.BlockNumber) (map[string]interface{}, error) {
blocks := make(map[string]interface{})
if start == nil || end == nil {
logger.Trace("the range values should not be nil.", "start", start, "end", end)
return nil, errRangeNil
}
// check error status.
s := start.Int64()
e := end.Int64()
if s < 0 {
logger.Trace("start should be positive", "start", s)
return nil, errStartNotPositive
}
eChain := api.chain.CurrentHeader().Number.Int64()
if e > eChain {
logger.Trace("end should be smaller than the lastest block number", "end", end, "eChain", eChain)
return nil, errEndLargetThanLatest
}
if s > e {
logger.Trace("start should be smaller than end", "start", s, "end", e)
return nil, errStartLargerThanEnd
}
if (e - s) > 50 {
logger.Trace("number of requested blocks should be smaller than 50", "start", s, "end", e)
return nil, errRequestedBlocksTooLarge
}
// gather s~e blocks
for i := s; i <= e; i++ {
strIdx := fmt.Sprintf("0x%x", i)
blockNum := rpc.BlockNumber(i)
b, err := api.GetBlockWithConsensusInfoByNumber(&blockNum)
if err != nil {
logger.Error("error on GetBlockWithConsensusInfoByNumber", "err", err)
blocks[strIdx] = nil
} else {
blocks[strIdx] = b
}
}
return blocks, nil
}
func (api *APIExtension) GetBlockWithConsensusInfoByHash(blockHash common.Hash) (map[string]interface{}, error) {
b, ok := api.chain.(*blockchain.BlockChain)
if !ok {
logger.Error("chain is not a type of blockchain.Blockchain, returning...", "type", reflect.TypeOf(api.chain))
return nil, errInternalError
}
block := b.GetBlockByHash(blockHash)
if block == nil {
logger.Trace("Finding a block failed.", "blockHash", blockHash)
return nil, fmt.Errorf("the block does not exist (block hash: %s)", blockHash.String())
}
cInfo, err := api.getConsensusInfo(block)
if err != nil {
logger.Error("Getting the proposer and validators failed.", "blockHash", blockHash, "err", err)
return nil, errInternalError
}
receipts := b.GetBlockReceiptsInCache(blockHash)
if receipts == nil {
receipts = b.GetReceiptsByBlockHash(blockHash)
}
return api.makeRPCBlockOutput(block, cInfo, block.Transactions(), receipts), nil
}
func (api *API) GetTimeout() uint64 {
return istanbul.DefaultConfig.Timeout
}
// Retrieve the header at requested block number
func headerByRpcNumber(chain consensus.ChainReader, number *rpc.BlockNumber) (*types.Header, error) {
var header *types.Header
if number == nil || *number == rpc.LatestBlockNumber {
header = chain.CurrentHeader()
} else if *number == rpc.PendingBlockNumber {
logger.Trace("Cannot get snapshot of the pending block.", "number", number)
return nil, errPendingNotAllowed
} else {
header = chain.GetHeaderByNumber(uint64(number.Int64()))
}
// Ensure we have an actually valid block and return its snapshot
if header == nil {
return nil, errUnknownBlock
}
return header, nil
}