Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

refactor: define trait HashOutput for some Hash associate type #14220

Merged

Conversation

yjhmelody
Copy link
Contributor

I think some associate type is too verbose to be used. e.g this one type Hash: ....

So it will reduce so much code for downstream to use it if define a new trait for it.

primitives/runtime/src/traits.rs Outdated Show resolved Hide resolved
@bkchr bkchr requested a review from cheme May 25, 2023 13:11
@bkchr bkchr added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). labels May 25, 2023
@yjhmelody
Copy link
Contributor Author

yjhmelody commented May 25, 2023

@bkchr Hi, maybe it's still a draft. Maybe should remove all Block::Hash: Ords and add FromStr and so is Hash: FromStr ?

I also think should we Define a super BlockNumber trait. It could reduce so many type constraints code.

@yjhmelody yjhmelody requested a review from andresilva as a code owner May 25, 2023 15:11
@yjhmelody yjhmelody requested a review from a team May 26, 2023 03:19
@yjhmelody yjhmelody force-pushed the refactor-some-traits branch from c15364c to b370d9d Compare May 26, 2023 03:31
Copy link
Contributor

@cheme cheme left a comment

Choose a reason for hiding this comment

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

I also think should we Define a super BlockNumber trait. It could reduce so many type constraints code.

👍 For my part I generally like grouping trait constraint, it can result in a few unneeded constraint here or there (having two variant: hash at trie lower level and one at substrate level is already enough probably). And things get more readable.


impl InspectCmd {
/// Run the inspect command, passing the inspector.
pub fn run<B, RA, D>(&self, config: Configuration) -> Result<()>
where
B: Block,
B::Hash: FromStr,
Copy link
Contributor

Choose a reason for hiding this comment

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

Was it the case that this trait bound is only required in situations where std is enabled?

Copy link
Contributor Author

@yjhmelody yjhmelody May 27, 2023

Choose a reason for hiding this comment

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

Yeah, there is MaybeFromStr trait which is parent of HashOutput.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you answered a different question: I was asking if it was required in situations where std is enabled, i.e. before you introduced MaybeFromStr, not whether it is the case right now.

Copy link
Contributor Author

@yjhmelody yjhmelody Jun 1, 2023

Choose a reason for hiding this comment

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

You know that the runtime does not encourage the use of the String related type. So sp_std have not exported str mod.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I personally can't understand why this is done, it may be to prevent problems such as code bloat or utf-8 encoding problems

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think my intention is getting across -- you changed the trait bounds on this type parameter, and hence the API as well. Previously, FromStr is required to be implemented on the Hasher associated type no matter what, but now it's been changed so that it will only be required when you have std enabled.

@yjhmelody
Copy link
Contributor Author

@KiChjang Hi any other problem?

primitives/runtime/src/generic/header.rs Outdated Show resolved Hide resolved
primitives/runtime/src/generic/header.rs Outdated Show resolved Hide resolved
primitives/runtime/src/generic/header.rs Outdated Show resolved Hide resolved
@@ -715,6 +703,47 @@ pub trait Hash:
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, state_version: StateVersion) -> Self::Output;
}

/// Super trait with all the attributes for a hashing output.
pub trait HashOutput:
Copy link
Member

@davxy davxy May 30, 2023

Choose a reason for hiding this comment

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

While just a nitpick about the name that has been used. To me would be more correct to generally stick with one naming convention.

That is, seems there is some confusion in the naming convention adopted for hashers, hashes, and hash outputs.

  • sp-core re-exports hash_db::Hasher as Hasher.
  • We have this Hash trait here that technically is defining a hasher.
  • We have HashOutput trait here that instrad is the Hash.

Can't we rename, just to be coherent these two traits?
Hash -> Hesher
HashOutput -> Hash

This would follow the convention adopted by the standard lib as well

Copy link
Member

Choose a reason for hiding this comment

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

Personally I would also favor this, but this will for sure result in some very big diff :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you. Maybe you could rename them after this PR merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, there still is Hashing which should be Hasher?

@yjhmelody yjhmelody requested a review from a team June 1, 2023 07:05
@yjhmelody
Copy link
Contributor Author

Please give me an approve :), ty.

@yjhmelody
Copy link
Contributor Author

@bkchr Hi, could you merge it?

@sam0x17
Copy link
Contributor

sam0x17 commented Jun 8, 2023

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 85b0807 into paritytech:master Jun 8, 2023
@yjhmelody yjhmelody deleted the refactor-some-traits branch June 8, 2023 15:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants