api.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 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. }
  29. // NewPrivateLightAPI creates a new LES service API.
  30. func NewPrivateLightAPI(backend *lesCommons) *PrivateLightAPI {
  31. return &PrivateLightAPI{backend: backend}
  32. }
  33. // LatestCheckpoint returns the latest local checkpoint package.
  34. //
  35. // The checkpoint package consists of 4 strings:
  36. // result[0], hex encoded latest section index
  37. // result[1], 32 bytes hex encoded latest section head hash
  38. // result[2], 32 bytes hex encoded latest section canonical hash trie root hash
  39. // result[3], 32 bytes hex encoded latest section bloom trie root hash
  40. func (api *PrivateLightAPI) LatestCheckpoint() ([4]string, error) {
  41. var res [4]string
  42. cp := api.backend.latestLocalCheckpoint()
  43. if cp.Empty() {
  44. return res, errNoCheckpoint
  45. }
  46. res[0] = hexutil.EncodeUint64(cp.SectionIndex)
  47. res[1], res[2], res[3] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
  48. return res, nil
  49. }
  50. // GetLocalCheckpoint returns the specific local checkpoint package.
  51. //
  52. // The checkpoint package consists of 3 strings:
  53. // result[0], 32 bytes hex encoded latest section head hash
  54. // result[1], 32 bytes hex encoded latest section canonical hash trie root hash
  55. // result[2], 32 bytes hex encoded latest section bloom trie root hash
  56. func (api *PrivateLightAPI) GetCheckpoint(index uint64) ([3]string, error) {
  57. var res [3]string
  58. cp := api.backend.localCheckpoint(index)
  59. if cp.Empty() {
  60. return res, errNoCheckpoint
  61. }
  62. res[0], res[1], res[2] = cp.SectionHead.Hex(), cp.CHTRoot.Hex(), cp.BloomRoot.Hex()
  63. return res, nil
  64. }
  65. // GetCheckpointContractAddress returns the contract contract address in hex format.
  66. func (api *PrivateLightAPI) GetCheckpointContractAddress() (string, error) {
  67. if api.backend.oracle == nil {
  68. return "", errNotActivated
  69. }
  70. return api.backend.oracle.config.Address.Hex(), nil
  71. }