Skip to content

Commit

Permalink
chore: fix possible crashes on graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Sep 28, 2023
1 parent 91461c4 commit 09dcabd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeModules/cpp_graphql_gen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 Down Expand Up @@ -34,6 +36,7 @@ class DagBlock {
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
6 changes: 6 additions & 0 deletions libraries/core_libs/network/graphql/src/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ std::shared_ptr<object::Block> Query::getBlock(std::optional<response::Value>&&
}
if (hash) {
block_number = final_chain_->block_number(dev::h256(hash->get<std::string>()));
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) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/core_libs/network/graphql/src/types/dag_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ response::Value DagBlock::getLevel() const noexcept {
}

std::optional<response::Value> DagBlock::getPbftPeriod() const noexcept {
std::lock_guard<std::mutex> lock{mu_};
if (period_) {
return response::Value(static_cast<int>(*period_));
}
Expand All @@ -49,6 +50,7 @@ std::optional<response::Value> DagBlock::getPbftPeriod() const noexcept {
}

std::shared_ptr<object::Account> DagBlock::getAuthor() const noexcept {
std::lock_guard<std::mutex> lock{mu_};
if (!period_) {
const auto [has_period, period] = pbft_manager_->getDagBlockPeriod(::taraxa::blk_hash_t(dag_block_->getHash()));
if (has_period) {
Expand Down

0 comments on commit 09dcabd

Please sign in to comment.