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

Merged
merged 30 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4b21dca
add macros crate
sam0x17 Jun 7, 2024
dc1550e
scaffold
sam0x17 Jun 7, 2024
d116b3b
almost working
sam0x17 Jun 7, 2024
54f31ce
working
sam0x17 Jun 7, 2024
2885c23
rip out cargo-husky 🐶✂️
sam0x17 Jun 7, 2024
d99f7dc
handle zero args case
sam0x17 Jun 13, 2024
54fee90
stable hashing algorithm
sam0x17 Jun 13, 2024
e6175e9
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jun 13, 2024
5e95bd0
delete random file
sam0x17 Jun 13, 2024
0618272
add macros to workspace deps
sam0x17 Jun 13, 2024
ad39bdd
add to existing storage structs
sam0x17 Jun 13, 2024
377086e
add freeze_struct_ignore_ra
sam0x17 Jun 13, 2024
4c05546
WIP
sam0x17 Jun 13, 2024
a079954
fix problematic cases
sam0x17 Jun 13, 2024
64784b9
working
sam0x17 Jun 14, 2024
a764728
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jun 14, 2024
1fe85aa
clippy fixes
sam0x17 Jun 14, 2024
a79a38a
fix Dockerfile
sam0x17 Jun 14, 2024
c8016ca
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jun 19, 2024
e444b64
properly use item_clone to generate hash
sam0x17 Jun 21, 2024
951009c
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jul 2, 2024
36a1aef
whoops
sam0x17 Jul 2, 2024
ac4c3c2
fix hash codes
sam0x17 Jul 2, 2024
7ef1cb5
update cargo.lock
sam0x17 Jul 3, 2024
b45bec8
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jul 3, 2024
dba168e
fix hash code
sam0x17 Jul 3, 2024
190b254
fix another hash code
sam0x17 Jul 3, 2024
0ab4a91
update another hash code
sam0x17 Jul 3, 2024
ae4cdf3
bump spec version
sam0x17 Jul 4, 2024
c572ed6
Merge remote-tracking branch 'origin/main' into sam-add-freeze-layout
sam0x17 Jul 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .cargo-husky/hooks/prepare-commit-msg

This file was deleted.

Empty file removed CITATION.cft
Empty file.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"pallets/commitments",
"pallets/subtensor",
"runtime",
"support/macros",
]
resolver = "2"

Expand Down Expand Up @@ -36,6 +37,8 @@ serde_with = { version = "=2.0.0", default-features = false }
smallvec = "1.13.2"
litep2p = { git = "https://github.com/paritytech/litep2p", branch = "master" }

subtensor-macros = { path = "support/macros" }

frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0", default-features = false }
frame-benchmarking-cli = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0" }
frame-executive = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.10.0", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ COPY ./raw_spec_finney.json /subtensor/raw_spec_finney.json
COPY ./node /subtensor/node
COPY ./pallets /subtensor/pallets
COPY ./runtime /subtensor/runtime
COPY ./support /subtensor/support

# Update to nightly toolchain
# Copy our toolchain
COPY rust-toolchain.toml /subtensor/
RUN /subtensor/scripts/init.sh

Expand Down
1 change: 1 addition & 0 deletions pallets/admin-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
Expand Down
1 change: 1 addition & 0 deletions pallets/collective/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [
"derive",
] }
Expand Down
2 changes: 2 additions & 0 deletions pallets/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod benchmarking;
pub mod weights;

pub use pallet::*;
use subtensor_macros::freeze_struct;
pub use weights::WeightInfo;

const LOG_TARGET: &str = "runtime::collective";
Expand Down Expand Up @@ -150,6 +151,7 @@ impl<AccountId, I> GetBacking for RawOrigin<AccountId, I> {
}

/// Info for keeping track of a motion being voted on.
#[freeze_struct("a8e7b0b34ad52b17")]
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Votes<AccountId, BlockNumber> {
/// The proposal's unique index.
Expand Down
1 change: 1 addition & 0 deletions pallets/commitments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
"max-encoded-len",
Expand Down
2 changes: 2 additions & 0 deletions pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod types;
pub mod weights;

pub use pallet::*;
use subtensor_macros::freeze_struct;
pub use types::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -208,6 +209,7 @@ use {
},
};

#[freeze_struct("6a00398e14a8a984")]
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct CommitmentsSignedExtension<T: Config + Send + Sync + TypeInfo>(pub PhantomData<T>);

Expand Down
4 changes: 3 additions & 1 deletion pallets/commitments/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use sp_runtime::{
RuntimeDebug,
};
use sp_std::{fmt::Debug, iter::once, prelude::*};
use subtensor_macros::freeze_struct;

/// Either underlying data blob if it is at most 32 bytes, or a hash of it. If the data is greater
/// than 32-bytes then it will be truncated when encoding.
Expand Down Expand Up @@ -283,11 +284,12 @@ impl Default for Data {
}
}

