mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-14 15:09:23 +00:00
Merge branch 'next' into serai-rpc
This commit is contained in:
@@ -6,23 +6,22 @@ license = "AGPL-3.0-only"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/validator-sets/pallet"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
edition = "2021"
|
||||
rust-version = "1.74"
|
||||
rust-version = "1.80"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["scale", "scale-info"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
hashbrown = { version = "0.14", default-features = false, features = ["ahash", "inline-more"] }
|
||||
bitvec = { version = "1", default-features = false, features = ["alloc", "serde"] }
|
||||
|
||||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
|
||||
scale-info = { version = "2", default-features = false, features = ["derive"] }
|
||||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "bit-vec"] }
|
||||
scale-info = { version = "2", default-features = false, features = ["derive", "bit-vec"] }
|
||||
|
||||
serde = { version = "1", default-features = false, features = ["derive", "alloc"] }
|
||||
|
||||
sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
sp-io = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
@@ -45,8 +44,22 @@ validator-sets-primitives = { package = "serai-validator-sets-primitives", path
|
||||
coins-pallet = { package = "serai-coins-pallet", path = "../../coins/pallet", default-features = false }
|
||||
dex-pallet = { package = "serai-dex-pallet", path = "../../dex/pallet", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
pallet-timestamp = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
sp-consensus-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
|
||||
|
||||
ciphersuite = { path = "../../../crypto/ciphersuite", features = ["ristretto"] }
|
||||
frost = { package = "modular-frost", path = "../../../crypto/frost", features = ["tests"] }
|
||||
schnorrkel = { path = "../../../crypto/schnorrkel", package = "frost-schnorrkel" }
|
||||
|
||||
zeroize = "^1.5"
|
||||
rand_core = "0.6"
|
||||
|
||||
[features]
|
||||
std = [
|
||||
"bitvec/std",
|
||||
|
||||
"scale/std",
|
||||
"scale-info/std",
|
||||
|
||||
@@ -58,12 +71,15 @@ std = [
|
||||
"sp-runtime/std",
|
||||
"sp-session/std",
|
||||
"sp-staking/std",
|
||||
|
||||
"sp-consensus-babe/std",
|
||||
|
||||
"frame-system/std",
|
||||
"frame-support/std",
|
||||
|
||||
"pallet-babe/std",
|
||||
"pallet-grandpa/std",
|
||||
"pallet-timestamp/std",
|
||||
|
||||
"serai-primitives/std",
|
||||
"validator-sets-primitives/std",
|
||||
@@ -72,8 +88,12 @@ std = [
|
||||
"dex-pallet/std",
|
||||
]
|
||||
|
||||
# TODO
|
||||
try-runtime = []
|
||||
try-runtime = [
|
||||
"frame-system/try-runtime",
|
||||
"frame-support/try-runtime",
|
||||
|
||||
"sp-runtime/try-runtime",
|
||||
]
|
||||
|
||||
runtime-benchmarks = [
|
||||
"frame-system/runtime-benchmarks",
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[cfg(test)]
|
||||
mod mock;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use scale::{Encode, Decode};
|
||||
@@ -83,6 +89,12 @@ pub mod pallet {
|
||||
type ShouldEndSession: ShouldEndSession<BlockNumberFor<Self>>;
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, serde::Serialize, serde::Deserialize)]
|
||||
pub struct AllEmbeddedEllipticCurveKeysAtGenesis {
|
||||
pub embedwards25519: BoundedVec<u8, ConstU32<{ MAX_KEY_LEN }>>,
|
||||
pub secq256k1: BoundedVec<u8, ConstU32<{ MAX_KEY_LEN }>>,
|
||||
}
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
@@ -92,7 +104,7 @@ pub mod pallet {
|
||||
/// This stake cannot be withdrawn however as there's no actual stake behind it.
|
||||
pub networks: Vec<(NetworkId, Amount)>,
|
||||
/// List of participants to place in the initial validator sets.
|
||||
pub participants: Vec<T::AccountId>,
|
||||
pub participants: Vec<(T::AccountId, AllEmbeddedEllipticCurveKeysAtGenesis)>,
|
||||
}
|
||||
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
@@ -135,7 +147,7 @@ pub mod pallet {
|
||||
_,
|
||||
Identity,
|
||||
NetworkId,
|
||||
BoundedVec<(Public, u64), ConstU32<{ MAX_KEY_SHARES_PER_SET }>>,
|
||||
BoundedVec<(Public, u64), ConstU32<{ MAX_KEY_SHARES_PER_SET_U32 }>>,
|
||||
OptionQuery,
|
||||
>;
|
||||
/// The validators selected to be in-set, regardless of if removed.
|
||||
@@ -191,6 +203,18 @@ pub mod pallet {
|
||||
}
|
||||
}
|
||||
|
||||
/// A key on an embedded elliptic curve.
|
||||
#[pallet::storage]
|
||||
pub type EmbeddedEllipticCurveKeys<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
Public,
|
||||
Identity,
|
||||
EmbeddedEllipticCurve,
|
||||
BoundedVec<u8, ConstU32<{ MAX_KEY_LEN }>>,
|
||||
OptionQuery,
|
||||
>;
|
||||
|
||||
/// The total stake allocated to this network by the active set of validators.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn total_allocated_stake)]
|
||||
@@ -303,6 +327,7 @@ pub mod pallet {
|
||||
|
||||
/// Pending deallocations, keyed by the Session they become unlocked on.
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn pending_deallocations)]
|
||||
type PendingDeallocations<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
@@ -386,23 +411,25 @@ pub mod pallet {
|
||||
|
||||
// Clear the current InSet
|
||||
assert_eq!(
|
||||
InSet::<T>::clear_prefix(network, MAX_KEY_SHARES_PER_SET, None).maybe_cursor,
|
||||
InSet::<T>::clear_prefix(network, MAX_KEY_SHARES_PER_SET_U32, None).maybe_cursor,
|
||||
None
|
||||
);
|
||||
|
||||
let allocation_per_key_share = Self::allocation_per_key_share(network).unwrap().0;
|
||||
|
||||
let mut participants = vec![];
|
||||
let mut total_allocated_stake = 0;
|
||||
{
|
||||
let mut iter = SortedAllocationsIter::<T>::new(network);
|
||||
let mut key_shares = 0;
|
||||
while key_shares < u64::from(MAX_KEY_SHARES_PER_SET) {
|
||||
while key_shares < u64::from(MAX_KEY_SHARES_PER_SET_U32) {
|
||||
let Some((key, amount)) = iter.next() else { break };
|
||||
|
||||
let these_key_shares =
|
||||
(amount.0 / allocation_per_key_share).min(u64::from(MAX_KEY_SHARES_PER_SET));
|
||||
(amount.0 / allocation_per_key_share).min(u64::from(MAX_KEY_SHARES_PER_SET_U32));
|
||||
participants.push((key, these_key_shares));
|
||||
|
||||
total_allocated_stake += amount.0;
|
||||
key_shares += these_key_shares;
|
||||
}
|
||||
amortize_excess_key_shares(&mut participants);
|
||||
@@ -415,6 +442,12 @@ pub mod pallet {
|
||||
let set = ValidatorSet { network, session };
|
||||
Pallet::<T>::deposit_event(Event::NewSet { set });
|
||||
|
||||
// other networks set their Session(0) TAS once they set their keys but serai network
|
||||
// doesn't have that so we set it here.
|
||||
if network == NetworkId::Serai && session == Session(0) {
|
||||
TotalAllocatedStake::<T>::set(network, Some(Amount(total_allocated_stake)));
|
||||
}
|
||||
|
||||
Participants::<T>::set(network, Some(participants.try_into().unwrap()));
|
||||
SessionBeginBlock::<T>::set(
|
||||
network,
|
||||
@@ -428,6 +461,14 @@ pub mod pallet {
|
||||
pub enum Error<T> {
|
||||
/// 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.
|
||||
@@ -471,10 +512,20 @@ pub mod pallet {
|
||||
fn build(&self) {
|
||||
for (id, stake) in self.networks.clone() {
|
||||
AllocationPerKeyShare::<T>::set(id, Some(stake));
|
||||
for participant in self.participants.clone() {
|
||||
if Pallet::<T>::set_allocation(id, participant, stake) {
|
||||
for participant in &self.participants {
|
||||
if Pallet::<T>::set_allocation(id, participant.0, stake) {
|
||||
panic!("participants contained duplicates");
|
||||
}
|
||||
EmbeddedEllipticCurveKeys::<T>::set(
|
||||
participant.0,
|
||||
EmbeddedEllipticCurve::Embedwards25519,
|
||||
Some(participant.1.embedwards25519.clone()),
|
||||
);
|
||||
EmbeddedEllipticCurveKeys::<T>::set(
|
||||
participant.0,
|
||||
EmbeddedEllipticCurve::Secq256k1,
|
||||
Some(participant.1.secq256k1.clone()),
|
||||
);
|
||||
}
|
||||
Pallet::<T>::new_set(id);
|
||||
}
|
||||
@@ -501,7 +552,7 @@ pub mod pallet {
|
||||
top = Some(key_shares);
|
||||
}
|
||||
|
||||
if key_shares > u64::from(MAX_KEY_SHARES_PER_SET) {
|
||||
if key_shares > u64::from(MAX_KEY_SHARES_PER_SET_U32) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -513,7 +564,7 @@ pub mod pallet {
|
||||
// post_amortization_key_shares_for_top_validator yields what the top validator's key shares
|
||||
// would be after such a reduction, letting us evaluate this correctly
|
||||
let top = post_amortization_key_shares_for_top_validator(validators_len, top, key_shares);
|
||||
(top * 3) < key_shares.min(MAX_KEY_SHARES_PER_SET.into())
|
||||
(top * 3) < key_shares.min(MAX_KEY_SHARES_PER_SET_U32.into())
|
||||
}
|
||||
|
||||
fn increase_allocation(
|
||||
@@ -552,7 +603,7 @@ pub mod pallet {
|
||||
|
||||
// The above is_bft calls are only used to check a BFT net doesn't become non-BFT
|
||||
// Check here if this call would prevent a non-BFT net from *ever* becoming BFT
|
||||
if (new_allocation / allocation_per_key_share) >= (MAX_KEY_SHARES_PER_SET / 3).into() {
|
||||
if (new_allocation / allocation_per_key_share) >= (MAX_KEY_SHARES_PER_SET_U32 / 3).into() {
|
||||
Err(Error::<T>::AllocationWouldPreventFaultTolerance)?;
|
||||
}
|
||||
|
||||
@@ -622,7 +673,7 @@ pub mod pallet {
|
||||
// If we're not removing the entire allocation, yet the allocation is no longer at or above
|
||||
// the threshold for a key share, error
|
||||
let allocation_per_key_share = Self::allocation_per_key_share(network).unwrap().0;
|
||||
if (new_allocation != 0) && (new_allocation < allocation_per_key_share) {
|
||||
if (new_allocation > 0) && (new_allocation < allocation_per_key_share) {
|
||||
Err(Error::<T>::DeallocationWouldRemoveParticipant)?;
|
||||
}
|
||||
|
||||
@@ -783,7 +834,7 @@ pub mod pallet {
|
||||
PendingDeallocations::<T>::take((network, key), session)
|
||||
}
|
||||
|
||||
fn rotate_session() {
|
||||
pub(crate) fn rotate_session() {
|
||||
// next serai validators that is in the queue.
|
||||
let now_validators = Participants::<T>::get(NetworkId::Serai)
|
||||
.expect("no Serai participants upon rotate_session");
|
||||
@@ -960,14 +1011,15 @@ pub mod pallet {
|
||||
pub fn set_keys(
|
||||
origin: OriginFor<T>,
|
||||
network: ExternalNetworkId,
|
||||
removed_participants: BoundedVec<Public, ConstU32<{ MAX_KEY_SHARES_PER_SET / 3 }>>,
|
||||
key_pair: KeyPair,
|
||||
signature_participants: bitvec::vec::BitVec<u8, bitvec::order::Lsb0>,
|
||||
signature: Signature,
|
||||
) -> DispatchResult {
|
||||
ensure_none(origin)?;
|
||||
|
||||
// signature isn't checked as this is an unsigned transaction, and validate_unsigned
|
||||
// (called by pre_dispatch) checks it
|
||||
let _ = signature_participants;
|
||||
let _ = signature;
|
||||
|
||||
let session = Self::session(NetworkId::from(network)).unwrap();
|
||||
@@ -982,15 +1034,6 @@ pub mod pallet {
|
||||
Self::set_total_allocated_stake(NetworkId::from(network));
|
||||
}
|
||||
|
||||
// This does not remove from TotalAllocatedStake or InSet in order to:
|
||||
// 1) Not decrease the stake present in this set. This means removed participants are
|
||||
// still liable for the economic security of the external network. This prevents
|
||||
// a decided set, which is economically secure, from falling below the threshold.
|
||||
// 2) Not allow parties removed to immediately deallocate, per commentary on deallocation
|
||||
// scheduling (https://github.com/serai-dex/serai/issues/394).
|
||||
for removed in removed_participants {
|
||||
Self::deposit_event(Event::ParticipantRemoved { set: set.into(), removed });
|
||||
}
|
||||
Self::deposit_event(Event::KeyGen { set, key_pair });
|
||||
|
||||
Ok(())
|
||||
@@ -1001,7 +1044,7 @@ pub mod pallet {
|
||||
pub fn report_slashes(
|
||||
origin: OriginFor<T>,
|
||||
network: ExternalNetworkId,
|
||||
slashes: BoundedVec<(Public, u32), ConstU32<{ MAX_KEY_SHARES_PER_SET / 3 }>>,
|
||||
slashes: SlashReport,
|
||||
signature: Signature,
|
||||
) -> DispatchResult {
|
||||
ensure_none(origin)?;
|
||||
@@ -1026,8 +1069,42 @@ pub mod pallet {
|
||||
|
||||
#[pallet::call_index(2)]
|
||||
#[pallet::weight(0)] // TODO
|
||||
pub fn set_embedded_elliptic_curve_key(
|
||||
origin: OriginFor<T>,
|
||||
embedded_elliptic_curve: EmbeddedEllipticCurve,
|
||||
key: BoundedVec<u8, ConstU32<{ MAX_KEY_LEN }>>,
|
||||
) -> DispatchResult {
|
||||
let validator = ensure_signed(origin)?;
|
||||
|
||||
// We don't have the curve formulas, nor the BigInt arithmetic, necessary here to validate
|
||||
// these keys. Instead, we solely check the key lengths. Validators are responsible to not
|
||||
// provide invalid keys.
|
||||
let expected_len = match embedded_elliptic_curve {
|
||||
EmbeddedEllipticCurve::Embedwards25519 => 32,
|
||||
EmbeddedEllipticCurve::Secq256k1 => 33,
|
||||
};
|
||||
if key.len() != expected_len {
|
||||
Err(Error::<T>::InvalidEmbeddedEllipticCurveKey)?;
|
||||
}
|
||||
|
||||
// This does allow overwriting an existing key which... is unlikely to be done?
|
||||
// Yet it isn't an issue as we'll fix to the key as of any set's declaration (uncaring to if
|
||||
// it's distinct at the latest block)
|
||||
EmbeddedEllipticCurveKeys::<T>::set(validator, embedded_elliptic_curve, Some(key));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(3)]
|
||||
#[pallet::weight(0)] // TODO
|
||||
pub fn allocate(origin: OriginFor<T>, 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)
|
||||
for embedded_elliptic_curve in network.embedded_elliptic_curves() {
|
||||
if !EmbeddedEllipticCurveKeys::<T>::contains_key(validator, *embedded_elliptic_curve) {
|
||||
Err(Error::<T>::MissingEmbeddedEllipticCurveKey)?;
|
||||
}
|
||||
}
|
||||
Coins::<T>::transfer_internal(
|
||||
validator,
|
||||
Self::account(),
|
||||
@@ -1036,7 +1113,7 @@ pub mod pallet {
|
||||
Self::increase_allocation(network, validator, amount, false)
|
||||
}
|
||||
|
||||
#[pallet::call_index(3)]
|
||||
#[pallet::call_index(4)]
|
||||
#[pallet::weight(0)] // TODO
|
||||
pub fn deallocate(origin: OriginFor<T>, network: NetworkId, amount: Amount) -> DispatchResult {
|
||||
let account = ensure_signed(origin)?;
|
||||
@@ -1053,7 +1130,7 @@ pub mod pallet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pallet::call_index(4)]
|
||||
#[pallet::call_index(5)]
|
||||
#[pallet::weight((0, DispatchClass::Operational))] // TODO
|
||||
pub fn claim_deallocation(
|
||||
origin: OriginFor<T>,
|
||||
@@ -1081,7 +1158,7 @@ pub mod pallet {
|
||||
fn validate_unsigned(_: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||
// Match to be exhaustive
|
||||
match call {
|
||||
Call::set_keys { network, ref removed_participants, ref key_pair, ref signature } => {
|
||||
Call::set_keys { network, ref key_pair, ref signature_participants, ref signature } => {
|
||||
let network = *network;
|
||||
|
||||
// Confirm this set has a session
|
||||
@@ -1100,30 +1177,24 @@ pub mod pallet {
|
||||
// session on this assumption
|
||||
assert_eq!(Pallet::<T>::latest_decided_session(network.into()), Some(current_session));
|
||||
|
||||
// This does not slash the removed participants as that'll be done at the end of the
|
||||
// set's lifetime
|
||||
let mut removed = hashbrown::HashSet::new();
|
||||
for participant in removed_participants {
|
||||
// Confirm this wasn't duplicated
|
||||
if removed.contains(&participant.0) {
|
||||
Err(InvalidTransaction::Custom(2))?;
|
||||
}
|
||||
removed.insert(participant.0);
|
||||
}
|
||||
|
||||
let participants = Participants::<T>::get(NetworkId::from(network))
|
||||
.expect("session existed without participants");
|
||||
|
||||
// Check the bitvec is of the proper length
|
||||
if participants.len() != signature_participants.len() {
|
||||
Err(InvalidTransaction::Custom(2))?;
|
||||
}
|
||||
|
||||
let mut all_key_shares = 0;
|
||||
let mut signers = vec![];
|
||||
let mut signing_key_shares = 0;
|
||||
for participant in participants {
|
||||
for (participant, in_use) in participants.into_iter().zip(signature_participants) {
|
||||
let participant = participant.0;
|
||||
let shares = InSet::<T>::get(NetworkId::from(network), participant)
|
||||
.expect("participant from Participants wasn't InSet");
|
||||
all_key_shares += shares;
|
||||
|
||||
if removed.contains(&participant.0) {
|
||||
if !in_use {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1141,9 +1212,7 @@ pub mod pallet {
|
||||
// Verify the signature with the MuSig key of the signers
|
||||
// We theoretically don't need set_keys_message to bind to removed_participants, as the
|
||||
// key we're signing with effectively already does so, yet there's no reason not to
|
||||
if !musig_key(set.into(), &signers)
|
||||
.verify(&set_keys_message(&set, removed_participants, key_pair), signature)
|
||||
{
|
||||
if !musig_key(set.into(), &signers).verify(&set_keys_message(&set, key_pair), signature) {
|
||||
Err(InvalidTransaction::BadProof)?;
|
||||
}
|
||||
|
||||
@@ -1165,19 +1234,20 @@ pub mod pallet {
|
||||
network,
|
||||
session: Session(Self::session(NetworkId::from(network)).unwrap().0 - 1),
|
||||
};
|
||||
if !key.verify(&report_slashes_message(&set, slashes), signature) {
|
||||
if !key.verify(&slashes.report_slashes_message(), signature) {
|
||||
Err(InvalidTransaction::BadProof)?;
|
||||
}
|
||||
|
||||
ValidTransaction::with_tag_prefix("ValidatorSets")
|
||||
.and_provides((1, set))
|
||||
.longevity(MAX_KEY_SHARES_PER_SET.into())
|
||||
.longevity(MAX_KEY_SHARES_PER_SET_U32.into())
|
||||
.propagate(true)
|
||||
.build()
|
||||
}
|
||||
Call::allocate { .. } | Call::deallocate { .. } | Call::claim_deallocation { .. } => {
|
||||
Err(InvalidTransaction::Call)?
|
||||
}
|
||||
Call::set_embedded_elliptic_curve_key { .. } |
|
||||
Call::allocate { .. } |
|
||||
Call::deallocate { .. } |
|
||||
Call::claim_deallocation { .. } => Err(InvalidTransaction::Call)?,
|
||||
Call::__Ignore(_, _) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
210
substrate/validator-sets/pallet/src/mock.rs
Normal file
210
substrate/validator-sets/pallet/src/mock.rs
Normal file
@@ -0,0 +1,210 @@
|
||||
//! Test environment for ValidatorSets pallet.
|
||||
|
||||
use super::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use frame_support::{
|
||||
construct_runtime,
|
||||
traits::{ConstU16, ConstU32, ConstU64},
|
||||
};
|
||||
|
||||
use sp_core::{
|
||||
H256, Pair as PairTrait,
|
||||
sr25519::{Public, Pair},
|
||||
};
|
||||
use sp_runtime::{
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
BuildStorage,
|
||||
};
|
||||
|
||||
use serai_primitives::*;
|
||||
use validator_sets::{primitives::MAX_KEY_SHARES_PER_SET, MembershipProof};
|
||||
|
||||
pub use crate as validator_sets;
|
||||
pub use coins_pallet as coins;
|
||||
pub use dex_pallet as dex;
|
||||
pub use pallet_babe as babe;
|
||||
pub use pallet_grandpa as grandpa;
|
||||
pub use pallet_timestamp as timestamp;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
// Maximum number of authorities per session.
|
||||
pub type MaxAuthorities = ConstU32<{ MAX_KEY_SHARES_PER_SET }>;
|
||||
|
||||
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
|
||||
pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
|
||||
sp_consensus_babe::BabeEpochConfiguration {
|
||||
c: PRIMARY_PROBABILITY,
|
||||
allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryPlainSlots,
|
||||
};
|
||||
|
||||
pub const MEDIAN_PRICE_WINDOW_LENGTH: u16 = 10;
|
||||
|
||||
construct_runtime!(
|
||||
pub enum Test
|
||||
{
|
||||
System: frame_system,
|
||||
Timestamp: timestamp,
|
||||
Coins: coins,
|
||||
LiquidityTokens: coins::<Instance1>::{Pallet, Call, Storage, Event<T>},
|
||||
ValidatorSets: validator_sets,
|
||||
Dex: dex,
|
||||
Babe: babe,
|
||||
Grandpa: grandpa,
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_system::Config for Test {
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type Nonce = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = Public;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Block = Block;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type BlockHashCount = ConstU64<250>;
|
||||
type DbWeight = ();
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
|
||||
impl timestamp::Config for Test {
|
||||
type Moment = u64;
|
||||
type OnTimestampSet = Babe;
|
||||
type MinimumPeriod = ConstU64<{ (TARGET_BLOCK_TIME * 1000) / 2 }>;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
impl babe::Config for Test {
|
||||
type EpochDuration = ConstU64<{ FAST_EPOCH_DURATION }>;
|
||||
|
||||
type ExpectedBlockTime = ConstU64<{ TARGET_BLOCK_TIME * 1000 }>;
|
||||
type EpochChangeTrigger = babe::ExternalTrigger;
|
||||
type DisabledValidators = ValidatorSets;
|
||||
|
||||
type WeightInfo = ();
|
||||
type MaxAuthorities = MaxAuthorities;
|
||||
|
||||
type KeyOwnerProof = MembershipProof<Self>;
|
||||
type EquivocationReportSystem = ();
|
||||
}
|
||||
|
||||
impl grandpa::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
|
||||
type WeightInfo = ();
|
||||
type MaxAuthorities = MaxAuthorities;
|
||||
|
||||
type MaxSetIdSessionEntries = ConstU64<0>;
|
||||
type KeyOwnerProof = MembershipProof<Self>;
|
||||
type EquivocationReportSystem = ();
|
||||
}
|
||||
|
||||
impl coins::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type AllowMint = ValidatorSets;
|
||||
}
|
||||
|
||||
impl coins::Config<coins::Instance1> for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type AllowMint = ();
|
||||
}
|
||||
|
||||
impl dex::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
|
||||
type LPFee = ConstU32<3>; // 0.3%
|
||||
type MintMinLiquidity = ConstU64<10000>;
|
||||
|
||||
type MaxSwapPathLength = ConstU32<3>; // coin1 -> SRI -> coin2
|
||||
|
||||
type MedianPriceWindowLength = ConstU16<{ MEDIAN_PRICE_WINDOW_LENGTH }>;
|
||||
|
||||
type WeightInfo = dex::weights::SubstrateWeight<Test>;
|
||||
}
|
||||
|
||||
impl Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type ShouldEndSession = Babe;
|
||||
}
|
||||
|
||||
// For a const we can't define
|
||||
pub fn genesis_participants() -> Vec<Pair> {
|
||||
vec![
|
||||
insecure_pair_from_name("Alice"),
|
||||
insecure_pair_from_name("Bob"),
|
||||
insecure_pair_from_name("Charlie"),
|
||||
insecure_pair_from_name("Dave"),
|
||||
]
|
||||
}
|
||||
|
||||
// Amounts for single key share per network
|
||||
pub fn key_shares() -> HashMap<NetworkId, Amount> {
|
||||
HashMap::from([
|
||||
(NetworkId::Serai, Amount(50_000 * 10_u64.pow(8))),
|
||||
(NetworkId::External(ExternalNetworkId::Bitcoin), Amount(1_000_000 * 10_u64.pow(8))),
|
||||
(NetworkId::External(ExternalNetworkId::Ethereum), Amount(1_000_000 * 10_u64.pow(8))),
|
||||
(NetworkId::External(ExternalNetworkId::Monero), Amount(100_000 * 10_u64.pow(8))),
|
||||
])
|
||||
}
|
||||
|
||||
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||
let networks: Vec<(NetworkId, Amount)> = key_shares().into_iter().collect::<Vec<_>>();
|
||||
|
||||
coins::GenesisConfig::<Test> {
|
||||
accounts: genesis_participants()
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|a| (a.public(), Balance { coin: Coin::Serai, amount: Amount(1 << 60) }))
|
||||
.collect(),
|
||||
_ignore: Default::default(),
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
validator_sets::GenesisConfig::<Test> {
|
||||
networks,
|
||||
participants: genesis_participants().into_iter().map(|p| p.public()).collect(),
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
babe::GenesisConfig::<Test> {
|
||||
authorities: genesis_participants()
|
||||
.into_iter()
|
||||
.map(|validator| (validator.public().into(), 1))
|
||||
.collect(),
|
||||
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
|
||||
_config: PhantomData,
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
grandpa::GenesisConfig::<Test> {
|
||||
authorities: genesis_participants()
|
||||
.into_iter()
|
||||
.map(|validator| (validator.public().into(), 1))
|
||||
.collect(),
|
||||
_config: PhantomData,
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(|| System::set_block_number(0));
|
||||
ext
|
||||
}
|
||||
561
substrate/validator-sets/pallet/src/tests.rs
Normal file
561
substrate/validator-sets/pallet/src/tests.rs
Normal file
@@ -0,0 +1,561 @@
|
||||
use crate::{mock::*, primitives::*};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ciphersuite::{Ciphersuite, Ristretto};
|
||||
use frost::dkg::musig::musig;
|
||||
use schnorrkel::Schnorrkel;
|
||||
|
||||
use zeroize::Zeroizing;
|
||||
use rand_core::OsRng;
|
||||
|
||||
use frame_support::{
|
||||
assert_noop, assert_ok,
|
||||
pallet_prelude::{InvalidTransaction, TransactionSource},
|
||||
traits::{OnFinalize, OnInitialize},
|
||||
};
|
||||
use frame_system::RawOrigin;
|
||||
|
||||
use sp_core::{
|
||||
sr25519::{Public, Pair, Signature},
|
||||
Pair as PairTrait,
|
||||
};
|
||||
use sp_runtime::{traits::ValidateUnsigned, BoundedVec};
|
||||
|
||||
use serai_primitives::*;
|
||||
|
||||
fn active_network_validators(network: NetworkId) -> Vec<(Public, u64)> {
|
||||
if network == NetworkId::Serai {
|
||||
Babe::authorities().into_iter().map(|(id, key_share)| (id.into_inner(), key_share)).collect()
|
||||
} else {
|
||||
ValidatorSets::participants_for_latest_decided_set(network).unwrap().into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_session_and_active_validators(network: NetworkId, participants: &[Public], session: u32) {
|
||||
let mut validators: Vec<Public> = active_network_validators(network)
|
||||
.into_iter()
|
||||
.map(|(p, ks)| {
|
||||
assert_eq!(ks, 1);
|
||||
p
|
||||
})
|
||||
.collect();
|
||||
validators.sort();
|
||||
|
||||
assert_eq!(ValidatorSets::session(network).unwrap(), Session(session));
|
||||
assert_eq!(participants, validators);
|
||||
|
||||
// TODO: how to make sure block finalizations work as usual here?
|
||||
}
|
||||
|
||||
fn get_session_at_which_changes_activate(network: NetworkId) -> u32 {
|
||||
let current_session = ValidatorSets::session(network).unwrap().0;
|
||||
// changes should be active in the next session
|
||||
if network == NetworkId::Serai {
|
||||
// it takes 1 extra session for serai net to make the changes active.
|
||||
current_session + 2
|
||||
} else {
|
||||
current_session + 1
|
||||
}
|
||||
}
|
||||
|
||||
fn set_keys_for_session(network: ExternalNetworkId) {
|
||||
ValidatorSets::set_keys(
|
||||
RawOrigin::None.into(),
|
||||
network,
|
||||
BoundedVec::new(),
|
||||
KeyPair(insecure_pair_from_name("Alice").public(), vec![].try_into().unwrap()),
|
||||
Signature([0u8; 64]),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn set_keys_signature(set: &ExternalValidatorSet, key_pair: &KeyPair, pairs: &[Pair]) -> Signature {
|
||||
let mut pub_keys = vec![];
|
||||
for pair in pairs {
|
||||
let public_key =
|
||||
<Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut pair.public().0.as_ref()).unwrap();
|
||||
pub_keys.push(public_key);
|
||||
}
|
||||
|
||||
let mut threshold_keys = vec![];
|
||||
for i in 0 .. pairs.len() {
|
||||
let secret_key = <Ristretto as Ciphersuite>::read_F::<&[u8]>(
|
||||
&mut pairs[i].as_ref().secret.to_bytes()[.. 32].as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(Ristretto::generator() * secret_key, pub_keys[i]);
|
||||
|
||||
threshold_keys.push(
|
||||
musig::<Ristretto>(&musig_context((*set).into()), &Zeroizing::new(secret_key), &pub_keys)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut musig_keys = HashMap::new();
|
||||
for tk in threshold_keys {
|
||||
musig_keys.insert(tk.params().i(), tk.into());
|
||||
}
|
||||
|
||||
let sig = frost::tests::sign_without_caching(
|
||||
&mut OsRng,
|
||||
frost::tests::algorithm_machines(&mut OsRng, &Schnorrkel::new(b"substrate"), &musig_keys),
|
||||
&set_keys_message(set, &[], key_pair),
|
||||
);
|
||||
|
||||
Signature(sig.to_bytes())
|
||||
}
|
||||
|
||||
fn get_ordered_keys(network: NetworkId, participants: &[Pair]) -> Vec<Pair> {
|
||||
// retrieve the current session validators so that we know the order of the keys
|
||||
// that is necessary for the correct musig signature.
|
||||
let validators = ValidatorSets::participants_for_latest_decided_set(network).unwrap();
|
||||
|
||||
// collect the pairs of the validators
|
||||
let mut pairs = vec![];
|
||||
for (v, _) in validators {
|
||||
let p = participants.iter().find(|pair| pair.public() == v).unwrap().clone();
|
||||
pairs.push(p);
|
||||
}
|
||||
|
||||
pairs
|
||||
}
|
||||
|
||||
fn rotate_session_until(network: NetworkId, session: u32) {
|
||||
let mut current = ValidatorSets::session(network).unwrap().0;
|
||||
while current < session {
|
||||
Babe::on_initialize(System::block_number() + 1);
|
||||
ValidatorSets::rotate_session();
|
||||
if let NetworkId::External(n) = network {
|
||||
set_keys_for_session(n);
|
||||
}
|
||||
ValidatorSets::retire_set(ValidatorSet { session: Session(current), network });
|
||||
current += 1;
|
||||
}
|
||||
assert_eq!(current, session);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotate_session() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let genesis_participants: Vec<Public> =
|
||||
genesis_participants().into_iter().map(|p| p.public()).collect();
|
||||
let key_shares = key_shares();
|
||||
|
||||
let mut participants = HashMap::from([
|
||||
(NetworkId::Serai, genesis_participants.clone()),
|
||||
(NetworkId::External(ExternalNetworkId::Bitcoin), genesis_participants.clone()),
|
||||
(NetworkId::External(ExternalNetworkId::Ethereum), genesis_participants.clone()),
|
||||
(NetworkId::External(ExternalNetworkId::Monero), genesis_participants),
|
||||
]);
|
||||
|
||||
// rotate session
|
||||
for network in NETWORKS {
|
||||
let participants = participants.get_mut(&network).unwrap();
|
||||
|
||||
// verify for session 0
|
||||
participants.sort();
|
||||
if let NetworkId::External(n) = network {
|
||||
set_keys_for_session(n);
|
||||
}
|
||||
verify_session_and_active_validators(network, participants, 0);
|
||||
|
||||
// add 1 participant
|
||||
let new_participant = insecure_pair_from_name("new-guy").public();
|
||||
Coins::mint(new_participant, Balance { coin: Coin::Serai, amount: key_shares[&network] })
|
||||
.unwrap();
|
||||
ValidatorSets::allocate(
|
||||
RawOrigin::Signed(new_participant).into(),
|
||||
network,
|
||||
key_shares[&network],
|
||||
)
|
||||
.unwrap();
|
||||
participants.push(new_participant);
|
||||
|
||||
// move network to the activation session
|
||||
let activation_session = get_session_at_which_changes_activate(network);
|
||||
rotate_session_until(network, activation_session);
|
||||
|
||||
// verify
|
||||
participants.sort();
|
||||
verify_session_and_active_validators(network, participants, activation_session);
|
||||
|
||||
// remove 1 participant
|
||||
let participant_to_remove = participants[0];
|
||||
ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant_to_remove).into(),
|
||||
network,
|
||||
key_shares[&network],
|
||||
)
|
||||
.unwrap();
|
||||
participants
|
||||
.swap_remove(participants.iter().position(|k| *k == participant_to_remove).unwrap());
|
||||
|
||||
// check pending deallocations
|
||||
let pending = ValidatorSets::pending_deallocations(
|
||||
(network, participant_to_remove),
|
||||
Session(if network == NetworkId::Serai {
|
||||
activation_session + 3
|
||||
} else {
|
||||
activation_session + 2
|
||||
}),
|
||||
);
|
||||
assert_eq!(pending, Some(key_shares[&network]));
|
||||
|
||||
// move network to the activation session
|
||||
let activation_session = get_session_at_which_changes_activate(network);
|
||||
rotate_session_until(network, activation_session);
|
||||
|
||||
// verify
|
||||
participants.sort();
|
||||
verify_session_and_active_validators(network, participants, activation_session);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allocate() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let genesis_participants: Vec<Public> =
|
||||
genesis_participants().into_iter().map(|p| p.public()).collect();
|
||||
let key_shares = key_shares();
|
||||
let participant = insecure_pair_from_name("random1").public();
|
||||
let network = NetworkId::External(ExternalNetworkId::Ethereum);
|
||||
|
||||
// check genesis TAS
|
||||
set_keys_for_session(network.try_into().unwrap());
|
||||
assert_eq!(
|
||||
ValidatorSets::total_allocated_stake(network).unwrap().0,
|
||||
key_shares[&network].0 * u64::try_from(genesis_participants.len()).unwrap()
|
||||
);
|
||||
|
||||
// we can't allocate less than a key share
|
||||
let amount = Amount(key_shares[&network].0 * 3);
|
||||
Coins::mint(participant, Balance { coin: Coin::Serai, amount }).unwrap();
|
||||
assert_noop!(
|
||||
ValidatorSets::allocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
Amount(key_shares[&network].0 - 1)
|
||||
),
|
||||
validator_sets::Error::<Test>::InsufficientAllocation
|
||||
);
|
||||
|
||||
// we can't allocate too much that the net exhibits the ability to handle any single node
|
||||
// becoming byzantine
|
||||
assert_noop!(
|
||||
ValidatorSets::allocate(RawOrigin::Signed(participant).into(), network, amount),
|
||||
validator_sets::Error::<Test>::AllocationWouldRemoveFaultTolerance
|
||||
);
|
||||
|
||||
// we should be allocate a proper amount
|
||||
assert_ok!(ValidatorSets::allocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
assert_eq!(Coins::balance(participant, Coin::Serai).0, amount.0 - key_shares[&network].0);
|
||||
|
||||
// check new amount is reflected on TAS on new session
|
||||
rotate_session_until(network, 1);
|
||||
assert_eq!(
|
||||
ValidatorSets::total_allocated_stake(network).unwrap().0,
|
||||
key_shares[&network].0 * (u64::try_from(genesis_participants.len()).unwrap() + 1)
|
||||
);
|
||||
|
||||
// check that new participants match
|
||||
let mut active_participants: Vec<Public> =
|
||||
active_network_validators(network).into_iter().map(|(p, _)| p).collect();
|
||||
|
||||
let mut current_participants = genesis_participants.clone();
|
||||
current_participants.push(participant);
|
||||
|
||||
current_participants.sort();
|
||||
active_participants.sort();
|
||||
assert_eq!(current_participants, active_participants);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deallocate_pending() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let genesis_participants: Vec<Public> =
|
||||
genesis_participants().into_iter().map(|p| p.public()).collect();
|
||||
let key_shares = key_shares();
|
||||
let participant = insecure_pair_from_name("random1").public();
|
||||
let network = NetworkId::External(ExternalNetworkId::Bitcoin);
|
||||
|
||||
// check genesis TAS
|
||||
set_keys_for_session(network.try_into().unwrap());
|
||||
assert_eq!(
|
||||
ValidatorSets::total_allocated_stake(network).unwrap().0,
|
||||
key_shares[&network].0 * u64::try_from(genesis_participants.len()).unwrap()
|
||||
);
|
||||
|
||||
// allocate some amount
|
||||
Coins::mint(participant, Balance { coin: Coin::Serai, amount: key_shares[&network] }).unwrap();
|
||||
assert_ok!(ValidatorSets::allocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
assert_eq!(Coins::balance(participant, Coin::Serai).0, 0);
|
||||
|
||||
// move to next session
|
||||
let mut current_session = ValidatorSets::session(network).unwrap().0;
|
||||
current_session += 1;
|
||||
rotate_session_until(network, current_session);
|
||||
assert_eq!(
|
||||
ValidatorSets::total_allocated_stake(network).unwrap().0,
|
||||
key_shares[&network].0 * (u64::try_from(genesis_participants.len()).unwrap() + 1)
|
||||
);
|
||||
|
||||
// we can deallocate all of our allocation
|
||||
assert_ok!(ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
|
||||
// check pending deallocations
|
||||
let pending_session =
|
||||
if network == NetworkId::Serai { current_session + 3 } else { current_session + 2 };
|
||||
assert_eq!(
|
||||
ValidatorSets::pending_deallocations((network, participant), Session(pending_session)),
|
||||
Some(key_shares[&network])
|
||||
);
|
||||
|
||||
// we can't claim it immediately
|
||||
assert_noop!(
|
||||
ValidatorSets::claim_deallocation(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
Session(pending_session),
|
||||
),
|
||||
validator_sets::Error::<Test>::NonExistentDeallocation
|
||||
);
|
||||
|
||||
// we should be able to claim it in the pending session
|
||||
rotate_session_until(network, pending_session);
|
||||
assert_ok!(ValidatorSets::claim_deallocation(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
Session(pending_session),
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deallocate_immediately() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let genesis_participants: Vec<Public> =
|
||||
genesis_participants().into_iter().map(|p| p.public()).collect();
|
||||
let key_shares = key_shares();
|
||||
let participant = insecure_pair_from_name("random1").public();
|
||||
let network = NetworkId::External(ExternalNetworkId::Monero);
|
||||
|
||||
// check genesis TAS
|
||||
set_keys_for_session(network.try_into().unwrap());
|
||||
assert_eq!(
|
||||
ValidatorSets::total_allocated_stake(network).unwrap().0,
|
||||
key_shares[&network].0 * u64::try_from(genesis_participants.len()).unwrap()
|
||||
);
|
||||
|
||||
// we can't deallocate when we don't have an allocation
|
||||
assert_noop!(
|
||||
ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
),
|
||||
validator_sets::Error::<Test>::NonExistentValidator
|
||||
);
|
||||
|
||||
// allocate some amount
|
||||
Coins::mint(participant, Balance { coin: Coin::Serai, amount: key_shares[&network] }).unwrap();
|
||||
assert_ok!(ValidatorSets::allocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
assert_eq!(Coins::balance(participant, Coin::Serai).0, 0);
|
||||
|
||||
// we can't deallocate more than our allocation
|
||||
assert_noop!(
|
||||
ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
Amount(key_shares[&network].0 + 1)
|
||||
),
|
||||
validator_sets::Error::<Test>::NotEnoughAllocated
|
||||
);
|
||||
|
||||
// we can't deallocate an amount that would left us less than a key share as long as it isn't 0
|
||||
assert_noop!(
|
||||
ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
Amount(key_shares[&network].0 / 2)
|
||||
),
|
||||
validator_sets::Error::<Test>::DeallocationWouldRemoveParticipant
|
||||
);
|
||||
|
||||
// we can deallocate all of our allocation
|
||||
assert_ok!(ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
|
||||
// It should be immediately deallocated since we are not yet in an active set
|
||||
assert_eq!(Coins::balance(participant, Coin::Serai), key_shares[&network]);
|
||||
assert!(ValidatorSets::pending_deallocations((network, participant), Session(1)).is_none());
|
||||
|
||||
// allocate again
|
||||
assert_ok!(ValidatorSets::allocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
));
|
||||
assert_eq!(Coins::balance(participant, Coin::Serai).0, 0);
|
||||
|
||||
// make a pool so that we have security oracle value for the coin
|
||||
let liq_acc = insecure_pair_from_name("liq-acc").public();
|
||||
let coin = ExternalCoin::Monero;
|
||||
let balance = ExternalBalance { coin, amount: Amount(2 * key_shares[&network].0) };
|
||||
Coins::mint(liq_acc, balance.into()).unwrap();
|
||||
Coins::mint(liq_acc, Balance { coin: Coin::Serai, amount: balance.amount }).unwrap();
|
||||
Dex::add_liquidity(
|
||||
RawOrigin::Signed(liq_acc).into(),
|
||||
coin,
|
||||
balance.amount.0 / 2,
|
||||
balance.amount.0 / 2,
|
||||
1,
|
||||
1,
|
||||
liq_acc,
|
||||
)
|
||||
.unwrap();
|
||||
Dex::on_finalize(1);
|
||||
assert!(Dex::security_oracle_value(coin).unwrap().0 > 0);
|
||||
|
||||
// we can't deallocate if it would break economic security
|
||||
// The reason we don't have economic security for the network now is that we just set
|
||||
// the value for coin/SRI to 1:1 when making the pool and we minted 2 * key_share amount
|
||||
// of coin but we only allocated 1 key_share of SRI for the network although we need more than
|
||||
// 3 for the same amount of coin.
|
||||
assert_noop!(
|
||||
ValidatorSets::deallocate(
|
||||
RawOrigin::Signed(participant).into(),
|
||||
network,
|
||||
key_shares[&network]
|
||||
),
|
||||
validator_sets::Error::<Test>::DeallocationWouldRemoveEconomicSecurity
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_keys_keys_exist() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let network = ExternalNetworkId::Monero;
|
||||
|
||||
// set the keys first
|
||||
ValidatorSets::set_keys(
|
||||
RawOrigin::None.into(),
|
||||
network,
|
||||
Vec::new().try_into().unwrap(),
|
||||
KeyPair(insecure_pair_from_name("name").public(), Vec::new().try_into().unwrap()),
|
||||
Signature([0u8; 64]),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let call = validator_sets::Call::<Test>::set_keys {
|
||||
network,
|
||||
removed_participants: Vec::new().try_into().unwrap(),
|
||||
key_pair: KeyPair(insecure_pair_from_name("name").public(), Vec::new().try_into().unwrap()),
|
||||
signature: Signature([0u8; 64]),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
ValidatorSets::validate_unsigned(TransactionSource::External, &call),
|
||||
InvalidTransaction::Stale.into()
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_keys_invalid_signature() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let network = ExternalNetworkId::Ethereum;
|
||||
let mut participants = get_ordered_keys(network.into(), &genesis_participants());
|
||||
|
||||
// we can't have invalid set
|
||||
let mut set = ExternalValidatorSet { network, session: Session(1) };
|
||||
let key_pair =
|
||||
KeyPair(insecure_pair_from_name("name").public(), Vec::new().try_into().unwrap());
|
||||
let signature = set_keys_signature(&set, &key_pair, &participants);
|
||||
|
||||
let call = validator_sets::Call::<Test>::set_keys {
|
||||
network,
|
||||
removed_participants: Vec::new().try_into().unwrap(),
|
||||
key_pair: key_pair.clone(),
|
||||
signature,
|
||||
};
|
||||
assert_eq!(
|
||||
ValidatorSets::validate_unsigned(TransactionSource::External, &call),
|
||||
InvalidTransaction::BadProof.into()
|
||||
);
|
||||
|
||||
// fix the set
|
||||
set.session = Session(0);
|
||||
|
||||
// participants should match
|
||||
participants.push(insecure_pair_from_name("random1"));
|
||||
let signature = set_keys_signature(&set, &key_pair, &participants);
|
||||
|
||||
let call = validator_sets::Call::<Test>::set_keys {
|
||||
network,
|
||||
removed_participants: Vec::new().try_into().unwrap(),
|
||||
key_pair: key_pair.clone(),
|
||||
signature,
|
||||
};
|
||||
assert_eq!(
|
||||
ValidatorSets::validate_unsigned(TransactionSource::External, &call),
|
||||
InvalidTransaction::BadProof.into()
|
||||
);
|
||||
|
||||
// fix the participants
|
||||
participants.pop();
|
||||
|
||||
// msg key pair and the key pair to set should match
|
||||
let key_pair2 =
|
||||
KeyPair(insecure_pair_from_name("name2").public(), Vec::new().try_into().unwrap());
|
||||
let signature = set_keys_signature(&set, &key_pair2, &participants);
|
||||
|
||||
let call = validator_sets::Call::<Test>::set_keys {
|
||||
network,
|
||||
removed_participants: Vec::new().try_into().unwrap(),
|
||||
key_pair: key_pair.clone(),
|
||||
signature,
|
||||
};
|
||||
assert_eq!(
|
||||
ValidatorSets::validate_unsigned(TransactionSource::External, &call),
|
||||
InvalidTransaction::BadProof.into()
|
||||
);
|
||||
|
||||
// use the same key pair
|
||||
let signature = set_keys_signature(&set, &key_pair, &participants);
|
||||
let call = validator_sets::Call::<Test>::set_keys {
|
||||
network,
|
||||
removed_participants: Vec::new().try_into().unwrap(),
|
||||
key_pair,
|
||||
signature,
|
||||
};
|
||||
ValidatorSets::validate_unsigned(TransactionSource::External, &call).unwrap();
|
||||
|
||||
// TODO: removed_participants parameter isn't tested since it will be removed in upcoming
|
||||
// commits?
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: add report_slashes tests when the feature is complete.
|
||||
Reference in New Issue
Block a user