feat(rpc): add eth_syncing implementation (#1859)

This commit is contained in:
Matthias Seitz
2023-03-20 12:22:17 +01:00
committed by GitHub
parent 1891e13d30
commit 2a457c7023
7 changed files with 46 additions and 6 deletions

View File

@ -40,6 +40,9 @@ pub trait NetworkInfo: Send + Sync {
/// Returns the chain id
fn chain_id(&self) -> u64;
/// Returns `true` if the network is undergoing sync.
fn is_syncing(&self) -> bool;
}
/// Provides general purpose information about Peers in the network.

View File

@ -35,6 +35,10 @@ impl NetworkInfo for NoopNetwork {
fn chain_id(&self) -> u64 {
Mainnet.into()
}
fn is_syncing(&self) -> bool {
false
}
}
impl PeersInfo for NoopNetwork {

View File

@ -229,6 +229,10 @@ impl NetworkInfo for NetworkHandle {
fn chain_id(&self) -> u64 {
self.inner.chain_id.load(Ordering::Relaxed)
}
fn is_syncing(&self) -> bool {
SyncStateProvider::is_syncing(self)
}
}
impl StatusUpdater for NetworkHandle {

View File

@ -94,9 +94,9 @@ where
EthApiClient::call(client, call_request.clone(), Some(block_number.into()), None)
.await
.unwrap();
EthApiClient::syncing(client).await.unwrap();
// Unimplemented
assert!(is_unimplemented(EthApiClient::syncing(client).await.err().unwrap()));
assert!(is_unimplemented(EthApiClient::author(client).await.err().unwrap()));
assert!(is_unimplemented(EthApiClient::transaction_receipt(client, hash).await.err().unwrap()));
assert!(is_unimplemented(EthApiClient::gas_price(client).await.err().unwrap()));

View File

@ -7,9 +7,9 @@ use crate::eth::{cache::EthStateCache, signer::EthSigner};
use async_trait::async_trait;
use reth_interfaces::Result;
use reth_network_api::NetworkInfo;
use reth_primitives::{Address, BlockId, BlockNumberOrTag, ChainInfo, H256, U64};
use reth_primitives::{Address, BlockId, BlockNumberOrTag, ChainInfo, H256, U256, U64};
use reth_provider::{providers::ChainState, BlockProvider, EvmEnvProvider, StateProviderFactory};
use reth_rpc_types::FeeHistoryCache;
use reth_rpc_types::{FeeHistoryCache, SyncInfo, SyncStatus};
use reth_transaction_pool::TransactionPool;
use std::{num::NonZeroUsize, sync::Arc};
@ -41,6 +41,12 @@ pub trait EthApiSpec: EthTransactions + Send + Sync {
/// Returns a list of addresses owned by client.
fn accounts(&self) -> Vec<Address>;
/// Returns `true` if the network is undergoing sync.
fn is_syncing(&self) -> bool;
/// Returns the [SyncStatus] of the network
fn sync_status(&self) -> Result<SyncStatus>;
}
/// `Eth` API implementation.
@ -193,6 +199,29 @@ where
fn accounts(&self) -> Vec<Address> {
self.inner.signers.iter().flat_map(|s| s.accounts()).collect()
}
fn is_syncing(&self) -> bool {
self.network().is_syncing()
}
/// Returns the [SyncStatus] of the network
fn sync_status(&self) -> Result<SyncStatus> {
let status = if self.is_syncing() {
let current_block = U256::from(
self.client().chain_info().map(|info| info.best_number).unwrap_or_default(),
);
SyncStatus::Info(SyncInfo {
starting_block: U256::from(0),
current_block,
highest_block: current_block,
warp_chunks_amount: None,
warp_chunks_processed: None,
})
} else {
SyncStatus::None
};
Ok(status)
}
}
/// Container type `EthApi`

View File

@ -39,7 +39,7 @@ where
/// Handler for: `eth_syncing`
fn syncing(&self) -> Result<SyncStatus> {
Err(internal_rpc_err("unimplemented"))
EthApiSpec::sync_status(self).to_rpc_result()
}
/// Handler for: `eth_coinbase`

View File

@ -172,9 +172,9 @@ where
{
/// Returns the current sync status for the `syncing` subscription
async fn sync_status(&self, is_syncing: bool) -> EthSubscriptionResult {
if is_syncing {
let current_block =
self.client.chain_info().map(|info| info.best_number).unwrap_or_default();
if is_syncing {
EthSubscriptionResult::SyncState(PubSubSyncStatus::Detailed(SyncStatusMetadata {
syncing: true,
starting_block: 0,