Skip to content

Commit

Permalink
Merge branch 'master' into validation_v3
Browse files Browse the repository at this point in the history
  • Loading branch information
iceseer committed Jul 23, 2024
2 parents 97a51a1 + af3bc29 commit 2743a8d
Show file tree
Hide file tree
Showing 75 changed files with 1,430 additions and 284 deletions.
7 changes: 7 additions & 0 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ hunter_config(
CMAKE_ARGS WITH_GFLAGS=OFF
)

hunter_config(
bandersnatch_vrfs_crust
URL https://github.com/qdrvm/bandersnatch-vrfs-crust/archive/refs/heads/draft2.tar.gz
SHA1 8d4512287ff7744b87f222faae768dbaa7f0c77a
KEEP_PACKAGE_SOURCES
)

if ("${WASM_COMPILER}" STREQUAL "WasmEdge")
hunter_config(
WasmEdge
Expand Down
5 changes: 4 additions & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ find_package(schnorrkel_crust CONFIG REQUIRED)
hunter_add_package(arkworks_crust)
find_package(arkworks_crust CONFIG REQUIRED)

hunter_add_package(bandersnatch_vrfs_crust)
find_package(bandersnatch_vrfs_crust CONFIG REQUIRED)

hunter_add_package(jsonrpc-lean)
find_package(jsonrpc-lean REQUIRED)

Expand All @@ -83,7 +86,7 @@ find_package(scale CONFIG REQUIRED)
hunter_add_package(zstd)
find_package(zstd CONFIG REQUIRED)


if ("${WASM_COMPILER}" STREQUAL "WAVM")
hunter_add_package(wavm)
find_package(LLVM CONFIG REQUIRED)
Expand Down
10 changes: 6 additions & 4 deletions core/authorship/block_builder_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace kagome::authorship {
public:
virtual ~BlockBuilderFactory() = default;

using Result = outcome::result<
std::pair<std::unique_ptr<BlockBuilder>, ExtrinsicInclusionMode>>;

/**
* The make method prepares a BlockBuilder for creating a block on top of a
* parent block and using provided digests. It also initializes the block
Expand All @@ -40,10 +43,9 @@ namespace kagome::authorship {
* @return A unique pointer to a BlockBuilder, or an error if the
* BlockBuilder could not be created.
*/
virtual outcome::result<std::unique_ptr<BlockBuilder>> make(
const primitives::BlockInfo &parent_block,
primitives::Digest inherent_digest,
TrieChangesTrackerOpt changes_tracker) const = 0;
virtual Result make(const primitives::BlockInfo &parent_block,
primitives::Digest inherent_digest,
TrieChangesTrackerOpt changes_tracker) const = 0;
};

} // namespace kagome::authorship
8 changes: 5 additions & 3 deletions core/authorship/impl/block_builder_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace kagome::authorship {
BOOST_ASSERT(header_backend_ != nullptr);
}

