aboutsummaryrefslogtreecommitdiffstats
path: root/eth/downloader
diff options
context:
space:
mode:
authorBas van Kervel <bas@ethdev.com>2015-10-15 22:07:19 +0800
committerBas van Kervel <bas@ethdev.com>2015-12-14 23:34:05 +0800
commiteae81465c1c815c317cd30e4de6bdf4d59df2340 (patch)
treeb6f4b7787967a58416171adb79fd12ac29d89577 /eth/downloader
parent8db9d44ca9fb6baf406256cae491c475de2f4989 (diff)
downloaddexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.gz
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.bz2
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.lz
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.xz
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.zst
dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.zip
rpc: new RPC implementation with pub/sub support
Diffstat (limited to 'eth/downloader')
-rw-r--r--eth/downloader/api.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/eth/downloader/api.go b/eth/downloader/api.go
new file mode 100644
index 000000000..9deff22a1
--- /dev/null
+++ b/eth/downloader/api.go
@@ -0,0 +1,64 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of go-ethereum.
+//
+// go-ethereum is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// go-ethereum 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
+
+package downloader
+
+import (
+ rpc "github.com/ethereum/go-ethereum/rpc/v2"
+)
+
+// PublicDownloaderAPI provides an API which gives informatoin about the current synchronisation status.
+// It offers only methods that operates on data that can be available to anyone without security risks.
+type PublicDownloaderAPI struct {
+ d *Downloader
+}
+
+// NewPublicDownloaderAPI create a new PublicDownloaderAPI.
+func NewPublicDownloaderAPI(d *Downloader) *PublicDownloaderAPI {
+ return &PublicDownloaderAPI{d}
+}
+
+// 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"`
+}
+
+// SyncingResult provides information about the current synchronisation status for this node.
+type SyncingResult struct {
+ Syncing bool `json:"syncing"`
+ Status Progress `json:"status"`
+}
+
+// Syncing provides information when this nodes starts synchronising with the Ethereumn network and when it's finished.
+func (s *PublicDownloaderAPI) Syncing() (rpc.Subscription, error) {
+ sub := s.d.mux.Subscribe(StartEvent{}, DoneEvent{}, FailedEvent{})
+
+ output := func(event interface{}) interface{} {
+ switch event.(type) {
+ case StartEvent:
+ result := &SyncingResult{Syncing: true}
+ result.Status.Origin, result.Status.Current, result.Status.Height = s.d.Progress()
+ return result
+ case DoneEvent, FailedEvent:
+ return false
+ }
+ return nil
+ }
+
+ return rpc.NewSubscriptionWithOutputFormat(sub, output), nil
+}