api.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/consensus"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/rpc"
  22. )
  23. // API is a user facing RPC API to allow controlling the signer and voting
  24. // mechanisms of the proof-of-authority scheme.
  25. type API struct {
  26. chain consensus.ChainReader
  27. clique *Clique
  28. }
  29. // GetSnapshot retrieves the state snapshot at a given block.
  30. func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
  31. // Retrieve the requested block number (or current if none requested)
  32. var header *types.Header
  33. if number == nil || *number == rpc.LatestBlockNumber {
  34. header = api.chain.CurrentHeader()
  35. } else {
  36. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  37. }
  38. // Ensure we have an actually valid block and return its snapshot
  39. if header == nil {
  40. return nil, errUnknownBlock
  41. }
  42. return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  43. }
  44. // GetSnapshotAtHash retrieves the state snapshot at a given block.
  45. func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
  46. header := api.chain.GetHeaderByHash(hash)
  47. if header == nil {
  48. return nil, errUnknownBlock
  49. }
  50. return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  51. }
  52. // GetSigners retrieves the list of authorized signers at the specified block.
  53. func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
  54. // Retrieve the requested block number (or current if none requested)
  55. var header *types.Header
  56. if number == nil || *number == rpc.LatestBlockNumber {
  57. header = api.chain.CurrentHeader()
  58. } else {
  59. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  60. }
  61. // Ensure we have an actually valid block and return the signers from its snapshot
  62. if header == nil {
  63. return nil, errUnknownBlock
  64. }
  65. snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return snap.signers(), nil
  70. }
  71. // GetSignersAtHash retrieves the state snapshot at a given block.
  72. func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
  73. header := api.chain.GetHeaderByHash(hash)
  74. if header == nil {
  75. return nil, errUnknownBlock
  76. }
  77. snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return snap.signers(), nil
  82. }
  83. // Proposals returns the current proposals the node tries to uphold and vote on.
  84. func (api *API) Proposals() map[common.Address]bool {
  85. api.clique.lock.RLock()
  86. defer api.clique.lock.RUnlock()
  87. proposals := make(map[common.Address]bool)
  88. for address, auth := range api.clique.proposals {
  89. proposals[address] = auth
  90. }
  91. return proposals
  92. }
  93. // Propose injects a new authorization proposal that the signer will attempt to
  94. // push through.
  95. func (api *API) Propose(address common.Address, auth bool) {
  96. api.clique.lock.Lock()
  97. defer api.clique.lock.Unlock()
  98. api.clique.proposals[address] = auth
  99. }
  100. // Discard drops a currently running proposal, stopping the signer from casting
  101. // further votes (either for or against).
  102. func (api *API) Discard(address common.Address) {
  103. api.clique.lock.Lock()
  104. defer api.clique.lock.Unlock()
  105. delete(api.clique.proposals, address)
  106. }