Skip to content

Commit a578738

Browse files
authored
Merge pull request #1917 from opentensor/sasha/chore/currency-types
Make Tao a type-safe
2 parents e880964 + 608108d commit a578738

Some content is hidden

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

70 files changed

+2169
-1687
lines changed

common/src/currency.rs

Lines changed: 128 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -33,103 +33,101 @@ use subtensor_macros::freeze_struct;
3333
)]
3434
pub struct AlphaCurrency(u64);
3535

36-
impl TypeInfo for AlphaCurrency {
37-
type Identity = <u64 as TypeInfo>::Identity;
38-
fn type_info() -> scale_info::Type {
39-
<u64 as TypeInfo>::type_info()
40-
}
41-
}
42-
43-
impl Display for AlphaCurrency {
44-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45-
Display::fmt(&self.0, f)
46-
}
47-
}
48-
49-
impl CompactAs for AlphaCurrency {
50-
type As = u64;
51-
52-
fn encode_as(&self) -> &Self::As {
53-
&self.0
54-
}
55-
56-
fn decode_from(v: Self::As) -> Result<Self, CodecError> {
57-
Ok(Self(v))
58-
}
59-
}
60-
61-
impl From<Compact<AlphaCurrency>> for AlphaCurrency {
62-
fn from(c: Compact<AlphaCurrency>) -> Self {
63-
c.0
64-
}
65-
}
66-
67-
impl From<AlphaCurrency> for u64 {
68-
fn from(val: AlphaCurrency) -> Self {
69-
val.0
70-
}
71-
}
36+
#[freeze_struct("4d1bcb31c40c2594")]
37+
#[repr(transparent)]
38+
#[derive(
39+
Deserialize,
40+
Serialize,
41+
Clone,
42+
Copy,
43+
Decode,
44+
DecodeWithMemTracking,
45+
Default,
46+
Encode,
47+
Eq,
48+
Hash,
49+
MaxEncodedLen,
50+
Ord,
51+
PartialEq,
52+
PartialOrd,
53+
RuntimeDebug,
54+
)]
55+
pub struct TaoCurrency(u64);
7256

73-
impl From<u64> for AlphaCurrency {
74-
fn from(value: u64) -> Self {
75-
Self(value)
76-
}
77-
}
57+
// implements traits required by the Currency trait (ToFixed + Into<u64> + From<u64>) and CompactAs,
58+
// TypeInfo and Display. It expects a wrapper structure for u64 (CurrencyT(u64)).
59+
macro_rules! impl_currency_reqs {
60+
($currency_type:ident) => {
61+
impl $currency_type {
62+
pub const fn new(inner: u64) -> Self {
63+
Self(inner)
64+
}
65+
}
7866

79-
impl ToFixed for AlphaCurrency {
80-
fn to_fixed<F: Fixed>(self) -> F {
81-
self.0.to_fixed()
82-
}
67+
impl TypeInfo for $currency_type {
68+
type Identity = <u64 as TypeInfo>::Identity;
69+
fn type_info() -> scale_info::Type {
70+
<u64 as TypeInfo>::type_info()
71+
}
72+
}
8373

84-
fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
85-
self.0.checked_to_fixed()
86-
}
74+
impl Display for $currency_type {
75+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
76+
Display::fmt(&self.0, f)
77+
}
78+
}
8779

88-
fn saturating_to_fixed<F: Fixed>(self) -> F {
89-
self.0.saturating_to_fixed()
90-
}
91-
fn wrapping_to_fixed<F: Fixed>(self) -> F {
92-
self.0.wrapping_to_fixed()
93-
}
80+
impl CompactAs for $currency_type {
81+
type As = u64;
9482

95-
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
96-
self.0.overflowing_to_fixed()
97-
}
98-
}
83+
fn encode_as(&self) -> &Self::As {
84+
&self.0
85+
}
9986

100-
impl Currency for AlphaCurrency {
101-
const MAX: Self = Self(u64::MAX);
102-
const ZERO: Self = Self(0);
103-
}
87+
fn decode_from(v: Self::As) -> Result<Self, CodecError> {
88+
Ok(Self(v))
89+
}
90+
}
10491

