Skip to content

Commit

Permalink
Decompose consensus params (#514)
Browse files Browse the repository at this point in the history
* Break down ConsensusParameters

* Remove ConsensusParameters from everywhere

* Use default for individual fields not for types

* Get tests compiling, but not passing

* Fix one test

* Fix bug in transactor default

* Fix bug in test helper

* Fix fee test bug

* Include chain_id in tx builder to get chain_id to match

* Get all tests passing

* Partially appease Clippy-sama

* Group params for into_checked

* Create param for Interpreter init

* Simplify MemoryClient signature

* Appease Clippy-sama

* Move ConsensusParams back to original file

* Use ConsensusParams in more places

* Bump version, only use default `ChainId`

* Make ConsensusParams hold values instead of refs

* Consolidate ChainId and GasCosts into ConsensusParams

* Collect Interpreter parameters inside struct

* Fix tests

* Cleanup, appease Clippy-sama

* Simplify test setups

* Remove comment and TODO

* Rename `ConsensusParams` back to `ConsensusParameters`

---------

Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
  • Loading branch information
MitchTurner and xgreenx authored Jul 25, 2023
1 parent 20f1e9b commit e278038
Show file tree
Hide file tree
Showing 66 changed files with 1,876 additions and 1,104 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ Cargo.lock
**/*.rs.bk

.vscode/

.idea

.history
88 changes: 79 additions & 9 deletions fuel-tx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ use crate::{
},
Cacheable,
ConsensusParameters,
ContractParameters,
FeeParameters,
GasCosts,
Input,
Mint,
Output,
PredicateParameters,
ScriptParameters,
StorageSlot,
Transaction,
TxParameters,
TxPointer,
Witness,
};
Expand All @@ -28,6 +34,7 @@ use crate::Signable;
use fuel_crypto::SecretKey;
use fuel_types::{
BlockHeight,
ChainId,
Nonce,
Salt,
Word,
Expand Down Expand Up @@ -114,7 +121,7 @@ pub struct TransactionBuilder<Tx> {

should_prepare_script: bool,
should_prepare_predicate: bool,
parameters: ConsensusParameters,
params: ConsensusParameters,

// We take the key by reference so this lib won't have the responsibility to properly
// zeroize the keys
Expand Down Expand Up @@ -194,21 +201,79 @@ impl<Tx> TransactionBuilder<Tx> {
let should_prepare_predicate = false;
let sign_keys = HashMap::new();

// TODO: What is a default chain id?
let chain_id = ChainId::default();

Self {
tx,
should_prepare_script,
should_prepare_predicate,
params: ConsensusParameters::standard(chain_id),
sign_keys,
parameters: ConsensusParameters::DEFAULT,
}
}

pub fn get_params(&self) -> &ConsensusParameters {
&self.parameters
&self.params
}

pub fn get_tx_params(&self) -> &TxParameters {
self.params.tx_params()
}

pub fn get_predicate_params(&self) -> &PredicateParameters {
self.params.predicate_params()
}

pub fn get_script_params(&self) -> &ScriptParameters {
self.params.script_params()
}

pub fn get_contract_params(&self) -> &ContractParameters {
self.params.contract_params()
}

pub fn with_params(&mut self, parameters: ConsensusParameters) -> &mut Self {
self.parameters = parameters;
pub fn get_fee_params(&self) -> &FeeParameters {
self.params.fee_params()
}

pub fn get_chain_id(&self) -> ChainId {
self.params.chain_id()
}

pub fn with_tx_params(&mut self, tx_params: TxParameters) -> &mut Self {
self.params.tx_params = tx_params;
self
}

pub fn with_predicate_params(
&mut self,
predicate_params: PredicateParameters,
) -> &mut Self {
self.params.predicate_params = predicate_params;
self
}

pub fn with_script_params(&mut self, script_params: ScriptParameters) -> &mut Self {
self.params.script_params = script_params;
self
}

pub fn with_contract_params(
&mut self,
contract_params: ContractParameters,
) -> &mut Self {
self.params.contract_params = contract_params;
self
}

pub fn with_fee_params(&mut self, fee_params: FeeParameters) -> &mut Self {
self.params.fee_params = fee_params;
self
}

pub fn with_gas_costs(&mut self, gas_costs: GasCosts) -> &mut Self {
self.params.gas_costs = gas_costs;
self
}
}
Expand Down Expand Up @@ -240,6 +305,11 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
self
}

pub fn with_chain_id(&mut self, chain_id: ChainId) -> &mut Self {
self.params.chain_id = chain_id;
self
}

pub fn maturity(&mut self, maturity: BlockHeight) -> &mut Self {
self.tx.set_maturity(maturity);

Expand Down Expand Up @@ -373,9 +443,9 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {

self.sign_keys
.iter()
.for_each(|(k, _)| tx.sign_inputs(k, &self.parameters.chain_id));
.for_each(|(k, _)| tx.sign_inputs(k, &self.get_chain_id()));

tx.precompute(&self.parameters.chain_id)
tx.precompute(&self.get_chain_id())
.expect("Should be able to calculate cache");

tx
Expand All @@ -387,7 +457,7 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {

let mut tx = core::mem::take(&mut self.tx);

tx.precompute(&self.parameters.chain_id)
tx.precompute(&self.get_chain_id())
.expect("Should be able to calculate cache");

tx
Expand All @@ -411,7 +481,7 @@ pub trait Finalizable<Tx> {
impl Finalizable<Mint> for TransactionBuilder<Mint> {
fn finalize(&mut self) -> Mint {
let mut tx = core::mem::take(&mut self.tx);
tx.precompute(&self.parameters.chain_id)
tx.precompute(&self.get_chain_id())
.expect("Should be able to calculate cache");
tx
}
Expand Down
9 changes: 9 additions & 0 deletions fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,27 @@ pub use transaction::{
Chargeable,
CheckError,
ConsensusParameters,
ContractParameters,
Create,
DependentCost,
Executable,
FeeParameters,
FormatValidityChecks,
GasCosts,
GasCostsValues,
GasUnit,
Mint,
Output,
OutputRepr,
PredicateParameters,
Script,
ScriptParameters,
StorageSlot,
Transaction,
TransactionFee,
TransactionRepr,
TxId,
TxParameters,
UtxoId,
Witness,
};
Expand Down
15 changes: 8 additions & 7 deletions fuel-tx/src/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rand::{
SeedableRng,
};

use crate::TxParameters;
use fuel_tx::field::{
Inputs,
Script,
Expand Down Expand Up @@ -369,7 +370,7 @@ fn transaction() {
assert_encoding_correct(&[
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand All @@ -380,7 +381,7 @@ fn transaction() {
),
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand All @@ -391,7 +392,7 @@ fn transaction() {
),
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand All @@ -402,7 +403,7 @@ fn transaction() {
),
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand All @@ -413,7 +414,7 @@ fn transaction() {
),
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand All @@ -424,7 +425,7 @@ fn transaction() {
),
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
TxParameters::DEFAULT.max_gas_per_tx,
rng.gen(),
rng.gen(),
rng.gen(),
Expand Down Expand Up @@ -657,7 +658,7 @@ fn script_input_coin_data_offset() {
);

let mut tx_p = tx.clone();
tx_p.precompute(&ConsensusParameters::DEFAULT.chain_id)
tx_p.precompute(&Default::default())
.expect("Should be able to calculate cache");

buffer.iter_mut().for_each(|b| *b = 0x00);
Expand Down
12 changes: 8 additions & 4 deletions fuel-tx/src/tests/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use fuel_tx::{
*,
};
use fuel_tx_test_helpers::TransactionFactory;
use fuel_types::bytes::{
Deserializable,
SerializableVec,
use fuel_types::{
bytes::{
Deserializable,
SerializableVec,
},
ChainId,
};
use rand::{
rngs::StdRng,
Expand Down Expand Up @@ -466,7 +469,8 @@ fn iow_offset() {
let bytes = tx.to_bytes();

let mut tx_p = tx.clone();
tx_p.precompute(&ConsensusParameters::DEFAULT.chain_id)
let chain_id = ChainId::default();
tx_p.precompute(&chain_id)
.expect("Should be able to calculate cache");

tx.inputs().iter().enumerate().for_each(|(x, i)| {
Expand Down
39 changes: 35 additions & 4 deletions fuel-tx/src/tests/valid_cases.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
use fuel_tx::ConsensusParameters;
use fuel_tx::{
ConsensusParameters,
ContractParameters,
FeeParameters,
PredicateParameters,
ScriptParameters,
TxParameters,
};

use fuel_types::ChainId;

// override default settings to reduce testing overhead
pub const PARAMS: ConsensusParameters = ConsensusParameters::DEFAULT
.with_max_storage_slots(1024)
pub const CONTRACT_PARAMS: ContractParameters =
ContractParameters::DEFAULT.with_max_storage_slots(1024);

pub const SCRIPT_PARAMS: ScriptParameters = ScriptParameters::DEFAULT
.with_max_script_length(1024)
.with_max_script_data_length(1024)
.with_max_script_data_length(1024);

pub const TX_PARAMS: TxParameters = TxParameters::DEFAULT
.with_max_inputs(16)
.with_max_outputs(16)
.with_max_witnesses(16);

pub const PREDICATE_PARAMS: PredicateParameters = PredicateParameters::DEFAULT;

pub const FEE_PARAMS: FeeParameters = FeeParameters::DEFAULT;

pub const CHAIN_ID: ChainId = ChainId::new(0);

pub fn test_params() -> ConsensusParameters {
ConsensusParameters::new(
TX_PARAMS,
PREDICATE_PARAMS,
SCRIPT_PARAMS,
CONTRACT_PARAMS,
FEE_PARAMS,
CHAIN_ID,
Default::default(),
)
}

mod input;
mod output;
mod transaction;
Loading

0 comments on commit e278038

Please sign in to comment.