github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/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  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum"
    25  	"github.com/ethereum/go-ethereum/core"
    26  	"github.com/ethereum/go-ethereum/event"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  // DownloaderAPI provides an API which gives information about the current
    31  // synchronisation status. It offers only methods that operates on data that
    32  // can be available to anyone without security risks.
    33  type DownloaderAPI struct {
    34  	d                         *Downloader
    35  	chain                     *core.BlockChain
    36  	mux                       *event.TypeMux
    37  	installSyncSubscription   chan chan interface{}
    38  	uninstallSyncSubscription chan *uninstallSyncSubscriptionRequest
    39  }
    40  
    41  // NewDownloaderAPI creates a new DownloaderAPI. The API has an internal event loop that
    42  // listens for events from the downloader through the global event mux. In case it receives one of
    43  // these events it broadcasts it to all syncing subscriptions that are installed through the
    44  // installSyncSubscription channel.
    45  func NewDownloaderAPI(d *Downloader, chain *core.BlockChain, m *event.TypeMux) *DownloaderAPI {
    46  	api := &DownloaderAPI{
    47  		d:                         d,
    48  		chain:                     chain,
    49  		mux:                       m,
    50  		installSyncSubscription:   make(chan chan interface{}),
    51  		uninstallSyncSubscription: make(chan *uninstallSyncSubscriptionRequest),
    52  	}
    53  	go api.eventLoop()
    54  	return api
    55  }
    56  
    57  // eventLoop runs a loop until the event mux closes. It will install and uninstall
    58  // new sync subscriptions and broadcasts sync status updates to the installed sync
    59  // subscriptions.
    60  //
    61  // The sync status pushed to subscriptions can be a stream like:
    62  // >>> {Syncing: true, Progress: {...}}
    63  // >>> {false}
    64  //
    65  // If the node is already synced up, then only a single event subscribers will
    66  // receive is {false}.
    67  func (api *DownloaderAPI) eventLoop() {
    68  	var (
    69  		sub               = api.mux.Subscribe(StartEvent{})
    70  		syncSubscriptions = make(map[chan interface{}]struct{})
    71  		checkInterval     = time.Second * 60
    72  		checkTimer        = time.NewTimer(checkInterval)
    73  
    74  		// status flags
    75  		started bool
    76  		done    bool
    77  
    78  		getProgress = func() ethereum.SyncProgress {
    79  			prog := api.d.Progress()
    80  			if txProg, err := api.chain.TxIndexProgress(); err == nil {
    81  				prog.TxIndexFinishedBlocks = txProg.Indexed
    82  				prog.TxIndexRemainingBlocks = txProg.Remaining
    83  			}
    84  			return prog
    85  		}
    86  	)
    87  	defer checkTimer.Stop()
    88  
    89  	for {
    90  		select {
    91  		case i := <-api.installSyncSubscription:
    92  			syncSubscriptions[i] = struct{}{}
    93  			if done {
    94  				i <- false
    95  			}
    96  		case u := <-api.uninstallSyncSubscription:
    97  			delete(syncSubscriptions, u.c)
    98  			close(u.uninstalled)
    99  		case event := <-sub.Chan():
   100  			if event == nil {
   101  				return
   102  			}
   103  			switch event.Data.(type) {
   104  			case StartEvent:
   105  				started = true
   106  			}
   107  		case <-checkTimer.C:
   108  			if !started {
   109  				checkTimer.Reset(checkInterval)
   110  				continue
   111  			}
   112  			prog := getProgress()
   113  			if !prog.Done() {
   114  				notification := &SyncingResult{
   115  					Syncing: true,
   116  					Status:  prog,
   117  				}
   118  				for c := range syncSubscriptions {
   119  					c <- notification
   120  				}
   121  				checkTimer.Reset(checkInterval)
   122  				continue
   123  			}
   124  			for c := range syncSubscriptions {
   125  				c <- false
   126  			}
   127  			done = true
   128  		}
   129  	}
   130  }
   131  
   132  // Syncing provides information when this nodes starts synchronising with the Ethereum network and when it's finished.
   133  func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error) {
   134  	notifier, supported := rpc.NotifierFromContext(ctx)
   135  	if !supported {
   136  		return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
   137  	}
   138  
   139  	rpcSub := notifier.CreateSubscription()
   140  
   141  	go func() {
   142  		statuses := make(chan interface{})
   143  		sub := api.SubscribeSyncStatus(statuses)
   144  		defer sub.Unsubscribe()
   145  
   146  		for {
   147  			select {
   148  			case status := <-statuses:
   149  				notifier.Notify(rpcSub.ID, status)
   150  			case <-rpcSub.Err():
   151  				return
   152  			}
   153  		}
   154  	}()
   155  
   156  	return rpcSub, nil
   157  }
   158  
   159  // SyncingResult provides information about the current synchronisation status for this node.
   160  type SyncingResult struct {
   161  	Syncing bool                  `json:"syncing"`
   162  	Status  ethereum.SyncProgress `json:"status"`
   163  }
   164  
   165  // uninstallSyncSubscriptionRequest uninstalls a syncing subscription in the API event loop.
   166  type uninstallSyncSubscriptionRequest struct {
   167  	c           chan interface{}
   168  	uninstalled chan interface{}
   169  }
   170  
   171  // SyncStatusSubscription represents a syncing subscription.
   172  type SyncStatusSubscription struct {
   173  	api       *DownloaderAPI   // register subscription in event loop of this api instance
   174  	c         chan interface{} // channel where events are broadcasted to
   175  	unsubOnce sync.Once        // make sure unsubscribe logic is executed once
   176  }
   177  
   178  // Unsubscribe uninstalls the subscription from the DownloadAPI event loop.
   179  // The status channel that was passed to subscribeSyncStatus isn't used anymore
   180  // after this method returns.
   181  func (s *SyncStatusSubscription) Unsubscribe() {
   182  	s.unsubOnce.Do(func() {
   183  		req := uninstallSyncSubscriptionRequest{s.c, make(chan interface{})}
   184  		s.api.uninstallSyncSubscription <- &req
   185  
   186  		for {
   187  			select {
   188  			case <-s.c:
   189  				// drop new status events until uninstall confirmation
   190  				continue
   191  			case <-req.uninstalled:
   192  				return
   193  			}
   194  		}
   195  	})
   196  }
   197  
   198  // SubscribeSyncStatus creates a subscription that will broadcast new synchronisation updates.
   199  // The given channel must receive interface values, the result can either.
   200  func (api *DownloaderAPI) SubscribeSyncStatus(status chan interface{}) *SyncStatusSubscription {
   201  	api.installSyncSubscription <- status
   202  	return &SyncStatusSubscription{api: api, c: status}
   203  }