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

prevent accidental changes to storage structs #533

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

sam0x17
Copy link
Contributor

@sam0x17 sam0x17 commented Jun 13, 2024

Fixes #513

devnet companion: #550, #554

@sam0x17 sam0x17 self-assigned this Jun 13, 2024
@sam0x17 sam0x17 changed the title Sam add freeze layout prevent accidental changes to storage structs Jun 13, 2024
camfairchild
camfairchild previously approved these changes Jun 17, 2024
@open-junius
Copy link
Contributor

Look good! I have a question, how to get the hash value in freeze_struct()?

@sam0x17
Copy link
Contributor Author

sam0x17 commented Jun 18, 2024

Look good! I have a question, how to get the hash value in freeze_struct()?

from the compile error

@sam0x17 sam0x17 requested a review from a team June 18, 2024 20:58
open-junius
open-junius previously approved these changes Jun 19, 2024
@sam0x17 sam0x17 added blue team defensive programming, CI, etc devnet-pass PR has been tested on devnet on-devnet PR has been deployed to devnet and removed devnet-ready PR's companion has been merged into devnet-ready labels Jun 21, 2024
@sam0x17 sam0x17 removed the request for review from orriin June 26, 2024 13:26
@@ -0,0 +1,7 @@
use subtensor_macros::freeze_struct;

#[freeze_struct("ecdcaac0f6da589a")]

Choose a reason for hiding this comment

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

How can one calculate this hash in an easy way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so right now the dev ux is basically you are in one of two scenarios:

  1. you are adding it to a new struct, in which case you just do #[freeze_struct] with zero args and the compiler error will immediately tell you the proper hash code
  2. you are updating an existing struct that already has a hash code, in which case the compiler error the second you change the struct will tell you the new expected hash code, and also encourages you to write a migration etc. if applicable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

people running RA will see it literally in their IDE as well

Copy link
Contributor Author

@sam0x17 sam0x17 Jun 28, 2024

Choose a reason for hiding this comment

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

also worth noting it is immune to doc comment changes, they aren't part of the hash code calculation, and arbitrary additional nodes to ignore could also be added to the visitor pattern if desired

Choose a reason for hiding this comment

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

I see, that's cool! but will this work also for generic structs? It seems like it will also break if you rename a generic or field, even if it is the same encoding?

I think it is certainly a cool idea, and if no similar rust crate exists, we can make it an independent Rust crate, and reference it in our docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it could be modified to be immune to name changes. I wasn't sure if that was safe in all cases so I left that out of this one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same goes for generics, whatever you want it to ignore in the hash code calculation can be added to the visitor pattern

@sam0x17 sam0x17 requested review from a team, JohnReedV, gztensor and orriin July 2, 2024 14:19
@sam0x17 sam0x17 added the testnet-pass PR was successfully tested on testnet label Jul 2, 2024
@@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 154,
spec_version: 155,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just leave this unchanged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blue team defensive programming, CI, etc devnet-pass PR has been tested on devnet on-devnet PR has been deployed to devnet testnet-pass PR was successfully tested on testnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prevent accidental changes to storage structs
8 participants