From 9615caf3bb36bc82a6e514a7488ea44b27a30a02 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Tue, 28 Mar 2023 05:45:54 -0400 Subject: [PATCH] Move validator-sets from Key to (RistrettoPublic, Key) Part of #241. --- Cargo.lock | 1 + substrate/validator-sets/pallet/Cargo.toml | 4 +++ substrate/validator-sets/pallet/src/lib.rs | 38 +++++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 137c91a6..10ac70ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10883,6 +10883,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serai-primitives", + "sp-core", "validator-sets-primitives", ] diff --git a/substrate/validator-sets/pallet/Cargo.toml b/substrate/validator-sets/pallet/Cargo.toml index e5f423e4..c71beb04 100644 --- a/substrate/validator-sets/pallet/Cargo.toml +++ b/substrate/validator-sets/pallet/Cargo.toml @@ -15,6 +15,8 @@ rustdoc-args = ["--cfg", "docsrs"] scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2", default-features = false, features = ["derive"] } +sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false } + frame-system = { git = "https://github.com/serai-dex/substrate", default-features = false } frame-support = { git = "https://github.com/serai-dex/substrate", default-features = false } @@ -26,6 +28,8 @@ std = [ "scale/std", "scale-info/std", + "sp-core/std", + "frame-system/std", "frame-support/std", diff --git a/substrate/validator-sets/pallet/src/lib.rs b/substrate/validator-sets/pallet/src/lib.rs index 52307ccc..450c33b5 100644 --- a/substrate/validator-sets/pallet/src/lib.rs +++ b/substrate/validator-sets/pallet/src/lib.rs @@ -5,6 +5,8 @@ pub mod pallet { use scale::{Encode, Decode}; use scale_info::TypeInfo; + use sp_core::sr25519; + use frame_system::pallet_prelude::*; use frame_support::pallet_prelude::*; @@ -61,22 +63,28 @@ pub mod pallet { StorageMap<_, Twox64Concat, ValidatorSet, ValidatorSetData, OptionQuery>; type Key = BoundedVec; + // A validator set's key pair is defined as their Ristretto key, used for signing InInstructions, + // and their key on the external network + type KeyPair = (sr25519::Public, Key); - /// The key for a given validator set instance. + /// The key pair for a given validator set instance. #[pallet::storage] #[pallet::getter(fn key)] - pub type Keys = StorageMap<_, Twox64Concat, ValidatorSet, Key, OptionQuery>; + pub type Keys = StorageMap<_, Twox64Concat, ValidatorSet, KeyPair, OptionQuery>; - /// If an account has voted for a specific key or not. Prevents them from voting multiple times. + /// If an account has voted for a specific key pair or not. + // This prevents a validator from voting multiple times. #[pallet::storage] #[pallet::getter(fn voted)] - pub type Voted = StorageMap<_, Blake2_128Concat, (T::AccountId, Key), (), OptionQuery>; + pub type Voted = + StorageMap<_, Blake2_128Concat, (T::AccountId, KeyPair), (), OptionQuery>; - /// How many times a key has been voted for. Once consensus is reached, the keys will be adopted. + /// How many times a key pair has been voted for. Once consensus is reached, the keys will be + /// adopted. #[pallet::storage] #[pallet::getter(fn vote_count)] pub type VoteCount = - StorageMap<_, Blake2_128Concat, (ValidatorSet, Key), u16, ValueQuery>; + StorageMap<_, Blake2_128Concat, (ValidatorSet, KeyPair), u16, ValueQuery>; #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { @@ -102,13 +110,13 @@ pub mod pallet { Vote { voter: T::AccountId, set: ValidatorSet, - key: Key, + key_pair: KeyPair, // Amount of votes the key now has votes: u16, }, KeyGen { set: ValidatorSet, - key: Key, + key_pair: KeyPair, }, } @@ -128,7 +136,7 @@ pub mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight(0)] // TODO - pub fn vote(origin: OriginFor, network: NetworkId, key: Key) -> DispatchResult { + pub fn vote(origin: OriginFor, network: NetworkId, key_pair: KeyPair) -> DispatchResult { let signer = ensure_signed(origin)?; // TODO: Do we need to check the key is within the length bounds? // The docs suggest the BoundedVec will create/write, yet not read, which could be an issue @@ -150,23 +158,23 @@ pub mod pallet { } // Confirm this signer hasn't already voted for these keys - if Voted::::get((&signer, &key)).is_some() { + if Voted::::get((&signer, &key_pair)).is_some() { Err(Error::::AlreadyVoted)?; } - Voted::::set((&signer, &key), Some(())); + Voted::::set((&signer, &key_pair), Some(())); // Add their vote - let votes = VoteCount::::mutate((set, &key), |value| { + let votes = VoteCount::::mutate((set, &key_pair), |value| { *value += 1; *value }); - Self::deposit_event(Event::Vote { voter: signer, set, key: key.clone(), votes }); + Self::deposit_event(Event::Vote { voter: signer, set, key_pair: key_pair.clone(), votes }); // If we've reached consensus, set the key if usize::try_from(votes).unwrap() == data.participants.len() { - Keys::::set(set, Some(key.clone())); - Self::deposit_event(Event::KeyGen { set, key }); + Keys::::set(set, Some(key_pair.clone())); + Self::deposit_event(Event::KeyGen { set, key_pair }); } Ok(())