api.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package clique
  17. import (
  18. "errors"
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/consensus"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. )
  25. // API is a user facing RPC API to allow controlling the signer and voting
  26. // mechanisms of the proof-of-authority scheme.
  27. type API struct {
  28. chain consensus.ChainHeaderReader
  29. clique *Clique
  30. }
  31. // GetSnapshot retrieves the state snapshot at a given block.
  32. func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
  33. // Retrieve the requested block number (or current if none requested)
  34. var header *types.Header
  35. if number == nil || *number == rpc.LatestBlockNumber {
  36. header = api.chain.CurrentHeader()
  37. } else {
  38. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  39. }
  40. // Ensure we have an actually valid block and return its snapshot
  41. if header == nil {
  42. return nil, errUnknownBlock
  43. }
  44. return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  45. }
  46. // GetSnapshotAtHash retrieves the state snapshot at a given block.
  47. func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
  48. header := api.chain.GetHeaderByHash(hash)
  49. if header == nil {
  50. return nil, errUnknownBlock
  51. }
  52. return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  53. }
  54. // GetSigners retrieves the list of authorized signers at the specified block.
  55. func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
  56. // Retrieve the requested block number (or current if none requested)
  57. var header *types.Header
  58. if number == nil || *number == rpc.LatestBlockNumber {
  59. header = api.chain.CurrentHeader()
  60. } else {
  61. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  62. }
  63. // Ensure we have an actually valid block and return the signers from its snapshot
  64. if header == nil {
  65. return nil, errUnknownBlock
  66. }
  67. snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return snap.signers(), nil
  72. }
  73. // GetSignersAtHash retrieves the list of authorized signers at the specified block.
  74. func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
  75. header := api.chain.GetHeaderByHash(hash)
  76. if header == nil {
  77. return nil, errUnknownBlock
  78. }
  79. snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return snap.signers(), nil
  84. }
  85. // Proposals returns the current proposals the node tries to uphold and vote on.
  86. func (api *API) Proposals() map[common.Address]bool {
  87. api.clique.lock.RLock()
  88. defer api.clique.lock.RUnlock()
  89. proposals := make(map[common.Address]bool)
  90. for address, auth := range api.clique.proposals {
  91. proposals[address] = auth
  92. }
  93. return proposals
  94. }
  95. // Propose injects a new authorization proposal that the signer will attempt to
  96. // push through.
  97. func (api *API) Propose(address common.Address, auth bool) {
  98. api.clique.lock.Lock()
  99. defer api.clique.lock.Unlock()
  100. api.clique.proposals[address] = auth
  101. }
  102. // Discard drops a currently running proposal, stopping the signer from casting
  103. // further votes (either for or against).
  104. func (api *API) Discard(address common.Address) {
  105. api.clique.lock.Lock()
  106. defer api.clique.lock.Unlock()
  107. delete(api.clique.proposals, address)
  108. }
  109. type Status struct {
  110. InturnPercent float64 `json:"inturnPercent"`
  111. SigningStatus map[common.Address]int `json:"sealerActivity"`
  112. NumBlocks uint64 `json:"numBlocks"`
  113. }
  114. // Status returns the status of the last N blocks,
  115. // - the number of active signers,
  116. // - the number of signers,
  117. // - the percentage of in-turn blocks
  118. func (api *API) Status(startBlockNum *rpc.BlockNumber, endBlockNum *rpc.BlockNumber) (*Status, error) {
  119. var (
  120. numBlocks uint64
  121. header *types.Header
  122. diff = uint64(0)
  123. optimals = 0
  124. start uint64
  125. end uint64
  126. )
  127. if startBlockNum != nil && endBlockNum == nil {
  128. return nil, errors.New("pass the end block number")
  129. }
  130. if startBlockNum == nil && endBlockNum != nil {
  131. return nil, errors.New("pass the start block number")
  132. }
  133. if startBlockNum == nil && endBlockNum == nil {
  134. numBlocks = uint64(64)
  135. header = api.chain.CurrentHeader()
  136. end = header.Number.Uint64()
  137. start = end - numBlocks
  138. } else {
  139. end = uint64(*endBlockNum)
  140. start = uint64(*startBlockNum)
  141. if start > end {
  142. return nil, errors.New("start block number should be less than end block number")
  143. }
  144. if end > api.chain.CurrentHeader().Number.Uint64() {
  145. return nil, errors.New("end block number should be less than or equal to current block height")
  146. }
  147. numBlocks = end - start
  148. header = api.chain.GetHeaderByNumber(end)
  149. }
  150. snap, err := api.clique.snapshot(api.chain, end, header.Hash(), nil)
  151. if err != nil {
  152. return nil, err
  153. }
  154. var (
  155. signers = snap.signers()
  156. )
  157. if numBlocks > end {
  158. start = 1
  159. numBlocks = end - start
  160. }
  161. signStatus := make(map[common.Address]int)
  162. for _, s := range signers {
  163. signStatus[s] = 0
  164. }
  165. for n := start; n < end; n++ {
  166. h := api.chain.GetHeaderByNumber(n)
  167. if h == nil {
  168. return nil, fmt.Errorf("missing block %d", n)
  169. }
  170. if h.Difficulty.Cmp(diffInTurn) == 0 {
  171. optimals++
  172. }
  173. diff += h.Difficulty.Uint64()
  174. sealer, err := api.clique.Author(h)
  175. if err != nil {
  176. return nil, err
  177. }
  178. signStatus[sealer]++
  179. }
  180. return &Status{
  181. InturnPercent: float64(100*optimals) / float64(numBlocks),
  182. SigningStatus: signStatus,
  183. NumBlocks: numBlocks,
  184. }, nil
  185. }