api.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2018 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 les
  17. import (
  18. "errors"
  19. "github.com/ethereum/go-ethereum/common/hexutil"
  20. )
  21. var (
  22. errNoCheckpoint = errors.New("no local checkpoint provided")
  23. errNotActivated = errors.New("checkpoint registrar is not activated")
  24. )
  25. // PrivateLightAPI provides an API to access the LES light server or light client.
  26. type PrivateLightAPI struct {
  27. backend *lesCommons
  28. reg *checkpointOracle
  29. }
  30. // NewPrivateLightAPI creates a new LES service API.
  31. func NewPrivateLightAPI(backend *lesCommons, reg *checkpointOracle) *PrivateLightAPI {
  32. return &PrivateLightAPI{
  33. backend: backend,
  34. reg: reg,
  35. }
  36. }
  37. // LatestCheckpoint returns the latest local checkpoint package.
  38. //
  39. // The checkpoint package consists of 4 strings:
  40. // result[0], hex encoded latest section index
  41. // result[1], 32 bytes hex encoded latest section head hash
  42. // result[2], 32 bytes hex encoded latest section canonical hash trie root hash
  43. // result[3], 32 bytes hex encoded latest section bloom trie root hash
  44. func (api *PrivateLightAPI) LatestCheckpoint() ([4]string, error) {
  45. var res [4]string
  46. cp := api.backend.latestLocalCheckpoint()
  47. if cp.Empty() {
  48. return res, errNoCheckpoint
  49. }
  50. res[0] = hexutil.EncodeUint64(cp.SectionIndex)
  51. res[1], res[2], res[3] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
  52. return res, nil
  53. }
  54. // GetLocalCheckpoint returns the specific local checkpoint package.
  55. //
  56. // The checkpoint package consists of 3 strings:
  57. // result[0], 32 bytes hex encoded latest section head hash
  58. // result[1], 32 bytes hex encoded latest section canonical hash trie root hash
  59. // result[2], 32 bytes hex encoded latest section bloom trie root hash
  60. func (api *PrivateLightAPI) GetCheckpoint(index uint64) ([3]string, error) {
  61. var res [3]string
  62. cp := api.backend.getLocalCheckpoint(index)
  63. if cp.Empty() {
  64. return res, errNoCheckpoint
  65. }
  66. res[0], res[1], res[2] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
  67. return res, nil
  68. }
  69. // GetCheckpointContractAddress returns the contract contract address in hex format.
  70. func (api *PrivateLightAPI) GetCheckpointContractAddress() (string, error) {
  71. if api.reg == nil {
  72. return "", errNotActivated
  73. }
  74. return api.reg.config.Address.Hex(), nil
  75. }