2023-03-31 00:24:11 +00:00
|
|
|
use rand_core::{RngCore, OsRng};
|
|
|
|
|
|
|
|
use sp_core::{sr25519::Public, Pair};
|
|
|
|
|
|
|
|
use serai_client::{
|
2023-04-15 04:40:33 +00:00
|
|
|
primitives::{
|
|
|
|
BITCOIN_NET_ID, ETHEREUM_NET_ID, MONERO_NET_ID, BITCOIN_NET, insecure_pair_from_name,
|
|
|
|
},
|
2023-03-31 00:24:11 +00:00
|
|
|
validator_sets::{
|
|
|
|
primitives::{Session, ValidatorSet},
|
|
|
|
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-03-31 12:11:23 +00:00
|
|
|
use common::{serai, validator_sets::vote_in_keys};
|
2023-03-31 00:24:11 +00:00
|
|
|
|
|
|
|
serai_test!(
|
|
|
|
async fn vote_keys() {
|
|
|
|
let network = BITCOIN_NET_ID;
|
|
|
|
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);
|
|
|
|
let key_pair = (Public(ristretto_key), external_key.try_into().unwrap());
|
|
|
|
|
2023-03-31 10:34:09 +00:00
|
|
|
let serai = serai().await;
|
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-04-15 21:38:47 +00:00
|
|
|
.get_new_set_events(serai.get_block_by_number(0).await.unwrap().unwrap().hash())
|
2023-04-15 04:40:33 +00:00
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
[BITCOIN_NET_ID, ETHEREUM_NET_ID, MONERO_NET_ID]
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|network| ValidatorSetsEvent::NewSet {
|
|
|
|
set: ValidatorSet { session: Session(0), network }
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
);
|
|
|
|
|
2023-03-31 00:24:11 +00:00
|
|
|
let set_data = serai.get_validator_set(set).await.unwrap().unwrap();
|
|
|
|
assert_eq!(set_data.network, *BITCOIN_NET);
|
|
|
|
let participants_ref: &[_] = set_data.participants.as_ref();
|
|
|
|
assert_eq!(participants_ref, [(public, set_data.bond)].as_ref());
|
|
|
|
|
2023-03-31 12:11:23 +00:00
|
|
|
let block = vote_in_keys(set, key_pair.clone()).await;
|
2023-03-31 00:24:11 +00:00
|
|
|
|
2023-03-31 12:11:23 +00:00
|
|
|
// While the vote_in_keys function should handle this, it's beneficial to independently test it
|
2023-03-31 00:24:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
serai.get_vote_events(block).await.unwrap(),
|
|
|
|
vec![ValidatorSetsEvent::Vote { voter: public, set, key_pair: key_pair.clone(), votes: 1 }]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
serai.get_key_gen_events(block).await.unwrap(),
|
|
|
|
vec![ValidatorSetsEvent::KeyGen { set, key_pair: key_pair.clone() }]
|
|
|
|
);
|
|
|
|
assert_eq!(serai.get_keys(set).await.unwrap(), Some(key_pair));
|
|
|
|
}
|
|
|
|
);
|