From c19d0a3ec2c4e1c37c2c6712df65f638c808a8f0 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 16 Sep 2024 08:06:46 +0000 Subject: [PATCH 1/2] fix: thread-safe writes to map --- .../barretenberg/world_state/world_state.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp b/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp index 307c99cb9805..94a3f7024ea0 100644 --- a/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp +++ b/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp @@ -9,6 +9,7 @@ #include "barretenberg/world_state/tree_with_store.hpp" #include "barretenberg/world_state/types.hpp" #include +#include #include #include #include @@ -96,18 +97,19 @@ StateReference WorldState::get_state_reference(WorldStateRevision revision) cons { Signal signal(static_cast(_trees.size())); StateReference state_reference; + // multiple threads want to write to state_reference + std::mutex state_ref_mutex; + bool uncommitted = include_uncommitted(revision); for (const auto& [id, tree] : _trees) { - std::visit( - [&signal, &state_reference, id, uncommitted](auto&& wrapper) { - auto callback = [&signal, &state_reference, id](const TypedResponse& meta) { - state_reference.insert({ id, { meta.inner.root, meta.inner.size } }); - signal.signal_decrement(); - }; - wrapper.tree->get_meta_data(uncommitted, callback); - }, - tree); + auto callback = [&signal, &state_reference, &state_ref_mutex, id](const TypedResponse& meta) { + std::lock_guard lock(state_ref_mutex); + state_reference.insert({ id, { meta.inner.root, meta.inner.size } }); + signal.signal_decrement(); + }; + std::visit([&callback, uncommitted](auto&& wrapper) { wrapper.tree->get_meta_data(uncommitted, callback); }, + tree); } signal.wait_for_level(0); From 58f7b95ab3111c91b62d7f3e68df82e63a621b4f Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 16 Sep 2024 08:47:24 +0000 Subject: [PATCH 2/2] fix: update build script --- yarn-project/world-state/scripts/build.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/yarn-project/world-state/scripts/build.sh b/yarn-project/world-state/scripts/build.sh index 9ac0f1c305a4..17d34d494bee 100755 --- a/yarn-project/world-state/scripts/build.sh +++ b/yarn-project/world-state/scripts/build.sh @@ -5,15 +5,16 @@ set -e cd "$(dirname "$0")/.." # relatiev path from the directory containing package.json -WORLD_STATE_LIB_PATH=../../barretenberg/cpp/build/bin/world_state_napi.node +WORLD_STATE_LIB_PATH=../../barretenberg/cpp/build-pic/lib/world_state_napi.node +PRESET=${PRESET:-clang16-pic} build_addon() { - (cd ../../barretenberg/cpp; cmake --preset clang16-pic -DCMAKE_BUILD_TYPE=RelWithAssert; cmake --build --preset clang16-pic --target world_state_napi; echo $PWD; mkdir -p build/bin; cp ./build-pic/lib/world_state_napi.node ./build/bin/world_state_napi.node) + (cd ../../barretenberg/cpp; cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert; cmake --build --preset $PRESET --target world_state_napi; echo $PWD; mkdir -p build/bin; cp ./build-pic/lib/world_state_napi.node ./build/bin/world_state_napi.node) } cp_addon_lib() { if [ -f $WORLD_STATE_LIB_PATH ]; then - echo "Copying world_state_napi.node to build directory" + echo "Copying $(realpath $WORLD_STATE_LIB_PATH) to build directory" rm -rf build mkdir build cp $WORLD_STATE_LIB_PATH build/world_state_napi.node