api.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 satoshi
  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 query snapshot and validators
  24. type API struct {
  25. chain consensus.ChainHeaderReader
  26. satoshi *Satoshi
  27. }
  28. // GetSnapshot retrieves the state snapshot at a given block.
  29. func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
  30. // Retrieve the requested block number (or current if none requested)
  31. var header *types.Header
  32. if number == nil || *number == rpc.LatestBlockNumber {
  33. header = api.chain.CurrentHeader()
  34. } else {
  35. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  36. }
  37. // Ensure we have an actually valid block and return its snapshot
  38. if header == nil {
  39. return nil, errUnknownBlock
  40. }
  41. return api.satoshi.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  42. }
  43. // GetSnapshotAtHash retrieves the state snapshot at a given block.
  44. func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
  45. header := api.chain.GetHeaderByHash(hash)
  46. if header == nil {
  47. return nil, errUnknownBlock
  48. }
  49. return api.satoshi.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  50. }
  51. // GetValidators retrieves the list of validators at the specified block.
  52. func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error) {
  53. // Retrieve the requested block number (or current if none requested)
  54. var header *types.Header
  55. if number == nil || *number == rpc.LatestBlockNumber {
  56. header = api.chain.CurrentHeader()
  57. } else {
  58. header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
  59. }
  60. // Ensure we have an actually valid block and return the validators from its snapshot
  61. if header == nil {
  62. return nil, errUnknownBlock
  63. }
  64. snap, err := api.satoshi.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return snap.validators(), nil
  69. }
  70. // GetValidatorsAtHash retrieves the list of validators at the specified block.
  71. func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
  72. header := api.chain.GetHeaderByHash(hash)
  73. if header == nil {
  74. return nil, errUnknownBlock
  75. }
  76. snap, err := api.satoshi.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return snap.validators(), nil
  81. }