From 91461c416237532f334299ff39e2f4d4a321d316 Mon Sep 17 00:00:00 2001 From: Dmytro Kostenko Date: Thu, 20 Jul 2023 12:30:39 +0200 Subject: [PATCH 1/4] feat: add pbftHash to graphql pbft block --- CMakeModules/cpp_graphql_gen.cmake | 51 +++++++------- libraries/core_libs/CMakeLists.txt | 48 +++++++------ .../network/graphql/include/graphql/block.hpp | 6 +- .../network/graphql/include/graphql/query.hpp | 2 + .../graphql/include/graphql/transaction.hpp | 2 + .../include/graphql/types/dag_block.hpp | 4 +- .../graphql/schema/schema.taraxa.graphql | 2 + .../core_libs/network/graphql/src/block.cpp | 16 +++-- .../core_libs/network/graphql/src/query.cpp | 67 ++++++++++--------- .../network/graphql/src/transaction.cpp | 5 +- .../network/graphql/src/types/dag_block.cpp | 8 ++- 11 files changed, 120 insertions(+), 91 deletions(-) diff --git a/CMakeModules/cpp_graphql_gen.cmake b/CMakeModules/cpp_graphql_gen.cmake index 8cfbcfbafe..1ee15d1f86 100644 --- a/CMakeModules/cpp_graphql_gen.cmake +++ b/CMakeModules/cpp_graphql_gen.cmake @@ -1,4 +1,4 @@ -########### cppgraphqlgen ########### +# ########## cppgraphqlgen ########### include(FetchContent) set(Boost_NO_WARN_NEW_VERSIONS 1) @@ -6,36 +6,41 @@ set(Boost_NO_WARN_NEW_VERSIONS 1) FetchContent_Declare( cppgraphqlgen GIT_REPOSITORY https://github.com/microsoft/cppgraphqlgen.git - GIT_TAG 8c1623acc42392ef2a1bc0336482621386f98c77 # v4.5.0 + GIT_TAG 8c1623acc42392ef2a1bc0336482621386f98c77 # v4.5.0 ) set(GRAPHQL_BUILD_TESTS OFF) set(GRAPHQL_UPDATE_VERSION OFF) set(GRAPHQL_UPDATE_SAMPLES OFF) -#set(GRAPHQL_BUILD_SCHEMAGEN OFF) + +# set(GRAPHQL_BUILD_SCHEMAGEN OFF) set(GRAPHQL_BUILD_CLIENTGEN OFF) FetchContent_MakeAvailable(cppgraphqlgen) -###################################### + +# ##################################### set(GRAPHQL_GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/gen) list(APPEND GRAPHQL_GENERATED_SOURCES - ${GRAPHQL_GEN_DIR}/AccountObject.cpp - ${GRAPHQL_GEN_DIR}/BlockObject.cpp - ${GRAPHQL_GEN_DIR}/CallResultObject.cpp - ${GRAPHQL_GEN_DIR}/CurrentStateObject.cpp - ${GRAPHQL_GEN_DIR}/DagBlockObject.cpp - ${GRAPHQL_GEN_DIR}/LogObject.cpp - ${GRAPHQL_GEN_DIR}/MutationObject.cpp - ${GRAPHQL_GEN_DIR}/PendingObject.cpp - ${GRAPHQL_GEN_DIR}/QueryObject.cpp - ${GRAPHQL_GEN_DIR}/SubscriptionObject.cpp - ${GRAPHQL_GEN_DIR}/SyncStateObject.cpp - ${GRAPHQL_GEN_DIR}/TaraxaSchema.cpp - ${GRAPHQL_GEN_DIR}/TransactionObject.cpp + ${GRAPHQL_GEN_DIR}/AccountObject.cpp + ${GRAPHQL_GEN_DIR}/BlockObject.cpp + ${GRAPHQL_GEN_DIR}/CallResultObject.cpp + ${GRAPHQL_GEN_DIR}/CurrentStateObject.cpp + ${GRAPHQL_GEN_DIR}/DagBlockObject.cpp + ${GRAPHQL_GEN_DIR}/LogObject.cpp + ${GRAPHQL_GEN_DIR}/MutationObject.cpp + ${GRAPHQL_GEN_DIR}/PendingObject.cpp + ${GRAPHQL_GEN_DIR}/QueryObject.cpp + ${GRAPHQL_GEN_DIR}/SubscriptionObject.cpp + ${GRAPHQL_GEN_DIR}/SyncStateObject.cpp + ${GRAPHQL_GEN_DIR}/TaraxaSchema.cpp + ${GRAPHQL_GEN_DIR}/TransactionObject.cpp ) -add_custom_command(OUTPUT ${GRAPHQL_GENERATED_SOURCES} - COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.cpp - COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.h - COMMAND cppgraphqlgen::schemagen --schema="${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/schema/schema.taraxa.graphql" --prefix="Taraxa" --namespace="taraxa" - WORKING_DIRECTORY ${GRAPHQL_GEN_DIR} - COMMENT "Regenerating TaraxaSchema files") +add_custom_command( + OUTPUT ${GRAPHQL_GENERATED_SOURCES} + COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.cpp + COMMAND ${CMAKE_COMMAND} -E remove -f ${GRAPHQL_GEN_DIR}/*.h + COMMAND cppgraphqlgen::schemagen --schema="${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/schema/schema.taraxa.graphql" --prefix="Taraxa" --namespace="taraxa" + WORKING_DIRECTORY ${GRAPHQL_GEN_DIR} + COMMENT "Regenerating TaraxaSchema files" + DEPENDS ${GRAPHQL_GEN_DIR}/../schema/schema.taraxa.graphql +) diff --git a/libraries/core_libs/CMakeLists.txt b/libraries/core_libs/CMakeLists.txt index b7bb353dcd..5eae61bbb9 100644 --- a/libraries/core_libs/CMakeLists.txt +++ b/libraries/core_libs/CMakeLists.txt @@ -5,6 +5,7 @@ file(GLOB_RECURSE CONSENSUS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/consensus/*.cpp) file(GLOB_RECURSE NETWORK_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/network/*.hpp) file(GLOB_RECURSE NETWORK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/network/*.cpp) + # GraphQL include(${PROJECT_SOURCE_DIR}/CMakeModules/cpp_graphql_gen.cmake) file(GLOB_RECURSE NETWORK_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/network/*.h) @@ -16,18 +17,20 @@ file(GLOB_RECURSE NODE_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/node/*.hpp) file(GLOB_RECURSE NODE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/node/*.cpp) set(HEADERS - ${CONSENSUS_HEADERS} - ${NETWORK_HEADERS} - ${STORAGE_HEADERS} - ${NODE_HEADERS} + ${CONSENSUS_HEADERS} + ${NETWORK_HEADERS} + ${STORAGE_HEADERS} + ${NODE_HEADERS} + ${REWARDS_HEADERS} ) set(SOURCES - ${CONSENSUS_SOURCES} - ${NETWORK_SOURCES} - ${STORAGE_SOURCES} - ${NODE_SOURCES} - ${GRAPHQL_GENERATED_SOURCES} + ${CONSENSUS_SOURCES} + ${NETWORK_SOURCES} + ${STORAGE_SOURCES} + ${NODE_SOURCES} + ${GRAPHQL_GENERATED_SOURCES} + ${REWARDS_SOURCES} ) add_library(core_libs ${SOURCES} ${HEADERS}) @@ -37,25 +40,26 @@ target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/ target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/node/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/storage/include) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + # GraphQL target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/gen) target_include_directories(core_libs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/network/graphql/include) target_link_libraries(core_libs PUBLIC - cli - pbft_block - taraxa-evm - p2p - metrics - Jsonrpccpp - CONAN_PKG::rocksdb -# GraphQL - cppgraphqlgen::graphqlservice - cppgraphqlgen::graphqljson -) + cli + pbft_block + taraxa-evm + p2p + metrics + Jsonrpccpp + CONAN_PKG::rocksdb + # GraphQL + cppgraphqlgen::graphqlservice + cppgraphqlgen::graphqljson +) install(TARGETS core_libs - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) \ No newline at end of file diff --git a/libraries/core_libs/network/graphql/include/graphql/block.hpp b/libraries/core_libs/network/graphql/include/graphql/block.hpp index f253382002..cbee097866 100644 --- a/libraries/core_libs/network/graphql/include/graphql/block.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/block.hpp @@ -8,15 +8,17 @@ #include "transaction/transaction_manager.hpp" namespace graphql::taraxa { - class Block { public: explicit Block(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, + const ::taraxa::blk_hash_t& pbft_block_hash, std::shared_ptr block_header) noexcept; response::Value getNumber() const noexcept; response::Value getHash() const noexcept; + response::Value getPbftHash() const noexcept; std::shared_ptr getParent() const noexcept; response::Value getNonce() const noexcept; response::Value getTransactionsRoot() const noexcept; @@ -46,6 +48,8 @@ class Block { private: std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::TransactionManager> trx_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; + const ::taraxa::blk_hash_t kPBftBlockHash; std::shared_ptr block_header_; mutable std::vector> transactions_; }; diff --git a/libraries/core_libs/network/graphql/include/graphql/query.hpp b/libraries/core_libs/network/graphql/include/graphql/query.hpp index 78dc542fab..237e673689 100644 --- a/libraries/core_libs/network/graphql/include/graphql/query.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/query.hpp @@ -3,6 +3,7 @@ #include "QueryObject.h" #include "dag/dag_manager.hpp" #include "final_chain/final_chain.hpp" +#include "graphql/block.hpp" #include "network/network.hpp" #include "pbft/pbft_manager.hpp" #include "transaction/gas_pricer.hpp" @@ -46,6 +47,7 @@ class Query { std::shared_ptr<::taraxa::GasPricer> gas_pricer_; std::weak_ptr<::taraxa::Network> network_; const uint64_t kChainId; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; }; } // namespace graphql::taraxa \ No newline at end of file diff --git a/libraries/core_libs/network/graphql/include/graphql/transaction.hpp b/libraries/core_libs/network/graphql/include/graphql/transaction.hpp index 63e7fbd059..300d442e9e 100644 --- a/libraries/core_libs/network/graphql/include/graphql/transaction.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/transaction.hpp @@ -14,6 +14,7 @@ class Transaction final : public std::enable_shared_from_this { public: explicit Transaction(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)>, std::shared_ptr<::taraxa::Transaction> transaction) noexcept; response::Value getHash() const noexcept; @@ -38,6 +39,7 @@ class Transaction final : public std::enable_shared_from_this { private: std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::TransactionManager> trx_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; std::shared_ptr<::taraxa::Transaction> transaction_; // Caching for performance mutable std::optional<::taraxa::final_chain::TransactionReceipt> receipt_; diff --git a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp index 8267135dbe..6ef037d513 100644 --- a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp @@ -12,7 +12,8 @@ class DagBlock { explicit DagBlock(std::shared_ptr<::taraxa::DagBlock> dag_block, std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::PbftManager> pbft_manager, - std::shared_ptr<::taraxa::TransactionManager> transaction_manager) noexcept; + std::shared_ptr<::taraxa::TransactionManager> transaction_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num) noexcept; response::Value getHash() const noexcept; response::Value getPivot() const noexcept; @@ -31,6 +32,7 @@ class DagBlock { std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_; std::shared_ptr<::taraxa::PbftManager> pbft_manager_; std::shared_ptr<::taraxa::TransactionManager> transaction_manager_; + std::function(::taraxa::EthBlockNumber)> get_block_by_num_; mutable std::optional period_; }; diff --git a/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql b/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql index 4bfad2bbb2..4c9c7267ee 100644 --- a/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql +++ b/libraries/core_libs/network/graphql/schema/schema.taraxa.graphql @@ -159,6 +159,8 @@ type Block { number: Long! # Hash is the block hash of this block. hash: Bytes32! + # Pbft hash of this block. + pbftHash: Bytes32! # Parent is the parent block of this block. parent: Block # Nonce is the block nonce, an 8 byte sequence determined by the miner. diff --git a/libraries/core_libs/network/graphql/src/block.cpp b/libraries/core_libs/network/graphql/src/block.cpp index da39f2f36b..9158d37e1c 100644 --- a/libraries/core_libs/network/graphql/src/block.cpp +++ b/libraries/core_libs/network/graphql/src/block.cpp @@ -13,19 +13,23 @@ namespace graphql::taraxa { Block::Block(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, + const ::taraxa::blk_hash_t& pbft_block_hash, std::shared_ptr block_header) noexcept : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), + get_block_by_num_(std::move(get_block_by_num)), + kPBftBlockHash(pbft_block_hash), block_header_(std::move(block_header)) {} response::Value Block::getNumber() const noexcept { return response::Value(static_cast(block_header_->number)); } response::Value Block::getHash() const noexcept { return response::Value(block_header_->hash.toString()); } +response::Value Block::getPbftHash() const noexcept { return response::Value(kPBftBlockHash.toString()); } + std::shared_ptr Block::getParent() const noexcept { - return std::make_shared(std::make_shared( - final_chain_, trx_manager_, - final_chain_->block_header(final_chain_->block_number(dev::h256(block_header_->parent_hash))))); + return get_block_by_num_(block_header_->number - 1); } response::Value Block::getNonce() const noexcept { return response::Value(block_header_->nonce().toString()); } @@ -99,8 +103,8 @@ std::optional>> Block::getTrans } ret.reserve(transactions_.size()); for (auto& t : transactions_) { - ret.emplace_back( - std::make_shared(std::make_shared(final_chain_, trx_manager_, t))); + ret.emplace_back(std::make_shared( + std::make_shared(final_chain_, trx_manager_, get_block_by_num_, t))); } return ret; } @@ -114,7 +118,7 @@ std::shared_ptr Block::getTransactionAt(response::IntType&& return nullptr; } return std::make_shared( - std::make_shared(final_chain_, trx_manager_, transactions_[index])); + std::make_shared(final_chain_, trx_manager_, get_block_by_num_, transactions_[index])); } std::vector> Block::getLogs(BlockFilterCriteria&&) const noexcept { diff --git a/libraries/core_libs/network/graphql/src/query.cpp b/libraries/core_libs/network/graphql/src/query.cpp index 7606de867b..9afae748f1 100644 --- a/libraries/core_libs/network/graphql/src/query.cpp +++ b/libraries/core_libs/network/graphql/src/query.cpp @@ -26,40 +26,41 @@ Query::Query(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, db_(std::move(db)), gas_pricer_(std::move(gas_pricer)), network_(std::move(network)), - kChainId(chain_id) {} + kChainId(chain_id) { + get_block_by_num_ = [&](::taraxa::EthBlockNumber num) { + return getBlock(response::Value(static_cast(num)), std::nullopt); + }; +} std::shared_ptr Query::getBlock(std::optional&& number, std::optional&& hash) const { + std::optional<::taraxa::EthBlockNumber> block_number; if (number) { - const uint64_t block_number = number->get(); + block_number = number->get(); if (const auto last_block_number = final_chain_->last_block_number(); last_block_number < block_number) { return nullptr; } - if (auto block_header = final_chain_->block_header(block_number)) { - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(block_header))); - } - return nullptr; } if (hash) { - if (auto block_number = final_chain_->block_number(dev::h256(hash->get()))) { - if (auto block_header = final_chain_->block_header(block_number)) { - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(block_header))); - } - } + block_number = final_chain_->block_number(dev::h256(hash->get())); + } + auto block_header = final_chain_->block_header(block_number); + + auto pbft_block = db_->getPbftBlock(block_header->number); + if (!pbft_block) { + // shouldn't be possible return nullptr; } - return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, final_chain_->block_header())); + return std::make_shared(std::make_shared(final_chain_, transaction_manager_, get_block_by_num_, + pbft_block->getBlockHash(), block_header)); } std::vector> Query::getBlocks(response::Value&& fromArg, std::optional&& toArg) const { std::vector> blocks; - uint64_t start_block_num = fromArg.get(); - uint64_t end_block_num = toArg ? toArg->get() : (start_block_num + Query::kMaxPropagationLimit); + int start_block_num = fromArg.get(); + int end_block_num = toArg ? toArg->get() : (start_block_num + Query::kMaxPropagationLimit); // Incase of reverse order of blocks if (start_block_num > end_block_num) { @@ -68,11 +69,11 @@ std::vector> Query::getBlocks(response::Value&& f end_block_num = tmp; } - if (end_block_num - start_block_num > Query::kMaxPropagationLimit) { + if (end_block_num - start_block_num > static_cast(Query::kMaxPropagationLimit)) { end_block_num = start_block_num + Query::kMaxPropagationLimit; } - const auto last_block_number = final_chain_->last_block_number(); + const int last_block_number = final_chain_->last_block_number(); if (start_block_num > last_block_number) { return blocks; } else if (end_block_num > last_block_number) { @@ -81,9 +82,8 @@ std::vector> Query::getBlocks(response::Value&& f blocks.reserve(end_block_num - start_block_num); - for (uint64_t block_num = start_block_num; block_num <= end_block_num; block_num++) { - blocks.emplace_back(std::make_shared( - std::make_shared(final_chain_, transaction_manager_, final_chain_->block_header(block_num)))); + for (int block_num = start_block_num; block_num <= end_block_num; block_num++) { + blocks.emplace_back(getBlock(response::Value(block_num), std::nullopt)); } return blocks; @@ -92,7 +92,7 @@ std::vector> Query::getBlocks(response::Value&& f std::shared_ptr Query::getTransaction(response::Value&& hashArg) const { if (auto transaction = transaction_manager_->getTransaction(::taraxa::trx_hash_t(hashArg.get()))) { return std::make_shared( - std::make_shared(final_chain_, transaction_manager_, std::move(transaction))); + std::make_shared(final_chain_, transaction_manager_, get_block_by_num_, std::move(transaction))); } return nullptr; } @@ -130,10 +130,11 @@ std::shared_ptr Query::getDagBlock(std::optional(std::make_shared( - std::move(taraxa_dag_block), final_chain_, pbft_manager_, transaction_manager_)) - : nullptr; + if (taraxa_dag_block) { + return std::make_shared(std::make_shared( + std::move(taraxa_dag_block), final_chain_, pbft_manager_, transaction_manager_, get_block_by_num_)); + } + return nullptr; } std::vector> Query::getPeriodDagBlocks( @@ -149,8 +150,8 @@ std::vector> Query::getPeriodDagBlocks( if (dag_blocks.size()) { blocks.reserve(dag_blocks.size()); for (auto block : dag_blocks) { - blocks.emplace_back(std::make_shared( - std::make_shared(std::move(block), final_chain_, pbft_manager_, transaction_manager_))); + blocks.emplace_back(std::make_shared(std::make_shared( + std::move(block), final_chain_, pbft_manager_, transaction_manager_, get_block_by_num_))); } } return blocks; @@ -170,11 +171,11 @@ std::vector> Query::getDagBlocks(std::optional } auto addDagBlocks = [final_chain = final_chain_, pbft_manager = pbft_manager_, - transaction_manager = transaction_manager_](auto taraxa_dag_blocks, - auto& result_dag_blocks) -> size_t { + transaction_manager = transaction_manager_, get_block_by_num = get_block_by_num_]( + auto taraxa_dag_blocks, auto& result_dag_blocks) -> size_t { for (auto& dag_block : taraxa_dag_blocks) { - result_dag_blocks.emplace_back(std::make_shared( - std::make_shared(std::move(dag_block), final_chain, pbft_manager, transaction_manager))); + result_dag_blocks.emplace_back(std::make_shared(std::make_shared( + std::move(dag_block), final_chain, pbft_manager, transaction_manager, get_block_by_num))); } return taraxa_dag_blocks.size(); diff --git a/libraries/core_libs/network/graphql/src/transaction.cpp b/libraries/core_libs/network/graphql/src/transaction.cpp index afe45ce171..b6770ec7ac 100644 --- a/libraries/core_libs/network/graphql/src/transaction.cpp +++ b/libraries/core_libs/network/graphql/src/transaction.cpp @@ -13,9 +13,11 @@ namespace graphql::taraxa { Transaction::Transaction(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::TransactionManager> trx_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num, std::shared_ptr<::taraxa::Transaction> transaction) noexcept : final_chain_(std::move(final_chain)), trx_manager_(std::move(trx_manager)), + get_block_by_num_(std::move(get_block_by_num)), transaction_(std::move(transaction)) {} response::Value Transaction::getHash() const noexcept { return response::Value(transaction_->getHash().toString()); } @@ -70,8 +72,7 @@ std::shared_ptr Transaction::getBlock() const { location_ = final_chain_->transaction_location(transaction_->getHash()); if (!location_) return nullptr; } - return std::make_shared( - std::make_shared(final_chain_, trx_manager_, final_chain_->block_header(location_->blk_n))); + return get_block_by_num_(location_->blk_n); } std::optional Transaction::getStatus() const noexcept { diff --git a/libraries/core_libs/network/graphql/src/types/dag_block.cpp b/libraries/core_libs/network/graphql/src/types/dag_block.cpp index 7124be5fd2..a9b1c78a81 100644 --- a/libraries/core_libs/network/graphql/src/types/dag_block.cpp +++ b/libraries/core_libs/network/graphql/src/types/dag_block.cpp @@ -10,11 +10,13 @@ namespace graphql::taraxa { DagBlock::DagBlock(std::shared_ptr<::taraxa::DagBlock> dag_block, std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain, std::shared_ptr<::taraxa::PbftManager> pbft_manager, - std::shared_ptr<::taraxa::TransactionManager> transaction_manager) noexcept + std::shared_ptr<::taraxa::TransactionManager> transaction_manager, + std::function(::taraxa::EthBlockNumber)> get_block_by_num) noexcept : dag_block_(std::move(dag_block)), final_chain_(std::move(final_chain)), pbft_manager_(std::move(pbft_manager)), - transaction_manager_(std::move(transaction_manager)) {} + transaction_manager_(std::move(transaction_manager)), + get_block_by_num_(get_block_by_num) {} response::Value DagBlock::getHash() const noexcept { return response::Value(dag_block_->getHash().toString()); } @@ -72,7 +74,7 @@ std::optional>> DagBlock::getTr std::vector> transactions_result; for (const auto& trx_hash : dag_block_->getTrxs()) { transactions_result.push_back(std::make_shared(std::make_shared( - final_chain_, transaction_manager_, transaction_manager_->getTransaction(trx_hash)))); + final_chain_, transaction_manager_, get_block_by_num_, transaction_manager_->getTransaction(trx_hash)))); } return transactions_result; From 09dcabd0db0e4d445b0e94609b9082567d099b4c Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 28 Sep 2023 18:07:44 +0200 Subject: [PATCH 2/4] chore: fix possible crashes on graphql --- CMakeModules/cpp_graphql_gen.cmake | 2 +- .../network/graphql/include/graphql/types/dag_block.hpp | 3 +++ libraries/core_libs/network/graphql/src/query.cpp | 6 ++++++ libraries/core_libs/network/graphql/src/types/dag_block.cpp | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeModules/cpp_graphql_gen.cmake b/CMakeModules/cpp_graphql_gen.cmake index 1ee15d1f86..74fbabdcf3 100644 --- a/CMakeModules/cpp_graphql_gen.cmake +++ b/CMakeModules/cpp_graphql_gen.cmake @@ -6,7 +6,7 @@ set(Boost_NO_WARN_NEW_VERSIONS 1) FetchContent_Declare( cppgraphqlgen GIT_REPOSITORY https://github.com/microsoft/cppgraphqlgen.git - GIT_TAG 8c1623acc42392ef2a1bc0336482621386f98c77 # v4.5.0 + GIT_TAG 1d659227bfc51fb7d9bb5dc9862234e7cfd1b1e3 # v4.5.4 ) set(GRAPHQL_BUILD_TESTS OFF) set(GRAPHQL_UPDATE_VERSION OFF) diff --git a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp index 6ef037d513..3bb2f1a6a8 100644 --- a/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp +++ b/libraries/core_libs/network/graphql/include/graphql/types/dag_block.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "DagBlockObject.h" #include "final_chain/final_chain.hpp" #include "pbft/pbft_manager.hpp" @@ -34,6 +36,7 @@ class DagBlock { std::shared_ptr<::taraxa::TransactionManager> transaction_manager_; std::function(::taraxa::EthBlockNumber)> get_block_by_num_; + mutable std::mutex mu_; mutable std::optional period_; }; diff --git a/libraries/core_libs/network/graphql/src/query.cpp b/libraries/core_libs/network/graphql/src/query.cpp index 9afae748f1..a64245a567 100644 --- a/libraries/core_libs/network/graphql/src/query.cpp +++ b/libraries/core_libs/network/graphql/src/query.cpp @@ -43,8 +43,14 @@ std::shared_ptr Query::getBlock(std::optional&& } if (hash) { block_number = final_chain_->block_number(dev::h256(hash->get())); + if (!block_number) { + return nullptr; + } } auto block_header = final_chain_->block_header(block_number); + if (!block_header) { + return nullptr; + } auto pbft_block = db_->getPbftBlock(block_header->number); if (!pbft_block) { diff --git a/libraries/core_libs/network/graphql/src/types/dag_block.cpp b/libraries/core_libs/network/graphql/src/types/dag_block.cpp index a9b1c78a81..0d6d9d9721 100644 --- a/libraries/core_libs/network/graphql/src/types/dag_block.cpp +++ b/libraries/core_libs/network/graphql/src/types/dag_block.cpp @@ -37,6 +37,7 @@ response::Value DagBlock::getLevel() const noexcept { } std::optional DagBlock::getPbftPeriod() const noexcept { + std::lock_guard lock{mu_}; if (period_) { return response::Value(static_cast(*period_)); } @@ -49,6 +50,7 @@ std::optional DagBlock::getPbftPeriod() const noexcept { } std::shared_ptr DagBlock::getAuthor() const noexcept { + std::lock_guard lock{mu_}; if (!period_) { const auto [has_period, period] = pbft_manager_->getDagBlockPeriod(::taraxa::blk_hash_t(dag_block_->getHash())); if (has_period) { From 98e3b07883d772b41d466335a3cf5f4f151257ee Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 28 Sep 2023 18:08:34 +0200 Subject: [PATCH 3/4] chore: increase version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d6750fc8a..d66bc12350 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.20) # Set current version of the project set(TARAXA_MAJOR_VERSION 1) set(TARAXA_MINOR_VERSION 4) -set(TARAXA_PATCH_VERSION 4) +set(TARAXA_PATCH_VERSION 5) set(TARAXA_VERSION ${TARAXA_MAJOR_VERSION}.${TARAXA_MINOR_VERSION}.${TARAXA_PATCH_VERSION}) # Any time a change in the network protocol is introduced this version should be increased From 41a7e3bf9f58e7c96615995dfd199f4a5c04fb2c Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 28 Sep 2023 21:53:43 +0200 Subject: [PATCH 4/4] chore: httpconnection cleanup --- .../network/include/network/http_server.hpp | 3 ++- .../core_libs/network/src/http_server.cpp | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libraries/core_libs/network/include/network/http_server.hpp b/libraries/core_libs/network/include/network/http_server.hpp index bfe7e586ea..615f0cb954 100644 --- a/libraries/core_libs/network/include/network/http_server.hpp +++ b/libraries/core_libs/network/include/network/http_server.hpp @@ -60,10 +60,11 @@ class HttpServer : public std::enable_shared_from_this { class HttpConnection : public std::enable_shared_from_this { public: explicit HttpConnection(const std::shared_ptr& http_server); - virtual ~HttpConnection() = default; + virtual ~HttpConnection() { HttpConnection::stop(); } boost::asio::ip::tcp::socket& getSocket() { return socket_; } virtual std::shared_ptr getShared(); void read(); + void stop(); protected: std::shared_ptr server_; diff --git a/libraries/core_libs/network/src/http_server.cpp b/libraries/core_libs/network/src/http_server.cpp index 4c269a45b9..eb76d15f02 100644 --- a/libraries/core_libs/network/src/http_server.cpp +++ b/libraries/core_libs/network/src/http_server.cpp @@ -78,17 +78,32 @@ std::shared_ptr HttpConnection::getShared() { HttpConnection::HttpConnection(const std::shared_ptr &http_server) : server_(http_server), socket_(http_server->getIoContext()) {} +void HttpConnection::stop() { + if (socket_.is_open()) { + try { + LOG(server_->log_dg_) << "Closing connection..."; + boost::asio::socket_base::linger option(true, 0); + socket_.set_option(option); + socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both); + socket_.close(); + } catch (...) { + } + } +} + void HttpConnection::read() { boost::beast::http::async_read( socket_, buffer_, request_, [this, this_sp = getShared()](boost::system::error_code const &ec, size_t) { if (ec) { LOG(server_->log_er_) << "Error! HttpConnection connection read fail ... " << ec.message() << std::endl; + stop(); } else { assert(server_->request_processor_); LOG(server_->log_dg_) << "Received: " << request_; response_ = server_->request_processor_->process(request_); - boost::beast::http::async_write(socket_, response_, - [this_sp = getShared()](auto const & /*ec*/, auto /*bytes_transfered*/) {}); + boost::beast::http::async_write( + socket_, response_, + [this_sp = getShared()](auto const & /*ec*/, auto /*bytes_transfered*/) { this_sp->stop(); }); } }); }