From 0c807eac73acaa3d90dd38a2bad57694359cf38d Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 5 Dec 2023 16:29:57 +0000 Subject: [PATCH 1/6] #1199 eth syncing --- libethereum/Client.cpp | 5 +---- libethereum/SkaleHost.cpp | 11 +++++++++++ libethereum/SkaleHost.h | 2 ++ libweb3jsonrpc/Eth.cpp | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 00c90351c..14ad8e367 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -1128,10 +1128,7 @@ Transactions Client::pending() const { } SyncStatus Client::syncStatus() const { - // TODO implement this when syncing will be needed - SyncStatus s; - s.startBlockNumber = s.currentBlockNumber = s.highestBlockNumber = 0; - return s; + return m_skaleHost->syncStatus(); } TransactionSkeleton Client::populateTransactionWithDefaults( TransactionSkeleton const& _t ) const { diff --git a/libethereum/SkaleHost.cpp b/libethereum/SkaleHost.cpp index 0cbb4241f..216c5c1bf 100644 --- a/libethereum/SkaleHost.cpp +++ b/libethereum/SkaleHost.cpp @@ -942,6 +942,17 @@ u256 SkaleHost::getBlockRandom() const { return m_consensus->getRandomForBlockId( m_client.number() ); } +dev::eth::SyncStatus SkaleHost::syncStatus() const { + auto syncInfo = m_consensus->getSyncInfo(); + dev::eth::SyncStatus syncStatus; + syncStatus.state = syncInfo.isSyncing ? dev::eth::SyncState::Blocks : dev::eth::SyncState::Idle; + syncStatus.startBlockNumber = syncInfo.startingBlock; + syncStatus.currentBlockNumber = syncInfo.currentBlock; + syncStatus.highestBlockNumber = syncInfo.highestBlock; + syncStatus.majorSyncing = syncInfo.isSyncing; + return syncStatus; +} + std::map< std::string, uint64_t > SkaleHost::getConsensusDbUsage() const { return m_consensus->getConsensusDbUsage(); } diff --git a/libethereum/SkaleHost.h b/libethereum/SkaleHost.h index 82a4c6357..a92fc5462 100644 --- a/libethereum/SkaleHost.h +++ b/libethereum/SkaleHost.h @@ -51,6 +51,7 @@ namespace dev { namespace eth { +struct SyncStatus; class Client; class TransactionQueue; class BlockHeader; @@ -123,6 +124,7 @@ class SkaleHost { dev::u256 getGasPrice() const; dev::u256 getBlockRandom() const; + dev::eth::SyncStatus syncStatus() const; std::map< std::string, uint64_t > getConsensusDbUsage() const; std::array< std::string, 4 > getIMABLSPublicKey() const; diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 135b08584..4a7d20bd4 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -893,7 +893,7 @@ Json::Value Eth::eth_getWork() { Json::Value Eth::eth_syncing() { dev::eth::SyncStatus sync = client()->syncStatus(); - if ( sync.state == SyncState::Idle || !sync.majorSyncing ) + if ( !sync.majorSyncing ) return Json::Value( false ); Json::Value info( Json::objectValue ); From 164b7ca127640d6041ec7d1395063fe745ce0aff Mon Sep 17 00:00:00 2001 From: Oleh Nikolaiev Date: Tue, 5 Dec 2023 16:43:03 +0000 Subject: [PATCH 2/6] #1199 add comments --- libethereum/SkaleHost.cpp | 2 ++ libweb3jsonrpc/Eth.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/libethereum/SkaleHost.cpp b/libethereum/SkaleHost.cpp index 216c5c1bf..9ebea7d1a 100644 --- a/libethereum/SkaleHost.cpp +++ b/libethereum/SkaleHost.cpp @@ -945,6 +945,8 @@ u256 SkaleHost::getBlockRandom() const { dev::eth::SyncStatus SkaleHost::syncStatus() const { auto syncInfo = m_consensus->getSyncInfo(); dev::eth::SyncStatus syncStatus; + // SKALE: catchup downloads blocks with transactions, then the node executes them + // we don't download state changes separately syncStatus.state = syncInfo.isSyncing ? dev::eth::SyncState::Blocks : dev::eth::SyncState::Idle; syncStatus.startBlockNumber = syncInfo.startingBlock; syncStatus.currentBlockNumber = syncInfo.currentBlock; diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 4a7d20bd4..988022a29 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -892,6 +892,7 @@ Json::Value Eth::eth_getWork() { } Json::Value Eth::eth_syncing() { + // ask consensus whether the node is in catchup mode dev::eth::SyncStatus sync = client()->syncStatus(); if ( !sync.majorSyncing ) return Json::Value( false ); From 640b77b960285362531626e1327dece311b9f05d Mon Sep 17 00:00:00 2001 From: Oleh Date: Wed, 6 Dec 2023 18:56:22 +0000 Subject: [PATCH 3/6] #1199 add more exceptions --- libethereum/Client.cpp | 2 ++ libethereum/SkaleHost.cpp | 2 ++ libweb3jsonrpc/Eth.cpp | 26 ++++++++++++++++---------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 14ad8e367..61c897ec9 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -1128,6 +1128,8 @@ Transactions Client::pending() const { } SyncStatus Client::syncStatus() const { + if ( !m_skaleHost ) + BOOST_THROW_EXCEPTION( std::runtime_error( "SkaleHost was not initialized" ) ); return m_skaleHost->syncStatus(); } diff --git a/libethereum/SkaleHost.cpp b/libethereum/SkaleHost.cpp index 9ebea7d1a..0350fef51 100644 --- a/libethereum/SkaleHost.cpp +++ b/libethereum/SkaleHost.cpp @@ -943,6 +943,8 @@ u256 SkaleHost::getBlockRandom() const { } dev::eth::SyncStatus SkaleHost::syncStatus() const { + if ( !m_consensus ) + BOOST_THROW_EXCEPTION( std::runtime_error( "Consensus was not initialized" ) ); auto syncInfo = m_consensus->getSyncInfo(); dev::eth::SyncStatus syncStatus; // SKALE: catchup downloads blocks with transactions, then the node executes them diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 988022a29..5b75e1180 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -892,16 +892,22 @@ Json::Value Eth::eth_getWork() { } Json::Value Eth::eth_syncing() { - // ask consensus whether the node is in catchup mode - dev::eth::SyncStatus sync = client()->syncStatus(); - if ( !sync.majorSyncing ) - return Json::Value( false ); - - Json::Value info( Json::objectValue ); - info["startingBlock"] = sync.startBlockNumber; - info["highestBlock"] = sync.highestBlockNumber; - info["currentBlock"] = sync.currentBlockNumber; - return info; + try { + // ask consensus whether the node is in catchup mode + if ( !client() ) + BOOST_THROW_EXCEPTION( std::runtime_error( "Client was not initialized" ) ); + dev::eth::SyncStatus sync = client()->syncStatus(); + if ( !sync.majorSyncing ) + return Json::Value( false ); + + Json::Value info( Json::objectValue ); + info["startingBlock"] = sync.startBlockNumber; + info["highestBlock"] = sync.highestBlockNumber; + info["currentBlock"] = sync.currentBlockNumber; + return info; + } catch ( const Exception& e ) { + BOOST_THROW_EXCEPTION( jsonrpc::JsonRpcException( e.what() ) ); + } } string Eth::eth_chainId() { From b721cecde87c327c245b01e046bd48a044e41e6b Mon Sep 17 00:00:00 2001 From: Oleh Date: Wed, 6 Dec 2023 19:02:14 +0000 Subject: [PATCH 4/6] #1199 add more exceptions --- libweb3jsonrpc/Eth.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 5b75e1180..79bc92948 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -893,10 +893,12 @@ Json::Value Eth::eth_getWork() { Json::Value Eth::eth_syncing() { try { - // ask consensus whether the node is in catchup mode - if ( !client() ) + auto client = client(); + if ( !client ) BOOST_THROW_EXCEPTION( std::runtime_error( "Client was not initialized" ) ); - dev::eth::SyncStatus sync = client()->syncStatus(); + + // ask consensus whether the node is in catchup mode + dev::eth::SyncStatus sync = client->syncStatus(); if ( !sync.majorSyncing ) return Json::Value( false ); From 35f4f655275b443daa03250370da63fbdc099c58 Mon Sep 17 00:00:00 2001 From: Oleh Date: Wed, 6 Dec 2023 19:05:37 +0000 Subject: [PATCH 5/6] #1199 format --- libweb3jsonrpc/Eth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 79bc92948..11cddf207 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -896,7 +896,7 @@ Json::Value Eth::eth_syncing() { auto client = client(); if ( !client ) BOOST_THROW_EXCEPTION( std::runtime_error( "Client was not initialized" ) ); - + // ask consensus whether the node is in catchup mode dev::eth::SyncStatus sync = client->syncStatus(); if ( !sync.majorSyncing ) From 1af8e4d9527597d7a0c9e35d24797f2661c68cd9 Mon Sep 17 00:00:00 2001 From: Oleh Date: Thu, 7 Dec 2023 10:13:48 +0000 Subject: [PATCH 6/6] #1199 fix build --- libweb3jsonrpc/Eth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libweb3jsonrpc/Eth.cpp b/libweb3jsonrpc/Eth.cpp index 11cddf207..9686c5301 100644 --- a/libweb3jsonrpc/Eth.cpp +++ b/libweb3jsonrpc/Eth.cpp @@ -893,7 +893,7 @@ Json::Value Eth::eth_getWork() { Json::Value Eth::eth_syncing() { try { - auto client = client(); + auto client = this->client(); if ( !client ) BOOST_THROW_EXCEPTION( std::runtime_error( "Client was not initialized" ) );