Skip to content

Commit 1100804

Browse files
authored
Make subnet ID's type-safe #1733
2 parents b45fcb3 + 1d845d8 commit 1100804

File tree

98 files changed

+2363
-1994
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2363
-1994
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ targets = ["x86_64-unknown-linux-gnu"]
1313
[dependencies]
1414
codec = { workspace = true }
1515
frame-support = { workspace = true }
16+
precompile-utils = { workspace = true }
1617
scale-info = { workspace = true }
17-
sp-runtime = { workspace = true }
18+
serde = { workspace = true }
1819
sp-core = { workspace = true }
20+
sp-runtime = { workspace = true }
21+
subtensor-macros = { workspace = true }
1922

2023
[lints]
2124
workspace = true
@@ -26,7 +29,9 @@ fast-blocks = []
2629
std = [
2730
"codec/std",
2831
"frame-support/std",
32+
"precompile-utils/std",
2933
"scale-info/std",
34+
"serde/std",
3035
"sp-core/std",
3136
"sp-runtime/std",
3237
]

common/src/lib.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2+
use core::fmt::{self, Display, Formatter};
23

3-
use codec::{Decode, Encode, MaxEncodedLen};
4+
use codec::{Compact, CompactAs, Decode, Encode, Error as CodecError, MaxEncodedLen};
5+
use frame_support::pallet_prelude::*;
46
use scale_info::TypeInfo;
7+
use serde::{Deserialize, Serialize};
58
use sp_runtime::{
69
MultiSignature,
710
traits::{IdentifyAccount, Verify},
811
};
12+
use subtensor_macros::freeze_struct;
913

1014
/// Balance of an account.
1115
pub type Balance = u64;
@@ -31,6 +35,75 @@ pub type Nonce = u32;
3135
/// Transfers below SMALL_TRANSFER_LIMIT are considered small transfers
3236
pub const SMALL_TRANSFER_LIMIT: Balance = 500_000_000; // 0.5 TAO
3337

38+
#[freeze_struct("f1746d0b1911967")]
39+
#[repr(transparent)]
40+
#[derive(
41+
Deserialize,
42+
Serialize,
43+
Clone,
44+
Copy,
45+
Decode,
46+
Default,
47+
Encode,
48+
Eq,
49+
Hash,
50+
MaxEncodedLen,
51+
Ord,
52+
PartialEq,
53+
PartialOrd,
54+
RuntimeDebug,
55+
TypeInfo,
56+
)]
57+
pub struct NetUid(u16);
58+
59+
impl NetUid {
60+
pub const ROOT: NetUid = Self(0);
61+
62+
pub fn is_root(&self) -> bool {
63+
*self == Self::ROOT
64+
}
65+
66+
pub fn next(&self) -> NetUid {
67+
Self(self.0.saturating_add(1))
68+
}
69+
}
70+
71+
impl Display for NetUid {
72+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
73+
Display::fmt(&self.0, f)
74+
}
75+
}
76+
77+
impl CompactAs for NetUid {
78+
type As = u16;
79+
80+
fn encode_as(&self) -> &Self::As {
81+
&self.0
82+
}
83+
84+
fn decode_from(v: Self::As) -> Result<Self, CodecError> {
85+
Ok(Self(v))
86+
}
87+
}
88+
89+
impl From<Compact<NetUid>> for NetUid {
90+
fn from(c: Compact<NetUid>) -> Self {
91+
c.0
92+
}
93+
}
94+
95+
impl From<NetUid> for u16 {
96+
fn from(val: NetUid) -> Self {
97+
val.0
98+
}
99+
}
100+
101+
impl From<u16> for NetUid {
102+
fn from(value: u16) -> Self {
103+
Self(value)
104+
}
105+
}
106+
34107
#[derive(
35108
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug, MaxEncodedLen, TypeInfo,
36109
)]

pallets/admin-utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ substrate-fixed = { workspace = true }
3232
pallet-evm-chain-id = { workspace = true }
3333
pallet-drand = { workspace = true, default-features = false }
3434
sp-consensus-grandpa = { workspace = true }
35+
subtensor-runtime-common = { workspace = true }
3536

3637
[dev-dependencies]
3738
sp-core = { workspace = true }
@@ -67,6 +68,7 @@ std = [
6768
"sp-tracing/std",
6869
"sp-weights/std",
6970
"substrate-fixed/std",
71+
"subtensor-runtime-common/std"
7072
]
7173
runtime-benchmarks = [
7274
"frame-benchmarking/runtime-benchmarks",

0 commit comments

Comments
 (0)