api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 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 downloader
  17. import (
  18. "sync"
  19. "golang.org/x/net/context"
  20. "github.com/ethereum/go-ethereum/event"
  21. "github.com/ethereum/go-ethereum/rpc"
  22. )
  23. // PublicDownloaderAPI provides an API which gives information about the current synchronisation status.
  24. // It offers only methods that operates on data that can be available to anyone without security risks.
  25. type PublicDownloaderAPI struct {
  26. d *Downloader
  27. mux *event.TypeMux
  28. muSyncSubscriptions sync.Mutex
  29. syncSubscriptions map[string]rpc.Subscription
  30. }
  31. // NewPublicDownloaderAPI create a new PublicDownloaderAPI.
  32. func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI {
  33. api := &PublicDownloaderAPI{d: d, mux: m, syncSubscriptions: make(map[string]rpc.Subscription)}
  34. go api.run()
  35. return api
  36. }
  37. func (api *PublicDownloaderAPI) run() {
  38. sub := api.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{})
  39. for event := range sub.Chan() {
  40. var notification interface{}
  41. switch event.Data.(type) {
  42. case StartEvent:
  43. result := &SyncingResult{Syncing: true}
  44. result.Status.Origin, result.Status.Current, result.Status.Height, result.Status.Pulled, result.Status.Known = api.d.Progress()
  45. notification = result
  46. case DoneEvent, FailedEvent:
  47. notification = false
  48. }
  49. api.muSyncSubscriptions.Lock()
  50. for id, sub := range api.syncSubscriptions {
  51. if sub.Notify(notification) == rpc.ErrNotificationNotFound {
  52. delete(api.syncSubscriptions, id)
  53. }
  54. }
  55. api.muSyncSubscriptions.Unlock()
  56. }
  57. }
  58. // Progress gives progress indications when the node is synchronising with the Ethereum network.
  59. type Progress struct {
  60. Origin uint64 `json:"startingBlock"`
  61. Current uint64 `json:"currentBlock"`
  62. Height uint64 `json:"highestBlock"`
  63. Pulled uint64 `json:"pulledStates"`
  64. Known uint64 `json:"knownStates"`
  65. }
  66. // SyncingResult provides information about the current synchronisation status for this node.
  67. type SyncingResult struct {
  68. Syncing bool `json:"syncing"`
  69. Status Progress `json:"status"`
  70. }
  71. // Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
  72. func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (rpc.Subscription, error) {
  73. notifier, supported := ctx.Value(rpc.NotifierContextKey).(rpc.Notifier)
  74. if !supported {
  75. return nil, rpc.ErrNotificationsUnsupported
  76. }
  77. subscription, err := notifier.NewSubscription(func(id string) {
  78. api.muSyncSubscriptions.Lock()
  79. delete(api.syncSubscriptions, id)
  80. api.muSyncSubscriptions.Unlock()
  81. })
  82. if err != nil {
  83. return nil, err
  84. }
  85. api.muSyncSubscriptions.Lock()
  86. api.syncSubscriptions[subscription.ID()] = subscription
  87. api.muSyncSubscriptions.Unlock()
  88. return subscription, nil
  89. }