github.com/r8d8/go-ethereum@v5.5.2+incompatible/eth/downloader/api.go (about) 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 17 package downloader 18 19 import ( 20 "context" 21 "sync" 22 23 "math/big" 24 25 "github.com/ethereumproject/go-ethereum/common" 26 "github.com/ethereumproject/go-ethereum/event" 27 "github.com/ethereumproject/go-ethereum/rpc" 28 ) 29 30 type DoneEvent struct { 31 Peer *peer 32 Hash common.Hash 33 TD *big.Int 34 } 35 type StartEvent struct { 36 Peer *peer 37 Hash common.Hash 38 TD *big.Int 39 } 40 type FailedEvent struct { 41 Peer *peer 42 Err error 43 } 44 45 // PublicDownloaderAPI provides an API which gives information about the current synchronisation status. 46 // It offers only methods that operates on data that can be available to anyone without security risks. 47 type PublicDownloaderAPI struct { 48 d *Downloader 49 mux *event.TypeMux 50 muSyncSubscriptions sync.Mutex 51 syncSubscriptions map[string]rpc.Subscription 52 } 53 54 // NewPublicDownloaderAPI create a new PublicDownloaderAPI. 55 func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI { 56 api := &PublicDownloaderAPI{d: d, mux: m, syncSubscriptions: make(map[string]rpc.Subscription)} 57 58 go api.run() 59 60 return api 61 } 62 63 func (api *PublicDownloaderAPI) run() { 64 sub := api.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{}) 65 66 for event := range sub.Chan() { 67 var notification interface{} 68 69 switch event.Data.(type) { 70 case StartEvent: 71 result := &SyncingResult{Syncing: true} 72 result.Status.Origin, result.Status.Current, result.Status.Height, result.Status.Pulled, result.Status.Known = api.d.Progress() 73 notification = result 74 case DoneEvent, FailedEvent: 75 notification = false 76 } 77 78 api.muSyncSubscriptions.Lock() 79 for id, sub := range api.syncSubscriptions { 80 if sub.Notify(notification) == rpc.ErrNotificationNotFound { 81 delete(api.syncSubscriptions, id) 82 } 83 } 84 api.muSyncSubscriptions.Unlock() 85 } 86 } 87 88 // Progress gives progress indications when the node is synchronising with the Ethereum network. 89 type Progress struct { 90 Origin uint64 `json:"startingBlock"` 91 Current uint64 `json:"currentBlock"` 92 Height uint64 `json:"highestBlock"` 93 Pulled uint64 `json:"pulledStates"` 94 Known uint64 `json:"knownStates"` 95 } 96 97 // SyncingResult provides information about the current synchronisation status for this node. 98 type SyncingResult struct { 99 Syncing bool `json:"syncing"` 100 Status Progress `json:"status"` 101 } 102 103 // Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished. 104 func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (rpc.Subscription, error) { 105 notifier, supported := rpc.NotifierFromContext(ctx) 106 if !supported { 107 return nil, rpc.ErrNotificationsUnsupported 108 } 109 110 subscription, err := notifier.NewSubscription(func(id string) { 111 api.muSyncSubscriptions.Lock() 112 delete(api.syncSubscriptions, id) 113 api.muSyncSubscriptions.Unlock() 114 }) 115 116 if err != nil { 117 return nil, err 118 } 119 120 api.muSyncSubscriptions.Lock() 121 api.syncSubscriptions[subscription.ID()] = subscription 122 api.muSyncSubscriptions.Unlock() 123 124 return subscription, nil 125 }