Skip to content

Commit

Permalink
chore: seal traits (FuelLabs#1276)
Browse files Browse the repository at this point in the history
Seals the following traits to preventing downstream implementation

`Transaction`
`TransactionBuilder`
`BuildableTransaction`
`EstimablePredicates`
`GasValidation`
  • Loading branch information
MujkicA authored Feb 16, 2024
1 parent 4cafbdf commit b6c5a8b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
10 changes: 7 additions & 3 deletions packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 36,7 @@ use crate::{
unresolved_bytes::UnresolvedBytes,
Address, AssetId, ContractId,
},
utils::calculate_witnesses_size,
utils::{calculate_witnesses_size, sealed},
};

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
Expand Down Expand Up @@ -67,7 67,7 @@ struct UnresolvedWitnessIndexes {
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait BuildableTransaction {
pub trait BuildableTransaction: sealed::Sealed {
type TxType: Transaction;

async fn build(self, provider: &impl DryRunner) -> Result<Self::TxType>;
Expand All @@ -78,6 78,8 @@ pub trait BuildableTransaction {
async fn build_without_signatures(self, provider: &impl DryRunner) -> Result<Self::TxType>;
}

impl sealed::Sealed for ScriptTransactionBuilder {}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl BuildableTransaction for ScriptTransactionBuilder {
type TxType = ScriptTransaction;
Expand All @@ -94,6 96,8 @@ impl BuildableTransaction for ScriptTransactionBuilder {
}
}

impl sealed::Sealed for CreateTransactionBuilder {}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl BuildableTransaction for CreateTransactionBuilder {
type TxType = CreateTransaction;
Expand All @@ -111,7 115,7 @@ impl BuildableTransaction for CreateTransactionBuilder {
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait TransactionBuilder: BuildableTransaction Send {
pub trait TransactionBuilder: BuildableTransaction Send sealed::Sealed {
type TxType: Transaction;

fn add_signer(&mut self, signer: impl Signer Send Sync) -> Result<&mut Self>;
Expand Down
10 changes: 6 additions & 4 deletions packages/fuels-core/src/types/wrappers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,7 @@ use crate::{
constants::BASE_ASSET_ID,
traits::Signer,
types::{bech32::Bech32Address, errors::error, Result},
utils::calculate_witnesses_size,
utils::{calculate_witnesses_size, sealed},
};

#[derive(Default, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -162,21 162,21 @@ pub enum TransactionType {
Mint(MintTransaction),
}

pub trait EstimablePredicates {
pub trait EstimablePredicates: sealed::Sealed {
/// If a transaction contains predicates, we have to estimate them
/// before sending the transaction to the node. The estimation will check
/// all predicates and set the `predicate_gas_used` to the actual consumed gas.
fn estimate_predicates(&mut self, consensus_parameters: &ConsensusParameters) -> Result<()>;
}

pub trait GasValidation {
pub trait GasValidation: sealed::Sealed {
fn validate_gas(&self, min_gas_price: u64, gas_used: u64) -> Result<()>;
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Transaction:
Into<FuelTransaction> EstimablePredicates GasValidation Clone Debug
Into<FuelTransaction> EstimablePredicates GasValidation Clone Debug sealed::Sealed
{
fn fee_checked_from_tx(
&self,
Expand Down Expand Up @@ -298,6 298,8 @@ macro_rules! impl_tx_wrapper {
}
}

impl sealed::Sealed for $wrapper {}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Transaction for $wrapper {
Expand Down
4 changes: 4 additions & 0 deletions packages/fuels-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 34,7 @@ pub(crate) fn calculate_witnesses_size<'a, I: IntoIterator<Item = &'a Witness>>(
.map(|w| w.as_ref().len() WITNESS_STATIC_SIZE)
.sum()
}

pub(crate) mod sealed {
pub trait Sealed {}
}
6 changes: 5 additions & 1 deletion packages/fuels-programs/src/call_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 37,12 @@ pub(crate) struct CallOpcodeParamsOffset {
/// How many times to attempt to resolve missing tx dependencies.
pub const DEFAULT_TX_DEP_ESTIMATION_ATTEMPTS: u64 = 10;

pub(crate) mod sealed {
pub trait Sealed {}
}

#[async_trait::async_trait]
pub trait TxDependencyExtension: Sized {
pub trait TxDependencyExtension: Sized sealed::Sealed {
async fn simulate(&mut self) -> Result<()>;

/// Appends `num` [`fuel_tx::Output::Variable`]s to the transaction.
Expand Down
6 changes: 5 additions & 1 deletion packages/fuels-programs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 32,7 @@ use fuels_core::{
use crate::{
call_response::FuelCallResponse,
call_utils::{
build_tx_from_contract_calls, new_variable_outputs,
build_tx_from_contract_calls, new_variable_outputs, sealed,
transaction_builder_from_contract_calls, TxDependencyExtension,
},
receipt_parser::ReceiptParser,
Expand Down Expand Up @@ -664,6 664,8 @@ where
}
}

impl<T: Account, D> sealed::Sealed for ContractCallHandler<T, D> {}

#[async_trait::async_trait]
impl<T, D> TxDependencyExtension for ContractCallHandler<T, D>
where
Expand Down Expand Up @@ -971,6 973,8 @@ impl<T: Account> MultiContractCallHandler<T> {
}
}

impl<T: Account> sealed::Sealed for MultiContractCallHandler<T> {}

#[async_trait::async_trait]
impl<T> TxDependencyExtension for MultiContractCallHandler<T>
where
Expand Down
4 changes: 3 additions & 1 deletion packages/fuels-programs/src/script_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 28,7 @@ use itertools::chain;
use crate::{
call_response::FuelCallResponse,
call_utils::{
generate_contract_inputs, generate_contract_outputs, new_variable_outputs,
generate_contract_inputs, generate_contract_outputs, new_variable_outputs, sealed,
TxDependencyExtension,
},
contract::SettableContract,
Expand Down Expand Up @@ -302,6 302,8 @@ where
}
}

impl<T: Account, D> sealed::Sealed for ScriptCallHandler<T, D> {}

#[async_trait::async_trait]
impl<T, D> TxDependencyExtension for ScriptCallHandler<T, D>
where
Expand Down

0 comments on commit b6c5a8b

Please sign in to comment.