Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ find_package(Python3 REQUIRED)
find_package(PkgConfig REQUIRED)
#pkg_check_modules(libb2 REQUIRED IMPORTED_TARGET GLOBAL libb2)

find_package(Boost CONFIG REQUIRED COMPONENTS algorithm outcome program_options property_tree)
find_package(fmt CONFIG REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)
find_package(sszpp CONFIG REQUIRED)
find_package(soralog CONFIG REQUIRED)
find_package(Boost CONFIG REQUIRED COMPONENTS algorithm filesystem outcome program_options property_tree random)
find_package(Boost.DI CONFIG REQUIRED)
find_package(qtils CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(hashtree CONFIG REQUIRED)
find_package(libp2p CONFIG REQUIRED)
find_package(libsecp256k1 CONFIG REQUIRED)
find_package(lsquic CONFIG REQUIRED)
find_package(OpenSSL CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
find_package(qtils CONFIG REQUIRED)
find_package(RocksDB CONFIG REQUIRED)
find_package(hashtree CONFIG REQUIRED)
find_package(soralog CONFIG REQUIRED)
find_package(sszpp CONFIG REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)

include(vcpkg-overlay/cppcodec.cmake)

# TODO Temporarily commented out until gcc is updated (gcc-13 crashes because of this).
# if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Executables (should contain `main()` function)
add_subdirectory(executable)

add_subdirectory(serde)

# Application's thinks
add_subdirectory(app)

Expand Down
11 changes: 10 additions & 1 deletion src/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "types/justification.hpp"

namespace lean {
struct BlockBody;
struct Block;
struct BlockBody;
struct SignedBlock;
struct StatusMessage;
} // namespace lean

namespace lean::blockchain {
Expand Down Expand Up @@ -150,6 +152,13 @@ namespace lean::blockchain {
* @return hash of the block
*/
[[nodiscard]] virtual BlockIndex lastFinalized() const = 0;

virtual StatusMessage getStatusMessage() const = 0;

virtual outcome::result<std::optional<SignedBlock>> tryGetSignedBlock(
const BlockHash block_hash) const = 0;

virtual void import(std::vector<SignedBlock> blocks) = 0;
};

} // namespace lean::blockchain
2 changes: 1 addition & 1 deletion src/blockchain/impl/block_storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace lean::blockchain {
outcome::result<BlockHash> BlockStorageImpl::putBlockHeader(
const BlockHeader &header) {
OUTCOME_TRY(encoded_header, encode(header));
header.updateHash(*hasher_);
header.updateHash();
const auto &block_hash = header.hash();
OUTCOME_TRY(putToSpace(*storage_,
storage::Space::Header,
Expand Down
53 changes: 46 additions & 7 deletions src/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "tests/testutil/literals.hpp"
#include "types/block.hpp"
#include "types/block_header.hpp"
#include "types/signed_block.hpp"
#include "types/status_message.hpp"

namespace lean::blockchain {
BlockTreeImpl::SafeBlockTreeData::SafeBlockTreeData(BlockTreeData data)
Expand Down Expand Up @@ -126,13 +128,8 @@ namespace lean::blockchain {
}
const auto &parent = parent_opt.value();

BlockHeader header;
header.slot = block.slot;
header.proposer_index = block.proposer_index;
header.parent_root = block.parent_root;
header.state_root = block.state_root;
header.body_root = {}; // ssz::hash_tree_root(block.body);
header.updateHash(*p.hasher_);
auto header = block.getHeader();
header.updateHash();

SL_DEBUG(log_, "Adding block {}", header.index());

Expand Down Expand Up @@ -797,6 +794,48 @@ namespace lean::blockchain {
[&](const BlockTreeData &p) { return getLastFinalizedNoLock(p); });
}

StatusMessage BlockTreeImpl::getStatusMessage() const {
auto finalized = lastFinalized();
auto head = bestBlock();
return StatusMessage{
.finalized = {.root = finalized.hash, .slot = finalized.slot},
.head = {.root = head.hash, .slot = head.slot},
};
}

outcome::result<std::optional<SignedBlock>> BlockTreeImpl::tryGetSignedBlock(
const BlockHash block_hash) const {
auto header_res = getBlockHeader(block_hash);
if (not header_res.has_value()) {
return std::nullopt;
}
auto &header = header_res.value();
auto body_res = getBlockBody(block_hash);
if (not body_res.has_value()) {
return std::nullopt;
}
auto &body = body_res.value();
return SignedBlock{
.message =
{
.slot = header.slot,
.proposer_index = header.proposer_index,
.parent_root = header.parent_root,
.state_root = header.state_root,
.body = std::move(body),
},
// TODO: signature
.signature = {},
};
}

void BlockTreeImpl::import(std::vector<SignedBlock> blocks) {
for (auto &block : blocks) {
// TODO: signature
std::ignore = addBlock(block.message);
}
}

outcome::result<void> BlockTreeImpl::reorgAndPrune(
const BlockTreeData &p, const ReorgAndPrune &changes) {
OUTCOME_TRY(p.storage_->setBlockTreeLeaves(p.tree_->leafHashes()));
Expand Down
9 changes: 9 additions & 0 deletions src/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace lean::blockchain {
class BlockTreeInitializer;
}

namespace lean::crypto {
class Hasher;
} // namespace lean::crypto

namespace lean::blockchain {

class BlockTreeImpl final
Expand Down Expand Up @@ -104,6 +108,11 @@ namespace lean::blockchain {

BlockIndex lastFinalized() const override;

StatusMessage getStatusMessage() const override;
outcome::result<std::optional<SignedBlock>> tryGetSignedBlock(
const BlockHash block_hash) const override;
void import(std::vector<SignedBlock> blocks) override;

// BlockHeaderRepository methods

outcome::result<Slot> getNumberByHash(
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain/impl/genesis_block_header_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace lean::blockchain {
res.error());
qtils::raise_on_err(res);
}
updateHash(*hasher);
updateHash();
}

} // namespace lean::blockchain
13 changes: 7 additions & 6 deletions src/injector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ add_library(node_injector
node_injector.cpp
)
target_link_libraries(node_injector
Boost::Boost.DI
logger
app_configurator
chain_spec
app_state_manager
application
metrics
blockchain
Boost::Boost.DI
chain_spec
clock
hasher
se_async
logger
metrics
modules
p2p::libp2p
se_async
storage
blockchain
timeline
)
26 changes: 26 additions & 0 deletions src/libp2p/peer/peer_id_from.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <libp2p/crypto/key_marshaller.hpp>
#include <libp2p/crypto/secp256k1_types.hpp>
#include <libp2p/peer/peer_id.hpp>
#include <qtils/bytes.hpp>

namespace libp2p {
inline PeerId peerIdFromSecp256k1(
const crypto::secp256k1::PublicKey &public_key) {
return libp2p::PeerId::fromPublicKey(
libp2p::crypto::marshaller::KeyMarshaller{nullptr}
.marshal(libp2p::crypto::PublicKey{
libp2p::crypto::Key::Type::Secp256k1,
qtils::ByteVec(public_key),
})
.value())
.value();
}
} // namespace libp2p
56 changes: 26 additions & 30 deletions src/loaders/impl/networking_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@
#include "modules/shared/networking_types.tmp.hpp"
#include "se/subscription.hpp"

namespace lean::blockchain {
class BlockTree;
} // namespace lean::blockchain

namespace lean::loaders {

class NetworkingLoader final
: public std::enable_shared_from_this<NetworkingLoader>,
public Loader,
public modules::NetworkingLoader {
log::Logger logger_;
qtils::SharedRef<blockchain::BlockTree> block_tree_;

std::shared_ptr<BaseSubscriber<qtils::Empty>> on_init_complete_;

std::shared_ptr<BaseSubscriber<qtils::Empty>> on_loading_finished_;

std::shared_ptr<
BaseSubscriber<qtils::Empty,
std::shared_ptr<const messages::BlockRequestMessage>>>
on_block_request_;
ON_DISPATCH_SUBSCRIPTION(SendSignedBlock);
ON_DISPATCH_SUBSCRIPTION(SendSignedVote);

public:
NetworkingLoader(std::shared_ptr<log::LoggingSystem> logsys,
std::shared_ptr<Subscription> se_manager)
std::shared_ptr<Subscription> se_manager,
qtils::SharedRef<blockchain::BlockTree> block_tree)
: Loader(std::move(logsys), std::move(se_manager)),
logger_(logsys_->getLogger("Networking", "networking_module")) {}
logger_(logsys_->getLogger("Networking", "networking_module")),
block_tree_{std::move(block_tree)} {}

NetworkingLoader(const NetworkingLoader &) = delete;
NetworkingLoader &operator=(const NetworkingLoader &) = delete;
Expand All @@ -51,14 +56,15 @@ namespace lean::loaders {
get_module()
->getFunctionFromLibrary<std::weak_ptr<lean::modules::Networking>,
modules::NetworkingLoader &,
std::shared_ptr<log::LoggingSystem>>(
std::shared_ptr<log::LoggingSystem>,
qtils::SharedRef<blockchain::BlockTree>>(
"query_module_instance");

if (not module_accessor) {
return;
}

auto module_internal = (*module_accessor)(*this, logsys_);
auto module_internal = (*module_accessor)(*this, logsys_, block_tree_);

on_init_complete_ = se::SubscriberCreator<qtils::Empty>::template create<
EventTypes::NetworkingIsLoaded>(
Expand All @@ -83,20 +89,8 @@ namespace lean::loaders {
}
});

on_block_request_ = se::SubscriberCreator<
qtils::Empty,
std::shared_ptr<const messages::BlockRequestMessage>>::
template create<EventTypes::BlockRequest>(
*se_manager_,
SubscriptionEngineHandlers::kTest,
[module_internal, this](auto &, const auto &msg) {
if (auto m = module_internal.lock()) {
SL_TRACE(
logger_, "Handle BlockRequest; rid={}", msg->ctx.rid);
m->on_block_request(msg);
}
});

ON_DISPATCH_SUBSCRIBE(SendSignedBlock);
ON_DISPATCH_SUBSCRIBE(SendSignedVote);

se_manager_->notify(lean::EventTypes::NetworkingIsLoaded);
}
Expand All @@ -113,16 +107,18 @@ namespace lean::loaders {
se_manager_->notify(lean::EventTypes::PeerDisconnected, msg);
}

void dispatch_block_announce(
std::shared_ptr<const messages::BlockAnnounceMessage> msg) override {
SL_TRACE(logger_, "Dispatch BlockAnnounceReceived");
se_manager_->notify(lean::EventTypes::BlockAnnounceReceived, msg);
DISPATCH_OVERRIDE(StatusMessageReceived) {
SL_TRACE(logger_,
"Dispatch StatusMessageReceived peer={} finalized={} head={}",
message->from_peer,
message->notification.finalized.slot,
message->notification.head.slot);
dispatchDerive(*se_manager_, message);
}

void dispatch_block_response(
std::shared_ptr<const messages::BlockResponseMessage> msg) override {
SL_TRACE(logger_, "Dispatch BlockResponse; rid={}", msg->ctx.rid);
se_manager_->notify(lean::EventTypes::BlockResponse, std::move(msg));
DISPATCH_OVERRIDE(SignedVoteReceived) {
SL_TRACE(logger_, "Dispatch SignedVoteReceived");
dispatchDerive(*se_manager_, message);
}
};
} // namespace lean::loaders
4 changes: 4 additions & 0 deletions src/loaders/impl/production_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ namespace lean::loaders {
void dispatch_block_produced(std::shared_ptr<const Block> msg) override {
se_manager_->notify(EventTypes::BlockProduced, msg);
}

DISPATCH_OVERRIDE(SendSignedBlock) {
dispatchDerive(*se_manager_, message);
}
};

} // namespace lean::loaders
Loading
Loading