Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Commitments Deposit Error #1470

Merged
merged 4 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub mod pallet {
CommitmentSetRateLimitExceeded,
/// Space Limit Exceeded for the current interval
SpaceLimitExceeded,
/// Indicates that unreserve returned a leftover, which is unexpected.
UnexpectedUnreserveLeftover,
}

#[pallet::type_value]
Expand Down Expand Up @@ -266,7 +268,9 @@ pub mod pallet {
if old_deposit > id.deposit {
let err_amount =
T::Currency::unreserve(&who, old_deposit.saturating_sub(id.deposit));
debug_assert!(err_amount.is_zero());
if !err_amount.is_zero() {
return Err(Error::<T>::UnexpectedUnreserveLeftover.into());
}
}

<CommitmentOf<T>>::insert(netuid, &who, id);
Expand Down
48 changes: 45 additions & 3 deletions pallets/commitments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use sp_std::prelude::*;
#[cfg(test)]
use crate::{
CommitmentInfo, CommitmentOf, Config, Data, Error, Event, MaxSpace, Pallet, RateLimit,
RevealedCommitments, TimelockedIndex,
Registration, RevealedCommitments, TimelockedIndex,
mock::{
DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
Balances, DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
new_test_ext, produce_ciphertext,
},
};
use frame_support::pallet_prelude::Hooks;
use frame_support::{BoundedVec, assert_noop, assert_ok, traits::Get};
use frame_support::{
BoundedVec, assert_noop, assert_ok,
traits::{Currency, Get, ReservableCurrency},
};
use frame_system::Pallet as System;

#[allow(clippy::indexing_slicing)]
Expand Down Expand Up @@ -1220,3 +1223,42 @@ fn on_initialize_reveals_matured_timelocks() {
}
});
}

#[test]
fn set_commitment_unreserve_leftover_fails() {
new_test_ext().execute_with(|| {
use frame_system::RawOrigin;

let netuid = 999;
let who = 99;

Balances::make_free_balance_be(&who, 10_000);

let fake_deposit = 100;
let dummy_info = CommitmentInfo {
fields: BoundedVec::try_from(vec![]).expect("empty fields is fine"),
};
let registration = Registration {
deposit: fake_deposit,
info: dummy_info,
block: 0u64.into(),
};

CommitmentOf::<Test>::insert(netuid, who, registration);

assert_ok!(Balances::reserve(&who, fake_deposit));
assert_eq!(Balances::reserved_balance(who), 100);

Balances::unreserve(&who, 10_000);
assert_eq!(Balances::reserved_balance(who), 0);

let commit_small = Box::new(CommitmentInfo {
fields: BoundedVec::try_from(vec![]).expect("no fields is fine"),
});

assert_noop!(
Pallet::<Test>::set_commitment(RawOrigin::Signed(who).into(), netuid, commit_small),
Error::<Test>::UnexpectedUnreserveLeftover
);
});
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 255,
spec_version: 256,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading