2023-09-29 07:51:01 +00:00
|
|
|
use blake2::{
|
|
|
|
digest::{consts::U32, Digest},
|
|
|
|
Blake2b,
|
|
|
|
};
|
|
|
|
|
|
|
|
use scale::Encode;
|
|
|
|
|
2023-03-31 12:11:23 +00:00
|
|
|
use sp_core::Pair;
|
|
|
|
|
|
|
|
use serai_client::{
|
|
|
|
primitives::insecure_pair_from_name,
|
|
|
|
validator_sets::primitives::{Session, ValidatorSet},
|
|
|
|
in_instructions::{
|
2023-05-13 08:20:13 +00:00
|
|
|
primitives::{Batch, SignedBatch, batch_message},
|
2023-03-31 12:11:23 +00:00
|
|
|
InInstructionsEvent,
|
|
|
|
},
|
2023-10-23 04:33:38 +00:00
|
|
|
SeraiInInstructions, Serai,
|
2023-03-31 12:11:23 +00:00
|
|
|
};
|
|
|
|
|
2023-10-23 04:33:38 +00:00
|
|
|
use crate::common::{tx::publish_tx, validator_sets::set_keys};
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2023-10-23 04:33:38 +00:00
|
|
|
pub async fn provide_batch(serai: &Serai, batch: Batch) -> [u8; 32] {
|
2023-03-31 12:11:23 +00:00
|
|
|
// TODO: Get the latest session
|
|
|
|
let set = ValidatorSet { session: Session(0), network: batch.network };
|
|
|
|
let pair = insecure_pair_from_name(&format!("ValidatorSet {:?}", set));
|
2023-10-10 10:53:24 +00:00
|
|
|
let keys = if let Some(keys) =
|
2023-10-14 06:47:58 +00:00
|
|
|
serai.with_current_latest_block().await.unwrap().validator_sets().keys(set).await.unwrap()
|
2023-10-10 10:53:24 +00:00
|
|
|
{
|
2023-03-31 12:11:23 +00:00
|
|
|
keys
|
|
|
|
} else {
|
|
|
|
let keys = (pair.public(), vec![].try_into().unwrap());
|
2023-10-23 04:33:38 +00:00
|
|
|
set_keys(serai, set, keys.clone()).await;
|
2023-03-31 12:11:23 +00:00
|
|
|
keys
|
|
|
|
};
|
|
|
|
assert_eq!(keys.0, pair.public());
|
|
|
|
|
2023-10-23 04:33:38 +00:00
|
|
|
let block = publish_tx(
|
|
|
|
serai,
|
|
|
|
&SeraiInInstructions::execute_batch(SignedBatch {
|
|
|
|
batch: batch.clone(),
|
|
|
|
signature: pair.sign(&batch_message(&batch)),
|
|
|
|
}),
|
|
|
|
)
|
2023-03-31 12:11:23 +00:00
|
|
|
.await;
|
2023-03-31 10:34:09 +00:00
|
|
|
|
2023-10-14 06:47:58 +00:00
|
|
|
let batches = serai.as_of(block).in_instructions().batch_events().await.unwrap();
|
2023-03-31 10:34:09 +00:00
|
|
|
// TODO: impl From<Batch> for BatchEvent?
|
|
|
|
assert_eq!(
|
|
|
|
batches,
|
2023-09-29 07:51:01 +00:00
|
|
|
vec![InInstructionsEvent::Batch {
|
|
|
|
network: batch.network,
|
|
|
|
id: batch.id,
|
|
|
|
block: batch.block,
|
|
|
|
instructions_hash: Blake2b::<U32>::digest(batch.instructions.encode()).into(),
|
|
|
|
}],
|
2023-03-31 10:34:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: Check the tokens events
|
|
|
|
|
|
|
|
block
|
|
|
|
}
|