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

Decompose consensus params #514

merged 27 commits into from
Jul 25, 2023

Conversation

MitchTurner
Copy link
Member

@MitchTurner MitchTurner commented Jul 19, 2023

FuelLabs/fuel-core#1243

The idea behind this PR was that ConsensusParameters was being passed to any function that needed a value in ConsensusParameters. We wanted the functions to define what they needed instead of giving them the whole kitchen sink.

The work included:

  1. Removing ConsensusParams from all function signatures
  2. Adding the required fields back to the function signatures
  3. Grouping parameters that are used together into sub structs: e.g. TxParameters, FeeParameters
  4. Fixing tests
  5. Fixing tests
  6. Fixing tests
  7. Combining fields of large function signatures

@MitchTurner
Copy link
Member Author

In addition to tests not passing, There are a number of function signatures I'd like to improve before merging this. I'll try to document what those changes are here so we can track them.

@@ -237,25 +242,29 @@ impl FormatValidityChecks for Create {
fn check_without_signatures(
&self,
block_height: BlockHeight,
parameters: &ConsensusParameters,
tx_params: &TxParameters,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we need a combination of the parameters, it is better to pass only ConsensusParameters. It is not a problem if some fields are not used. The API should be simple for the user and transparent regarding requirements(like you don't need to pass ConsensusParameters if you only need a ChainId).

Copy link
Member Author

Choose a reason for hiding this comment

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

For sure. I've combined them back together in some other places. Clippy just didn't catch this because it's at the limit (7). I can do the same here. Good catch.

@MitchTurner MitchTurner changed the title Draft: Decompose consensus params Decompose consensus params Jul 20, 2023
Comment on lines 122 to 127
tx_params: TxParameters,
predicate_params: PredicateParameters,
script_params: ScriptParameters,
contract_params: ContractParameters,
fee_params: FeeParameters,
chain_id: ChainId,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it is still better to store all params like ConsensusParameters inside. In this case get_params will be useful too(it is commented now). In this case all new methods are still useable but you support old once too=)

@@ -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::new(0);
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.

let chain_id = ChainId::new(0);

tx.sign_inputs(&secret, &chain_id);
keys.iter().for_each(|sk| tx.sign_inputs(sk, &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.

The same here, let's use one approach everywhere either Default::default() or ChainId::default() or ChainId::new(0) or ChainId::DEFAULT=)

It applies to all 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.

@@ -648,7 +772,11 @@ fn transaction_with_duplicate_contract_inputs_is_invalid() {
.add_output(o)
.add_output(p)
.finalize()
.check_without_signatures(Default::default(), &Default::default())
.check_without_signatures(
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems we don't need to modify it=) You don't use chain_id
image

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a trait method. It gets used in the other impls of this method.

But it sounds like you want chain_id as part of ConsensusParams so I think it will get removed in that case anyway.


/// A collection of parameters for convenience
#[derive(Debug, Clone, Copy)]
pub struct ConsensusParams<'a> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ChainId should be part of the ConsensusParams, because it is the same for the whole network and without it it is impossible to continue the network

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is true that the ConsensusParams is a combination of the other parameters and references them. But at the same time, this type can be the place that actually aggregates all of them and holds them during the lifecycle of the fuel-core. If we want to pass somewhere a reference to all params, we can pass &ConsensusParams. The ConsensusParams itself doesn't need to work with references to the Copy types.

It makes the API harder to use and creates situations like in several places of your PR(Where you create each parameter -> you create a ConsensusParams -> you pass it to the corresponding place).

I think we need to work with values instead of references, if you don't have any strong reason why we want to use references=)

Copy link
Member Author

Choose a reason for hiding this comment

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

It does make it harder to work with.

I had reasons I preferred references, but I'm not really convinced by them anymore, so I'll just get rid of the refs. Especially since all of them are Copy.

params: &ConsensusParameters,
gas_costs: &GasCosts,
) -> Result<Self, CheckError>;
fn check_predicates(self, params: CheckPredicateParams) -> Result<Self, CheckError>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer &CheckPredicateParams more than CheckPredicateParams because we usually store this configuration somewhere. Plus, downstream code decides whether it needs to clone or not.

@@ -109,7 +112,14 @@ pub struct Interpreter<S, Tx = ()> {
balances: RuntimeBalances,
gas_costs: GasCosts,
profiler: Profiler,
params: ConsensusParameters,
fee_params: FeeParameters,
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 want to use InterpreterParams here=)

@@ -181,6 +191,12 @@ impl<S, Tx> Interpreter<S, Tx> {
pub const fn profiler(&self) -> &Profiler {
&self.profiler
}

/// Modify the FeeParameters
pub fn with_fee_params(mut self, fee_params: FeeParameters) -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be specified during creation of the Interpreter

Comment on lines 57 to 63
let tx_params = TxParameters::default();
let predicate_params = PredicateParameters::default();
let script_params = ScriptParameters::default();
let contract_params = ContractParameters::default();
let fee_params = FeeParameters::default();
let chain_id = ChainId::default();
let gas_costs = GasCosts::default();
Copy link
Collaborator

Choose a reason for hiding this comment

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

It adds a lot of overhead=) if ConsensusParams works with values instead, you don't need to do it

Copy link
Member Author

Choose a reason for hiding this comment

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

I missed this one. Adding now.

self.params,
self.gas_costs.clone(),
);
let interpreter_params = InterpreterParams {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is better if Self contains InterpreterParams instead=)

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the ConsesensusParams to Self and used the into.

@xgreenx xgreenx requested a review from a team July 24, 2023 17:17
@MitchTurner MitchTurner requested a review from xgreenx July 24, 2023 22:50
Comment on lines 22 to 23
// TODO: This should be pub(crate) for the `fuel_tx` tests, but for some reason the
// tests can't see the fields with `pub(crate)`
Copy link
Member

Choose a reason for hiding this comment

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

Integration tests cannot see pub(crate) items, as they are outside the crate. Typically you'd do a public re-export behind test-helpers feature flag, but I don't see much point in not having this as a part of the public interface anyways.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. That's a good point. There are probably places outside of fuel-tx that could use this as well. I was referring to something in fuel-tx/src/tests, not in the integ tests.

I usually prefer getters/setters over accessing fields directly, but I'm not going to worry about it right now.

Copy link
Member

@Dentosal Dentosal left a comment

Choose a reason for hiding this comment

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

One todo and some commented out code should be cleaned up, but other than that LGTM

Comment on lines 130 to 134
// /// Consensus parameters
// pub const fn params(&self) -> &ConsensusParameters {
// self.interpreter.params()
// }
//
Copy link
Member

Choose a reason for hiding this comment

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

Commented-out code that likely should be removed

@MitchTurner MitchTurner added this pull request to the merge queue Jul 25, 2023
Merged via the queue into master with commit e278038 Jul 25, 2023
30 checks passed
@MitchTurner MitchTurner deleted the decompose-consensus-params branch July 25, 2023 17:09
@xgreenx xgreenx mentioned this pull request Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants