Skip to content

Commit b6c5a8b

Browse files
authored
chore: seal traits (#1276)
Seals the following traits to preventing downstream implementation `Transaction` `TransactionBuilder` `BuildableTransaction` `EstimablePredicates` `GasValidation`
1 parent 4cafbdf commit b6c5a8b

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

packages/fuels-core/src/types/transaction_builders.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
unresolved_bytes::UnresolvedBytes,
3737
Address, AssetId, ContractId,
3838
},
39-
utils::calculate_witnesses_size,
39+
utils::{calculate_witnesses_size, sealed},
4040
};
4141

4242
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
@@ -67,7 +67,7 @@ struct UnresolvedWitnessIndexes {
6767
}
6868

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

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

81+
impl sealed::Sealed for ScriptTransactionBuilder {}
82+
8183
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
8284
impl BuildableTransaction for ScriptTransactionBuilder {
8385
type TxType = ScriptTransaction;
@@ -94,6 +96,8 @@ impl BuildableTransaction for ScriptTransactionBuilder {
9496
}
9597
}
9698

99+
impl sealed::Sealed for CreateTransactionBuilder {}
100+
97101
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
98102
impl BuildableTransaction for CreateTransactionBuilder {
99103
type TxType = CreateTransaction;
@@ -111,7 +115,7 @@ impl BuildableTransaction for CreateTransactionBuilder {
111115
}
112116

113117
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
114-
pub trait TransactionBuilder: BuildableTransaction + Send {
118+
pub trait TransactionBuilder: BuildableTransaction + Send + sealed::Sealed {
115119
type TxType: Transaction;
116120

117121
fn add_signer(&mut self, signer: impl Signer + Send + Sync) -> Result<&mut Self>;

packages/fuels-core/src/types/wrappers/transaction.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
constants::BASE_ASSET_ID,
2626
traits::Signer,
2727
types::{bech32::Bech32Address, errors::error, Result},
28-
utils::calculate_witnesses_size,
28+
utils::{calculate_witnesses_size, sealed},
2929
};
3030

3131
#[derive(Default, Debug, Clone, PartialEq, Eq)]
@@ -162,21 +162,21 @@ pub enum TransactionType {
162162
Mint(MintTransaction),
163163
}
164164

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

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

176176
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
177177
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
178178
pub trait Transaction:
179-
Into<FuelTransaction> + EstimablePredicates + GasValidation + Clone + Debug
179+
Into<FuelTransaction> + EstimablePredicates + GasValidation + Clone + Debug + sealed::Sealed
180180
{
181181
fn fee_checked_from_tx(
182182
&self,
@@ -298,6 +298,8 @@ macro_rules! impl_tx_wrapper {
298298
}
299299
}
300300

301+
impl sealed::Sealed for $wrapper {}
302+
301303
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
302304
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
303305
impl Transaction for $wrapper {

packages/fuels-core/src/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ pub(crate) fn calculate_witnesses_size<'a, I: IntoIterator<Item = &'a Witness>>(
3434
.map(|w| w.as_ref().len() + WITNESS_STATIC_SIZE)
3535
.sum()
3636
}
37+
38+
pub(crate) mod sealed {
39+
pub trait Sealed {}
40+
}

packages/fuels-programs/src/call_utils.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ pub(crate) struct CallOpcodeParamsOffset {
3737
/// How many times to attempt to resolve missing tx dependencies.
3838
pub const DEFAULT_TX_DEP_ESTIMATION_ATTEMPTS: u64 = 10;
3939

40+
pub(crate) mod sealed {
41+
pub trait Sealed {}
42+
}
43+
4044
#[async_trait::async_trait]
41-
pub trait TxDependencyExtension: Sized {
45+
pub trait TxDependencyExtension: Sized + sealed::Sealed {
4246
async fn simulate(&mut self) -> Result<()>;
4347

4448
/// Appends `num` [`fuel_tx::Output::Variable`]s to the transaction.

packages/fuels-programs/src/contract.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use fuels_core::{
3232
use crate::{
3333
call_response::FuelCallResponse,
3434
call_utils::{
35-
build_tx_from_contract_calls, new_variable_outputs,
35+
build_tx_from_contract_calls, new_variable_outputs, sealed,
3636
transaction_builder_from_contract_calls, TxDependencyExtension,
3737
},
3838
receipt_parser::ReceiptParser,
@@ -664,6 +664,8 @@ where
664664
}
665665
}
666666

667+
impl<T: Account, D> sealed::Sealed for ContractCallHandler<T, D> {}
668+
667669
#[async_trait::async_trait]
668670
impl<T, D> TxDependencyExtension for ContractCallHandler<T, D>
669671
where
@@ -971,6 +973,8 @@ impl<T: Account> MultiContractCallHandler<T> {
971973
}
972974
}
973975

976+
impl<T: Account> sealed::Sealed for MultiContractCallHandler<T> {}
977+
974978
#[async_trait::async_trait]
975979
impl<T> TxDependencyExtension for MultiContractCallHandler<T>
976980
where

packages/fuels-programs/src/script_calls.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use itertools::chain;
2828
use crate::{
2929
call_response::FuelCallResponse,
3030
call_utils::{
31-
generate_contract_inputs, generate_contract_outputs, new_variable_outputs,
31+
generate_contract_inputs, generate_contract_outputs, new_variable_outputs, sealed,
3232
TxDependencyExtension,
3333
},
3434
contract::SettableContract,
@@ -302,6 +302,8 @@ where
302302
}
303303
}
304304

305+
impl<T: Account, D> sealed::Sealed for ScriptCallHandler<T, D> {}
306+
305307
#[async_trait::async_trait]
306308
impl<T, D> TxDependencyExtension for ScriptCallHandler<T, D>
307309
where

0 commit comments

Comments
 (0)