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

BoundedString to BoundedVec #622

Closed
wants to merge 1 commit into from
Closed

Conversation

Klapeyron
Copy link
Contributor

@Klapeyron Klapeyron commented Mar 31, 2025

Description

Proposition to replace BoundedString with BoundedVec, in order to not make assumptions of content of underlying data provided by the user.

If I understand correctly the execution path, it probably is the following:

  • user stores metadata with non-utf8 characters on-chain.
  • metadata are extracted through associated RPC, which is not able to decode URL.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages.
  • The size limit of 400 LOC isn't needlessly exceeded
  • The PR refers to a JIRA ticket (if one exists)
  • New tests are added if needed and existing tests are updated.
  • Relevant logging and metrics added
  • Any changes are noted in the changelog.md for affected crate
  • Self-reviewed the diff

@Klapeyron Klapeyron force-pushed the ETCM-9674_small_refactoring branch from a01972a to 8d5408e Compare March 31, 2025 09:05
@@ -461,13 +461,15 @@ impl AsCardanoSPO for BlockAuthor {
}
}

pub const MAX_METADATA_URL_LENGTH: u32 = 512;
parameter_types! {
pub const MaxMetadataUrlLength: u32 = 512;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

looks quite long, should we short it?


#[derive(
Clone, Debug, MaxEncodedLen, Encode, Decode, Serialize, Deserialize, PartialEq, Eq, TypeInfo,
)]
pub struct BlockProducerMetadataType {
pub url: BoundedString<MAX_METADATA_URL_LENGTH>,
pub url: BoundedVec<u8, MaxMetadataUrlLength>,
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this change reflected in RPC?

I would like to see "before" and "after" before approving such a change.

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 think it is hard to answer clearly that one, it actually depends on what will be the source of data. If type will be created from runtime (i.e. Decode trait will be involved), then in theory it may contain non utf-8 characters, so further serialization of this type with serde::Serialize may cause permanent error.
Other scenario will be if type will be created with serde::Deserialize, then we will have validation being involved (utf-8 check), but later there are no guarantees that data will remain utf-8 (code before changes).
From the user perspective providing of the data should stay the same, user will provide some arbitrary blob of bytes that may or may not decode to string, point here is to not make any assumptions regarding what is inside (due we not validate either utf-8 data or url format).
After the changes serialization of the data back to readable format may change, proposed changes will try to save them in bytes format, without attempt to provide "readable" utf-8 form, due underlying data may be invalid

@Klapeyron Klapeyron changed the base branch from master to metadata-e2e April 1, 2025 07:54
@Klapeyron Klapeyron changed the base branch from metadata-e2e to master April 1, 2025 07:55
@Klapeyron Klapeyron force-pushed the ETCM-9674_small_refactoring branch from aec15f1 to 7d55434 Compare April 1, 2025 07:56
@Klapeyron Klapeyron force-pushed the ETCM-9674_small_refactoring branch from 98215ec to adc3de0 Compare April 1, 2025 08:53
@gilligan
Copy link
Contributor

gilligan commented Apr 1, 2025

Proposition of small refactoring

Could you add a bit context in the description like the rationale/benefit of the refactoring? I might learn something :)
Using BoundedVec means various traits already exist and don't have to be manually generated right? Doesn't that also imply changes on the call-site? (I guess that's what Lech's comment might also be hinting at)

@Klapeyron
Copy link
Contributor Author

I put some details regarding changes in this PR in associated Jira item

Copy link
Contributor

@AmbientTea AmbientTea left a comment

Choose a reason for hiding this comment

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

I don't think there is a problem to be solved here


#[derive(
Clone, Debug, MaxEncodedLen, Encode, Decode, Serialize, Deserialize, PartialEq, Eq, TypeInfo,
)]
pub struct BlockProducerMetadataType {
pub url: BoundedString<MAX_METADATA_URL_LENGTH>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is our own reference runtime implementation, not some code that anyone other than us is forced to use. We can make any assumptions that are convenient for us to do here. I went with a string-like type, because that's easy and intuitive for us to work with.

@Klapeyron
Copy link
Contributor Author

If you wish to keep the changes in this form, I will consider the matter closed

@Klapeyron Klapeyron closed this Apr 1, 2025
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.

4 participants