From 32d09736e0bf026d357d14fcbdc2249c1d0d0915 Mon Sep 17 00:00:00 2001 From: Giovanni Vignone <72773059+giovannivignone@users.noreply.github.com> Date: Sat, 11 Feb 2023 18:05:22 -0500 Subject: [PATCH] feat: add eth_syncing (#188) * adding documentation for rpc.md * adding rpc methods in table for rpc.md * adjusting readme to link to rpc.md * fixing grammar * grammar * adding RPC Methods according to documentation and listing column as Client Function * adding more description space * undoing description spacing * adding get block transaction count by hash to node.rs and rpc.rs * functioning getblocktransactioncountbyhash function * removing documentation * adding second rpc method and simplifying logic * adjusting example and node.rs * formatting * fixing clippy errors * adding to client and to rpc.md * formatting * integrating into client * u64 return types, rpc.md updated to get_nonce, get_transaction_count -> get_nonce revert * cargo fmt * readme architecture init * removing blockchain * removing complexity * updating mermaid with links from evm -> execution, renaming, and recoloring * coloring letters black * removing uncessary styling and adding untrustedexecutionrpc and untrustedconsensusrpc * initial syncing work * adding in proper computations to syncing() * simplificaiton of logic * cargo fmt * updaing rpc.md and rpc * removing unwrapping --- client/src/client.rs | 6 +++++- client/src/node.rs | 35 ++++++++++++++++++++++++++++++++++- client/src/rpc.rs | 9 ++++++++- rpc.md | 4 +++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index 1095dda..77abdce 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use config::networks::Network; use consensus::errors::ConsensusError; use ethers::prelude::{Address, U256}; -use ethers::types::{Filter, Log, Transaction, TransactionReceipt, H256}; +use ethers::types::{Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256}; use eyre::{eyre, Result}; use common::types::BlockTag; @@ -541,6 +541,10 @@ impl Client { self.node.read().await.chain_id() } + pub async fn syncing(&self) -> Result { + self.node.read().await.syncing() + } + pub async fn get_header(&self) -> Result
{ self.node.read().await.get_header() } diff --git a/client/src/node.rs b/client/src/node.rs index cb4e35d..6f06cee 100644 --- a/client/src/node.rs +++ b/client/src/node.rs @@ -3,7 +3,9 @@ use std::sync::Arc; use std::time::Duration; use ethers::prelude::{Address, U256}; -use ethers::types::{Filter, Log, Transaction, TransactionReceipt, H256}; +use ethers::types::{ + Filter, Log, SyncProgress, SyncingStatus, Transaction, TransactionReceipt, H256, +}; use eyre::{eyre, Result}; use common::errors::BlockNotFoundError; @@ -315,6 +317,37 @@ impl Node { self.config.chain.chain_id } + pub fn syncing(&self) -> Result { + if self.check_head_age().is_ok() { + Ok(SyncingStatus::IsFalse) + } else { + let latest_synced_block = self.get_block_number()?; + let oldest_payload = self.payloads.first_key_value(); + let oldest_synced_block = + oldest_payload.map_or(latest_synced_block, |(key, _value)| *key); + let highest_block = self.consensus.expected_current_slot(); + Ok(SyncingStatus::IsSyncing(Box::new(SyncProgress { + current_block: latest_synced_block.into(), + highest_block: highest_block.into(), + starting_block: oldest_synced_block.into(), + pulled_states: None, + known_states: None, + healed_bytecode_bytes: None, + healed_bytecodes: None, + healed_trienode_bytes: None, + healed_trienodes: None, + healing_bytecode: None, + healing_trienodes: None, + synced_account_bytes: None, + synced_accounts: None, + synced_bytecode_bytes: None, + synced_bytecodes: None, + synced_storage: None, + synced_storage_bytes: None, + }))) + } + } + pub fn get_header(&self) -> Result
{ self.check_head_age()?; Ok(self.consensus.get_header().clone()) diff --git a/client/src/rpc.rs b/client/src/rpc.rs index 8044da2..392e547 100644 --- a/client/src/rpc.rs +++ b/client/src/rpc.rs @@ -1,6 +1,6 @@ use ethers::{ abi::AbiEncode, - types::{Address, Filter, Log, Transaction, TransactionReceipt, H256, U256}, + types::{Address, Filter, Log, SyncingStatus, Transaction, TransactionReceipt, H256, U256}, }; use eyre::Result; use log::info; @@ -114,6 +114,8 @@ trait EthRpc { ) -> Result; #[method(name = "getCoinbase")] async fn get_coinbase(&self) -> Result; + #[method(name = "syncing")] + async fn syncing(&self) -> Result; } #[rpc(client, server, namespace = "net")] @@ -278,6 +280,11 @@ impl EthRpcServer for RpcInner { Ok(node.get_coinbase().unwrap()) } + async fn syncing(&self) -> Result { + let node = self.node.read().await; + convert_err(node.syncing()) + } + async fn get_logs(&self, filter: Filter) -> Result, Error> { let node = self.node.read().await; convert_err(node.get_logs(&filter).await) diff --git a/rpc.md b/rpc.md index 07c5097..2672cfd 100644 --- a/rpc.md +++ b/rpc.md @@ -22,4 +22,6 @@ Helios provides a variety of RPC methods for interacting with the Ethereum netwo | `eth_getLogs` | `get_logs` | Returns an array of logs matching the filter. | `client.get_logs(&self, filter: Filter)` | | `eth_getStorageAt` | `get_storage_at` | Returns the value from a storage position at a given address. | `client.get_storage_at(&self, address: &str, slot: H256, block: BlockTag)` | | `eth_getBlockTransactionCountByHash` | `get_block_transaction_count_by_hash` | Returns the number of transactions in a block from a block matching the transaction hash. | `client.get_block_transaction_count_by_hash(&self, hash: &str)` | -| `eth_getBlockTransactionCountByNumber` | `get_block_transaction_count_by_number` | Returns the number of transactions in a block from a block matching the block number. | `client.get_block_transaction_count_by_number(&self, block: BlockTag)` | \ No newline at end of file +| `eth_getBlockTransactionCountByNumber` | `get_block_transaction_count_by_number` | Returns the number of transactions in a block from a block matching the block number. | `client.get_block_transaction_count_by_number(&self, block: BlockTag)` | +| `eth_coinbase` | `get_coinbase` | Returns the client coinbase address. | `client.get_coinbase(&self)` | +| `eth_syncing` | `syncing` | Returns an object with data about the sync status or false. | `client.syncing(&self)` | \ No newline at end of file