outcome::result<std::unique_ptr<BlockBuilder>> BlockBuilderFactoryImpl::make(
BlockBuilderFactory::Result BlockBuilderFactoryImpl::make(
const kagome::primitives::BlockInfo &parent,
primitives::Digest inherent_digest,
TrieChangesTrackerOpt changes_tracker) const {
Expand All @@ -48,8 +48,10 @@ namespace kagome::authorship {
logger_->error("Core_initialize_block failed: {}", res.error());
return res.error();
} else {
return std::make_unique<BlockBuilderImpl>(
header, std::move(res.value()), r_block_builder_);
auto &[ctx, mode] = res.value();
return std::make_pair(std::make_unique<BlockBuilderImpl>(
header, std::move(ctx), r_block_builder_),
mode);
}
}

Expand Down
7 changes: 3 additions & 4 deletions core/authorship/impl/block_builder_factory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ namespace kagome::authorship {
* @return A result containing a unique pointer to the new BlockBuilder
* instance, or an error if the operation failed.
*/
outcome::result<std::unique_ptr<BlockBuilder>> make(
const kagome::primitives::BlockInfo &parent_block,
primitives::Digest inherent_digest,
TrieChangesTrackerOpt changes_tracker) const override;
Result make(const kagome::primitives::BlockInfo &parent_block,
primitives::Digest inherent_digest,
TrieChangesTrackerOpt changes_tracker) const override;

private:
std::shared_ptr<runtime::Core> r_core_;
Expand Down
181 changes: 94 additions & 87 deletions core/authorship/impl/proposer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ namespace kagome::authorship {
const primitives::InherentData &inherent_data,
const primitives::Digest &inherent_digest,
TrieChangesTrackerOpt changes_tracker) {
OUTCOME_TRY(block_builder,
OUTCOME_TRY(block_builder_mode,
block_builder_factory_->make(
parent_block, inherent_digest, std::move(changes_tracker)));
auto &[block_builder, mode] = block_builder_mode;

// Retrieve and add the inherent extrinsics to the block
auto inherent_xts_res = block_builder->getInherentExtrinsics(inherent_data);
Expand Down Expand Up @@ -83,106 +84,112 @@ namespace kagome::authorship {
}
}

// Remove stale transactions from the transaction pool
auto remove_res = transaction_pool_->removeStale(parent_block.number);
if (remove_res.has_error()) {
SL_ERROR(logger_,
"Stale transactions remove failure: {}, Parent is {}",
remove_res.error(),
parent_block);
}

/// TODO(iceseer): switch to callback case(this case is needed to make tests
/// complete)

// Retrieve ready transactions from the transaction pool
std::vector<std::pair<primitives::Transaction::Hash,
std::shared_ptr<const primitives::Transaction>>>
ready_txs = transaction_pool_->getReadyTransactions();

bool transaction_pushed = false;
bool hit_block_size_limit = false;

auto skipped = 0;
auto block_size_limit = kBlockSizeLimit;
const auto kMaxVarintLength = 9; /// Max varint size in bytes when encoded
// we move estimateBlockSize() out of the loop for optimization purposes.
// to avoid varint bytes length recalculation which indicates extrinsics
// quantity, we add the maximum varint length at once.
auto block_size = block_builder->estimateBlockSize() + kMaxVarintLength;
// at the moment block_size includes block headers and a counter to hold a
// number of transactions to be pushed to the block

size_t included_tx_count = 0;
std::vector<primitives::Transaction::Hash> included_hashes;

// Iterate through the ready transactions
for (const auto &[hash, tx] : ready_txs) {
// Check if the deadline has been reached
if (deadline && clock_->now() >= deadline) {
break;
if (mode == ExtrinsicInclusionMode::AllExtrinsics) {
// Remove stale transactions from the transaction pool
auto remove_res = transaction_pool_->removeStale(parent_block.number);
if (remove_res.has_error()) {
SL_ERROR(logger_,
"Stale transactions remove failure: {}, Parent is {}",
remove_res.error(),
parent_block);
}

// Estimate the size of the transaction
scale::ScaleEncoderStream s(true);
s << tx->ext;
auto estimate_tx_size = s.size();

// Check if adding the transaction would exceed the block size limit
if (block_size + estimate_tx_size > block_size_limit) {
if (skipped < kMaxSkippedTransactions) {
++skipped;
SL_DEBUG(logger_,
"Transaction would overflow the block size limit, but will "
"try {} more transactions before quitting.",
kMaxSkippedTransactions - skipped);
continue;
/// TODO(iceseer): switch to callback case(this case is needed to make
/// tests complete)

// Retrieve ready transactions from the transaction pool
std::vector<std::pair<primitives::Transaction::Hash,
std::shared_ptr<const primitives::Transaction>>>
ready_txs = transaction_pool_->getReadyTransactions();

bool transaction_pushed = false;
bool hit_block_size_limit = false;

auto skipped = 0;
auto block_size_limit = kBlockSizeLimit;
const auto kMaxVarintLength =
9; /// Max varint size in bytes when encoded
// we move estimateBlockSize() out of the loop for optimization purposes.
// to avoid varint bytes length recalculation which indicates extrinsics
// quantity, we add the maximum varint length at once.
auto block_size = block_builder->estimateBlockSize() + kMaxVarintLength;
// at the moment block_size includes block headers and a counter to hold a
// number of transactions to be pushed to the block

size_t included_tx_count = 0;

// Iterate through the ready transactions
for (const auto &[hash, tx] : ready_txs) {
// Check if the deadline has been reached
if (deadline && clock_->now() >= deadline) {
break;
}
// Reached the block size limit, stop adding transactions
SL_DEBUG(logger_,
"Reached block size limit, proceeding with proposing.");
hit_block_size_limit = true;
break;
}

// Add the transaction to the block
SL_DEBUG(logger_, "Adding extrinsic: {}", tx->ext.data);
auto inserted_res = block_builder->pushExtrinsic(tx->ext);
if (not inserted_res) {
if (BlockBuilderError::EXHAUSTS_RESOURCES == inserted_res.error()) {
// Estimate the size of the transaction
scale::ScaleEncoderStream s(true);
s << tx->ext;
auto estimate_tx_size = s.size();

// Check if adding the transaction would exceed the block size limit
if (block_size + estimate_tx_size > block_size_limit) {
if (skipped < kMaxSkippedTransactions) {
// Skip the transaction and continue with the next one
++skipped;
SL_DEBUG(logger_,
"Block seems full, but will try {} more transactions "
"before quitting.",
kMaxSkippedTransactions - skipped);
SL_DEBUG(
logger_,
"Transaction would overflow the block size limit, but will "
"try {} more transactions before quitting.",
kMaxSkippedTransactions - skipped);
continue;
}
// Reached the block size limit, stop adding transactions
SL_DEBUG(logger_,
"Reached block size limit, proceeding with proposing.");
hit_block_size_limit = true;
break;
}

// Add the transaction to the block
SL_DEBUG(logger_, "Adding extrinsic: {}", tx->ext.data);
auto inserted_res = block_builder->pushExtrinsic(tx->ext);
if (not inserted_res) {
if (BlockBuilderError::EXHAUSTS_RESOURCES == inserted_res.error()) {
if (skipped < kMaxSkippedTransactions) {
// Skip the transaction and continue with the next one
++skipped;
SL_DEBUG(logger_,
"Block seems full, but will try {} more transactions "
"before quitting.",
kMaxSkippedTransactions - skipped);
} else {
// Maximum number of transactions reached, stop adding
// transactions
SL_DEBUG(logger_, "Block is full, proceed with proposing.");
break;
}
} else {
// Maximum number of transactions reached, stop adding transactions
SL_DEBUG(logger_, "Block is full, proceed with proposing.");
break;
logger_->warn("Extrinsic {} was not added to the block. Reason: {}",
tx->ext.data,
inserted_res.error());
}
} else {
logger_->warn("Extrinsic {} was not added to the block. Reason: {}",
tx->ext.data,
inserted_res.error());
// Transaction was successfully added to the block
block_size += estimate_tx_size;
transaction_pushed = true;
++included_tx_count;
included_hashes.emplace_back(hash);
}
} else {
// Transaction was successfully added to the block
block_size += estimate_tx_size;
transaction_pushed = true;
++included_tx_count;
included_hashes.emplace_back(hash);
}
}

// Set the number of included transactions in the block metric
metric_tx_included_in_block_->set(included_tx_count);
// Set the number of included transactions in the block metric
metric_tx_included_in_block_->set(included_tx_count);

if (hit_block_size_limit and not transaction_pushed) {
SL_WARN(logger_,
"Hit block size limit of `{}` without including any transaction!",
block_size_limit);
if (hit_block_size_limit and not transaction_pushed) {
SL_WARN(
logger_,
"Hit block size limit of `{}` without including any transaction!",
block_size_limit);
}
}

// Create the block
Expand Down
16 changes: 16 additions & 0 deletions core/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ target_link_libraries(sr25519_provider
)
kagome_install(sr25519_provider)

add_library(bandersnatch_provider
bandersnatch/bandersnatch_provider_impl.cpp
)
target_link_libraries(bandersnatch_provider
bandersnatch_vrfs_crust::bandersnatch_vrfs_crust
)
kagome_install(bandersnatch_provider)

add_library(bandersnatch_vrf
bandersnatch/vrf.cpp
)
target_link_libraries(bandersnatch_vrf
bandersnatch_vrfs_crust::bandersnatch_vrfs_crust
)
kagome_install(bandersnatch_vrf)

add_library(ecdsa_provider
ecdsa/ecdsa_provider_impl.cpp
ecdsa_types.cpp
Expand Down
Loading

0 comments on commit 2743a8d

Please sign in to comment.