diff --git a/CHANGELOG.md b/CHANGELOG.md index b0cfa4f8f3..9eb3e29a1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed #### Breaking +- [#821](https://github.com/FuelLabs/fuel-vm/pull/821): Added `block_transaction_size_limit` to `ConsensusParameters`. It adds a new `ConensusParametersV2` as a variant of the `ConsensusParameters`. - [#670](https://github.com/FuelLabs/fuel-vm/pull/670): The `predicate` field of `fuel_tx::input::Coin` is now a wrapper struct `PredicateCode`. ## [Version 0.56.0] diff --git a/fuel-tx/src/tests/valid_cases.rs b/fuel-tx/src/tests/valid_cases.rs index cf3e1e79ce..4c4fd3d5de 100644 --- a/fuel-tx/src/tests/valid_cases.rs +++ b/fuel-tx/src/tests/valid_cases.rs @@ -40,6 +40,7 @@ pub fn test_params() -> ConsensusParameters { Default::default(), Default::default(), Default::default(), + Default::default(), ) } diff --git a/fuel-tx/src/transaction/consensus_parameters.rs b/fuel-tx/src/transaction/consensus_parameters.rs index 391d0ee575..e2e410f81b 100644 --- a/fuel-tx/src/transaction/consensus_parameters.rs +++ b/fuel-tx/src/transaction/consensus_parameters.rs @@ -22,11 +22,15 @@ const MAX_GAS: u64 = 100_000_000; #[cfg(feature = "test-helpers")] const MAX_SIZE: u64 = 110 * 1024; +#[derive(Debug)] +pub struct SettingBlockTransactionSizeLimitNotSupported; + /// A versioned set of consensus parameters. #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum ConsensusParameters { /// Version 1 of the consensus parameters V1(ConsensusParametersV1), + V2(ConsensusParametersV2), } #[cfg(feature = "test-helpers")] @@ -40,13 +44,13 @@ impl ConsensusParameters { #[cfg(feature = "test-helpers")] /// Constructor for the `ConsensusParameters` with Standard values. pub fn standard() -> Self { - ConsensusParametersV1::standard().into() + ConsensusParametersV2::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() + ConsensusParametersV2::standard_with_id(chain_id).into() } /// Constructor for the `ConsensusParameters` @@ -60,9 +64,10 @@ impl ConsensusParameters { gas_costs: GasCosts, base_asset_id: AssetId, block_gas_limit: u64, + block_transaction_size_limit: u64, privileged_address: Address, ) -> Self { - Self::V1(ConsensusParametersV1 { + Self::V2(ConsensusParametersV2 { tx_params, predicate_params, script_params, @@ -72,6 +77,7 @@ impl ConsensusParameters { gas_costs, base_asset_id, block_gas_limit, + block_transaction_size_limit, privileged_address, }) } @@ -80,6 +86,7 @@ impl ConsensusParameters { pub const fn tx_params(&self) -> &TxParameters { match self { Self::V1(params) => ¶ms.tx_params, + Self::V2(params) => ¶ms.tx_params, } } @@ -87,6 +94,7 @@ impl ConsensusParameters { pub const fn predicate_params(&self) -> &PredicateParameters { match self { Self::V1(params) => ¶ms.predicate_params, + Self::V2(params) => ¶ms.predicate_params, } } @@ -94,6 +102,7 @@ impl ConsensusParameters { pub const fn script_params(&self) -> &ScriptParameters { match self { Self::V1(params) => ¶ms.script_params, + Self::V2(params) => ¶ms.script_params, } } @@ -101,6 +110,7 @@ impl ConsensusParameters { pub const fn contract_params(&self) -> &ContractParameters { match self { Self::V1(params) => ¶ms.contract_params, + Self::V2(params) => ¶ms.contract_params, } } @@ -108,6 +118,7 @@ impl ConsensusParameters { pub const fn fee_params(&self) -> &FeeParameters { match self { Self::V1(params) => ¶ms.fee_params, + Self::V2(params) => ¶ms.fee_params, } } @@ -115,6 +126,7 @@ impl ConsensusParameters { pub const fn chain_id(&self) -> ChainId { match self { Self::V1(params) => params.chain_id, + Self::V2(params) => params.chain_id, } } @@ -122,6 +134,7 @@ impl ConsensusParameters { pub const fn gas_costs(&self) -> &GasCosts { match self { Self::V1(params) => ¶ms.gas_costs, + Self::V2(params) => ¶ms.gas_costs, } } @@ -129,6 +142,7 @@ impl ConsensusParameters { pub const fn base_asset_id(&self) -> &AssetId { match self { Self::V1(params) => ¶ms.base_asset_id, + Self::V2(params) => ¶ms.base_asset_id, } } @@ -136,6 +150,19 @@ impl ConsensusParameters { pub const fn block_gas_limit(&self) -> u64 { match self { Self::V1(params) => params.block_gas_limit, + Self::V2(params) => params.block_gas_limit, + } + } + + /// Get the block transaction size limit + pub fn block_transaction_size_limit(&self) -> u64 { + match self { + Self::V1(_) => { + // In V1 there was no limit on the transaction size. For the sake of + // backwards compatibility we allow for a largest limit possible. + u64::MAX + } + Self::V2(params) => params.block_transaction_size_limit, } } @@ -143,6 +170,7 @@ impl ConsensusParameters { pub const fn privileged_address(&self) -> &Address { match self { Self::V1(params) => ¶ms.privileged_address, + Self::V2(params) => ¶ms.privileged_address, } } } @@ -152,6 +180,7 @@ impl ConsensusParameters { pub fn set_tx_params(&mut self, tx_params: TxParameters) { match self { Self::V1(params) => params.tx_params = tx_params, + Self::V2(params) => params.tx_params = tx_params, } } @@ -159,6 +188,7 @@ impl ConsensusParameters { pub fn set_predicate_params(&mut self, predicate_params: PredicateParameters) { match self { Self::V1(params) => params.predicate_params = predicate_params, + Self::V2(params) => params.predicate_params = predicate_params, } } @@ -166,6 +196,7 @@ impl ConsensusParameters { pub fn set_script_params(&mut self, script_params: ScriptParameters) { match self { Self::V1(params) => params.script_params = script_params, + Self::V2(params) => params.script_params = script_params, } } @@ -173,6 +204,7 @@ impl ConsensusParameters { pub fn set_contract_params(&mut self, contract_params: ContractParameters) { match self { Self::V1(params) => params.contract_params = contract_params, + Self::V2(params) => params.contract_params = contract_params, } } @@ -180,6 +212,7 @@ impl ConsensusParameters { pub fn set_fee_params(&mut self, fee_params: FeeParameters) { match self { Self::V1(params) => params.fee_params = fee_params, + Self::V2(params) => params.fee_params = fee_params, } } @@ -187,6 +220,7 @@ impl ConsensusParameters { pub fn set_chain_id(&mut self, chain_id: ChainId) { match self { Self::V1(params) => params.chain_id = chain_id, + Self::V2(params) => params.chain_id = chain_id, } } @@ -194,6 +228,7 @@ impl ConsensusParameters { pub fn set_gas_costs(&mut self, gas_costs: GasCosts) { match self { Self::V1(params) => params.gas_costs = gas_costs, + Self::V2(params) => params.gas_costs = gas_costs, } } @@ -201,6 +236,7 @@ impl ConsensusParameters { pub fn set_base_asset_id(&mut self, base_asset_id: AssetId) { match self { Self::V1(params) => params.base_asset_id = base_asset_id, + Self::V2(params) => params.base_asset_id = base_asset_id, } } @@ -208,6 +244,21 @@ impl ConsensusParameters { pub fn set_block_gas_limit(&mut self, block_gas_limit: u64) { match self { Self::V1(params) => params.block_gas_limit = block_gas_limit, + Self::V2(params) => params.block_gas_limit = block_gas_limit, + } + } + + /// Set the block transaction size limit. + pub fn set_block_transaction_size_limit( + &mut self, + block_transaction_size_limit: u64, + ) -> Result<(), SettingBlockTransactionSizeLimitNotSupported> { + match self { + Self::V1(_) => Err(SettingBlockTransactionSizeLimitNotSupported), + Self::V2(params) => { + params.block_transaction_size_limit = block_transaction_size_limit; + Ok(()) + } } } @@ -215,6 +266,7 @@ impl ConsensusParameters { pub fn set_privileged_address(&mut self, privileged_address: Address) { match self { Self::V1(params) => params.privileged_address = privileged_address, + Self::V2(params) => params.privileged_address = privileged_address, } } } @@ -273,6 +325,66 @@ impl From for ConsensusParameters { } } +/// A collection of parameters for convenience +/// The difference with [`ConsensusParametersV1`]: +/// - `block_transaction_size_limit` has been added. +#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub struct ConsensusParametersV2 { + pub tx_params: TxParameters, + pub predicate_params: PredicateParameters, + pub script_params: ScriptParameters, + pub contract_params: ContractParameters, + pub fee_params: FeeParameters, + pub chain_id: ChainId, + pub gas_costs: GasCosts, + pub base_asset_id: AssetId, + pub block_gas_limit: u64, + pub block_transaction_size_limit: u64, + /// The privileged address(user or predicate) that can perform permissioned + /// operations(like upgrading the network). + pub privileged_address: Address, +} + +#[cfg(feature = "test-helpers")] +impl ConsensusParametersV2 { + const DEFAULT_BLOCK_TRANSACTION_SIZE_LIMIT: u64 = 126 * 1024; + + /// Constructor for the `ConsensusParameters` with Standard values. + pub fn standard() -> Self { + Self::standard_with_id(ChainId::default()) + } + + /// Constructor for the `ConsensusParameters` with Standard values around `ChainId`. + pub fn standard_with_id(chain_id: ChainId) -> Self { + Self { + tx_params: TxParameters::DEFAULT, + predicate_params: PredicateParameters::DEFAULT, + script_params: ScriptParameters::DEFAULT, + contract_params: ContractParameters::DEFAULT, + fee_params: FeeParameters::DEFAULT, + chain_id, + gas_costs: GasCosts::default(), + base_asset_id: Default::default(), + block_gas_limit: TxParameters::DEFAULT.max_gas_per_tx(), + block_transaction_size_limit: Self::DEFAULT_BLOCK_TRANSACTION_SIZE_LIMIT, + privileged_address: Default::default(), + } + } +} + +#[cfg(feature = "test-helpers")] +impl Default for ConsensusParametersV2 { + fn default() -> Self { + Self::standard() + } +} + +impl From for ConsensusParameters { + fn from(params: ConsensusParametersV2) -> Self { + Self::V2(params) + } +} + /// The versioned fee parameters. #[derive( Copy, Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, @@ -916,3 +1028,39 @@ pub mod typescript { } } } + +#[cfg(test)] +mod tests { + use crate::consensus_parameters::{ + ConsensusParametersV2, + SettingBlockTransactionSizeLimitNotSupported, + }; + + use super::{ + ConsensusParameters, + ConsensusParametersV1, + }; + + #[test] + fn error_when_setting_block_size_limit_in_consensus_parameters_v1() { + let mut consensus_params: ConsensusParameters = + ConsensusParametersV1::default().into(); + + let result = consensus_params.set_block_transaction_size_limit(0); + + assert!(matches!( + result, + Err(SettingBlockTransactionSizeLimitNotSupported) + )) + } + + #[test] + fn ok_when_setting_block_size_limit_in_consensus_parameters_v2() { + let mut consensus_params: ConsensusParameters = + ConsensusParametersV2::default().into(); + + let result = consensus_params.set_block_transaction_size_limit(0); + + assert!(matches!(result, Ok(()))) + } +} diff --git a/fuel-vm/src/checked_transaction.rs b/fuel-vm/src/checked_transaction.rs index 36c33dc0c1..a77975b746 100644 --- a/fuel-vm/src/checked_transaction.rs +++ b/fuel-vm/src/checked_transaction.rs @@ -920,6 +920,7 @@ mod tests { Default::default(), Default::default(), Default::default(), + Default::default(), ) } diff --git a/fuel-vm/src/tests/outputs.rs b/fuel-vm/src/tests/outputs.rs index e1c40aa372..498bc500e5 100644 --- a/fuel-vm/src/tests/outputs.rs +++ b/fuel-vm/src/tests/outputs.rs @@ -166,6 +166,7 @@ fn correct_change_is_provided_for_coin_outputs_create() { context.get_gas_costs().to_owned(), *context.get_base_asset_id(), context.get_block_gas_limit(), + context.get_block_transaction_size_limit(), *context.get_privileged_address(), ); let create = create diff --git a/fuel-vm/src/util.rs b/fuel-vm/src/util.rs index 041756ddcc..8efe843486 100644 --- a/fuel-vm/src/util.rs +++ b/fuel-vm/src/util.rs @@ -396,6 +396,10 @@ pub mod test_helpers { self.consensus_params.block_gas_limit() } + pub fn get_block_transaction_size_limit(&self) -> u64 { + self.consensus_params.block_transaction_size_limit() + } + pub fn get_privileged_address(&self) -> &Address { self.consensus_params.privileged_address() }