2021-09-10 10:55:48 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2022-06-21 12:05:43 +03:00
|
|
|
// DownloaderAPI provides an API which gives information about the current synchronisation status.
|
2021-09-10 10:55:48 +03:00
|
|
|
// It offers only methods that operates on data that can be available to anyone without security risks.
|
2022-06-21 12:05:43 +03:00
|
|
|
type DownloaderAPI struct {
|
2021-09-10 10:55:48 +03:00
|
|
|
d *Downloader
|
|
|
|
mux *event.TypeMux
|
|
|
|
installSyncSubscription chan chan interface{}
|
|
|
|
uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:05:43 +03:00
|
|
|
// NewDownloaderAPI create a new PublicDownloaderAPI. The API has an internal event loop that
|
2021-09-10 10:55:48 +03:00
|
|
|
// listens for events from the downloader through the global event mux. In case it receives one of
|
|
|
|
// these events it broadcasts it to all syncing subscriptions that are installed through the
|
|
|
|
// installSyncSubscription channel.
|
2022-06-21 12:05:43 +03:00
|
|
|
func NewDownloaderAPI(d *Downloader, m *event.TypeMux) *DownloaderAPI {
|
|
|
|
api := &DownloaderAPI{
|
2021-09-10 10:55:48 +03:00
|
|
|
d: d,
|
|
|
|
mux: m,
|
|
|
|
installSyncSubscription: make(chan chan interface{}),
|
|
|
|
uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
go api.eventLoop()
|
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventLoop runs a loop until the event mux closes. It will install and uninstall new
|
|
|
|
// sync subscriptions and broadcasts sync status updates to the installed sync subscriptions.
|
2022-06-21 12:05:43 +03:00
|
|
|
func (api *DownloaderAPI) eventLoop() {
|
2021-09-10 10:55:48 +03:00
|
|
|
var (
|
|
|
|
sub = api.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{})
|
|
|
|
syncSubscriptions = make(map[chan interface{}]struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case i := <-api.installSyncSubscription:
|
|
|
|
syncSubscriptions[i] = struct{}{}
|
|
|
|
case u := <-api.uninstallSyncSubscription:
|
|
|
|
delete(syncSubscriptions, u.c)
|
|
|
|
close(u.uninstalled)
|
|
|
|
case event := <-sub.Chan():
|
|
|
|
if event == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var notification interface{}
|
|
|
|
switch event.Data.(type) {
|
|
|
|
case StartEvent:
|
|
|
|
notification = &SyncingResult{
|
|
|
|
Syncing: true,
|
|
|
|
Status: api.d.Progress(),
|
|
|
|
}
|
|
|
|
case DoneEvent, FailedEvent:
|
|
|
|
notification = false
|
|
|
|
}
|
|
|
|
// broadcast
|
|
|
|
for c := range syncSubscriptions {
|
|
|
|
c <- notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
|
2022-06-21 12:05:43 +03:00
|
|
|
func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error) {
|
2021-09-10 10:55:48 +03:00
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcSub := notifier.CreateSubscription()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
statuses := make(chan interface{})
|
|
|
|
sub := api.SubscribeSyncStatus(statuses)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case status := <-statuses:
|
|
|
|
notifier.Notify(rpcSub.ID, status)
|
|
|
|
case <-rpcSub.Err():
|
|
|
|
sub.Unsubscribe()
|
|
|
|
return
|
|
|
|
case <-notifier.Closed():
|
|
|
|
sub.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rpcSub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncingResult provides information about the current synchronisation status for this node.
|
|
|
|
type SyncingResult struct {
|
|
|
|
Syncing bool `json:"syncing"`
|
|
|
|
Status ethereum.SyncProgress `json:"status"`
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:00:21 +03:00
|
|
|
// uninstallSyncSubscriptionRequest uninstalls a syncing subscription in the API event loop.
|
2021-09-10 10:55:48 +03:00
|
|
|
type uninstallSyncSubscriptionRequest struct {
|
|
|
|
c chan interface{}
|
|
|
|
uninstalled chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncStatusSubscription represents a syncing subscription.
|
|
|
|
type SyncStatusSubscription struct {
|
2022-06-21 12:05:43 +03:00
|
|
|
api *DownloaderAPI // register subscription in event loop of this api instance
|
|
|
|
c chan interface{} // channel where events are broadcasted to
|
|
|
|
unsubOnce sync.Once // make sure unsubscribe logic is executed once
|
2021-09-10 10:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe uninstalls the subscription from the DownloadAPI event loop.
|
|
|
|
// The status channel that was passed to subscribeSyncStatus isn't used anymore
|
|
|
|
// after this method returns.
|
|
|
|
func (s *SyncStatusSubscription) Unsubscribe() {
|
|
|
|
s.unsubOnce.Do(func() {
|
|
|
|
req := uninstallSyncSubscriptionRequest{s.c, make(chan interface{})}
|
|
|
|
s.api.uninstallSyncSubscription <- &req
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.c:
|
|
|
|
// drop new status events until uninstall confirmation
|
|
|
|
continue
|
|
|
|
case <-req.uninstalled:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates.
|
|
|
|
// The given channel must receive interface values, the result can either
|
2022-06-21 12:05:43 +03:00
|
|
|
func (api *DownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription {
|
2021-09-10 10:55:48 +03:00
|
|
|
api.installSyncSubscription <- status
|
|
|
|
return &SyncStatusSubscription{api: api, c: status}
|
|
|
|
}
|