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 (
|
2017-03-22 12:20:33 -05:00
|
|
|
"context"
|
2016-03-29 08:07:40 -05:00
|
|
|
"sync"
|
2024-01-22 14:05:18 -06:00
|
|
|
"time"
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2020-11-25 14:00:23 -06:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2024-01-22 14:05:18 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2016-03-29 08:07:40 -05:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-01-22 14:05:18 -06:00
|
|
|
// DownloaderAPI provides an API which gives information about the current
|
|
|
|
// synchronisation status. It offers only methods that operates on data that
|
|
|
|
// can be available to anyone without security risks.
|
2022-06-21 04:05:43 -05:00
|
|
|
type DownloaderAPI struct {
|
2016-07-27 10:47:46 -05:00
|
|
|
d *Downloader
|
2024-01-22 14:05:18 -06:00
|
|
|
chain *core.BlockChain
|
2016-07-27 10:47:46 -05:00
|
|
|
mux *event.TypeMux
|
|
|
|
installSyncSubscription chan chan interface{}
|
|
|
|
uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2024-02-20 05:42:48 -06:00
|
|
|
// NewDownloaderAPI creates a new DownloaderAPI. The API has an internal event loop that
|
2016-07-27 10:47:46 -05: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.
|
2024-01-22 14:05:18 -06:00
|
|
|
func NewDownloaderAPI(d *Downloader, chain *core.BlockChain, m *event.TypeMux) *DownloaderAPI {
|
2022-06-21 04:05:43 -05:00
|
|
|
api := &DownloaderAPI{
|
2018-09-19 03:47:09 -05:00
|
|
|
d: d,
|
2024-01-22 14:05:18 -06:00
|
|
|
chain: chain,
|
2018-09-19 03:47:09 -05:00
|
|
|
mux: m,
|
2016-07-27 10:47:46 -05:00
|
|
|
installSyncSubscription: make(chan chan interface{}),
|
|
|
|
uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest),
|
|
|
|
}
|
|
|
|
go api.eventLoop()
|
2016-03-29 08:07:40 -05:00
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:05:18 -06:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The sync status pushed to subscriptions can be a stream like:
|
|
|
|
// >>> {Syncing: true, Progress: {...}}
|
|
|
|
// >>> {false}
|
|
|
|
//
|
|
|
|
// If the node is already synced up, then only a single event subscribers will
|
|
|
|
// receive is {false}.
|
2022-06-21 04:05:43 -05:00
|
|
|
func (api *DownloaderAPI) eventLoop() {
|
2016-07-27 10:47:46 -05:00
|
|
|
var (
|
2024-01-22 14:05:18 -06:00
|
|
|
sub = api.mux.Subscribe(StartEvent{})
|
2016-07-27 10:47:46 -05:00
|
|
|
syncSubscriptions = make(map[chan interface{}]struct{})
|
2024-01-22 14:05:18 -06:00
|
|
|
checkInterval = time.Second * 60
|
|
|
|
checkTimer = time.NewTimer(checkInterval)
|
|
|
|
|
|
|
|
// status flags
|
|
|
|
started bool
|
|
|
|
done bool
|
|
|
|
|
|
|
|
getProgress = func() ethereum.SyncProgress {
|
|
|
|
prog := api.d.Progress()
|
|
|
|
if txProg, err := api.chain.TxIndexProgress(); err == nil {
|
|
|
|
prog.TxIndexFinishedBlocks = txProg.Indexed
|
|
|
|
prog.TxIndexRemainingBlocks = txProg.Remaining
|
|
|
|
}
|
|
|
|
return prog
|
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
)
|
2024-01-22 14:05:18 -06:00
|
|
|
defer checkTimer.Stop()
|
2016-07-27 10:47:46 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case i := <-api.installSyncSubscription:
|
|
|
|
syncSubscriptions[i] = struct{}{}
|
2024-01-22 14:05:18 -06:00
|
|
|
if done {
|
|
|
|
i <- false
|
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
case u := <-api.uninstallSyncSubscription:
|
|
|
|
delete(syncSubscriptions, u.c)
|
|
|
|
close(u.uninstalled)
|
|
|
|
case event := <-sub.Chan():
|
|
|
|
if event == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch event.Data.(type) {
|
|
|
|
case StartEvent:
|
2024-01-22 14:05:18 -06:00
|
|
|
started = true
|
|
|
|
}
|
|
|
|
case <-checkTimer.C:
|
|
|
|
if !started {
|
|
|
|
checkTimer.Reset(checkInterval)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prog := getProgress()
|
|
|
|
if !prog.Done() {
|
|
|
|
notification := &SyncingResult{
|
2016-09-06 04:39:14 -05:00
|
|
|
Syncing: true,
|
2024-01-22 14:05:18 -06:00
|
|
|
Status: prog,
|
|
|
|
}
|
|
|
|
for c := range syncSubscriptions {
|
|
|
|
c <- notification
|
2016-09-06 04:39:14 -05:00
|
|
|
}
|
2024-01-22 14:05:18 -06:00
|
|
|
checkTimer.Reset(checkInterval)
|
|
|
|
continue
|
2016-07-27 10:47:46 -05:00
|
|
|
}
|
|
|
|
for c := range syncSubscriptions {
|
2024-01-22 14:05:18 -06:00
|
|
|
c <- false
|
2016-07-27 10:47:46 -05:00
|
|
|
}
|
2024-01-22 14:05:18 -06:00
|
|
|
done = true
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
|
2022-06-21 04:05:43 -05:00
|
|
|
func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error) {
|
2016-07-27 10:47:46 -05:00
|
|
|
notifier, supported := rpc.NotifierFromContext(ctx)
|
|
|
|
if !supported {
|
|
|
|
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
rpcSub := notifier.CreateSubscription()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
statuses := make(chan interface{})
|
|
|
|
sub := api.SubscribeSyncStatus(statuses)
|
2024-01-05 06:49:31 -06:00
|
|
|
defer sub.Unsubscribe()
|
2016-07-27 10:47:46 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case status := <-statuses:
|
|
|
|
notifier.Notify(rpcSub.ID, status)
|
|
|
|
case <-rpcSub.Err():
|
|
|
|
return
|
2016-03-29 08:07:40 -05:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 10:47:46 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
return rpcSub, nil
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncingResult provides information about the current synchronisation status for this node.
|
|
|
|
type SyncingResult struct {
|
2016-09-06 04:39:14 -05:00
|
|
|
Syncing bool `json:"syncing"`
|
|
|
|
Status ethereum.SyncProgress `json:"status"`
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|
|
|
|
|
2022-08-19 01:00:21 -05:00
|
|
|
// uninstallSyncSubscriptionRequest uninstalls a syncing subscription in the API event loop.
|
2016-07-27 10:47:46 -05:00
|
|
|
type uninstallSyncSubscriptionRequest struct {
|
|
|
|
c chan interface{}
|
|
|
|
uninstalled chan interface{}
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// SyncStatusSubscription represents a syncing subscription.
|
|
|
|
type SyncStatusSubscription struct {
|
2022-06-21 04:05:43 -05: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
|
2016-07-27 10:47:46 -05:00
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-03-29 08:07:40 -05:00
|
|
|
|
2016-07-27 10:47:46 -05:00
|
|
|
// SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates.
|
2022-06-15 02:29:23 -05:00
|
|
|
// The given channel must receive interface values, the result can either.
|
2022-06-21 04:05:43 -05:00
|
|
|
func (api *DownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription {
|
2016-07-27 10:47:46 -05:00
|
|
|
api.installSyncSubscription <- status
|
|
|
|
return &SyncStatusSubscription{api: api, c: status}
|
2015-10-15 09:07:19 -05:00
|
|
|
}
|