Skip to content

Commit

Permalink
Fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrm committed Jul 24, 2023
1 parent 8cd5694 commit a57d936
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# cmake-format: off
option(TESTING "Build and run test suite" ON )
option(BENCHMARK "Build benchmarks" OFF)
option(CLANG_FORMAT "Enable clang-format target" ON )
option(CLANG_TIDY "Enable clang-tidy checks during compilation" OFF)
option(COVERAGE "Enable generation of coverage info" OFF)
Expand Down Expand Up @@ -194,4 +195,8 @@ if(TESTING)
add_subdirectory(test)
endif()

if(BENCHMARK)
add_subdirectory(benchmarks)
endif()

add_subdirectory(node)
3 changes: 3 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

add_executable(trie_pruner_benchmark trie_pruner_benchmark.cpp)
target_link_libraries(trie_pruner_benchmark benchmark::benchmark storage)
23 changes: 23 additions & 0 deletions benchmarks/trie_pruner_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();
18 changes: 9 additions & 9 deletions core/consensus/babe/impl/babe_config_repository_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,14 @@ namespace kagome::consensus::babe {
}

bool BabeConfigRepositoryImpl::prepare() {
std::unique_lock lock{indexer_mutex_};
auto finalized = block_tree_->getLastFinalized();
auto finalized_header = block_tree_->getBlockHeader(finalized.hash).value();

primitives::BlockNumber indexer_last_finalized;
{
std::unique_lock lock{indexer_mutex_};
indexer_last_finalized = indexer_.last_finalized_indexed_.number;
}

if (finalized.number - indexer_last_finalized
if (finalized.number - indexer_.last_finalized_indexed_.number
> kMaxUnindexedBlocksNum
and trie_storage_->getEphemeralBatchAt(finalized_header.state_root)) {
warp(finalized);
warp(lock, finalized);
}

auto genesis_res = config({block_tree_->getGenesisBlockHash(), 0}, false);
Expand Down Expand Up @@ -243,9 +238,14 @@ namespace kagome::consensus::babe {
return 0;
}

void BabeConfigRepositoryImpl::warp(std::unique_lock<std::mutex> &lock,
const primitives::BlockInfo &block) {
indexer_.put(block, {}, true);
}

void BabeConfigRepositoryImpl::warp(const primitives::BlockInfo &block) {
std::unique_lock lock{indexer_mutex_};
indexer_.put(block, {}, true);
warp(lock, block);
}

outcome::result<std::shared_ptr<const primitives::BabeConfiguration>>
Expand Down
5 changes: 4 additions & 1 deletion core/consensus/babe/impl/babe_config_repository_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace kagome::consensus::babe {
std::optional<std::shared_ptr<const primitives::BabeConfiguration>> state;
/**
* Next epoch lazily computed from `config` and digests.
*/
*/
std::optional<std::shared_ptr<const primitives::BabeConfiguration>>
next_state;
};
Expand Down Expand Up @@ -125,6 +125,9 @@ namespace kagome::consensus::babe {
outcome::result<std::shared_ptr<const primitives::BabeConfiguration>>
loadPrev(const std::optional<primitives::BlockInfo> &prev) const;

void warp(std::unique_lock<std::mutex> &lock,
const primitives::BlockInfo &block);

std::shared_ptr<storage::BufferStorage> persistent_storage_;
bool config_warp_sync_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
Expand Down

0 comments on commit a57d936

Please sign in to comment.