11 constexpr
const char *kTransactionsIncludedInBlock =
12 "kagome_proposer_number_of_transactions";
18 std::shared_ptr<BlockBuilderFactory> block_builder_factory,
19 std::shared_ptr<transaction_pool::TransactionPool> transaction_pool,
20 std::shared_ptr<primitives::events::ExtrinsicSubscriptionEngine>
22 std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
23 extrinsic_event_key_repo)
24 : block_builder_factory_{std::move(block_builder_factory)},
34 kTransactionsIncludedInBlock,
35 "Number of transactions included in block");
44 OUTCOME_TRY(block_builder,
47 auto inherent_xts_res = block_builder->getInherentExtrinsics(inherent_data);
48 if (not inherent_xts_res) {
49 logger_->error(
"BlockBuilder->inherent_extrinsics failed with error: {}",
50 inherent_xts_res.error().message());
51 return inherent_xts_res.error();
53 const auto &inherent_xts = inherent_xts_res.value();
55 for (
const auto &xt : inherent_xts) {
56 SL_DEBUG(
logger_,
"Adding inherent extrinsic: {}", xt.data);
57 auto inserted_res = block_builder->pushExtrinsic(xt);
58 if (not inserted_res) {
61 "Dropping non-mandatory inherent extrinsic from overweight " 65 "Mandatory inherent extrinsic returned error. Block cannot " 67 return inserted_res.error();
71 "Inherent extrinsic returned unexpected error: {}. Dropping.",
72 inserted_res.error().message());
73 return inserted_res.error();
79 if (remove_res.has_error()) {
81 "Stale transactions remove failure: {}, Parent is {}",
82 remove_res.error().message(),
87 bool transaction_pushed =
false;
88 bool hit_block_size_limit =
false;
92 const auto kMaxVarintLength = 9;
96 auto block_size = block_builder->estimateBlockSize() + kMaxVarintLength;
100 size_t included_tx_count = 0;
101 for (
const auto &[hash, tx] : ready_txs) {
102 const auto &tx_ref = tx;
103 scale::ScaleEncoderStream s(
true);
105 auto estimate_tx_size = s.size();
107 if (block_size + estimate_tx_size > block_size_limit) {
111 "Transaction would overflow the block size limit, but will " 112 "try {} more transactions before quitting.",
117 "Reached block size limit, proceeding with proposing.");
118 hit_block_size_limit =
true;
122 SL_DEBUG(
logger_,
"Adding extrinsic: {}", tx_ref->ext.data);
123 auto inserted_res = block_builder->pushExtrinsic(tx->ext);
124 if (not inserted_res) {
129 "Block seems full, but will try {} more transactions " 133 SL_DEBUG(
logger_,
"Block is full, proceed with proposing.");
137 logger_->warn(
"Extrinsic {} was not added to the block. Reason: {}",
139 inserted_res.error().message());
142 block_size += estimate_tx_size;
143 transaction_pushed =
true;
149 if (hit_block_size_limit and not transaction_pushed) {
151 "Hit block size limit of `{}` without including any transaction!",
155 OUTCOME_TRY(block, block_builder->bake());
157 for (
const auto &[hash, tx] : ready_txs) {
159 if (not removed_res) {
161 "Can't remove extrinsic {} after adding to the block. Reason: {}",
163 removed_res.error().message());
167 return std::move(block);
std::shared_ptr< BlockBuilderFactory > block_builder_factory_
ProposerImpl(std::shared_ptr< BlockBuilderFactory > block_builder_factory, std::shared_ptr< transaction_pool::TransactionPool > transaction_pool, std::shared_ptr< primitives::events::ExtrinsicSubscriptionEngine > ext_sub_engine, std::shared_ptr< subscription::ExtrinsicEventKeyRepository > extrinsic_event_key_repo)
std::shared_ptr< subscription::ExtrinsicEventKeyRepository > extrinsic_event_key_repo_
static constexpr size_t kBlockSizeLimit
Default block size limit in bytes.
virtual void set(double val)=0
Set the gauge to the given value.
std::shared_ptr< transaction_pool::TransactionPool > transaction_pool_
std::shared_ptr< primitives::events::ExtrinsicSubscriptionEngine > ext_sub_engine_
metrics::Gauge * metric_tx_included_in_block_
metrics::RegistryPtr metrics_registry_
static constexpr auto kMaxSkippedTransactions
outcome::result< primitives::Block > propose(const primitives::BlockInfo &parent_block, const primitives::InherentData &inherent_data, const primitives::Digest &inherent_digest) override