mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(rpc): add eth_syncing implementation (#1859)
This commit is contained in:
@ -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()));
|
||||
|
||||
@ -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`
|
||||
|
||||
@ -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`
|
||||
|
||||
@ -172,9 +172,9 @@ where
|
||||
{
|
||||
/// Returns the current sync status for the `syncing` subscription
|
||||
async fn sync_status(&self, is_syncing: bool) -> EthSubscriptionResult {
|
||||
let current_block =
|
||||
self.client.chain_info().map(|info| info.best_number).unwrap_or_default();
|
||||
if is_syncing {
|
||||
let current_block =
|
||||
self.client.chain_info().map(|info| info.best_number).unwrap_or_default();
|
||||
EthSubscriptionResult::SyncState(PubSubSyncStatus::Detailed(SyncStatusMetadata {
|
||||
syncing: true,
|
||||
starting_block: 0,
|
||||
|
||||
Reference in New Issue
Block a user