mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Reorganize serai-client
Instead of functions taking a block hash, has a scope to a block hash before functions can be called. Separates functions by pallets.
This commit is contained in:
@@ -14,10 +14,10 @@ use serai_client::{
|
||||
primitives::{Batch, SignedBatch, batch_message},
|
||||
InInstructionsEvent,
|
||||
},
|
||||
Serai,
|
||||
SeraiInInstructions,
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx, validator_sets::set_validator_set_keys};
|
||||
use crate::common::{serai, tx::publish_tx, validator_sets::set_keys};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
||||
@@ -27,23 +27,23 @@ pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
||||
let set = ValidatorSet { session: Session(0), network: batch.network };
|
||||
let pair = insecure_pair_from_name(&format!("ValidatorSet {:?}", set));
|
||||
let keys = if let Some(keys) =
|
||||
serai.get_keys(set, serai.get_latest_block_hash().await.unwrap()).await.unwrap()
|
||||
serai.with_current_latest_block().await.unwrap().validator_sets().keys(set).await.unwrap()
|
||||
{
|
||||
keys
|
||||
} else {
|
||||
let keys = (pair.public(), vec![].try_into().unwrap());
|
||||
set_validator_set_keys(set, keys.clone()).await;
|
||||
set_keys(set, keys.clone()).await;
|
||||
keys
|
||||
};
|
||||
assert_eq!(keys.0, pair.public());
|
||||
|
||||
let block = publish_tx(&Serai::execute_batch(SignedBatch {
|
||||
let block = publish_tx(&SeraiInInstructions::execute_batch(SignedBatch {
|
||||
batch: batch.clone(),
|
||||
signature: pair.sign(&batch_message(&batch)),
|
||||
}))
|
||||
.await;
|
||||
|
||||
let batches = serai.get_batch_events(block).await.unwrap();
|
||||
let batches = serai.as_of(block).in_instructions().batch_events().await.unwrap();
|
||||
// TODO: impl From<Batch> for BatchEvent?
|
||||
assert_eq!(
|
||||
batches,
|
||||
|
||||
@@ -60,7 +60,7 @@ macro_rules! serai_test {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
let serai = serai().await;
|
||||
while serai.get_latest_block_hash().await.is_err() {
|
||||
while serai.latest_block_hash().await.is_err() {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
// TODO: https://github.com/serai-dex/serai/247
|
||||
|
||||
@@ -11,7 +11,7 @@ pub async fn publish_tx(tx: &Encoded) -> [u8; 32] {
|
||||
let serai = serai().await;
|
||||
|
||||
let mut latest =
|
||||
serai.get_block(serai.get_latest_block_hash().await.unwrap()).await.unwrap().unwrap().number();
|
||||
serai.block(serai.latest_block_hash().await.unwrap()).await.unwrap().unwrap().number();
|
||||
|
||||
serai.publish(tx).await.unwrap();
|
||||
|
||||
@@ -24,7 +24,7 @@ pub async fn publish_tx(tx: &Encoded) -> [u8; 32] {
|
||||
let block = {
|
||||
let mut block;
|
||||
while {
|
||||
block = serai.get_block_by_number(latest).await.unwrap();
|
||||
block = serai.block_by_number(latest).await.unwrap();
|
||||
block.is_none()
|
||||
} {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
|
||||
@@ -15,13 +15,13 @@ use serai_client::{
|
||||
primitives::{ValidatorSet, KeyPair, musig_context, musig_key, set_keys_message},
|
||||
ValidatorSetsEvent,
|
||||
},
|
||||
Serai,
|
||||
SeraiValidatorSets,
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn set_validator_set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
pub async fn set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
let pair = insecure_pair_from_name("Alice");
|
||||
let public = pair.public();
|
||||
|
||||
@@ -29,7 +29,11 @@ pub async fn set_validator_set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8
|
||||
let public_key = <Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut public.0.as_ref()).unwrap();
|
||||
assert_eq!(
|
||||
serai
|
||||
.get_validator_set_musig_key(set, serai.get_latest_block_hash().await.unwrap())
|
||||
.with_current_latest_block()
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
.musig_key(set)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
@@ -45,7 +49,11 @@ pub async fn set_validator_set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8
|
||||
musig::<Ristretto>(&musig_context(set), &Zeroizing::new(secret_key), &[public_key]).unwrap();
|
||||
assert_eq!(
|
||||
serai
|
||||
.get_validator_set_musig_key(set, serai.get_latest_block_hash().await.unwrap())
|
||||
.with_current_latest_block()
|
||||
.await
|
||||
.unwrap()
|
||||
.validator_sets()
|
||||
.musig_key(set)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
@@ -63,7 +71,7 @@ pub async fn set_validator_set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8
|
||||
);
|
||||
|
||||
// Vote in a key pair
|
||||
let block = publish_tx(&Serai::set_validator_set_keys(
|
||||
let block = publish_tx(&SeraiValidatorSets::set_keys(
|
||||
set.network,
|
||||
key_pair.clone(),
|
||||
Signature(sig.to_bytes()),
|
||||
@@ -71,10 +79,10 @@ pub async fn set_validator_set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
serai.get_key_gen_events(block).await.unwrap(),
|
||||
serai.as_of(block).validator_sets().key_gen_events().await.unwrap(),
|
||||
vec![ValidatorSetsEvent::KeyGen { set, key_pair: key_pair.clone() }]
|
||||
);
|
||||
assert_eq!(serai.get_keys(set, block).await.unwrap(), Some(key_pair));
|
||||
assert_eq!(serai.as_of(block).validator_sets().keys(set).await.unwrap(), Some(key_pair));
|
||||
|
||||
block
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user