105-
pub trait Currency: ToFixed + Into<u64> + From<u64> + Clone + Copy {
106-
const MAX: Self;
107-
const ZERO: Self;
92+
impl From<Compact<$currency_type>> for $currency_type {
93+
fn from(c: Compact<$currency_type>) -> Self {
94+
c.0
95+
}
96+
}
10897

109-
fn is_zero(&self) -> bool {
110-
Into::<u64>::into(*self) == 0
111-
}
98+
impl From<$currency_type> for u64 {
99+
fn from(val: $currency_type) -> Self {
100+
val.0
101+
}
102+
}
112103

113-
fn to_u64(&self) -> u64 {
114-
(*self).into()
115-
}
104+
impl From<u64> for $currency_type {
105+
fn from(value: u64) -> Self {
106+
Self(value)
107+
}
108+
}
116109

117-
fn saturating_add(&self, rhv: Self) -> Self {
118-
Into::<u64>::into(*self).saturating_add(rhv.into()).into()
119-
}
110+
impl ToFixed for $currency_type {
111+
fn to_fixed<F: Fixed>(self) -> F {
112+
self.0.to_fixed()
113+
}
120114

121-
#[allow(clippy::arithmetic_side_effects)]
122-
fn saturating_div(&self, rhv: Self) -> Self {
123-
Into::<u64>::into(*self).saturating_div(rhv.into()).into()
124-
}
115+
fn checked_to_fixed<F: Fixed>(self) -> Option<F> {
116+
self.0.checked_to_fixed()
117+
}
125118

126-
fn saturating_sub(&self, rhv: Self) -> Self {
127-
Into::<u64>::into(*self).saturating_sub(rhv.into()).into()
128-
}
119+
fn saturating_to_fixed<F: Fixed>(self) -> F {
120+
self.0.saturating_to_fixed()
121+
}
122+
fn wrapping_to_fixed<F: Fixed>(self) -> F {
123+
self.0.wrapping_to_fixed()
124+
}
129125

130-
fn saturating_mul(&self, rhv: Self) -> Self {
131-
Into::<u64>::into(*self).saturating_mul(rhv.into()).into()
132-
}
126+
fn overflowing_to_fixed<F: Fixed>(self) -> (F, bool) {
127+
self.0.overflowing_to_fixed()
128+
}
129+
}
130+
};
133131
}
134132

135133
macro_rules! impl_arithmetic_operators {
@@ -208,8 +206,6 @@ macro_rules! impl_arithmetic_operators {
208206
};
209207
}
210208

211-
impl_arithmetic_operators!(AlphaCurrency);
212-
213209
macro_rules! impl_approx {
214210
($currency_type:ident) => {
215211
#[cfg(feature = "approx")]
@@ -231,4 +227,50 @@ macro_rules! impl_approx {
231227
};
232228
}
233229

230+
pub trait Currency: ToFixed + Into<u64> + From<u64> + Clone + Copy {
231+
const MAX: Self;
232+
const ZERO: Self;
233+
234+
fn is_zero(&self) -> bool {
235+
Into::<u64>::into(*self) == 0
236+
}
237+
238+
fn to_u64(&self) -> u64 {
239+
(*self).into()
240+
}
241+
242+
fn saturating_add(&self, rhv: Self) -> Self {
243+
Into::<u64>::into(*self).saturating_add(rhv.into()).into()
244+
}
245+
246+
#[allow(clippy::arithmetic_side_effects)]
247+
fn saturating_div(&self, rhv: Self) -> Self {
248+
Into::<u64>::into(*self).saturating_div(rhv.into()).into()
249+
}
250+
251+
fn saturating_sub(&self, rhv: Self) -> Self {
252+
Into::<u64>::into(*self).saturating_sub(rhv.into()).into()
253+
}
254+
255+
fn saturating_mul(&self, rhv: Self) -> Self {
256+
Into::<u64>::into(*self).saturating_mul(rhv.into()).into()
257+
}
258+
}
259+
260+
impl_arithmetic_operators!(AlphaCurrency);
234261
impl_approx!(AlphaCurrency);
262+
impl_currency_reqs!(AlphaCurrency);
263+
264+
impl_arithmetic_operators!(TaoCurrency);
265+
impl_approx!(TaoCurrency);
266+
impl_currency_reqs!(TaoCurrency);
267+
268+
impl Currency for AlphaCurrency {
269+
const MAX: Self = Self(u64::MAX);
270+
const ZERO: Self = Self(0);
271+
}
272+
273+
impl Currency for TaoCurrency {
274+
const MAX: Self = Self(u64::MAX);
275+
const ZERO: Self = Self(0);
276+
}

common/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,21 @@ impl Default for ProxyType {
168168
}
169169

170170
pub trait SubnetInfo<AccountId> {
171-
fn tao_reserve(netuid: NetUid) -> u64;
171+
fn tao_reserve(netuid: NetUid) -> TaoCurrency;
172172
fn alpha_reserve(netuid: NetUid) -> AlphaCurrency;
173173
fn exists(netuid: NetUid) -> bool;
174174
fn mechanism(netuid: NetUid) -> u16;
175175
fn is_owner(account_id: &AccountId, netuid: NetUid) -> bool;
176176
}
177177

178178
pub trait BalanceOps<AccountId> {
179-
fn tao_balance(account_id: &AccountId) -> u64;
179+
fn tao_balance(account_id: &AccountId) -> TaoCurrency;
180180
fn alpha_balance(netuid: NetUid, coldkey: &AccountId, hotkey: &AccountId) -> AlphaCurrency;
181-
fn increase_balance(coldkey: &AccountId, tao: u64);
182-
fn decrease_balance(coldkey: &AccountId, tao: u64) -> Result<u64, DispatchError>;
181+
fn increase_balance(coldkey: &AccountId, tao: TaoCurrency);
182+
fn decrease_balance(
183+
coldkey: &AccountId,
184+
tao: TaoCurrency,
185+
) -> Result<TaoCurrency, DispatchError>;
183186
fn increase_stake(
184187
coldkey: &AccountId,
185188
hotkey: &AccountId,
@@ -192,8 +195,8 @@ pub trait BalanceOps<AccountId> {
192195
netuid: NetUid,
193196
alpha: AlphaCurrency,
194197
) -> Result<AlphaCurrency, DispatchError>;
195-
fn increase_provided_tao_reserve(netuid: NetUid, tao: u64);
196-
fn decrease_provided_tao_reserve(netuid: NetUid, tao: u64);
198+
fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
199+
fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency);
197200
fn increase_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
198201
fn decrease_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency);
199202
}

