Skip to content

Commit

Permalink
feat(collator): receive gas used from executor output
Browse files Browse the repository at this point in the history
  • Loading branch information
serejkaaa512 committed Jun 20, 2024
1 parent 46d16ae commit aae1998
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 26 additions & 32 deletions collator/src/collator/do_collate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ impl CollatorStdImpl {
collation_data.gen_utime_ms = (next_chain_time % 1000) as u16;
collation_data.start_lt = Self::calc_start_lt(mc_data, prev_shard_data, &collation_data)?;
collation_data.next_lt = collation_data.start_lt + 1;
collation_data.stats.lt_start = collation_data.start_lt;
collation_data.block_limit.lt_start = collation_data.start_lt;
collation_data.block_limit.lt_current = collation_data.start_lt;
collation_data.block_limit.load_block_limits(
mc_data
.config()
.get_block_limits(self.shard_id.is_masterchain())?,
);

collation_data.processed_upto = prev_shard_data.processed_upto().clone();
tracing::debug!(target: tracing_targets::COLLATOR, "initial processed_upto.externals = {:?}",
Expand Down Expand Up @@ -195,13 +201,21 @@ impl CollatorStdImpl {
loop {
let mut timer = Instant::now();

let soft_level_reached = collation_data.block_limit.reached(BlockLimitsLevel::Soft);
if soft_level_reached {
tracing::debug!(target: tracing_targets::COLLATOR,
"STUB: soft block limit reached: {:?}",
collation_data.block_limit,
);
}
let mut executed_internal_messages = vec![];
let mut internal_messages_sources = FastHashMap::default();
// build messages set
let mut msgs_set: Vec<Box<ParsedMessage>> = vec![];

// 1. First try to read min externals amount
let mut ext_msgs = if self.has_pending_externals {

let mut ext_msgs = if !soft_level_reached && self.has_pending_externals {
self.read_next_externals(min_externals_per_set, &mut collation_data)?
} else {
vec![]
Expand Down Expand Up @@ -251,7 +265,7 @@ impl CollatorStdImpl {
// If not enough existing internals to fill the set then try read more externals
msgs_set.append(&mut ext_msgs);
remaining_capacity = max_messages_per_set - msgs_set.len();
if remaining_capacity > 0 && self.has_pending_externals {
if remaining_capacity > 0 && self.has_pending_externals && !soft_level_reached {
ext_msgs = self.read_next_externals(remaining_capacity, &mut collation_data)?;
tracing::debug!(target: tracing_targets::COLLATOR,
ext_count = ext_msgs.len(),
Expand Down Expand Up @@ -327,7 +341,7 @@ impl CollatorStdImpl {
fill_msgs_total_elapsed += timer.elapsed();

// execute msgs processing by groups
'execute_groups: while !msgs_set_full_processed {
while !msgs_set_full_processed {
// Process messages
timer = std::time::Instant::now();
let tick = exec_manager.tick(msgs_set_offset).await?;
Expand Down Expand Up @@ -375,7 +389,7 @@ impl CollatorStdImpl {
}

collation_data.next_lt = exec_manager.min_next_lt();
collation_data.stats.lt_current = collation_data.next_lt;
collation_data.block_limit.lt_current = collation_data.next_lt;
}

msgs_set_offset = tick.new_offset;
Expand All @@ -396,18 +410,14 @@ impl CollatorStdImpl {
if msgs_set_offset == msgs_set_len {
msgs_set_full_processed = true;
}
}

if let BlockLimitsLevel::Hard = collation_data
.stats
.current_level(&self.config.block_limits)
{
if collation_data.block_limit.reached(BlockLimitsLevel::Hard) {
tracing::debug!(target: tracing_targets::COLLATOR,
"STUB: block limit reached: {:?}/{:?}",
collation_data.stats, self.config.block_limits,
"STUB: block limit reached: {:?}",
collation_data.block_limit,
);
block_limits_reached = true;
break 'execute_groups;
break;
}
}

Expand Down Expand Up @@ -1306,24 +1316,8 @@ fn new_transaction(
);

collation_data.execute_count_all += 1;
let transaction = executor_output.transaction.load()?;
let gas_used = 'gas: {
match transaction.load_info()? {
TxInfo::Ordinary(info) => {
if let ComputePhase::Executed(phase) = &info.compute_phase {
break 'gas phase.gas_used.into_inner();
}
}
TxInfo::TickTock(info) => {
if let ComputePhase::Executed(phase) = &info.compute_phase {
break 'gas phase.gas_used.into_inner();
}
}
};
0
};
collation_data.stats.gas_used += gas_used as u32;
collation_data.stats.add_cell(&*in_msg.cell)?;
collation_data.block_limit.gas_used += executor_output.gas_used as u32;
collation_data.block_limit.add_cell(&*in_msg.cell)?;

let import_fees;
let in_msg_hash = *in_msg.cell.repr_hash();
Expand Down Expand Up @@ -1458,7 +1452,7 @@ fn new_transaction(

for out_msg_cell in executor_output.out_msgs.values() {
let out_msg_cell = out_msg_cell?;
collation_data.stats.add_cell(&*out_msg_cell)?;
collation_data.block_limit.add_cell(&*out_msg_cell)?;
let out_msg_hash = *out_msg_cell.repr_hash();
let out_msg_info = out_msg_cell.parse::<MsgInfo>()?;

Expand Down
41 changes: 24 additions & 17 deletions collator/src/collator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ pub(super) struct BlockCollationData {

pub tx_count: u32,

pub stats: BlockStats,
pub block_limit: BlockLimitStats,

pub total_execute_msgs_time_mc: u128,

Expand Down Expand Up @@ -378,15 +378,20 @@ pub(super) struct BlockCollationData {
pub created_by: HashBytes,
}
#[derive(Debug, Default)]
pub struct BlockStats {
pub struct BlockLimitStats {
pub gas_used: u32,
pub lt_current: u64,
pub lt_start: u64,
pub cells_seen: HashSet<HashBytes>,
pub cells_bits: u32,
pub block_limits: Option<BlockLimits>,
}

impl BlockStats {
impl BlockLimitStats {
pub fn load_block_limits(&mut self, block_limits: BlockLimits) {
self.block_limits = Some(block_limits);
}

pub fn add_cell(&mut self, cell: &DynCell) -> Result<()> {
if !self.cells_seen.insert(*cell.repr_hash()) {
return Ok(());
Expand All @@ -399,13 +404,15 @@ impl BlockStats {
}
Ok(())
}
pub fn current_level(&self, block_limits: &BlockLimits) -> BlockLimitsLevel {
let mut result = BlockLimitsLevel::Underload;
let BlockLimits {
pub fn reached(&self, level: BlockLimitsLevel) -> bool {
let Some(BlockLimits {
bytes,
gas,
lt_delta,
} = block_limits;
}) = &self.block_limits
else {
return false;
};

let BlockParamLimits {
soft_limit,
Expand All @@ -415,10 +422,10 @@ impl BlockStats {

let cells_bytes = self.cells_bits / 8;
if cells_bytes >= *hard_limit {
return BlockLimitsLevel::Hard;
return true;
}
if cells_bytes >= *soft_limit {
result = BlockLimitsLevel::Soft
if cells_bytes >= *soft_limit && level == BlockLimitsLevel::Soft {
return true;
}

let BlockParamLimits {
Expand All @@ -428,10 +435,10 @@ impl BlockStats {
} = gas;

if self.gas_used >= *hard_limit {
return BlockLimitsLevel::Hard;
return true;
}
if self.gas_used >= *soft_limit {
result = BlockLimitsLevel::Soft
if self.gas_used >= *soft_limit && level == BlockLimitsLevel::Soft {
return true;
}

let BlockParamLimits {
Expand All @@ -442,12 +449,12 @@ impl BlockStats {

let delta_lt = (self.lt_current - self.lt_start) as u32;
if delta_lt >= *hard_limit {
return BlockLimitsLevel::Hard;
return true;
}
if delta_lt >= *soft_limit {
result = BlockLimitsLevel::Soft
if delta_lt >= *soft_limit && level == BlockLimitsLevel::Soft {
return true;
}
result
false
}
}

Expand Down
24 changes: 2 additions & 22 deletions collator/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::time::Duration;
use everscale_crypto::ed25519::KeyPair;
use everscale_types::cell::HashBytes;
use everscale_types::models::{
Block, BlockId, BlockInfo, BlockLimits, BlockParamLimits, CurrencyCollection,
GlobalCapabilities, GlobalCapability, IntAddr, ShardIdent, Signature, ValueFlow,
Block, BlockId, BlockInfo, CurrencyCollection, GlobalCapabilities, GlobalCapability, IntAddr,
ShardIdent, Signature, ValueFlow,
};
use serde::{Deserialize, Serialize};
use tycho_block_util::block::{BlockStuffAug, ValidatorSubsetInfo};
Expand All @@ -25,8 +25,6 @@ pub struct CollationConfig {
pub max_uncommitted_chain_length: u32,
pub uncommitted_chain_to_import_next_anchor: u32,

pub block_limits: BlockLimits,

pub msgs_exec_params: MsgsExecutionParams,
}

Expand All @@ -42,24 +40,6 @@ impl Default for CollationConfig {
max_uncommitted_chain_length: 31,
uncommitted_chain_to_import_next_anchor: 4,

block_limits: BlockLimits {
bytes: BlockParamLimits {
underload: 131072,
soft_limit: 524288,
hard_limit: 1048576,
},
gas: BlockParamLimits {
underload: 900000,
soft_limit: 1200000,
hard_limit: 2000000,
},
lt_delta: BlockParamLimits {
underload: 1000,
soft_limit: 5000,
hard_limit: 10000,
},
},

msgs_exec_params: MsgsExecutionParams::default(),
}
}
Expand Down

0 comments on commit aae1998

Please sign in to comment.