Skip to content

Commit fbc809d

Browse files
authored
Merge pull request #1638 from opentensor/Add-BigRaw-Commitments-Variant
Add `BigRaw` Commitments Variant
2 parents 5e76180 + 6597e3c commit fbc809d

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

pallets/commitments/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod pallet {
4747
/// Because this pallet emits events, it depends on the runtime's definition of an event.
4848
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
4949

50-
/// Currency type that will be used to place deposits on neurons
50+
///Currency type that will be used to reserve deposits for commitments
5151
type Currency: ReservableCurrency<Self::AccountId> + Send + Sync;
5252

5353
/// Weight information for extrinsics in this pallet.
@@ -71,11 +71,11 @@ pub mod pallet {
7171
#[pallet::constant]
7272
type FieldDeposit: Get<BalanceOf<Self>>;
7373

74-
/// Used to retreive the given subnet's tempo
74+
/// Used to retrieve the given subnet's tempo
7575
type TempoInterface: GetTempoInterface;
7676
}
7777

78-
/// Used to retreive the given subnet's tempo
78+
/// Used to retrieve the given subnet's tempo
7979
pub trait GetTempoInterface {
8080
/// Used to retreive the epoch index for the given subnet.
8181
fn get_epoch_index(netuid: u16, cur_block: u64) -> u64;
@@ -113,7 +113,7 @@ pub mod pallet {
113113
pub enum Error<T> {
114114
/// Account passed too many additional fields to their commitment
115115
TooManyFieldsInCommitmentInfo,
116-
/// Account is not allow to make commitments to the chain
116+
/// Account is not allowed to make commitments to the chain
117117
AccountNotAllowedCommit,
118118
/// Space Limit Exceeded for the current interval
119119
SpaceLimitExceeded,

pallets/commitments/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn manual_data_type_info() {
3535
Data::Raw(bytes) => format!("Raw{}", bytes.len()),
3636
Data::TimelockEncrypted { .. } => "TimelockEncrypted".to_string(),
3737
Data::ResetBondsFlag => "ResetBondsFlag".to_string(),
38+
Data::BigRaw(_) => "BigRaw".to_string(),
3839
};
3940
if let scale_info::TypeDef::Variant(variant) = &type_info.type_def {
4041
let variant = variant
@@ -51,6 +52,7 @@ fn manual_data_type_info() {
5152
let expected_len = match data {
5253
Data::None => 0,
5354
Data::Raw(bytes) => bytes.len() as u32,
55+
Data::BigRaw(bytes) => bytes.len() as u32,
5456
Data::BlakeTwo256(_)
5557
| Data::Sha256(_)
5658
| Data::Keccak256(_)

pallets/commitments/src/types.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ use sp_runtime::{
3131
use sp_std::{fmt::Debug, iter::once, prelude::*};
3232
use subtensor_macros::freeze_struct;
3333

34-
/// Either underlying data blob if it is at most 32 bytes, or a hash of it. If the data is greater
35-
/// than 32-bytes then it will be truncated when encoding.
36-
///
37-
/// Can also be `None`.
34+
/// Represents stored data which can be:
35+
/// - `Raw`: a direct blob up to 128 bytes
36+
/// - `BigRaw`: a larger blob up to 512 bytes
37+
/// - A cryptographic hash (BlakeTwo256, Sha256, Keccak256, ShaThree256)
38+
/// - A timelock-encrypted blob with a reveal round
39+
/// - A reset flag (`ResetBondsFlag`)
40+
/// Can also be `None`.
3841
#[derive(Clone, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
3942
pub enum Data {
4043
/// No data here.
4144
None,
42-
/// The data is stored directly.
43-
Raw(BoundedVec<u8, ConstU32<512>>),
45+
/// The data is stored directly (up to 128 bytes).
46+
Raw(BoundedVec<u8, ConstU32<128>>),
4447
/// Only the Blake2 hash of the data is stored. The preimage of the hash may be retrieved
4548
/// through some hash-lookup service.
4649
BlakeTwo256([u8; 32]),
@@ -60,6 +63,8 @@ pub enum Data {
6063
},
6164
/// Flag to trigger bonds reset for subnet
6265
ResetBondsFlag,
66+
/// The data is stored directly (up to 512 bytes).
67+
BigRaw(BoundedVec<u8, ConstU32<MAX_BIGRAW_COMMITMENT_SIZE_BYTES>>),
6368
}
6469

6570
impl Data {
@@ -82,6 +87,7 @@ impl Data {
8287
| Data::ShaThree256(arr) => arr.len() as u64,
8388
Data::TimelockEncrypted { encrypted, .. } => encrypted.len() as u64,
8489
Data::ResetBondsFlag => 0,
90+
Data::BigRaw(bytes) => bytes.len() as u64,
8591
}
8692
}
8793
}
@@ -112,6 +118,11 @@ impl Decode for Data {
112118
}
113119
}
114120
135 => Data::ResetBondsFlag,
121+
136 => {
122+
let bigvec =
123+
BoundedVec::<u8, ConstU32<MAX_BIGRAW_COMMITMENT_SIZE_BYTES>>::decode(input)?;
124+
Data::BigRaw(bigvec)
125+
}
115126
_ => return Err(codec::Error::from("invalid leading byte")),
116127
})
117128
}
@@ -141,6 +152,11 @@ impl Encode for Data {
141152
r
142153
}
143154
Data::ResetBondsFlag => vec![135],
155+
Data::BigRaw(bigvec) => {
156+
let mut r = vec![136];
157+
r.extend_from_slice(&bigvec.encode());
158+
r
159+
}
144160
}
145161
}
146162
}
@@ -327,7 +343,12 @@ impl TypeInfo for Data {
327343
.field(|f| f.name("reveal_round").ty::<u64>()),
328344
)
329345
})
330-
.variant("ResetBondsFlag", |v| v.index(135));
346+
.variant("ResetBondsFlag", |v| v.index(135))
347+
.variant("BigRaw", |v| {
348+
v.index(136).fields(Fields::unnamed().field(|f| {
349+
f.ty::<BoundedVec<u8, ConstU32<MAX_BIGRAW_COMMITMENT_SIZE_BYTES>>>()
350+
}))
351+
});
331352

332353
Type::builder()
333354
.path(Path::new("Data", module_path!()))
@@ -354,6 +375,7 @@ pub struct CommitmentInfo<FieldLimit: Get<u32>> {
354375

355376
/// Maximum size of the serialized timelock commitment in bytes
356377
pub const MAX_TIMELOCK_COMMITMENT_SIZE_BYTES: u32 = 1024;
378+
pub const MAX_BIGRAW_COMMITMENT_SIZE_BYTES: u32 = 512;
357379

358380
/// Contains the decrypted data of a revealed commitment.
359381
#[freeze_struct("bf575857b57f9bef")]

0 commit comments

Comments
 (0)