pallets/admin-utils/src/benchmarking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ mod benchmarks {
266266
);
267267

268268
#[extrinsic_call]
269-
_(RawOrigin::Root, 1u16.into()/*netuid*/, 10u64/*max_burn*/)/*sudo_set_max_burn*/;
269+
_(RawOrigin::Root, 1u16.into()/*netuid*/, 10.into()/*max_burn*/)/*sudo_set_max_burn*/;
270270
}
271271

272272
#[benchmark]
@@ -277,7 +277,7 @@ mod benchmarks {
277277
);
278278

279279
#[extrinsic_call]
280-
_(RawOrigin::Root, 1u16.into()/*netuid*/, 10u64/*min_burn*/)/*sudo_set_min_burn*/;
280+
_(RawOrigin::Root, 1u16.into()/*netuid*/, 10.into()/*min_burn*/)/*sudo_set_min_burn*/;
281281
}
282282

283283
#[benchmark]

pallets/admin-utils/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub mod pallet {
3131
use pallet_subtensor::utils::rate_limiting::TransactionType;
3232
use sp_runtime::BoundedVec;
3333
use substrate_fixed::types::I96F32;
34-
use subtensor_runtime_common::NetUid;
34+
use subtensor_runtime_common::{NetUid, TaoCurrency};
3535

3636
/// The main data structure of the module.
3737
#[pallet::pallet]
@@ -665,7 +665,7 @@ pub mod pallet {
665665
pub fn sudo_set_min_burn(
666666
origin: OriginFor<T>,
667667
netuid: NetUid,
668-
min_burn: u64,
668+
min_burn: TaoCurrency,
669669
) -> DispatchResult {
670670
ensure_root(origin)?;
671671

@@ -688,7 +688,7 @@ pub mod pallet {
688688
pub fn sudo_set_max_burn(
689689
origin: OriginFor<T>,
690690
netuid: NetUid,
691-
max_burn: u64,
691+
max_burn: TaoCurrency,
692692
) -> DispatchResult {
693693
ensure_root(origin)?;
694694

@@ -904,7 +904,7 @@ pub mod pallet {
904904
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
905905
pub fn sudo_set_total_issuance(
906906
origin: OriginFor<T>,
907-
total_issuance: u64,
907+
total_issuance: TaoCurrency,
908908
) -> DispatchResult {
909909
ensure_root(origin)?;
910910

@@ -948,7 +948,7 @@ pub mod pallet {
948948
))]
949949
pub fn sudo_set_network_min_lock_cost(
950950
origin: OriginFor<T>,
951-
lock_cost: u64,
951+
lock_cost: TaoCurrency,
952952
) -> DispatchResult {
953953
ensure_root(origin)?;
954954

@@ -1005,7 +1005,7 @@ pub mod pallet {
10051005
pub fn sudo_set_rao_recycled(
10061006
origin: OriginFor<T>,
10071007
netuid: NetUid,
1008-
rao_recycled: u64,
1008+
rao_recycled: TaoCurrency,
10091009
) -> DispatchResult {
10101010
ensure_root(origin)?;
10111011
ensure!(

0 commit comments

Comments
 (0)