Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompose consensus params #514

Merged
merged 27 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
905441d
Break down ConsensusParameters
MitchTurner Jul 17, 2023
0f25bc8
Remove ConsensusParameters from everywhere
MitchTurner Jul 18, 2023
a1b10c2
Use default for individual fields not for types
MitchTurner Jul 18, 2023
e42678a
Get tests compiling, but not passing
MitchTurner Jul 19, 2023
6bdd9a0
Fix one test
MitchTurner Jul 19, 2023
0554711
Fix bug in transactor default
MitchTurner Jul 19, 2023
261b4cc
Fix bug in test helper
MitchTurner Jul 19, 2023
3f30841
Fix fee test bug
MitchTurner Jul 20, 2023
8171431
Include chain_id in tx builder to get chain_id to match
MitchTurner Jul 20, 2023
c17fd88
Get all tests passing
MitchTurner Jul 20, 2023
d412263
Partially appease Clippy-sama
MitchTurner Jul 20, 2023
6de4203
Group params for into_checked
MitchTurner Jul 20, 2023
35a6790
Create param for Interpreter init
MitchTurner Jul 20, 2023
d134584
Simplify MemoryClient signature
MitchTurner Jul 20, 2023
a655e12
Appease Clippy-sama
MitchTurner Jul 20, 2023
52bcef5
Move ConsensusParams back to original file
MitchTurner Jul 20, 2023
02b7572
Use ConsensusParams in more places
MitchTurner Jul 20, 2023
5cf29da
Bump version, only use default `ChainId`
MitchTurner Jul 24, 2023
2441d99
Make ConsensusParams hold values instead of refs
MitchTurner Jul 24, 2023
8e90661
Consolidate ChainId and GasCosts into ConsensusParams
MitchTurner Jul 24, 2023
1b44647
Collect Interpreter parameters inside struct
MitchTurner Jul 24, 2023
9cf95c2
Fix tests
MitchTurner Jul 24, 2023
cbb56e4
Cleanup, appease Clippy-sama
MitchTurner Jul 24, 2023
6466901
Simplify test setups
MitchTurner Jul 24, 2023
415ab30
Merge branch 'master' into decompose-consensus-params
xgreenx Jul 25, 2023
5021d87
Remove comment and TODO
MitchTurner Jul 25, 2023
1d7350b
Rename `ConsensusParams` back to `ConsensusParameters`
MitchTurner Jul 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can use Default::default like you did in other places=)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

.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
Loading