Skip to content

Commit

Permalink
Hide Default params under the "test-helper" feature (#708)
Browse files Browse the repository at this point in the history
* Versioned `GasCosts`

* Versioned `ConsensusParameters`ю
Reduced default `MAX_SIZE` to be 110kb.
Reduced default `MAX_CONTRACT_SIZE` to be 100kb.

* Updated CHANGELOG.md

* Versioned `FeeParameters`

* Versioned `PredicateParameters`, `ScriptParameters`, `ContractParameters`,

* Versioned `TxParameters`

* Updated CHANGELOG.md

* Make CI happy

* Moved all `Default` parameters under the "test-helper" flag

* Updated CHANGELOG.md

* Fixed WASM compilation
  • Loading branch information
xgreenx authored Mar 25, 2024
1 parent d5824dd commit c876027
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Breaking

- [#708](https://github.com/FuelLabs/fuel-vm/pull/708): Hidden `Default` params under the "test-helper" feature to avoid accidental use in production code. It is a huge breaking change for any code that has used them before in production, and instead, it should be fetched from the network. In the case of tests simply use the "test-helper" feature in your `[dev-dependencies]` section.
- [#702](https://github.com/FuelLabs/fuel-vm/pull/702): Wrapped `FeeParameters`, `PredicateParameters`, `TxParameters`, `ScriptParameters` and `ContractParameters` into an enum to support versioning.
- [#701](https://github.com/FuelLabs/fuel-vm/pull/701): Wrapped `ConsensusParameters` and `GasCosts` into an enum to support versioning. Moved `block_gas_limit` from `fuel_core_chain_config::ChainConfig` to `ConsensusPataremeters`. Reduced default `MAX_SIZE` to be [110kb](https://github.com/FuelLabs/fuel-core/pull/1761) and `MAX_CONTRACT_SIZE` to be [100kb](https://github.com/FuelLabs/fuel-core/pull/1761).
- [#692](https://github.com/FuelLabs/fuel-vm/pull/692): Add GTF getters for tx size and address.
Expand Down
4 changes: 2 additions & 2 deletions fuel-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ wasm-bindgen = { version = "0.2.88", optional = true }
[dev-dependencies]
bincode = { workspace = true }
fuel-crypto = { workspace = true, default-features = false, features = ["random"] }
fuel-tx = { path = ".", features = ["builder", "random", "serde"] }
fuel-tx = { path = ".", features = ["random", "serde", "test-helpers"] }
fuel-tx-test-helpers = { path = "test-helpers" }
fuel-types = { workspace = true, default-features = false, features = ["random"] }
hex = { version = "0.4", default-features = false }
Expand All @@ -45,7 +45,7 @@ serde_json = { version = "1.0" }

[features]
default = ["fuel-asm/default", "fuel-crypto/default", "fuel-merkle/default", "fuel-types/default", "std"]
builder = ["alloc", "internals"]
test-helpers = ["alloc", "internals"]
internals = []
typescript = ["alloc", "js-sys", "wasm-bindgen", "serde", "serde-wasm-bindgen", "fuel-types/typescript"]
random = ["fuel-crypto/random", "fuel-types/random", "rand"]
Expand Down
4 changes: 2 additions & 2 deletions fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use fuel_types::{
};
pub use tx_pointer::TxPointer;

#[cfg(feature = "builder")]
#[cfg(feature = "test-helpers")]
mod builder;

#[cfg(feature = "alloc")]
Expand All @@ -51,7 +51,7 @@ mod transaction;
#[cfg(test)]
mod tests;

#[cfg(feature = "builder")]
#[cfg(feature = "test-helpers")]
pub use builder::{
Buildable,
Finalizable,
Expand Down
3 changes: 2 additions & 1 deletion fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum Transaction {
Mint(Mint),
}

#[cfg(feature = "test-helpers")]
impl Default for Transaction {
fn default() -> Self {
Script::default().into()
Expand All @@ -104,7 +105,7 @@ impl Default for Transaction {

impl Transaction {
/// Return default valid transaction useful for tests.
#[cfg(all(feature = "rand", feature = "std", feature = "builder"))]
#[cfg(all(feature = "rand", feature = "std", feature = "test-helpers"))]
pub fn default_test_tx() -> Self {
use crate::Finalizable;

Expand Down
52 changes: 38 additions & 14 deletions fuel-tx/src/transaction/consensus_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub use gas::{
GasUnit,
};

#[cfg(feature = "test-helpers")]
const MAX_GAS: u64 = 100_000_000;
#[cfg(feature = "test-helpers")]
const MAX_SIZE: u64 = 110 * 1024;

/// A versioned set of consensus parameters.
Expand All @@ -26,18 +28,21 @@ pub enum ConsensusParameters {
V1(ConsensusParametersV1),
}

#[cfg(feature = "test-helpers")]
impl Default for ConsensusParameters {
fn default() -> Self {
Self::standard()
}
}

impl ConsensusParameters {
#[cfg(feature = "test-helpers")]
/// Constructor for the `ConsensusParameters` with Standard values.
pub fn standard() -> Self {
ConsensusParametersV1::standard().into()
}

#[cfg(feature = "test-helpers")]
/// Constructor for the `ConsensusParameters` with Standard values around `ChainId`.
pub fn standard_with_id(chain_id: ChainId) -> Self {
ConsensusParametersV1::standard_with_id(chain_id).into()
Expand Down Expand Up @@ -141,7 +146,6 @@ impl ConsensusParameters {
}
}

#[cfg(feature = "builder")]
impl ConsensusParameters {
/// Set the transaction parameters.
pub fn set_tx_params(&mut self, tx_params: TxParameters) {
Expand Down Expand Up @@ -217,7 +221,6 @@ impl ConsensusParameters {
/// A collection of parameters for convenience
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct ConsensusParametersV1 {
pub tx_params: TxParameters,
pub predicate_params: PredicateParameters,
Expand All @@ -233,6 +236,7 @@ pub struct ConsensusParametersV1 {
pub privileged_address: Address,
}

#[cfg(feature = "test-helpers")]
impl ConsensusParametersV1 {
/// Constructor for the `ConsensusParameters` with Standard values.
pub fn standard() -> Self {
Expand All @@ -256,6 +260,7 @@ impl ConsensusParametersV1 {
}
}

#[cfg(feature = "test-helpers")]
impl Default for ConsensusParametersV1 {
fn default() -> Self {
Self::standard()
Expand All @@ -276,6 +281,7 @@ pub enum FeeParameters {
}

impl FeeParameters {
#[cfg(feature = "test-helpers")]
/// Default fee parameters just for testing.
pub const DEFAULT: Self = Self::V1(FeeParametersV1::DEFAULT);

Expand Down Expand Up @@ -315,6 +321,7 @@ impl FeeParameters {
}
}

#[cfg(feature = "test-helpers")]
impl Default for FeeParameters {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -330,14 +337,14 @@ impl From<FeeParametersV1> for FeeParameters {
/// Consensus configurable parameters used for verifying transactions
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct FeeParametersV1 {
/// Factor to convert between gas and transaction assets value.
pub gas_price_factor: u64,
/// A fixed ratio linking metered bytes to gas price
pub gas_per_byte: u64,
}

#[cfg(feature = "test-helpers")]
impl FeeParametersV1 {
/// Default fee parameters just for tests.
pub const DEFAULT: Self = FeeParametersV1 {
Expand All @@ -346,6 +353,7 @@ impl FeeParametersV1 {
};
}

#[cfg(feature = "test-helpers")]
impl Default for FeeParametersV1 {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -360,6 +368,7 @@ pub enum PredicateParameters {
}

impl PredicateParameters {
#[cfg(feature = "test-helpers")]
/// Default parameters just for testing.
pub const DEFAULT: Self = Self::V1(PredicateParametersV1::DEFAULT);

Expand Down Expand Up @@ -446,6 +455,7 @@ impl From<PredicateParametersV1> for PredicateParameters {
}
}

#[cfg(feature = "test-helpers")]
impl Default for PredicateParameters {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -455,7 +465,6 @@ impl Default for PredicateParameters {
/// Consensus configurable parameters used for verifying transactions
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct PredicateParametersV1 {
/// Maximum length of predicate, in instructions.
pub max_predicate_length: u64,
Expand All @@ -467,6 +476,7 @@ pub struct PredicateParametersV1 {
pub max_gas_per_predicate: u64,
}

#[cfg(feature = "test-helpers")]
impl PredicateParametersV1 {
/// Default parameters just for testing.
pub const DEFAULT: Self = Self {
Expand All @@ -477,6 +487,7 @@ impl PredicateParametersV1 {
};
}

#[cfg(feature = "test-helpers")]
impl Default for PredicateParametersV1 {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -492,6 +503,7 @@ pub enum TxParameters {
}

impl TxParameters {
#[cfg(feature = "test-helpers")]
/// Default parameters just for testing.
pub const DEFAULT: Self = Self::V1(TxParametersV1::DEFAULT);

Expand Down Expand Up @@ -592,13 +604,13 @@ impl TxParameters {
}
}

#[cfg(feature = "test-helpers")]
impl Default for TxParameters {
fn default() -> Self {
Self::DEFAULT
}
}

#[cfg(feature = "builder")]
impl TxParameters {
pub fn set_max_size(&mut self, max_size: u64) {
match self {
Expand All @@ -615,7 +627,6 @@ impl From<TxParametersV1> for TxParameters {

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct TxParametersV1 {
/// Maximum number of inputs.
pub max_inputs: u16,
Expand All @@ -629,6 +640,7 @@ pub struct TxParametersV1 {
pub max_size: u64,
}

#[cfg(feature = "test-helpers")]
impl TxParametersV1 {
/// Default parameters just for testing.
pub const DEFAULT: Self = Self {
Expand All @@ -640,6 +652,7 @@ impl TxParametersV1 {
};
}

#[cfg(feature = "test-helpers")]
impl Default for TxParametersV1 {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -654,6 +667,7 @@ pub enum ScriptParameters {
}

impl ScriptParameters {
#[cfg(feature = "test-helpers")]
/// Default parameters just for testing.
pub const DEFAULT: Self = Self::V1(ScriptParametersV1::DEFAULT);

Expand Down Expand Up @@ -700,6 +714,7 @@ impl From<ScriptParametersV1> for ScriptParameters {
}
}

#[cfg(feature = "test-helpers")]
impl Default for ScriptParameters {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -708,14 +723,14 @@ impl Default for ScriptParameters {

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct ScriptParametersV1 {
/// Maximum length of script, in instructions.
pub max_script_length: u64,
/// Maximum length of script data, in bytes.
pub max_script_data_length: u64,
}

#[cfg(feature = "test-helpers")]
impl ScriptParametersV1 {
/// Default parameters just for testing.
pub const DEFAULT: Self = Self {
Expand All @@ -724,6 +739,7 @@ impl ScriptParametersV1 {
};
}

#[cfg(feature = "test-helpers")]
impl Default for ScriptParametersV1 {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -738,6 +754,7 @@ pub enum ContractParameters {
}

impl ContractParameters {
#[cfg(feature = "test-helpers")]
/// Default parameters just for testing.
pub const DEFAULT: Self = Self::V1(ContractParametersV1::DEFAULT);

Expand Down Expand Up @@ -784,6 +801,7 @@ impl From<ContractParametersV1> for ContractParameters {
}
}

#[cfg(feature = "test-helpers")]
impl Default for ContractParameters {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -792,7 +810,6 @@ impl Default for ContractParameters {

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct ContractParametersV1 {
/// Maximum contract size, in bytes.
pub contract_max_size: u64,
Expand All @@ -801,6 +818,7 @@ pub struct ContractParametersV1 {
pub max_storage_slots: u64,
}

#[cfg(feature = "test-helpers")]
impl ContractParametersV1 {
/// Default parameters just for testing.
pub const DEFAULT: Self = Self {
Expand All @@ -809,6 +827,7 @@ impl ContractParametersV1 {
};
}

#[cfg(feature = "test-helpers")]
impl Default for ContractParametersV1 {
fn default() -> Self {
Self::DEFAULT
Expand All @@ -819,7 +838,10 @@ impl Default for ContractParametersV1 {
pub mod typescript {
use wasm_bindgen::prelude::*;

use super::PredicateParameters as PredicateParametersRust;
use super::{
PredicateParameters as PredicateParametersRust,
PredicateParametersV1,
};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "typescript", wasm_bindgen::prelude::wasm_bindgen)]
Expand All @@ -840,11 +862,13 @@ pub mod typescript {
max_message_data_length: u64,
max_gas_per_predicate: u64,
) -> Self {
let params = PredicateParametersRust::default()
.with_max_predicate_length(max_predicate_length)
.with_max_predicate_data_length(max_predicate_data_length)
.with_max_message_data_length(max_message_data_length)
.with_max_gas_per_predicate(max_gas_per_predicate);
let params: PredicateParametersRust = PredicateParametersV1 {
max_predicate_length,
max_predicate_data_length,
max_message_data_length,
max_gas_per_predicate,
}
.into();

PredicateParameters(params.into())
}
Expand Down
6 changes: 1 addition & 5 deletions fuel-tx/src/transaction/types/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
field::WitnessLimit,
policies::Policies,
transaction::{
consensus_parameters::TxParameters,
field::{
Inputs,
Outputs,
Expand Down Expand Up @@ -82,10 +81,7 @@ impl Default for Script {
let script = fuel_asm::op::ret(0x10).to_bytes().to_vec();

Self {
// We want to use any values much less than `max_gas_per_tx`
// to avoid the `TransactionMaxGasExceeded` error. For example,
// `max_gas_per_tx / 4`.
script_gas_limit: TxParameters::DEFAULT.max_gas_per_tx() / 4,
script_gas_limit: Default::default(),
script,
script_data: Default::default(),
policies: Policies::new()
Expand Down
2 changes: 1 addition & 1 deletion fuel-tx/test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
fuel-crypto = { path = "../../fuel-crypto", default-features = false, features = ["random"] }
fuel-tx = { path = "../../fuel-tx", default-features = false, features = ["builder", "random"] }
fuel-tx = { path = "../../fuel-tx", default-features = false, features = ["test-helpers", "random"] }
fuel-types = { path = "../../fuel-types", default-features = false, features = ["random"] }
rand = { version = "0.8", default-features = false }
strum = "0.24"
Expand Down
Loading

0 comments on commit c876027

Please sign in to comment.