mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Resolve #327
Also runs `cargo update` and moves where we install the wasm toolchain in the Dockerfile for better caching properties.
This commit is contained in:
@@ -14,15 +14,13 @@ use serai_client::{
|
||||
primitives::{Batch, SignedBatch, batch_message},
|
||||
InInstructionsEvent,
|
||||
},
|
||||
SeraiInInstructions,
|
||||
SeraiInInstructions, Serai,
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx, validator_sets::set_keys};
|
||||
use crate::common::{tx::publish_tx, validator_sets::set_keys};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
||||
let serai = serai().await;
|
||||
|
||||
pub async fn provide_batch(serai: &Serai, batch: Batch) -> [u8; 32] {
|
||||
// TODO: Get the latest session
|
||||
let set = ValidatorSet { session: Session(0), network: batch.network };
|
||||
let pair = insecure_pair_from_name(&format!("ValidatorSet {:?}", set));
|
||||
@@ -32,15 +30,18 @@ pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
||||
keys
|
||||
} else {
|
||||
let keys = (pair.public(), vec![].try_into().unwrap());
|
||||
set_keys(set, keys.clone()).await;
|
||||
set_keys(serai, set, keys.clone()).await;
|
||||
keys
|
||||
};
|
||||
assert_eq!(keys.0, pair.public());
|
||||
|
||||
let block = publish_tx(&SeraiInInstructions::execute_batch(SignedBatch {
|
||||
batch: batch.clone(),
|
||||
signature: pair.sign(&batch_message(&batch)),
|
||||
}))
|
||||
let block = publish_tx(
|
||||
serai,
|
||||
&SeraiInInstructions::execute_batch(SignedBatch {
|
||||
batch: batch.clone(),
|
||||
signature: pair.sign(&batch_message(&batch)),
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
let batches = serai.as_of(block).in_instructions().batch_events().await.unwrap();
|
||||
|
||||
@@ -1,89 +1,57 @@
|
||||
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)*) => {
|
||||
($($name: ident: $test: expr)*) => {
|
||||
$(
|
||||
#[tokio::test]
|
||||
async fn $name() {
|
||||
use std::process::Command;
|
||||
use dockertest::{
|
||||
PullPolicy, StartPolicy, LogOptions, LogAction, LogPolicy, LogSource, Image, Composition,
|
||||
DockerTest,
|
||||
};
|
||||
|
||||
let guard = common::SEQUENTIAL.lock().await;
|
||||
serai_docker_tests::build("serai".to_string());
|
||||
|
||||
let is_running = || {
|
||||
!(
|
||||
if let Ok(res) = Command::new("pidof").arg("serai-node").output() {
|
||||
res
|
||||
} else {
|
||||
Command::new("pgrep")
|
||||
.arg("serai-node")
|
||||
.output()
|
||||
.expect("neither pidof nor pgrep were available")
|
||||
let mut composition = Composition::with_image(
|
||||
Image::with_repository("serai-dev-serai").pull_policy(PullPolicy::Never),
|
||||
)
|
||||
.with_cmd(vec![
|
||||
"serai-node".to_string(),
|
||||
"--dev".to_string(),
|
||||
"--unsafe-rpc-external".to_string(),
|
||||
"--rpc-cors".to_string(),
|
||||
"all".to_string(),
|
||||
])
|
||||
.with_start_policy(StartPolicy::Strict)
|
||||
.with_log_options(Some(LogOptions {
|
||||
action: LogAction::Forward,
|
||||
policy: LogPolicy::Always,
|
||||
source: LogSource::Both,
|
||||
}));
|
||||
composition.publish_all_ports();
|
||||
|
||||
let handle = composition.handle();
|
||||
|
||||
let mut test = DockerTest::new();
|
||||
test.add_composition(composition);
|
||||
test.run_async(|ops| async move {
|
||||
// Sleep until the Substrate RPC starts
|
||||
let serai_rpc = ops.handle(&handle).host_port(9944).unwrap();
|
||||
let serai_rpc = format!("ws://{}:{}", serai_rpc.0, serai_rpc.1);
|
||||
// Bound execution to 60 seconds
|
||||
for _ in 0 .. 60 {
|
||||
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
|
||||
let Ok(client) = Serai::new(&serai_rpc).await else { continue };
|
||||
if client.latest_block_hash().await.is_err() {
|
||||
continue;
|
||||
}
|
||||
).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;
|
||||
break;
|
||||
}
|
||||
let serai = serai().await;
|
||||
while serai.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());
|
||||
#[allow(clippy::redundant_closure_call)]
|
||||
$test(Serai::new(&serai_rpc).await.unwrap()).await;
|
||||
}).await;
|
||||
}
|
||||
)*
|
||||
|
||||
@@ -2,14 +2,10 @@ use core::time::Duration;
|
||||
|
||||
use tokio::time::sleep;
|
||||
|
||||
use serai_client::subxt::utils::Encoded;
|
||||
|
||||
use crate::common::serai;
|
||||
use serai_client::{subxt::utils::Encoded, Serai};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn publish_tx(tx: &Encoded) -> [u8; 32] {
|
||||
let serai = serai().await;
|
||||
|
||||
pub async fn publish_tx(serai: &Serai, tx: &Encoded) -> [u8; 32] {
|
||||
let mut latest =
|
||||
serai.block(serai.latest_block_hash().await.unwrap()).await.unwrap().unwrap().number();
|
||||
|
||||
|
||||
@@ -15,17 +15,16 @@ use serai_client::{
|
||||
primitives::{ValidatorSet, KeyPair, musig_context, musig_key, set_keys_message},
|
||||
ValidatorSetsEvent,
|
||||
},
|
||||
SeraiValidatorSets,
|
||||
SeraiValidatorSets, Serai,
|
||||
};
|
||||
|
||||
use crate::common::{serai, tx::publish_tx};
|
||||
use crate::common::tx::publish_tx;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
pub async fn set_keys(serai: &Serai, set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
let pair = insecure_pair_from_name("Alice");
|
||||
let public = pair.public();
|
||||
|
||||
let serai = serai().await;
|
||||
let public_key = <Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut public.0.as_ref()).unwrap();
|
||||
assert_eq!(
|
||||
serai
|
||||
@@ -71,11 +70,10 @@ pub async fn set_keys(set: ValidatorSet, key_pair: KeyPair) -> [u8; 32] {
|
||||
);
|
||||
|
||||
// Vote in a key pair
|
||||
let block = publish_tx(&SeraiValidatorSets::set_keys(
|
||||
set.network,
|
||||
key_pair.clone(),
|
||||
Signature(sig.to_bytes()),
|
||||
))
|
||||
let block = publish_tx(
|
||||
serai,
|
||||
&SeraiValidatorSets::set_keys(set.network, key_pair.clone(), Signature(sig.to_bytes())),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user