forked from seeleteam/go-seele
/
api.go
107 lines (94 loc) · 3.32 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
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package backend
import (
"github.com/seeleteam/go-seele/common"
"github.com/seeleteam/go-seele/consensus"
"github.com/seeleteam/go-seele/core/types"
"github.com/seeleteam/go-seele/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)
var header *types.BlockHeader
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByHeight(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.Height, 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.Height, 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.BlockHeader
if number == nil || *number == rpc.LatestBlockNumber {
header = api.chain.CurrentHeader()
} else {
header = api.chain.GetHeaderByHeight(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.Height, 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.Height, 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)
}