Skip to content

Commit

Permalink
Remove gas_price from predicate execution functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Feb 21, 2024
1 parent 9dddcfe commit d7546e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 76 deletions.
72 changes: 19 additions & 53 deletions fuel-vm/src/checked_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,7 @@ where
{
fn default() -> Self {
let gas_price = 0;
Tx::default()
.into_checked(
Default::default(),
&ConsensusParameters::standard(),
gas_price,
)
.expect("default tx should produce a valid fully checked transaction")
Self::default_with_gas_price(gas_price)
}
}

Expand Down Expand Up @@ -222,7 +216,7 @@ pub trait IntoChecked: FormatValidityChecks + Sized {
let check_predicate_params = consensus_params.into();
self.into_checked_basic(block_height, consensus_params, gas_price)?
.check_signatures(&consensus_params.chain_id)?
.check_predicates(gas_price, &check_predicate_params)
.check_predicates(&check_predicate_params)
}

/// Returns transaction that passed only `Checks::Basic`.
Expand Down Expand Up @@ -292,16 +286,11 @@ impl From<&ConsensusParameters> for CheckPredicateParams {
#[async_trait::async_trait]
pub trait CheckPredicates: Sized {
/// Performs predicates verification of the transaction.
fn check_predicates(
self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError>;
fn check_predicates(self, params: &CheckPredicateParams) -> Result<Self, CheckError>;

/// Performs predicates verification of the transaction in parallel.
async fn check_predicates_async<E: ParallelExecutor>(
self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError>;
}
Expand All @@ -312,14 +301,12 @@ pub trait EstimatePredicates: Sized {
/// Estimates predicates of the transaction.
fn estimate_predicates(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError>;

/// Estimates predicates of the transaction in parallel.
async fn estimate_predicates_async<E: ParallelExecutor>(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError>;
}
Expand Down Expand Up @@ -351,29 +338,25 @@ where
{
fn check_predicates(
mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError> {
if !self.checks_bitmask.contains(Checks::Predicates) {
Interpreter::<PredicateStorage, _>::check_predicates(
&self, gas_price, params,
)?;
Interpreter::<PredicateStorage, _>::check_predicates(&self, params)?;
self.checks_bitmask.insert(Checks::Predicates);
}
Ok(self)
}

async fn check_predicates_async<E>(
mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError>
where
E: ParallelExecutor,
{
if !self.checks_bitmask.contains(Checks::Predicates) {
Interpreter::<PredicateStorage, _>::check_predicates_async::<E>(
&self, gas_price, params,
&self, params,
)
.await?;

Expand All @@ -390,25 +373,21 @@ where
impl<Tx: ExecutableTransaction + Send + Sync + 'static> EstimatePredicates for Tx {
fn estimate_predicates(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError> {
Interpreter::<PredicateStorage, _>::estimate_predicates(self, gas_price, params)?;
Interpreter::<PredicateStorage, _>::estimate_predicates(self, params)?;
Ok(())
}

async fn estimate_predicates_async<E>(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError>
where
E: ParallelExecutor,
{
Interpreter::<PredicateStorage, _>::estimate_predicates_async::<E>(
self, gas_price, params,
)
.await?;
Interpreter::<PredicateStorage, _>::estimate_predicates_async::<E>(self, params)
.await?;

Ok(())
}
Expand All @@ -418,31 +397,25 @@ impl<Tx: ExecutableTransaction + Send + Sync + 'static> EstimatePredicates for T
impl EstimatePredicates for Transaction {
fn estimate_predicates(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError> {
match self {
Transaction::Script(script) => script.estimate_predicates(gas_price, params),
Transaction::Create(create) => create.estimate_predicates(gas_price, params),
Transaction::Script(script) => script.estimate_predicates(params),
Transaction::Create(create) => create.estimate_predicates(params),
Transaction::Mint(_) => Ok(()),
}
}

async fn estimate_predicates_async<E: ParallelExecutor>(
&mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<(), CheckError> {
match self {
Transaction::Script(script) => {
script
.estimate_predicates_async::<E>(gas_price, params)
.await
script.estimate_predicates_async::<E>(params).await
}
Transaction::Create(create) => {
create
.estimate_predicates_async::<E>(gas_price, params)
.await
create.estimate_predicates_async::<E>(params).await
}
Transaction::Mint(_) => Ok(()),
}
Expand All @@ -453,7 +426,6 @@ impl EstimatePredicates for Transaction {
impl CheckPredicates for Checked<Mint> {
fn check_predicates(
mut self,
_gas_price: Word,
_params: &CheckPredicateParams,
) -> Result<Self, CheckError> {
self.checks_bitmask.insert(Checks::Predicates);
Expand All @@ -462,7 +434,6 @@ impl CheckPredicates for Checked<Mint> {

async fn check_predicates_async<E: ParallelExecutor>(
mut self,
_gas_price: Word,
_params: &CheckPredicateParams,
) -> Result<Self, CheckError> {
self.checks_bitmask.insert(Checks::Predicates);
Expand All @@ -472,29 +443,24 @@ impl CheckPredicates for Checked<Mint> {

#[async_trait::async_trait]
impl CheckPredicates for Checked<Transaction> {
fn check_predicates(
self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError> {
fn check_predicates(self, params: &CheckPredicateParams) -> Result<Self, CheckError> {
let checked_transaction: CheckedTransaction = self.into();
let checked_transaction: CheckedTransaction = match checked_transaction {
CheckedTransaction::Script(tx) => {
CheckPredicates::check_predicates(tx, gas_price, params)?.into()
CheckPredicates::check_predicates(tx, params)?.into()
}
CheckedTransaction::Create(tx) => {
CheckPredicates::check_predicates(tx, gas_price, params)?.into()
CheckPredicates::check_predicates(tx, params)?.into()
}
CheckedTransaction::Mint(tx) => {
CheckPredicates::check_predicates(tx, gas_price, params)?.into()
CheckPredicates::check_predicates(tx, params)?.into()
}
};
Ok(checked_transaction.into())
}

async fn check_predicates_async<E>(
mut self,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<Self, CheckError>
where
Expand All @@ -504,17 +470,17 @@ impl CheckPredicates for Checked<Transaction> {

let checked_transaction: CheckedTransaction = match checked_transaction {
CheckedTransaction::Script(tx) => {
CheckPredicates::check_predicates_async::<E>(tx, gas_price, params)
CheckPredicates::check_predicates_async::<E>(tx, params)
.await?
.into()
}
CheckedTransaction::Create(tx) => {
CheckPredicates::check_predicates_async::<E>(tx, gas_price, params)
CheckPredicates::check_predicates_async::<E>(tx, params)
.await?
.into()
}
CheckedTransaction::Mint(tx) => {
CheckPredicates::check_predicates_async::<E>(tx, gas_price, params)
CheckPredicates::check_predicates_async::<E>(tx, params)
.await?
.into()
}
Expand Down
31 changes: 8 additions & 23 deletions fuel-vm/src/interpreter/executors/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,13 @@ where
/// predicates.
pub fn check_predicates(
checked: &Checked<Tx>,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed>
where
<Tx as IntoChecked>::Metadata: CheckedMetadata,
{
let tx = checked.transaction();
Self::run_predicate(PredicateRunKind::Verifying(tx), gas_price, params)
Self::run_predicate(PredicateRunKind::Verifying(tx), params)
}

/// Initialize the VM with the provided transaction and check all predicates defined
Expand All @@ -148,7 +147,6 @@ where
/// predicates.
pub async fn check_predicates_async<E>(
checked: &Checked<Tx>,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed>
where
Expand All @@ -158,12 +156,9 @@ where
{
let tx = checked.transaction();

let predicates_checked = Self::run_predicate_async::<E>(
PredicateRunKind::Verifying(tx),
gas_price,
params,
)
.await?;
let predicates_checked =
Self::run_predicate_async::<E>(PredicateRunKind::Verifying(tx), params)
.await?;

Ok(predicates_checked)
}
Expand All @@ -176,14 +171,10 @@ where
/// predicates.
pub fn estimate_predicates(
transaction: &mut Tx,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed> {
let predicates_checked = Self::run_predicate(
PredicateRunKind::Estimating(transaction),
gas_price,
params,
)?;
let predicates_checked =
Self::run_predicate(PredicateRunKind::Estimating(transaction), params)?;
Ok(predicates_checked)
}

Expand All @@ -195,7 +186,6 @@ where
/// predicates.
pub async fn estimate_predicates_async<E>(
transaction: &mut Tx,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed>
where
Expand All @@ -204,7 +194,6 @@ where
{
let predicates_checked = Self::run_predicate_async::<E>(
PredicateRunKind::Estimating(transaction),
gas_price,
params,
)
.await?;
Expand All @@ -214,7 +203,6 @@ where

async fn run_predicate_async<E>(
kind: PredicateRunKind<'_, Tx>,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed>
where
Expand All @@ -238,7 +226,6 @@ where
index,
predicate_action,
predicate,
gas_price,
my_params,
)
});
Expand All @@ -254,7 +241,6 @@ where

fn run_predicate(
kind: PredicateRunKind<'_, Tx>,
gas_price: Word,
params: &CheckPredicateParams,
) -> Result<PredicatesChecked, PredicateVerificationFailed> {
let predicate_action = PredicateAction::from(&kind);
Expand All @@ -271,7 +257,6 @@ where
index,
predicate_action,
predicate,
gas_price,
params.clone(),
));
}
Expand All @@ -285,7 +270,6 @@ where
index: usize,
predicate_action: PredicateAction,
predicate: RuntimePredicate,
gas_price: Word,
params: CheckPredicateParams,
) -> Result<(Word, usize), PredicateVerificationFailed> {
match &tx.inputs()[index] {
Expand Down Expand Up @@ -313,7 +297,8 @@ where

let max_gas_per_tx = params.max_gas_per_tx;
let max_gas_per_predicate = params.max_gas_per_predicate;
let interpreter_params = InterpreterParams::new(gas_price, params);
let zero_gas_price = 0;
let interpreter_params = InterpreterParams::new(zero_gas_price, params);

let mut vm = Self::with_storage(PredicateStorage {}, interpreter_params);

Expand Down

0 comments on commit d7546e2

Please sign in to comment.