diff --git a/substrate/validator-sets/src/embedded_elliptic_curve_keys.rs b/substrate/validator-sets/src/embedded_elliptic_curve_keys.rs index f72b476a..15c01699 100644 --- a/substrate/validator-sets/src/embedded_elliptic_curve_keys.rs +++ b/substrate/validator-sets/src/embedded_elliptic_curve_keys.rs @@ -1,4 +1,4 @@ -use sp_core::{Encode, sr25519::Public}; +use sp_core::sr25519::Public; use serai_primitives::{crypto::SignedEmbeddedEllipticCurveKeys, network_id::*}; diff --git a/substrate/validator-sets/src/lib.rs b/substrate/validator-sets/src/lib.rs index 9174ac5d..672e51cc 100644 --- a/substrate/validator-sets/src/lib.rs +++ b/substrate/validator-sets/src/lib.rs @@ -219,6 +219,11 @@ mod pallet { type DelayedDeallocations = DelayedDeallocations; } + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event {} + + /* /// The generated key pair for a given validator set instance. #[pallet::storage] #[pallet::getter(fn keys)] @@ -285,42 +290,16 @@ mod pallet { */ } } + */ #[pallet::error] pub enum Error { - /// Validator Set doesn't exist. - NonExistentValidatorSet, - /// An invalid embedded elliptic curve key was specified. - /// - /// This error not being raised does not mean the key was valid. Solely that it wasn't detected - /// by this pallet as invalid. - InvalidEmbeddedEllipticCurveKey, - /// Trying to perform an operation requiring an embedded elliptic curve key, without an - /// embedded elliptic curve key. - MissingEmbeddedEllipticCurveKey, - /// Not enough allocation to obtain a key share in the set. - InsufficientAllocation, - /// Trying to deallocate more than allocated. - NotEnoughAllocated, - /// Allocation would cause the validator set to no longer achieve fault tolerance. - AllocationWouldRemoveFaultTolerance, - /// Allocation would cause the validator set to never be able to achieve fault tolerance. - AllocationWouldPreventFaultTolerance, - /// Deallocation would remove the participant from the set, despite the validator not - /// specifying so. - DeallocationWouldRemoveParticipant, - /// Deallocation would cause the validator set to no longer achieve fault tolerance. - DeallocationWouldRemoveFaultTolerance, - /// Deallocation to be claimed doesn't exist. - NonExistentDeallocation, - /// Validator Set already generated keys. - AlreadyGeneratedKeys, - /// An invalid MuSig signature was provided. - BadSignature, - /// Validator wasn't registered or active. - NonExistentValidator, - /// Deallocation would take the stake below what is required. - DeallocationWouldRemoveEconomicSecurity, + /// The provided embedded elliptic curve keys were invalid. + InvalidEmbeddedEllipticCurveKeys, + /// Allocation was erroneous. + AllocationError(AllocationError), + /// Deallocation was erroneous. + DeallocationError(DeallocationError), } /* TODO @@ -816,7 +795,8 @@ mod pallet { let signer = ensure_signed(origin)?; as crate::EmbeddedEllipticCurveKeys>::set_embedded_elliptic_curve_keys( signer, keys, - )?; + ) + .map_err(|()| Error::::InvalidEmbeddedEllipticCurveKeys)?; Ok(()) } @@ -824,21 +804,13 @@ mod pallet { #[pallet::weight(0)] // TODO pub fn allocate(origin: OriginFor, network: NetworkId, amount: Amount) -> DispatchResult { let validator = ensure_signed(origin)?; - // If this network utilizes embedded elliptic curve(s), require the validator to have set the - // appropriate key(s) - if < - Abstractions:: - as - crate::EmbeddedEllipticCurveKeys - >::still_needs_to_set_embedded_elliptic_curve_keys(network, validator) { - Err(Error::::MissingEmbeddedEllipticCurveKey)?; - } Coins::::transfer_internal( validator, Self::account(), Balance { coin: Coin::Serai, amount }, )?; - Abstractions::::increase_allocation(network, validator, amount, false)?; + Abstractions::::increase_allocation(network, validator, amount, false) + .map_err(Error::::AllocationError)?; Ok(()) } @@ -847,7 +819,8 @@ mod pallet { pub fn deallocate(origin: OriginFor, network: NetworkId, amount: Amount) -> DispatchResult { let account = ensure_signed(origin)?; - let deallocation_timeline = Abstractions::::decrease_allocation(network, account, amount)?; + let deallocation_timeline = Abstractions::::decrease_allocation(network, account, amount) + .map_err(Error::::DeallocationError)?; if matches!(deallocation_timeline, DeallocationTimeline::Immediate) { Coins::::transfer_internal( Self::account(), diff --git a/substrate/validator-sets/src/sessions.rs b/substrate/validator-sets/src/sessions.rs index 0d6086f6..d186bcef 100644 --- a/substrate/validator-sets/src/sessions.rs +++ b/substrate/validator-sets/src/sessions.rs @@ -9,7 +9,7 @@ use serai_primitives::{ use frame_support::storage::{StorageValue, StorageMap, StorageDoubleMap, StoragePrefixedMap}; -use crate::allocations::*; +use crate::{embedded_elliptic_curve_keys::EmbeddedEllipticCurveKeys, allocations::Allocations}; /// The list of genesis validators. pub(crate) type GenesisValidators = BoundedVec>; @@ -17,7 +17,7 @@ pub(crate) type GenesisValidators = BoundedVec>(set: ValidatorSet )); } -pub(crate) enum AllocationError { +/// An error when allocating. +#[derive( + scale::Encode, + scale::Decode, + scale::DecodeWithMemTracking, + scale_info::TypeInfo, + frame_support::PalletError, +)] +pub enum AllocationError { + /// The validator set didn't define an allocation requirement for a key share. NoAllocationPerKeyShareSet, + /// Validator is missing embedded elliptic curve keys. + MissingEmbeddedEllipticCurveKeys, + /// The allocation is less than the key share. AllocationLessThanKeyShare, + /// This allocation would introduce a single point of failure. IntroducesSinglePointOfFailure, } -#[must_use] pub(crate) enum DeallocationTimeline { Immediate, Delayed { unlocks_at: Session }, } -pub(crate) enum DeallocationError { + +/// An error when deallocating. +#[derive( + scale::Encode, + scale::Decode, + scale::DecodeWithMemTracking, + scale_info::TypeInfo, + frame_support::PalletError, +)] +pub enum DeallocationError { + /// The validator set didn't define an allocation requirement for a key share. NoAllocationPerKeyShareSet, + /// Not enough was allocated to enable this amount to be deallocated. NotEnoughAllocated, + /// The remaining allocation was non-zero and would be less than a key share. RemainingAllocationLessThanKeyShare, } @@ -272,6 +296,10 @@ impl Sessions for Storage { Err(AllocationError::NoAllocationPerKeyShareSet)? }; + if Self::still_needs_to_set_embedded_elliptic_curve_keys(network, validator) { + Err(AllocationError::MissingEmbeddedEllipticCurveKeys)?; + } + let old_allocation = Self::get_allocation(network, validator).unwrap_or(Amount(0)); // Safe so long as the SRI supply fits within a u64, per assumptions on how this is called let new_allocation = (old_allocation + amount).unwrap();