Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Mar 4, 2024
1 parent 799d800 commit ea54bd4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
115 changes: 69 additions & 46 deletions crates/services/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,7 @@ pub mod block_component {
impl<'a> PartialBlockComponent<'a, OnceTransactionsSource> {
pub fn from_partial_block(block: &'a mut PartialFuelBlock) -> Self {
let transaction = core::mem::take(&mut block.transactions);
let gas_price = if let Some(Transaction::Mint(mint)) = transaction.last() {
*mint.gas_price()
} else {
0
};
let gas_price = get_gas_price_from_mint_tx(&transaction[..]);

Self {
empty_block: block,
Expand All @@ -407,6 +403,14 @@ pub mod block_component {
}
}

fn get_gas_price_from_mint_tx(transactions: &[Transaction]) -> u64 {
if let Some(Transaction::Mint(mint)) = transactions.last() {
*mint.gas_price()
} else {
0
}
}

impl<'a, TxSource> PartialBlockComponent<'a, TxSource> {
pub fn from_component(
block: &'a mut PartialFuelBlock,
Expand Down Expand Up @@ -444,52 +448,18 @@ where

// If there is full fuel block for validation then map it into
// a partial header.
let block = block.map_v(PartialFuelBlock::from);
let executable_block = block.map_validation(PartialFuelBlock::from);

// Create a new storage transaction.
let mut block_st_transaction = self.database.transaction();

let (block, execution_data) = match block {
ExecutionTypes::DryRun(component) => {
let mut block =
PartialFuelBlock::new(component.header_to_produce, vec![]);
let component = PartialBlockComponent::from_component(
&mut block,
component.transactions_source,
component.gas_price,
component.gas_limit,
);

let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::DryRun(component),
)?;
(block, execution_data)
}
ExecutionTypes::Production(component) => {
let mut block =
PartialFuelBlock::new(component.header_to_produce, vec![]);
let component = PartialBlockComponent::from_component(
&mut block,
component.transactions_source,
component.gas_price,
component.gas_limit,
);

let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::Production(component),
)?;
(block, execution_data)
let (block, execution_data) = match executable_block {
ExecutionTypes::DryRun(block_components) => {
self.execute_dry_run(block_components, &mut block_st_transaction)?
}
ExecutionTypes::Validation(mut block) => {
let component = PartialBlockComponent::from_partial_block(&mut block);
let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::Validation(component),
)?;
(block, execution_data)
ExecutionTypes::Production(block_components) => {
self.execute_production(block_components, &mut block_st_transaction)?
}
ExecutionTypes::Validation(block) => self.execute_validation(block)?,
};

let ExecutionData {
Expand Down Expand Up @@ -534,6 +504,59 @@ where
Ok(UncommittedResult::new(result, block_st_transaction))
}

fn execute_dry_run<TxSource: TransactionsSource>(
&self,
component: Components<TxSource>,
block_st_transaction: &mut StorageTransaction<D>,
) -> ExecutorResult<(PartialFuelBlock, ExecutionData)> {
let mut block = PartialFuelBlock::new(component.header_to_produce, vec![]);
let component = PartialBlockComponent::from_component(
&mut block,
component.transactions_source,
component.gas_price,
component.gas_limit,
);

let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::DryRun(component),
)?;
Ok((block, execution_data))
}

fn execute_production<TxSource: TransactionsSource>(
&self,
component: Components<TxSource>,
block_st_transaction: &mut StorageTransaction<D>,
) -> ExecutorResult<(PartialFuelBlock, ExecutionData)> {
let mut block = PartialFuelBlock::new(component.header_to_produce, vec![]);
let component = PartialBlockComponent::from_component(
&mut block,
component.transactions_source,
component.gas_price,
component.gas_limit,
);

let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::Production(component),
)?;
Ok((block, execution_data))
}

fn execute_validation(
&self,
mut block: PartialFuelBlock,
) -> ExecutorResult<(PartialFuelBlock, ExecutionData)> {
let component = PartialBlockComponent::from_partial_block(&mut block);
let mut block_st_transaction = self.database.transaction();
let execution_data = self.execute_block(
block_st_transaction.as_mut(),
ExecutionType::Validation(component),
)?;
Ok((block, execution_data))
}

#[tracing::instrument(skip_all)]
/// Execute the fuel block with all transactions.
fn execute_block<TxSource>(
Expand Down
4 changes: 2 additions & 2 deletions crates/types/src/services/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub type ExecutionType<T> = ExecutionTypes<T, T>;

impl<P, V> ExecutionTypes<P, V> {
/// Map the production type if producing.
pub fn map_p<Q, F>(self, f: F) -> ExecutionTypes<Q, V>
pub fn map_production<Q, F>(self, f: F) -> ExecutionTypes<Q, V>
where
F: FnOnce(P) -> Q,
{
Expand All @@ -164,7 +164,7 @@ impl<P, V> ExecutionTypes<P, V> {
}

/// Map the validation type if validating.
pub fn map_v<W, F>(self, f: F) -> ExecutionTypes<P, W>
pub fn map_validation<W, F>(self, f: F) -> ExecutionTypes<P, W>
where
F: FnOnce(V) -> W,
{
Expand Down

0 comments on commit ea54bd4

Please sign in to comment.