2023-03-31 00:24:11 +00:00
|
|
|
use rand_core::{RngCore, OsRng};
|
|
|
|
|
|
|
|
use sp_core::{sr25519::Public, Pair};
|
|
|
|
|
|
|
|
use serai_client::{
|
2023-10-13 03:59:21 +00:00
|
|
|
primitives::{NETWORKS, NetworkId, insecure_pair_from_name},
|
2023-03-31 00:24:11 +00:00
|
|
|
validator_sets::{
|
2023-11-25 09:01:11 +00:00
|
|
|
primitives::{Session, ValidatorSet, KeyPair, musig_key},
|
2023-03-31 00:24:11 +00:00
|
|
|
ValidatorSetsEvent,
|
|
|
|
},
|
2023-03-31 10:34:09 +00:00
|
|
|
Serai,
|
2023-03-31 00:24:11 +00:00
|
|
|
};
|
|
|
|
|
2023-03-31 10:34:09 +00:00
|
|
|
mod common;
|
2023-10-23 04:33:38 +00:00
|
|
|
use common::validator_sets::set_keys;
|
2023-03-31 00:24:11 +00:00
|
|
|
|
|
|
|
serai_test!(
|
2023-10-23 04:33:38 +00:00
|
|
|
set_keys_test: (|serai: Serai| async move {
|
2023-04-18 06:01:53 +00:00
|
|
|
let network = NetworkId::Bitcoin;
|
2023-03-31 00:24:11 +00:00
|
|
|
let set = ValidatorSet { session: Session(0), network };
|
|
|
|
|
2023-03-31 10:34:09 +00:00
|
|
|
let public = insecure_pair_from_name("Alice").public();
|
|
|
|
|
2023-03-31 00:24:11 +00:00
|
|
|
// Neither of these keys are validated
|
|
|
|
// The external key is infeasible to validate on-chain, the Ristretto key is feasible
|
|
|
|
// TODO: Should the Ristretto key be validated?
|
|
|
|
let mut ristretto_key = [0; 32];
|
|
|
|
OsRng.fill_bytes(&mut ristretto_key);
|
|
|
|
let mut external_key = vec![0; 33];
|
|
|
|
OsRng.fill_bytes(&mut external_key);
|
2023-11-25 09:01:11 +00:00
|
|
|
let key_pair = KeyPair(Public(ristretto_key), external_key.try_into().unwrap());
|
2023-03-31 00:24:11 +00:00
|
|
|
|
|
|
|
// Make sure the genesis is as expected
|
2023-04-15 04:40:33 +00:00
|
|
|
assert_eq!(
|
|
|
|
serai
|
2023-10-14 06:47:58 +00:00
|
|
|
.as_of(serai.block_by_number(0).await.unwrap().unwrap().hash())
|
|
|
|
.validator_sets()
|
|
|
|
.new_set_events()
|
2023-04-15 04:40:33 +00:00
|
|
|
.await
|
|
|
|
.unwrap(),
|
2023-10-13 03:59:21 +00:00
|
|
|
NETWORKS
|
2023-04-15 04:40:33 +00:00
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|network| ValidatorSetsEvent::NewSet {
|
|
|
|
set: ValidatorSet { session: Session(0), network }
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
|
2023-10-14 06:47:58 +00:00
|
|
|
{
|
|
|
|
let vs_serai = serai.with_current_latest_block().await.unwrap().validator_sets();
|
2023-11-22 11:22:46 +00:00
|
|
|
let participants = vs_serai.participants(set.network).await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, _)| k)
|
|
|
|
.collect::<Vec<_>>();
|
2023-10-14 06:47:58 +00:00
|
|
|
let participants_ref: &[_] = participants.as_ref();
|
|
|
|
assert_eq!(participants_ref, [public].as_ref());
|
|
|
|
assert_eq!(vs_serai.musig_key(set).await.unwrap().unwrap(), musig_key(set, &[public]).0);
|
|
|
|
}
|
2023-05-13 06:02:47 +00:00
|
|
|
|
2023-10-23 04:33:38 +00:00
|
|
|
let block = set_keys(&serai, set, key_pair.clone()).await;
|
2023-05-13 06:02:47 +00:00
|
|
|
|
2023-10-14 06:47:58 +00:00
|
|
|
// While the set_keys function should handle this, it's beneficial to
|
2023-05-13 06:02:47 +00:00
|
|
|
// independently test it
|
2023-10-14 06:47:58 +00:00
|
|
|
let serai = serai.as_of(block).validator_sets();
|
2023-03-31 00:24:11 +00:00
|
|
|
assert_eq!(
|
2023-10-14 06:47:58 +00:00
|
|
|
serai.key_gen_events().await.unwrap(),
|
2023-03-31 00:24:11 +00:00
|
|
|
vec![ValidatorSetsEvent::KeyGen { set, key_pair: key_pair.clone() }]
|
|
|
|
);
|
2023-10-14 06:47:58 +00:00
|
|
|
assert_eq!(serai.keys(set).await.unwrap(), Some(key_pair));
|
2023-10-23 04:33:38 +00:00
|
|
|
})
|
2023-03-31 00:24:11 +00:00
|
|
|
);
|