mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Test the processor's Substrate signer
This commit is contained in:
@@ -9,6 +9,8 @@ pub(crate) use scanner::test_scanner;
|
|||||||
mod signer;
|
mod signer;
|
||||||
pub(crate) use signer::{sign, test_signer};
|
pub(crate) use signer::{sign, test_signer};
|
||||||
|
|
||||||
|
mod substrate_signer;
|
||||||
|
|
||||||
mod wallet;
|
mod wallet;
|
||||||
pub(crate) use wallet::test_wallet;
|
pub(crate) use wallet::test_wallet;
|
||||||
|
|
||||||
|
|||||||
139
processor/src/tests/substrate_signer.rs
Normal file
139
processor/src/tests/substrate_signer.rs
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
use std::{
|
||||||
|
time::{Duration, SystemTime},
|
||||||
|
collections::HashMap,
|
||||||
|
};
|
||||||
|
|
||||||
|
use rand_core::OsRng;
|
||||||
|
|
||||||
|
use group::GroupEncoding;
|
||||||
|
use frost::{
|
||||||
|
curve::Ristretto,
|
||||||
|
Participant,
|
||||||
|
dkg::tests::{key_gen, clone_without},
|
||||||
|
};
|
||||||
|
|
||||||
|
use tokio::time::timeout;
|
||||||
|
|
||||||
|
use scale::Encode;
|
||||||
|
use sp_application_crypto::{RuntimePublic, sr25519::Public};
|
||||||
|
|
||||||
|
use serai_client::{primitives::*, in_instructions::primitives::*};
|
||||||
|
|
||||||
|
use messages::{sign::SignId, coordinator::*};
|
||||||
|
use crate::{
|
||||||
|
substrate_signer::{SubstrateSignerEvent, SubstrateSigner},
|
||||||
|
tests::util::db::MemDb,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_substrate_signer() {
|
||||||
|
let mut keys = key_gen::<_, Ristretto>(&mut OsRng);
|
||||||
|
|
||||||
|
let participant_one = Participant::new(1).unwrap();
|
||||||
|
|
||||||
|
let block = BlockHash([0xaa; 32]);
|
||||||
|
let actual_id =
|
||||||
|
SignId { key: keys[&participant_one].group_key().to_bytes().to_vec(), id: block.0, attempt: 0 };
|
||||||
|
|
||||||
|
let batch = Batch {
|
||||||
|
network: MONERO_NET_ID,
|
||||||
|
id: 5,
|
||||||
|
block,
|
||||||
|
instructions: vec![
|
||||||
|
InInstructionWithBalance {
|
||||||
|
instruction: InInstruction::Transfer(SeraiAddress([0xbb; 32])),
|
||||||
|
balance: Balance { coin: BITCOIN, amount: Amount(1000) },
|
||||||
|
},
|
||||||
|
InInstructionWithBalance {
|
||||||
|
instruction: InInstruction::Call(ApplicationCall {
|
||||||
|
application: Application::DEX,
|
||||||
|
data: Data::new(vec![0xcc; 128]).unwrap(),
|
||||||
|
}),
|
||||||
|
balance: Balance { coin: MONERO, amount: Amount(9999999999999999) },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
let signing_set = actual_id.signing_set(&keys[&participant_one].params());
|
||||||
|
for these_keys in keys.values() {
|
||||||
|
assert_eq!(actual_id.signing_set(&these_keys.params()), signing_set);
|
||||||
|
}
|
||||||
|
|
||||||
|
let start = SystemTime::now();
|
||||||
|
let mut signers = HashMap::new();
|
||||||
|
for i in 1 ..= keys.len() {
|
||||||
|
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||||
|
let signer = SubstrateSigner::new(MemDb::new(), keys.remove(&i).unwrap());
|
||||||
|
signer.sign(start, batch.clone()).await;
|
||||||
|
signers.insert(i, signer);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut preprocesses = HashMap::new();
|
||||||
|
for i in &signing_set {
|
||||||
|
if let Some(SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchPreprocess {
|
||||||
|
id,
|
||||||
|
preprocess,
|
||||||
|
})) = signers.get_mut(i).unwrap().events.recv().await
|
||||||
|
{
|
||||||
|
assert_eq!(id, actual_id);
|
||||||
|
preprocesses.insert(*i, preprocess);
|
||||||
|
} else {
|
||||||
|
panic!("didn't get preprocess back");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut shares = HashMap::new();
|
||||||
|
for i in &signing_set {
|
||||||
|
signers[i]
|
||||||
|
.handle(CoordinatorMessage::BatchPreprocesses {
|
||||||
|
id: actual_id.clone(),
|
||||||
|
preprocesses: clone_without(&preprocesses, i),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
if let Some(SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchShare {
|
||||||
|
id,
|
||||||
|
share,
|
||||||
|
})) = signers.get_mut(i).unwrap().events.recv().await
|
||||||
|
{
|
||||||
|
assert_eq!(id, actual_id);
|
||||||
|
shares.insert(*i, share);
|
||||||
|
} else {
|
||||||
|
panic!("didn't get share back");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in &signing_set {
|
||||||
|
signers[i]
|
||||||
|
.handle(CoordinatorMessage::BatchShares {
|
||||||
|
id: actual_id.clone(),
|
||||||
|
shares: clone_without(&shares, i),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if let Some(SubstrateSignerEvent::SignedBatch(signed_batch)) =
|
||||||
|
signers.get_mut(i).unwrap().events.recv().await
|
||||||
|
{
|
||||||
|
assert_eq!(signed_batch.batch, batch);
|
||||||
|
assert!(Public::from_raw(actual_id.key.clone().try_into().unwrap())
|
||||||
|
.verify(&batch.encode(), &signed_batch.signature));
|
||||||
|
} else {
|
||||||
|
panic!("didn't get signed batch back");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the signers not included didn't do anything
|
||||||
|
let mut excluded = (1 ..= signers.len())
|
||||||
|
.map(|i| Participant::new(u16::try_from(i).unwrap()).unwrap())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
for i in signing_set {
|
||||||
|
excluded.remove(excluded.binary_search(&i).unwrap());
|
||||||
|
}
|
||||||
|
for i in excluded {
|
||||||
|
assert!(timeout(
|
||||||
|
Duration::from_secs(5),
|
||||||
|
signers.get_mut(&Participant::new(u16::try_from(i).unwrap()).unwrap()).unwrap().events.recv()
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,6 +83,9 @@ pub mod pallet {
|
|||||||
block: batch.block,
|
block: batch.block,
|
||||||
});
|
});
|
||||||
for (i, instruction) in batch.instructions.drain(..).enumerate() {
|
for (i, instruction) in batch.instructions.drain(..).enumerate() {
|
||||||
|
// TODO: Check this balance's coin belongs to this network
|
||||||
|
// If they don't, the validator set should be completely slashed, without question
|
||||||
|
|
||||||
if Self::execute(instruction).is_err() {
|
if Self::execute(instruction).is_err() {
|
||||||
Self::deposit_event(Event::InstructionFailure {
|
Self::deposit_event(Event::InstructionFailure {
|
||||||
network: batch.network,
|
network: batch.network,
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ pub enum Application {
|
|||||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||||
#[cfg_attr(feature = "std", derive(Zeroize, Serialize, Deserialize))]
|
#[cfg_attr(feature = "std", derive(Zeroize, Serialize, Deserialize))]
|
||||||
pub struct ApplicationCall {
|
pub struct ApplicationCall {
|
||||||
application: Application,
|
pub application: Application,
|
||||||
data: Data,
|
pub data: Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||||
|
|||||||
Reference in New Issue
Block a user