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

remove chain specific macros and one metadata file for all chains #387

Merged
merged 20 commits into from
Oct 5, 2022

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Sep 29, 2022

Builds on top of #357

Requires polkadot v0.9.30 because of Weight v2 proof size

@niklasad1 niklasad1 changed the title WIP: kill macros + one metadata file remove chain specific macros and one metadata file for all chains Sep 30, 2022
@niklasad1 niklasad1 marked this pull request as ready for review September 30, 2022 15:30
Comment on lines 138 to 152
fn to_scale_value<T: scale_info::TypeInfo + 'static + Encode>(val: T) -> Result<Value, Error> {
let (ty_id, types) = make_type::<T>();

let bytes = val.encode();

decode_as_type(&mut bytes.as_ref(), ty_id, &types)
.map(|v| v.remove_context())
.map_err(|e| {
Error::DynamicTransaction(format!(
"Failed to decode {}: {:?}",
std::any::type_name::<T>(),
e
))
})
}
Copy link
Contributor

@jsdw jsdw Sep 30, 2022

Choose a reason for hiding this comment

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

Ooh, I see, this is a clever way to take some type implementing TypeInfo + Encode into a scale_value::Value! It's a shame to have to encode it to bytes and then decode from the bytes, but offhand I can't think of a better/more reliable way (sortof just a limitation of those traits and their expectations).

You can go from a T: Serialize straight into a scale_value::Value, which would be more efficient (no intermediate step), but it also goes through the serde data model. Might be worth trying if you wanted faster and fewer lines of code, but the downside is that the serde and scale impls don't necessarily need to agree with eachother (eg H256 serializes to a string, which would turn into a scale_value primitive String rather than a sequence of bytes, and the string then wouldn't encode to bytes as would be needed to encode it properly).

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 but these types doesn't implement serde maybe I can convince @kianenigma to add deserialize/serialize on these types :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense! Aside from using Serde, or some other similar machinery, I think what you've done is the best possible approach anyways :)

Comment on lines 154 to 167
fn encode_scale_value<T: TypeInfo + 'static>(val: &Value) -> Result<Vec<u8>, Error> {
let (ty_id, types) = make_type::<T>();

let mut bytes = Vec::new();

encode_as_type(val, ty_id, &types, &mut bytes).map_err(|e| {
Error::DynamicTransaction(format!(
"Failed to encode {}: {:?}",
std::any::type_name::<T>(),
e
))
})?;
Ok(bytes)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If you wanted to pass this to some call (api.tx().submit..) as a Value, this would use the runtime metadata to encode properly I'd hope. But I'm guessing you ran into a problem or couldn't do that so I'm intrigued :)

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 had to use this because I wanted to read a storage value and then codec::Decode::decode -> SignedSubmission see fn signed_submission_at above.

The reason is that the epm pallet types doesn't implement serde, not sure if there is a more clever way to do it :)

src/helpers.rs Outdated
static_types::MaxWeight::set(max_weight);
static_types::MaxLength::set(max_length);
static_types::MaxVotesPerVoter::set(max_votes_per_voter);
pub async fn snapshot<T: MinerConfig + Send + Sync + 'static>(
Copy link
Contributor

Choose a reason for hiding this comment

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

I am generally inclined to ask you to document all public functions in here. Some are clear, some are not. And you and I both know this codebase fairly well, which means we're biased.

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 tried to write some docs for these and move the epm related stuff to the epm module instead.

src/helpers.rs Outdated
.await?
.unwrap_or_default();

let voters: Vec<_> = voters
Copy link
Contributor

Choose a reason for hiding this comment

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

I recall I once looked at why we need this hacky code and it seemed neccessary, I want to revisit it now again.

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's to fetch to the snapshot data as we use Miner::mine_solution_with_snapshot added some docs.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

Would like to clone to code and peek around myself as well, but certainly LGTM.

@niklasad1 niklasad1 merged commit d5515bb into main Oct 5, 2022
@niklasad1 niklasad1 deleted the same-metadata-for-chains branch October 5, 2022 17:33
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