-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a new CPP module for managing the merkle trees that make up the world state. A new implementetion of `MerkleTreeDb` is provided to interact with the native code. msgpack is used to pass messages across the js<->cpp boundary. Tests have been added to assert that the two world state implementations work in the same way (more tests would be better). This PR builds on top of #7037. PS: I'm not as experienced with C++ --------- Co-authored-by: PhilWindle <[email protected]>
- Loading branch information
1 parent
02c3330
commit c1aa6f7
Showing
40 changed files
with
3,438 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
# run commands relative to parent directory | ||
cd $(dirname $0)/.. | ||
|
||
TEST=${1:-*} | ||
PRESET=${PRESET:-clang16} | ||
|
||
cmake --build --preset $PRESET --target world_state_tests | ||
./build/bin/world_state_tests --gtest_filter=WorldStateTest.${TEST} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
barretenberg/cpp/src/barretenberg/messaging/dispatcher.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "barretenberg/messaging/header.hpp" | ||
#include "barretenberg/serialize/cbind.hpp" | ||
#include <cstdint> | ||
#include <functional> | ||
#include <stdexcept> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace bb::messaging { | ||
|
||
using message_handler = std::function<bool(msgpack::object&, msgpack::sbuffer&)>; | ||
|
||
class MessageDispatcher { | ||
private: | ||
std::unordered_map<uint32_t, message_handler> messageHandlers; | ||
|
||
public: | ||
MessageDispatcher() = default; | ||
|
||
bool onNewData(msgpack::object& obj, msgpack::sbuffer& buffer) | ||
{ | ||
bb::messaging::HeaderOnlyMessage header; | ||
obj.convert(header); | ||
|
||
auto iter = messageHandlers.find(header.msgType); | ||
if (iter == messageHandlers.end()) { | ||
throw std::runtime_error("No registered handler for message of type " + std::to_string(header.msgType)); | ||
} | ||
|
||
return (iter->second)(obj, buffer); | ||
} | ||
|
||
void registerTarget(uint32_t msgType, const message_handler& handler) | ||
{ | ||
messageHandlers.insert({ msgType, handler }); | ||
} | ||
}; | ||
|
||
} // namespace bb::messaging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
barretenberg_module(world_state crypto_merkle_tree stdlib_poseidon2) |
34 changes: 34 additions & 0 deletions
34
barretenberg/cpp/src/barretenberg/world_state/tree_with_store.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
namespace bb::world_state { | ||
|
||
template <typename Tree> struct TreeWithStore { | ||
using TreeType = Tree; | ||
std::unique_ptr<Tree> tree; | ||
std::unique_ptr<typename Tree::StoreType> store; | ||
std::unique_ptr<typename Tree::StoreType::PersistedStoreType> persisted_store; | ||
|
||
TreeWithStore(std::unique_ptr<Tree> t, | ||
std::unique_ptr<typename Tree::StoreType> s, | ||
std::unique_ptr<typename Tree::StoreType::PersistedStoreType> p) | ||
: tree(std::move(t)) | ||
, store(std::move(s)) | ||
, persisted_store(std::move(p)) | ||
{} | ||
|
||
TreeWithStore(TreeWithStore&& other) noexcept | ||
: tree(std::move(other.tree)) | ||
, store(std::move(other.store)) | ||
, persisted_store(std::move(other.persisted_store)) | ||
{} | ||
|
||
TreeWithStore(const TreeWithStore& other) = delete; | ||
~TreeWithStore() = default; | ||
|
||
TreeWithStore& operator=(TreeWithStore&& other) = delete; | ||
TreeWithStore& operator=(const TreeWithStore& other) = delete; | ||
}; | ||
|
||
} // namespace bb::world_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "barretenberg/crypto/merkle_tree/indexed_tree/indexed_leaf.hpp" | ||
#include "barretenberg/crypto/merkle_tree/types.hpp" | ||
#include "barretenberg/ecc/curves/bn254/fr.hpp" | ||
#include <cstdint> | ||
#include <variant> | ||
|
||
namespace bb::world_state { | ||
|
||
enum MerkleTreeId { | ||
NULLIFIER_TREE = 0, | ||
NOTE_HASH_TREE = 1, | ||
PUBLIC_DATA_TREE = 2, | ||
L1_TO_L2_MESSAGE_TREE = 3, | ||
ARCHIVE = 4, | ||
}; | ||
|
||
using TreeStateReference = std::pair<bb::fr, bb::crypto::merkle_tree::index_t>; | ||
using StateReference = std::unordered_map<MerkleTreeId, TreeStateReference>; | ||
|
||
struct WorldStateRevision { | ||
struct FinalisedBlock { | ||
uint32_t block; | ||
}; | ||
|
||
struct CurrentState { | ||
bool uncommitted; | ||
}; | ||
|
||
using Revision = std::variant<WorldStateRevision::FinalisedBlock, WorldStateRevision::CurrentState>; | ||
Revision inner; | ||
|
||
static WorldStateRevision committed() { return { CurrentState{ false } }; } | ||
static WorldStateRevision uncommitted() { return { CurrentState{ true } }; } | ||
static WorldStateRevision finalised_block(uint32_t block_number) { return { FinalisedBlock{ block_number } }; } | ||
}; | ||
} // namespace bb::world_state |
Oops, something went wrong.