Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

add eth_syncing RPC #848

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Unreleased

- Add `eth_syncing` [848](https://github.com/gakonst/ethers-rs/pull/848)
- Fix overflow and possible divide-by-zero in `estimate_priority_fee`
- Add BSC mainnet and testnet to the list of known chains
[831](https://github.com/gakonst/ethers-rs/pull/831)
Expand Down
14 changes: 13 additions & 1 deletion ethers-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use pubsub::{PubsubClient, SubscriptionStream};
use async_trait::async_trait;
use auto_impl::auto_impl;
use ethers_core::types::transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed};
use serde::{de::DeserializeOwned, Serialize};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{error::Error, fmt::Debug, future::Future, pin::Pin};

pub use provider::{FilterKind, Provider, ProviderError};
Expand Down Expand Up @@ -79,6 +79,14 @@ where
}
}

/// Structure used in eth_syncing RPC
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum SyncingStatus {
IsFalse(bool),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a note on how this works? not clear to me what IsFalse(false) vs IsFalse(true) means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments, but IsFalse(true) is not possible to happen by eth spec. wdyt about adding an additional struct, first for deserialization and second for returning to library call?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh as in the commit you pushed does not work? yeah I'm OK with adding an intermediate non-public datatype that helps with the desert and then having the "simple" enum.

Copy link
Contributor Author

@rakita rakita Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea i made mistake with that commit, now it should work as expected (tested it with infure when return is false). Can you check it again if it is okay, I embedded intermediate type inside function.

IsSyncing { starting_block: U256, current_block: U256, highest_block: U256 },
}

/// A middleware allows customizing requests send and received from an ethereum node.
///
/// Writing a middleware is as simple as:
Expand Down Expand Up @@ -303,6 +311,10 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().call(tx, block).await.map_err(FromErr::from)
}

async fn syncing(&self) -> Result<SyncingStatus, Self::Error> {
self.inner().syncing().await.map_err(FromErr::from)
}

async fn get_chainid(&self) -> Result<U256, Self::Error> {
self.inner().get_chainid().await.map_err(FromErr::from)
}
Expand Down
7 changes: 6 additions & 1 deletion ethers-providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
pubsub::{PubsubClient, SubscriptionStream},
stream::{FilterWatcher, DEFAULT_POLL_INTERVAL},
FromErr, Http as HttpProvider, JsonRpcClient, JsonRpcClientWrapper, MockProvider,
PendingTransaction, QuorumProvider,
PendingTransaction, QuorumProvider, SyncingStatus,
};

#[cfg(feature = "celo")]
Expand Down Expand Up @@ -497,6 +497,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.request("eth_chainId", ()).await
}

/// Return current client syncing status. If IsFalse sync is over.
async fn syncing(&self) -> Result<SyncingStatus, Self::Error> {
self.request("eth_syncing", ()).await
}

/// Returns the network version.
async fn get_net_version(&self) -> Result<U64, ProviderError> {
self.request("net_version", ()).await
Expand Down