aboutsummaryrefslogtreecommitdiffstats
path: root/ethclient
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-09-06 18:59:41 +0800
committerGitHub <noreply@github.com>2016-09-06 18:59:41 +0800
commit6fb8ae2bd6eb2b28f9b44799a8299183d634be80 (patch)
tree8ff6bb9a21870f6b26d2887ad82949a923d0d4df /ethclient
parenteac390f28955d66f9152102058e0d85d972bc033 (diff)
parent2924fdfcf7ab67a66a4ed3fb95cb9140be0cc809 (diff)
downloadgo-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar.gz
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar.bz2
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar.lz
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar.xz
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.tar.zst
go-tangerine-6fb8ae2bd6eb2b28f9b44799a8299183d634be80.zip
Merge pull request #2979 from karalabe/ethclient-sync-api
ethereum, ethclient: add SyncProgress API endpoint
Diffstat (limited to 'ethclient')
-rw-r--r--ethclient/ethclient.go33
-rw-r--r--ethclient/ethclient_test.go1
2 files changed, 34 insertions, 0 deletions
diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go
index 575a7cb44..50346f6ae 100644
--- a/ethclient/ethclient.go
+++ b/ethclient/ethclient.go
@@ -191,6 +191,39 @@ func toBlockNumArg(number *big.Int) string {
return fmt.Sprintf("%#x", number)
}
+type rpcProgress struct {
+ StartingBlock rpc.HexNumber
+ CurrentBlock rpc.HexNumber
+ HighestBlock rpc.HexNumber
+ PulledStates rpc.HexNumber
+ KnownStates rpc.HexNumber
+}
+
+// SyncProgress retrieves the current progress of the sync algorithm. If there's
+// no sync currently running, it returns nil.
+func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
+ var raw json.RawMessage
+ if err := ec.c.CallContext(ctx, &raw, "eth_syncing"); err != nil {
+ return nil, err
+ }
+ // Handle the possible response types
+ var syncing bool
+ if err := json.Unmarshal(raw, &syncing); err == nil {
+ return nil, nil // Not syncing (always false)
+ }
+ var progress *rpcProgress
+ if err := json.Unmarshal(raw, &progress); err != nil {
+ return nil, err
+ }
+ return &ethereum.SyncProgress{
+ StartingBlock: progress.StartingBlock.Uint64(),
+ CurrentBlock: progress.CurrentBlock.Uint64(),
+ HighestBlock: progress.HighestBlock.Uint64(),
+ PulledStates: progress.PulledStates.Uint64(),
+ KnownStates: progress.KnownStates.Uint64(),
+ }, nil
+}
+
// SubscribeNewHead subscribes to notifications about the current blockchain head
// on the given channel.
func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go
index 47e37c0ce..0cc11eb5b 100644
--- a/ethclient/ethclient_test.go
+++ b/ethclient/ethclient_test.go
@@ -6,6 +6,7 @@ import "github.com/ethereum/go-ethereum"
var (
_ = ethereum.ChainReader(&Client{})
_ = ethereum.ChainStateReader(&Client{})
+ _ = ethereum.ChainSyncReader(&Client{})
_ = ethereum.ChainHeadEventer(&Client{})
_ = ethereum.ContractCaller(&Client{})
_ = ethereum.GasEstimator(&Client{})