Skip to content

Commit

Permalink
Merge pull request #2575 from Taraxa-project/fix-crash-on-graphql
Browse files Browse the repository at this point in the history
Fix crash on graphql
  • Loading branch information
JakubFornadel authored Sep 28, 2023
2 parents 14621db + 41a7e3b commit 43ab0f9
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 94 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 28 additions & 23 deletions CMakeModules/cpp_graphql_gen.cmake
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
########### cppgraphqlgen ###########
# ########## cppgraphqlgen ###########
include(FetchContent)

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)
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
)
48 changes: 26 additions & 22 deletions libraries/core_libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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})
Expand All @@ -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}
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num,
const ::taraxa::blk_hash_t& pbft_block_hash,
std::shared_ptr<const ::taraxa::final_chain::BlockHeader> block_header) noexcept;

response::Value getNumber() const noexcept;
response::Value getHash() const noexcept;
response::Value getPbftHash() const noexcept;
std::shared_ptr<object::Block> getParent() const noexcept;
response::Value getNonce() const noexcept;
response::Value getTransactionsRoot() const noexcept;
Expand Down Expand Up @@ -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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num_;
const ::taraxa::blk_hash_t kPBftBlockHash;
std::shared_ptr<const ::taraxa::final_chain::BlockHeader> block_header_;
mutable std::vector<std::shared_ptr<::taraxa::Transaction>> transactions_;
};
Expand Down
2 changes: 2 additions & 0 deletions libraries/core_libs/network/graphql/include/graphql/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num_;
};

} // namespace graphql::taraxa
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Transaction final : public std::enable_shared_from_this<Transaction> {
public:
explicit Transaction(std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain,
std::shared_ptr<::taraxa::TransactionManager> trx_manager,
std::function<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)>,
std::shared_ptr<::taraxa::Transaction> transaction) noexcept;

response::Value getHash() const noexcept;
Expand All @@ -38,6 +39,7 @@ class Transaction final : public std::enable_shared_from_this<Transaction> {
private:
std::shared_ptr<::taraxa::final_chain::FinalChain> final_chain_;
std::shared_ptr<::taraxa::TransactionManager> trx_manager_;
std::function<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num_;
std::shared_ptr<::taraxa::Transaction> transaction_;
// Caching for performance
mutable std::optional<::taraxa::final_chain::TransactionReceipt> receipt_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <mutex>

#include "DagBlockObject.h"
#include "final_chain/final_chain.hpp"
#include "pbft/pbft_manager.hpp"
Expand All @@ -12,7 +14,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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num) noexcept;

response::Value getHash() const noexcept;
response::Value getPivot() const noexcept;
Expand All @@ -31,7 +34,9 @@ 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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num_;

mutable std::mutex mu_;
mutable std::optional<uint64_t> period_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 10 additions & 6 deletions libraries/core_libs/network/graphql/src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_ptr<object::Block>(::taraxa::EthBlockNumber)> get_block_by_num,
const ::taraxa::blk_hash_t& pbft_block_hash,
std::shared_ptr<const ::taraxa::final_chain::BlockHeader> 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<int>(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<object::Block> Block::getParent() const noexcept {
return std::make_shared<object::Block>(std::make_shared<Block>(
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()); }
Expand Down Expand Up @@ -99,8 +103,8 @@ std::optional<std::vector<std::shared_ptr<object::Transaction>>> Block::getTrans
}
ret.reserve(transactions_.size());
for (auto& t : transactions_) {
ret.emplace_back(
std::make_shared<object::Transaction>(std::make_shared<Transaction>(final_chain_, trx_manager_, t)));
ret.emplace_back(std::make_shared<object::Transaction>(
std::make_shared<Transaction>(final_chain_, trx_manager_, get_block_by_num_, t)));
}
return ret;
}
Expand All @@ -114,7 +118,7 @@ std::shared_ptr<object::Transaction> Block::getTransactionAt(response::IntType&&
return nullptr;
}
return std::make_shared<object::Transaction>(
std::make_shared<Transaction>(final_chain_, trx_manager_, transactions_[index]));
std::make_shared<Transaction>(final_chain_, trx_manager_, get_block_by_num_, transactions_[index]));
}

std::vector<std::shared_ptr<object::Log>> Block::getLogs(BlockFilterCriteria&&) const noexcept {
Expand Down
Loading

0 comments on commit 43ab0f9

Please sign in to comment.