2022-07-17 21:17:23 +00:00
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
|
|
|
|
use ink_lang as ink;
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
use serai_extension::{Curve, GlobalValidatorSetId, ValidatorSetIndex, Key};
|
|
|
|
|
|
|
|
type KeysHash = [u8; 32];
|
|
|
|
|
2022-07-18 00:56:10 +00:00
|
|
|
#[ink::contract(env = serai_extension::SeraiEnvironment)]
|
2022-07-17 21:17:23 +00:00
|
|
|
mod multisig {
|
|
|
|
use scale::Encode;
|
|
|
|
|
|
|
|
use ink_storage::{traits::SpreadAllocate, Mapping};
|
|
|
|
use ink_env::{hash::Blake2x256, hash_encoded};
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
use super::*;
|
|
|
|
|
2022-07-17 21:17:23 +00:00
|
|
|
/// A contract which tracks the current multisig keys.
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Mapping of each validator set to their multisigs.
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(storage)]
|
|
|
|
#[derive(SpreadAllocate)]
|
|
|
|
pub struct Multisig {
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Global validator set ID under which this multisig was updated.
|
|
|
|
/// Used to track if the multisig has been updated to the latest instantiation of a validator
|
|
|
|
/// set or not.
|
|
|
|
/// May be behind, and still healthy, if a validator set didn't change despite the global
|
|
|
|
/// validator set doing so.
|
|
|
|
updated_at: Mapping<ValidatorSetIndex, GlobalValidatorSetId>,
|
|
|
|
/// Mapping from a curve's index to the multisig's current public key for it, if it has one.
|
2022-07-17 21:17:23 +00:00
|
|
|
// This is a mapping due to ink's eager loading. Considering we're right now only considering
|
2022-07-20 08:57:35 +00:00
|
|
|
// Secp256k1 and Ed25519, it may be notably more efficient to use a Vec here.
|
|
|
|
// In practice, we're likely discussing up to 7 curves in total, so it may always be better to
|
|
|
|
// simply use a Vec here, especially since it'd be Vec<Option<Key>>.
|
|
|
|
keys: Mapping<(ValidatorSetIndex, Curve), Key>,
|
|
|
|
/// Validator + Keys -> Voted already or not.
|
|
|
|
/// Prevents voting multiple times on the same set of keys.
|
|
|
|
voted: Mapping<(AccountId, KeysHash), ()>,
|
|
|
|
/// Global Validator Set ID + Validator + Keys -> Vote Count.
|
|
|
|
/// Including the GVSID locks it to a specific time period, preventing a validator from joining
|
|
|
|
/// a set, voting on old keys, and then moving their bond to a new account to vote again.
|
|
|
|
votes: Mapping<(GlobalValidatorSetId, ValidatorSetIndex, KeysHash), u16>,
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Event emitted when a new set of multisig keys is voted on.
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(event)]
|
|
|
|
pub struct Vote {
|
|
|
|
/// Validator who issued the vote.
|
|
|
|
#[ink(topic)]
|
|
|
|
validator: AccountId,
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Global validator set ID under which keys are being generated.
|
|
|
|
#[ink(topic)]
|
|
|
|
global_validator_set: GlobalValidatorSetId,
|
2022-07-17 21:17:23 +00:00
|
|
|
/// Validator set for which keys are being generated.
|
|
|
|
#[ink(topic)]
|
2022-07-20 08:57:35 +00:00
|
|
|
validator_set: ValidatorSetIndex,
|
2022-07-17 21:17:23 +00:00
|
|
|
/// Hash of the keys voted on.
|
|
|
|
#[ink(topic)]
|
2022-07-20 08:57:35 +00:00
|
|
|
hash: KeysHash,
|
|
|
|
/// Keys voted on. Only present in the first event for a given set of keys.
|
|
|
|
keys: Option<Vec<Option<Key>>>,
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Event emitted when the new keys are fully generated for a validator set, having been fully
|
|
|
|
/// voted on.
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(event)]
|
|
|
|
pub struct KeyGen {
|
2022-07-18 07:06:07 +00:00
|
|
|
#[ink(topic)]
|
2022-07-20 08:57:35 +00:00
|
|
|
global_validator_set: GlobalValidatorSetId,
|
|
|
|
#[ink(topic)]
|
|
|
|
validator_set: ValidatorSetIndex,
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(topic)]
|
2022-07-20 08:57:35 +00:00
|
|
|
hash: KeysHash,
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The Multisig error types.
|
|
|
|
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
|
|
|
|
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
|
|
|
|
pub enum Error {
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Returned if a validator set hasn't had keys registered for it yet.
|
|
|
|
NonExistentValidatorSet,
|
|
|
|
/// Returned if a validator set and curve index doesn't have a key registered for it.
|
|
|
|
NonExistentKey,
|
|
|
|
/// Returned if a curve index doesn't exist.
|
2022-07-17 21:17:23 +00:00
|
|
|
NonExistentCurve,
|
|
|
|
/// Returned if a non-validator is voting.
|
|
|
|
NotValidator,
|
|
|
|
/// Returned if this validator set already generated keys.
|
|
|
|
AlreadyGeneratedKeys,
|
|
|
|
/// Returned if this validator has already voted for these keys.
|
|
|
|
AlreadyVoted,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Multisig result type.
|
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
|
|
|
|
|
|
|
impl Multisig {
|
|
|
|
/// Deploys the Multisig contract.
|
|
|
|
#[ink(constructor)]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ink_lang::utils::initialize_contract(|_| {})
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Global validator set ID under which a validator set updated their multisig.
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(message)]
|
2022-07-20 08:57:35 +00:00
|
|
|
pub fn updated_at(&self, validator_set: ValidatorSetIndex) -> Result<GlobalValidatorSetId> {
|
|
|
|
self.updated_at.get(validator_set).ok_or(Error::NonExistentValidatorSet)
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Returns the key currently in-use for a given validator set and curve.
|
|
|
|
/// This is then bound to a given chain by applying a network-specific additive offset, as done
|
|
|
|
/// by the processor. Each chain then has its own way of receiving funds to these keys, leaving
|
|
|
|
/// this not for usage by wallets, nor the processor which is expected to track events for this
|
|
|
|
/// information. This is really solely for debugging purposes.
|
2022-07-17 21:17:23 +00:00
|
|
|
#[ink(message)]
|
2022-07-20 08:57:35 +00:00
|
|
|
pub fn key(&self, validator_set: ValidatorSetIndex, curve: Curve) -> Result<Key> {
|
|
|
|
self.keys.get((validator_set, curve)).ok_or(Error::NonExistentKey)
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: voted
|
|
|
|
// TODO: votes
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
fn hash<T: Encode>(value: &T) -> KeysHash {
|
|
|
|
let mut output = KeysHash::default();
|
2022-07-17 21:17:23 +00:00
|
|
|
hash_encoded::<Blake2x256, _>(value, &mut output);
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Vote for a given set of keys.
|
|
|
|
#[ink(message)]
|
2022-07-20 08:57:35 +00:00
|
|
|
pub fn vote(&mut self, keys: Vec<Option<Key>>) -> Result<()> {
|
2022-07-17 21:17:23 +00:00
|
|
|
if keys.len() > 256 {
|
|
|
|
Err(Error::NonExistentCurve)?;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
// Make sure they're a valid validator.
|
2022-07-17 21:17:23 +00:00
|
|
|
let validator = self.env().caller();
|
2022-07-20 08:57:35 +00:00
|
|
|
let active_validator = self.env().extension().active_validator(&validator);
|
|
|
|
if active_validator.is_none() {
|
2022-07-17 21:17:23 +00:00
|
|
|
Err(Error::NotValidator)?;
|
|
|
|
}
|
2022-07-20 08:57:35 +00:00
|
|
|
let (validator_set, shares) = active_validator.unwrap();
|
2022-07-17 21:17:23 +00:00
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
// Prevent a validator set from generating keys multiple times. Only the first-voted-in keys
|
|
|
|
// should be acknowledged.
|
|
|
|
let global_validator_set = self.env().extension().global_validator_set_id();
|
|
|
|
if self.updated_at.get(validator_set) == Some(global_validator_set) {
|
2022-07-17 21:17:23 +00:00
|
|
|
Err(Error::AlreadyGeneratedKeys)?;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
// Prevent a validator from voting on keys multiple times.
|
2022-07-17 21:17:23 +00:00
|
|
|
let keys_hash = Self::hash(&keys);
|
|
|
|
if self.voted.get((validator, keys_hash)).is_some() {
|
|
|
|
Err(Error::AlreadyVoted)?;
|
|
|
|
}
|
|
|
|
self.voted.insert((validator, keys_hash), &());
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
let votes = if let Some(votes) = self.votes.get((global_validator_set, validator_set, keys_hash)) {
|
|
|
|
self.env().emit_event(Vote { validator, global_validator_set, validator_set, hash: keys_hash, keys: None });
|
|
|
|
votes + shares
|
2022-07-17 21:17:23 +00:00
|
|
|
} else {
|
|
|
|
self.env().emit_event(Vote {
|
|
|
|
validator,
|
2022-07-20 08:57:35 +00:00
|
|
|
global_validator_set,
|
2022-07-17 21:17:23 +00:00
|
|
|
validator_set,
|
|
|
|
hash: keys_hash,
|
2022-07-18 05:52:59 +00:00
|
|
|
keys: Some(keys.clone()),
|
2022-07-17 21:17:23 +00:00
|
|
|
});
|
2022-07-20 08:57:35 +00:00
|
|
|
shares
|
2022-07-17 21:17:23 +00:00
|
|
|
};
|
|
|
|
// We could skip writing this if we've reached consensus, yet best to keep our ducks in a row
|
2022-07-20 08:57:35 +00:00
|
|
|
self.votes.insert((global_validator_set, validator_set, keys_hash), &votes);
|
2022-07-17 21:17:23 +00:00
|
|
|
|
|
|
|
// If we've reached consensus, action this.
|
2022-07-20 08:57:35 +00:00
|
|
|
if votes == self.env().extension().validator_set_shares(validator_set) {
|
|
|
|
self.updated_at.insert(validator_set, &global_validator_set);
|
2022-07-17 21:17:23 +00:00
|
|
|
for (k, key) in keys.iter().enumerate() {
|
2022-07-20 08:57:35 +00:00
|
|
|
if let Some(key) = key {
|
|
|
|
self.keys.insert((validator_set, Curve::try_from(k).unwrap()), key);
|
|
|
|
}
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
2022-07-20 08:57:35 +00:00
|
|
|
self.env().emit_event(KeyGen { global_validator_set, validator_set, hash: keys_hash });
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 05:52:59 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-07-18 07:06:07 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-07-18 05:52:59 +00:00
|
|
|
|
|
|
|
use ink_env::{
|
|
|
|
hash::{CryptoHash, Blake2x256},
|
|
|
|
AccountId,
|
|
|
|
topics::PrefixedValue,
|
|
|
|
};
|
|
|
|
use ink_lang as ink;
|
|
|
|
|
2022-07-18 07:06:07 +00:00
|
|
|
use serai_extension::{test_validators, test_register};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2022-07-18 05:52:59 +00:00
|
|
|
type Event = <Multisig as ::ink_lang::reflect::ContractEventBase>::Type;
|
|
|
|
|
2022-07-18 07:06:07 +00:00
|
|
|
lazy_static! {
|
2022-07-20 08:57:35 +00:00
|
|
|
static ref EXPECTED_GLOBAL_VALIDATOR_SET: GlobalValidatorSetId = 1;
|
|
|
|
static ref EXPECTED_VALIDATOR_SET: ValidatorSetIndex = 0;
|
|
|
|
static ref KEYS: Vec<Option<Key>> = vec![Some(vec![0, 1]), Some(vec![2, 3])];
|
|
|
|
static ref EXPECTED_HASH: KeysHash = {
|
|
|
|
let mut hash = KeysHash::default();
|
2022-07-18 07:06:07 +00:00
|
|
|
ink_env::hash_encoded::<Blake2x256, _>(&*KEYS, &mut hash);
|
|
|
|
hash
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-18 05:52:59 +00:00
|
|
|
fn hash_prefixed<T: scale::Encode>(prefixed: PrefixedValue<T>) -> [u8; 32] {
|
|
|
|
let encoded = prefixed.encode();
|
2022-07-20 08:57:35 +00:00
|
|
|
let mut hash = KeysHash::default();
|
2022-07-18 05:52:59 +00:00
|
|
|
if encoded.len() < 32 {
|
|
|
|
hash[.. encoded.len()].copy_from_slice(&encoded);
|
|
|
|
} else {
|
|
|
|
Blake2x256::hash(&encoded, &mut hash);
|
|
|
|
}
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_vote(
|
|
|
|
event: &ink_env::test::EmittedEvent,
|
|
|
|
expected_validator: AccountId,
|
2022-07-18 07:06:07 +00:00
|
|
|
expected_keys: Option<()>,
|
2022-07-18 05:52:59 +00:00
|
|
|
) {
|
|
|
|
let decoded_event = <Event as scale::Decode>::decode(&mut &event.data[..])
|
|
|
|
.expect("encountered invalid contract event data buffer");
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
if let Event::Vote(Vote { validator, global_validator_set, validator_set, hash, keys: actual_keys }) = decoded_event
|
2022-07-18 07:06:07 +00:00
|
|
|
{
|
2022-07-18 05:52:59 +00:00
|
|
|
assert_eq!(validator, expected_validator);
|
2022-07-20 08:57:35 +00:00
|
|
|
assert_eq!(global_validator_set, *EXPECTED_GLOBAL_VALIDATOR_SET);
|
2022-07-18 07:06:07 +00:00
|
|
|
assert_eq!(validator_set, *EXPECTED_VALIDATOR_SET);
|
|
|
|
assert_eq!(hash, *EXPECTED_HASH);
|
|
|
|
assert_eq!(actual_keys.as_ref(), expected_keys.map(|_| &*KEYS));
|
2022-07-18 05:52:59 +00:00
|
|
|
} else {
|
|
|
|
panic!("invalid Vote event")
|
|
|
|
}
|
|
|
|
|
|
|
|
let expected_topics = vec![
|
|
|
|
hash_prefixed(PrefixedValue { prefix: b"", value: b"Multisig::Vote" }),
|
|
|
|
hash_prefixed(PrefixedValue {
|
|
|
|
prefix: b"Multisig::Vote::validator",
|
|
|
|
value: &expected_validator,
|
|
|
|
}),
|
2022-07-20 08:57:35 +00:00
|
|
|
hash_prefixed(PrefixedValue {
|
|
|
|
prefix: b"Multisig::Vote::global_validator_set",
|
|
|
|
value: &*EXPECTED_GLOBAL_VALIDATOR_SET,
|
|
|
|
}),
|
2022-07-18 05:52:59 +00:00
|
|
|
hash_prefixed(PrefixedValue {
|
|
|
|
prefix: b"Multisig::Vote::validator_set",
|
2022-07-18 07:06:07 +00:00
|
|
|
value: &*EXPECTED_VALIDATOR_SET,
|
|
|
|
}),
|
|
|
|
hash_prefixed(PrefixedValue { prefix: b"Multisig::Vote::hash", value: &*EXPECTED_HASH }),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (n, (actual_topic, expected_topic)) in
|
|
|
|
event.topics.iter().zip(expected_topics).enumerate()
|
|
|
|
{
|
|
|
|
assert_eq!(actual_topic, &expected_topic, "encountered invalid topic at {}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_key_gen(event: &ink_env::test::EmittedEvent) {
|
|
|
|
let decoded_event = <Event as scale::Decode>::decode(&mut &event.data[..])
|
|
|
|
.expect("encountered invalid contract event data buffer");
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
if let Event::KeyGen(KeyGen { global_validator_set, validator_set, hash }) = decoded_event {
|
|
|
|
assert_eq!(global_validator_set, *EXPECTED_GLOBAL_VALIDATOR_SET);
|
2022-07-18 07:06:07 +00:00
|
|
|
assert_eq!(validator_set, *EXPECTED_VALIDATOR_SET);
|
|
|
|
assert_eq!(hash, *EXPECTED_HASH);
|
|
|
|
} else {
|
|
|
|
panic!("invalid KeyGen event")
|
|
|
|
}
|
|
|
|
|
|
|
|
let expected_topics = vec![
|
|
|
|
hash_prefixed(PrefixedValue { prefix: b"", value: b"Multisig::KeyGen" }),
|
2022-07-20 08:57:35 +00:00
|
|
|
hash_prefixed(PrefixedValue {
|
|
|
|
prefix: b"Multisig::KeyGen::global_validator_set",
|
|
|
|
value: &*EXPECTED_GLOBAL_VALIDATOR_SET,
|
|
|
|
}),
|
2022-07-18 07:06:07 +00:00
|
|
|
hash_prefixed(PrefixedValue {
|
|
|
|
prefix: b"Multisig::KeyGen::validator_set",
|
|
|
|
value: &*EXPECTED_VALIDATOR_SET,
|
2022-07-18 05:52:59 +00:00
|
|
|
}),
|
2022-07-20 08:57:35 +00:00
|
|
|
hash_prefixed(PrefixedValue { prefix: b"Multisig::KeyGen::hash", value: &*EXPECTED_HASH }),
|
2022-07-18 05:52:59 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (n, (actual_topic, expected_topic)) in
|
|
|
|
event.topics.iter().zip(expected_topics).enumerate()
|
|
|
|
{
|
|
|
|
assert_eq!(actual_topic, &expected_topic, "encountered invalid topic at {}", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The default constructor does its job.
|
|
|
|
#[ink::test]
|
|
|
|
fn new() {
|
|
|
|
let multisig = Multisig::new();
|
2022-07-20 08:57:35 +00:00
|
|
|
assert_eq!(multisig.updated_at(0), Err(Error::NonExistentValidatorSet));
|
2022-07-18 05:52:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:57:35 +00:00
|
|
|
/// Non-existent keys error accordingly.
|
2022-07-18 05:52:59 +00:00
|
|
|
#[ink::test]
|
2022-07-20 08:57:35 +00:00
|
|
|
fn non_existent_key() {
|
|
|
|
assert_eq!(Multisig::new().key(0, 0), Err(Error::NonExistentKey));
|
2022-07-18 05:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[ink::test]
|
2022-07-18 07:06:07 +00:00
|
|
|
fn success() {
|
|
|
|
test_register();
|
2022-07-18 05:52:59 +00:00
|
|
|
let mut multisig = Multisig::new();
|
|
|
|
|
2022-07-18 07:06:07 +00:00
|
|
|
// Test voting on keys works without issue, emitting the keys for the first vote
|
|
|
|
let mut emitted_events = vec![];
|
|
|
|
for (i, validator) in test_validators().iter().enumerate() {
|
|
|
|
ink_env::test::set_caller::<ink_env::DefaultEnvironment>(*validator);
|
|
|
|
multisig.vote(KEYS.clone()).unwrap();
|
|
|
|
|
|
|
|
emitted_events = ink_env::test::recorded_events().collect::<Vec<_>>();
|
|
|
|
// If this is the last validator, it should also trigger a keygen event, hence the + 1
|
|
|
|
assert_eq!(emitted_events.len(), (i + 1) + (i / (test_validators().len() - 1)));
|
|
|
|
assert_vote(
|
|
|
|
&emitted_events[i],
|
|
|
|
*validator,
|
|
|
|
// Only the first event for this hash should have the keys
|
|
|
|
Some(()).filter(|_| i == 0),
|
|
|
|
);
|
|
|
|
}
|
2022-07-18 06:53:45 +00:00
|
|
|
|
2022-07-18 07:06:07 +00:00
|
|
|
// Since this should have key gen'd, verify that
|
2022-07-20 08:57:35 +00:00
|
|
|
assert_eq!(multisig.updated_at(0).unwrap(), *EXPECTED_GLOBAL_VALIDATOR_SET);
|
2022-07-18 07:06:07 +00:00
|
|
|
assert_key_gen(&emitted_events[test_validators().len()]);
|
2022-07-18 06:53:45 +00:00
|
|
|
}
|
2022-07-18 05:52:59 +00:00
|
|
|
}
|
2022-07-17 21:17:23 +00:00
|
|
|
}
|