mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Move substrate/serai/* to substrate/*
This commit is contained in:
49
substrate/client/tests/common/in_instructions.rs
Normal file
49
substrate/client/tests/common/in_instructions.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use scale::Encode;
|
||||
|
||||
use sp_core::Pair;
|
||||
|
||||
use serai_client::{
|
||||
primitives::insecure_pair_from_name,
|
||||
validator_sets::primitives::{Session, ValidatorSet},
|
||||
in_instructions::{
|
||||
primitives::{Batch, SignedBatch},
|
||||
InInstructionsEvent,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx, validator_sets::vote_in_keys};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
||||
let serai = serai().await;
|
||||
|
||||
// TODO: Get the latest session
|
||||
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).await.unwrap() {
|
||||
keys
|
||||
} else {
|
||||
let keys = (pair.public(), vec![].try_into().unwrap());
|
||||
vote_in_keys(set, keys.clone()).await;
|
||||
keys
|
||||
};
|
||||
assert_eq!(keys.0, pair.public());
|
||||
|
||||
let block = publish_tx(
|
||||
&serai
|
||||
.execute_batch(SignedBatch { batch: batch.clone(), signature: pair.sign(&batch.encode()) })
|
||||
.unwrap(),
|
||||
)
|
||||
.await;
|
||||
|
||||
let batches = serai.get_batch_events(block).await.unwrap();
|
||||
// TODO: impl From<Batch> for BatchEvent?
|
||||
assert_eq!(
|
||||
batches,
|
||||
vec![InInstructionsEvent::Batch { network: batch.network, id: batch.id, block: batch.block }],
|
||||
);
|
||||
|
||||
// TODO: Check the tokens events
|
||||
|
||||
block
|
||||
}
|
||||
82
substrate/client/tests/common/mod.rs
Normal file
82
substrate/client/tests/common/mod.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use serai_client::Serai;
|
||||
|
||||
pub mod tx;
|
||||
pub mod validator_sets;
|
||||
pub mod in_instructions;
|
||||
|
||||
pub const URL: &str = "ws://127.0.0.1:9944";
|
||||
|
||||
pub async fn serai() -> Serai {
|
||||
Serai::new(URL).await.unwrap()
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SEQUENTIAL: Mutex<()> = Mutex::new(());
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! serai_test {
|
||||
($(async fn $name: ident() $body: block)*) => {
|
||||
$(
|
||||
#[tokio::test]
|
||||
async fn $name() {
|
||||
use std::process::Command;
|
||||
|
||||
let guard = common::SEQUENTIAL.lock().await;
|
||||
|
||||
let is_running = || {
|
||||
!Command::new("pidof").arg("serai-node").output().unwrap().stdout.is_empty()
|
||||
};
|
||||
|
||||
// Spawn a fresh Serai node
|
||||
let mut command = {
|
||||
use core::time::Duration;
|
||||
use std::path::Path;
|
||||
|
||||
// Make sure a node isn't already running
|
||||
assert!(!is_running());
|
||||
|
||||
let node = {
|
||||
let this_crate = Path::new(env!("CARGO_MANIFEST_DIR"));
|
||||
let top_level = this_crate.join("../../../");
|
||||
top_level.join("target/debug/serai-node")
|
||||
};
|
||||
|
||||
let command = Command::new(node).arg("--dev").spawn().unwrap();
|
||||
while Serai::new(common::URL).await.is_err() {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
let serai = serai().await;
|
||||
while serai.get_latest_block_hash().await.is_err() {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
// TODO: https://github.com/serai-dex/serai/247
|
||||
if std::env::var("GITHUB_CI") == Ok("true".to_string()) {
|
||||
tokio::time::sleep(Duration::from_secs(60)).await;
|
||||
}
|
||||
|
||||
// Sanity check the pidof command is well-formed
|
||||
assert!(is_running());
|
||||
|
||||
command
|
||||
};
|
||||
|
||||
let local = tokio::task::LocalSet::new();
|
||||
local.run_until(async move {
|
||||
if let Err(err) = tokio::task::spawn_local(async move { $body }).await {
|
||||
drop(guard);
|
||||
let _ = command.kill();
|
||||
Err(err).unwrap()
|
||||
} else {
|
||||
command.kill().unwrap();
|
||||
}
|
||||
assert!(!is_running());
|
||||
}).await;
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
51
substrate/client/tests/common/tx.rs
Normal file
51
substrate/client/tests/common/tx.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use core::time::Duration;
|
||||
|
||||
use tokio::time::sleep;
|
||||
|
||||
use serai_client::subxt::{config::Header, utils::Encoded};
|
||||
|
||||
use crate::common::serai;
|
||||
|
||||
#[allow(dead_code)]
|
||||
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()
|
||||
.header
|
||||
.number();
|
||||
|
||||
serai.publish(tx).await.unwrap();
|
||||
|
||||
// Get the block it was included in
|
||||
// TODO: Add an RPC method for this/check the guarantee on the subscription
|
||||
let mut ticks = 0;
|
||||
loop {
|
||||
latest += 1;
|
||||
|
||||
let block = {
|
||||
let mut block;
|
||||
while {
|
||||
block = serai.get_block_by_number(latest).await.unwrap();
|
||||
block.is_none()
|
||||
} {
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
ticks += 1;
|
||||
|
||||
if ticks > 60 {
|
||||
panic!("60 seconds without inclusion in a finalized block");
|
||||
}
|
||||
}
|
||||
block.unwrap()
|
||||
};
|
||||
|
||||
for extrinsic in block.extrinsics {
|
||||
if extrinsic.0 == tx.0[2 ..] {
|
||||
return block.header.hash().into();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
47
substrate/client/tests/common/validator_sets.rs
Normal file
47
substrate/client/tests/common/validator_sets.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use sp_core::Pair;
|
||||
|
||||
use serai_client::{
|
||||
subxt::config::extrinsic_params::BaseExtrinsicParamsBuilder,
|
||||
primitives::{SeraiAddress, insecure_pair_from_name},
|
||||
validator_sets::{
|
||||
primitives::{ValidatorSet, KeyPair},
|
||||
ValidatorSetsEvent,
|
||||
},
|
||||
PairSigner, Serai,
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn vote_in_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
let pair = insecure_pair_from_name("Alice");
|
||||
let public = pair.public();
|
||||
|
||||
let serai = serai().await;
|
||||
|
||||
// Vote in a key pair
|
||||
let address = SeraiAddress::from(pair.public());
|
||||
let block = publish_tx(
|
||||
&serai
|
||||
.sign(
|
||||
&PairSigner::new(pair),
|
||||
&Serai::vote(set.network, key_pair.clone()),
|
||||
serai.get_nonce(&address).await.unwrap(),
|
||||
BaseExtrinsicParamsBuilder::new(),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.await;
|
||||
|
||||
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));
|
||||
|
||||
block
|
||||
}
|
||||
Reference in New Issue
Block a user