#[freeze_struct("25c84048dcc90813")]
#[derive(
CloneNoBound, Encode, Decode, Eq, MaxEncodedLen, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo,
)]
#[codec(mel_bound())]
#[cfg_attr(test, derive(frame_support::DefaultNoBound))]
#[derive(frame_support::DefaultNoBound)]
#[scale_info(skip_type_params(FieldLimit))]
pub struct CommitmentInfo<FieldLimit: Get<u32>> {
pub fields: BoundedVec<Data, FieldLimit>,
Expand Down
1 change: 1 addition & 0 deletions pallets/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
"max-encoded-len",
Expand Down
4 changes: 3 additions & 1 deletion pallets/registry/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sp_runtime::{
RuntimeDebug,
};
use sp_std::{fmt::Debug, iter::once, ops::Add, prelude::*};
use subtensor_macros::freeze_struct;

/// Either underlying data blob if it is at most 32 bytes, or a hash of it. If the data is greater
/// than 32-bytes then it will be truncated when encoding.
Expand Down Expand Up @@ -278,11 +279,12 @@ impl TypeInfo for IdentityFields {
///
/// NOTE: This should be stored at the end of the storage item to facilitate the addition of extra
/// fields in a backwards compatible way through a specialized `Decode` impl.
#[freeze_struct("98e2d7fc7536226b")]
#[derive(
CloneNoBound, Encode, Decode, Eq, MaxEncodedLen, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo,
)]
#[codec(mel_bound())]
#[cfg_attr(test, derive(frame_support::DefaultNoBound))]
#[derive(frame_support::DefaultNoBound)]
#[scale_info(skip_type_params(FieldLimit))]
pub struct IdentityInfo<FieldLimit: Get<u32>> {
/// Additional fields of the identity that are not catered for with the struct's explicit
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
subtensor-macros.workspace = true
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/src/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate alloc;
use codec::Compact;
use sp_core::hexdisplay::AsBytesRef;

#[freeze_struct("5752e4c650a83e0d")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct DelegateInfo<T: Config> {
delegate_ss58: T::AccountId,
Expand Down
6 changes: 6 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub mod pallet {
use sp_std::vec;
use sp_std::vec::Vec;

use subtensor_macros::freeze_struct;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -691,6 +693,7 @@ pub mod pallet {
pub type AxonInfoOf = AxonInfo;

/// Data structure for Axon information.
#[freeze_struct("3545cfb0cac4c1f5")]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct AxonInfo {
/// Axon serving block.
Expand All @@ -714,6 +717,7 @@ pub mod pallet {
/// Struct for Prometheus.
pub type PrometheusInfoOf = PrometheusInfo;
/// Data structure for Prometheus information.
#[freeze_struct("5dde687e63baf0cd")]
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub struct PrometheusInfo {
/// Prometheus serving block.
Expand Down Expand Up @@ -2235,6 +2239,7 @@ pub enum CallType {
Other,
}

#[freeze_struct("61e2b893d5ce6701")]
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct SubtensorSignedExtension<T: Config + Send + Sync + TypeInfo>(pub PhantomData<T>);

Expand Down Expand Up @@ -2468,6 +2473,7 @@ use sp_std::vec;
// used not 25 lines below
#[allow(unused)]
use sp_std::vec::Vec;
use subtensor_macros::freeze_struct;

/// Trait for managing a membership pallet instance in the runtime
pub trait MemberManagement<AccountId> {
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/neuron_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use frame_support::storage::IterableStorageDoubleMap;
extern crate alloc;
use codec::Compact;

#[freeze_struct("45e69321f5c74b4b")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct NeuronInfo<T: Config> {
hotkey: T::AccountId,
Expand All @@ -28,6 +29,7 @@ pub struct NeuronInfo<T: Config> {
pruning_score: Compact<u16>,
}

#[freeze_struct("c21f0f4f22bcb2a1")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct NeuronInfoLite<T: Config> {
hotkey: T::AccountId,
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/src/stake_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate alloc;
use codec::Compact;
use sp_core::hexdisplay::AsBytesRef;

#[freeze_struct("86d64c14d71d44b9")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct StakeInfo<T: Config> {
hotkey: T::AccountId,
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use frame_support::storage::IterableStorageMap;
extern crate alloc;
use codec::Compact;

#[freeze_struct("fe79d58173da662a")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct SubnetInfo<T: Config> {
netuid: Compact<u16>,
Expand All @@ -26,6 +27,7 @@ pub struct SubnetInfo<T: Config> {
owner: T::AccountId,
}

#[freeze_struct("55b472510f10e76a")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct SubnetHyperparams {
rho: Compact<u16>,
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ name = "spec_version"
path = "src/spec_version.rs"

[dependencies]
subtensor-macros.workspace = true
subtensor-custom-rpc-runtime-api = { version = "0.0.2", path = "../pallets/subtensor/runtime-api", default-features = false }
smallvec = { workspace = true }
log = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/check_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sp_runtime::{
Saturating,
};
use sp_std::vec;
use subtensor_macros::freeze_struct;

/// Nonce check and increment to give replay protection for transactions.
///
Expand All @@ -19,6 +20,7 @@ use sp_std::vec;
/// This extension affects `requires` and `provides` tags of validity, but DOES NOT
/// set the `priority` field. Make sure that AT LEAST one of the signed extension sets
/// some kind of priority upon validating transactions.
#[freeze_struct("610b76f62cdb521e")]
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct CheckNonce<T: Config>(#[codec(compact)] pub T::Nonce);
Expand Down
21 changes: 21 additions & 0 deletions support/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "subtensor-macros"
version = "0.1.0"
edition = "2021"
license = "MIT"

description = "support macros for Subtensor"
repository = "https://github.com/opentensor/subtensor"
homepage = "https://bittensor.com/"

[lib]
proc-macro = true

[dependencies]
syn = { version = "2", features = ["full", "visit-mut", "extra-traits"] }
proc-macro2 = "1"
quote = "1"
ahash = "0.8"

[lints]
workspace = true
Loading
Loading