Skip to content

Commit 8f1de13

Browse files
authored
Merge pull request #1472 from opentensor/improve-tlocked-commitments
feat/Multiple Revealed Commitments
2 parents 4ea7fb1 + da30941 commit 8f1de13

File tree

8 files changed

+695
-131
lines changed

8 files changed

+695
-131
lines changed

pallets/commitments/src/lib.rs

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub mod pallet {
172172
u16,
173173
Twox64Concat,
174174
T::AccountId,
175-
RevealedData<BalanceOf<T>, T::MaxFields, BlockNumberFor<T>>,
175+
Vec<(Vec<u8>, u64)>, // Reveals<(Data, RevealBlock)>
176176
OptionQuery,
177177
>;
178178

@@ -478,7 +478,6 @@ where
478478

479479
impl<T: Config> Pallet<T> {
480480
pub fn reveal_timelocked_commitments() -> DispatchResult {
481-
let current_block = <frame_system::Pallet<T>>::block_number();
482481
let index = TimelockedIndex::<T>::get();
483482
for (netuid, who) in index.clone() {
484483
let Some(mut registration) = <CommitmentOf<T>>::get(netuid, &who) else {
@@ -528,10 +527,7 @@ impl<T: Config> Pallet<T> {
528527
.ok();
529528

530529
let Some(sig) = sig else {
531-
remain_fields.push(Data::TimelockEncrypted {
532-
encrypted,
533-
reveal_round,
534-
});
530+
log::warn!("No sig after deserialization");
535531
continue;
536532
};
537533

@@ -547,10 +543,7 @@ impl<T: Config> Pallet<T> {
547543
.ok();
548544

549545
let Some(commit) = commit else {
550-
remain_fields.push(Data::TimelockEncrypted {
551-
encrypted,
552-
reveal_round,
553-
});
546+
log::warn!("No commit after deserialization");
554547
continue;
555548
};
556549

@@ -563,61 +556,41 @@ impl<T: Config> Pallet<T> {
563556
.unwrap_or_default();
564557

565558
if decrypted_bytes.is_empty() {
566-
remain_fields.push(Data::TimelockEncrypted {
567-
encrypted,
568-
reveal_round,
569-
});
559+
log::warn!("Bytes were decrypted for {:?} but they are empty", who);
570560
continue;
571561
}
572562

573-
let mut reader = &decrypted_bytes[..];
574-
let revealed_info: CommitmentInfo<T::MaxFields> =
575-
match Decode::decode(&mut reader) {
576-
Ok(info) => info,
577-
Err(e) => {
578-
log::warn!(
579-
"Failed to decode decrypted data for {:?}: {:?}",
580-
who,
581-
e
582-
);
583-
remain_fields.push(Data::TimelockEncrypted {
584-
encrypted,
585-
reveal_round,
586-
});
587-
continue;
588-
}
589-
};
590-
591-
revealed_fields.push(revealed_info);
563+
revealed_fields.push(decrypted_bytes);
592564
}
593565

594566
other => remain_fields.push(other),
595567
}
596568
}
597569

598570
if !revealed_fields.is_empty() {
599-
let mut all_revealed_data = Vec::new();
600-
for info in revealed_fields {
601-
all_revealed_data.extend(info.fields.into_inner());
602-
}
571+
let mut existing_reveals =
572+
RevealedCommitments::<T>::get(netuid, &who).unwrap_or_default();
603573

604-
let bounded_revealed = BoundedVec::try_from(all_revealed_data)
605-
.map_err(|_| "Could not build BoundedVec for revealed fields")?;
574+
let current_block = <frame_system::Pallet<T>>::block_number();
575+
let block_u64 = current_block.saturated_into::<u64>();
606576

607-
let combined_revealed_info = CommitmentInfo {
608-
fields: bounded_revealed,
609-
};
577+
// Push newly revealed items onto the tail of existing_reveals and emit the event
578+
for revealed_bytes in revealed_fields {
579+
existing_reveals.push((revealed_bytes, block_u64));
610580

611-
let revealed_data = RevealedData {
612-
info: combined_revealed_info,
613-
revealed_block: current_block,
614-
deposit: registration.deposit,
615-
};
616-
<RevealedCommitments<T>>::insert(netuid, &who, revealed_data);
617-
Self::deposit_event(Event::CommitmentRevealed {
618-
netuid,
619-
who: who.clone(),
620-
});
581+
Self::deposit_event(Event::CommitmentRevealed {
582+
netuid,
583+
who: who.clone(),
584+
});
585+
}
586+
587+
const MAX_REVEALS: usize = 10;
588+
if existing_reveals.len() > MAX_REVEALS {
589+
let remove_count = existing_reveals.len().saturating_sub(MAX_REVEALS);
590+
existing_reveals.drain(0..remove_count);
591+
}
592+
593+
RevealedCommitments::<T>::insert(netuid, &who, existing_reveals);
621594
}
622595

623596
registration.info.fields = BoundedVec::try_from(remain_fields)

pallets/commitments/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ use tle::{ibe::fullident::Identity, stream_ciphers::AESGCMStreamCipherProvider,
207207
pub const DRAND_QUICKNET_PUBKEY_HEX: &str = "83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6\
208208
a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809b\
209209
d274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a";
210+
pub const DRAND_QUICKNET_SIG_2000_HEX: &str = "b6cb8f482a0b15d45936a4c4ea08e98a087e71787caee3f4d07a8a9843b1bc5423c6b3c22f446488b3137eaca799c77e"; // round 20000
210211
pub const DRAND_QUICKNET_SIG_HEX: &str = "b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39";
211212

212213
/// Inserts a Drand pulse for `round` with the given `signature_bytes`.

0 commit comments

Comments
 (0)