2015-10-15 09:07:19 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-04-14 11:18:24 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
2015-10-15 09:07:19 -05:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-10-15 09:07:19 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 11:18:24 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
2015-10-15 09:07:19 -05:00
|
|
|
//
|
2016-04-14 11:18:24 -05:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-15 09:07:19 -05:00
|
|
|
|
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2016-03-29 08:07:40 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2015-12-16 03:58:01 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-10-15 09:07:19 -05:00
|
|
|
)
|
|
|
|
|
2016-03-15 13:27:49 -05:00
|
|
|
// PublicDownloaderAPI provides an API which gives information about the current synchronisation status.
|
2015-10-15 09:07:19 -05:00
|
|
|
// It offers only methods that operates on data that can be available to anyone without security risks.
|
|
|
|
type PublicDownloaderAPI struct {
|
2016-03-29 08:07:40 -05:00
|
|
|
d *Downloader
|
|
|
|
mux *event.TypeMux
|
|
|
|
muSyncSubscriptions sync.Mutex
|
|
|
|
syncSubscriptions map[string]rpc.Subscription
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicDownloaderAPI create a new PublicDownloaderAPI.
|
2016-03-29 08:07:40 -05:00
|
|
|
func NewPublicDownloaderAPI(d *Downloader, m *event.TypeMux) *PublicDownloaderAPI {
|
|
|
|
api := &PublicDownloaderAPI{d: d, mux: m, syncSubscriptions: make(map[string]rpc.Subscription)}
|
|
|
|
|
|
|
|
go api.run()
|
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *PublicDownloaderAPI) run() {
|
|
|
|
sub := api.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{})
|
|
|
|
|
|
|
|
for event := range sub.Chan() {
|
|
|
|
var notification interface{}
|
|
|
|
|
|
|
|
switch event.Data.(type) {
|
|
|
|
case StartEvent:
|
|
|
|
result := &SyncingResult{Syncing: true}
|
|
|
|
result.Status.Origin, result.Status.Current, result.Status.Height, result.Status.Pulled, result.Status.Known = api.d.Progress()
|
|
|
|
notification = result
|
|
|
|
case DoneEvent, FailedEvent:
|
|
|
|
notification = false
|
|
|
|
}
|
|
|
|
|
|
|
|
api.muSyncSubscriptions.Lock()
|
|
|
|
for id, sub := range api.syncSubscriptions {
|
|
|
|
if sub.Notify(notification) == rpc.ErrNotificationNotFound {
|
|
|
|
delete(api.syncSubscriptions, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
api.muSyncSubscriptions.Unlock()
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Progress gives progress indications when the node is synchronising with the Ethereum network.
|
|
|
|
type Progress struct {
|
|
|
|
Origin uint64 `json:"startingBlock"`
|
|
|
|
Current uint64 `json:"currentBlock"`
|
|
|
|
Height uint64 `json:"highestBlock"`
|
2016-02-10 03:56:15 -06:00
|
|
|
Pulled uint64 `json:"pulledStates"`
|
|
|
|
Known uint64 `json:"knownStates"`
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncingResult provides information about the current synchronisation status for this node.
|
|
|
|
type SyncingResult struct {
|
|
|
|
Syncing bool `json:"syncing"`
|
|
|
|
Status Progress `json:"status"`
|
|
|
|
}
|
|
|
|
|
2016-02-10 03:56:15 -06:00
|
|
|
// Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
|
2016-03-29 08:07:40 -05:00
|
|
|
func (api *PublicDownloaderAPI) Syncing(ctx context.Context) (rpc.Subscription, error) {
|
2016-04-15 11:05:24 -05:00
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
2016-03-29 08:07:40 -05:00
|
|
|
if !supported {
|
|
|
|
return nil, rpc.ErrNotificationsUnsupported
|
|
|
|
}
|
2015-10-15 09:07:19 -05:00
|
|
|
|
2016-03-29 08:07:40 -05:00
|
|
|
subscription, err := notifier.NewSubscription(func(id string) {
|
|
|
|
api.muSyncSubscriptions.Lock()
|
|
|
|
delete(api.syncSubscriptions, id)
|
|
|
|
api.muSyncSubscriptions.Unlock()
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
|
|
|
api.muSyncSubscriptions.Lock()
|
|
|
|
api.syncSubscriptions[subscription.ID()] = subscription
|
|
|
|
api.muSyncSubscriptions.Unlock()
|
|
|
|
|
|
|
|
return subscription, nil
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|