From c245bcdc9b6cc25eb5d145f82fb697782f67ef3c Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 25 Aug 2023 21:37:36 -0400 Subject: [PATCH] Extend Batch test with SubstrateBlock/arb Batch signing --- tests/coordinator/Cargo.toml | 2 +- tests/coordinator/src/tests/batch.rs | 75 +++++++++++++++++++++----- tests/coordinator/src/tests/key_gen.rs | 19 ++++--- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/tests/coordinator/Cargo.toml b/tests/coordinator/Cargo.toml index 71ec2e77..c02e1a20 100644 --- a/tests/coordinator/Cargo.toml +++ b/tests/coordinator/Cargo.toml @@ -19,7 +19,7 @@ hex = "0.4" zeroize = { version = "1", default-features = false } rand_core = { version = "0.6", default-features = false } -ciphersuite = { path = "../../crypto/ciphersuite", default-features = false, features = ["ristretto"] } +ciphersuite = { path = "../../crypto/ciphersuite", default-features = false, features = ["ristretto", "secp256k1"] } schnorrkel = { git = "https://github.com/serai-dex/schnorrkel" } dkg = { path = "../../crypto/dkg", default-features = false, features = ["tests"] } diff --git a/tests/coordinator/src/tests/batch.rs b/tests/coordinator/src/tests/batch.rs index 602993ff..bf7d46bf 100644 --- a/tests/coordinator/src/tests/batch.rs +++ b/tests/coordinator/src/tests/batch.rs @@ -6,7 +6,7 @@ use std::{ use zeroize::Zeroizing; use rand_core::{RngCore, OsRng}; -use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto}; +use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto, Secp256k1}; use dkg::Participant; @@ -17,27 +17,27 @@ use serai_client::{ InInstructionsEvent, }, }; -use messages::{sign::SignId, CoordinatorMessage}; +use messages::{sign::SignId, SubstrateContext, CoordinatorMessage}; use crate::{*, tests::*}; -pub async fn batch( +pub async fn batch( processors: &mut [Processor], substrate_key: &Zeroizing<::F>, + network_key: &Zeroizing, + batch: Batch, ) { let mut id = [0; 32]; OsRng.fill_bytes(&mut id); let id = SignId { key: vec![], id, attempt: 0 }; - let block = BlockHash([0x22; 32]); - // Select a random participant to sign first, guaranteeing their inclusion let first_signer = usize::try_from(OsRng.next_u64() % u64::try_from(processors.len()).unwrap()).unwrap(); processors[first_signer] .send_message(messages::coordinator::ProcessorMessage::BatchPreprocess { id: id.clone(), - block, + block: batch.block, preprocess: [u8::try_from(first_signer).unwrap(); 64].to_vec(), }) .await; @@ -54,7 +54,7 @@ pub async fn batch( processor .send_message(messages::coordinator::ProcessorMessage::BatchPreprocess { id: id.clone(), - block, + block: batch.block, preprocess: [u8::try_from(i).unwrap(); 64].to_vec(), }) .await; @@ -141,8 +141,6 @@ pub async fn batch( ); } - let batch = Batch { network: NetworkId::Bitcoin, id: 0, block, instructions: vec![] }; - // Expand to a key pair as Schnorrkel expects // It's the private key + 32-bytes of entropy for nonces + the public key let mut schnorrkel_key_pair = [0; 96]; @@ -187,7 +185,11 @@ pub async fn batch( assert_eq!(batch_events.len(), 1); assert_eq!( batch_events[0], - InInstructionsEvent::Batch { network: NetworkId::Bitcoin, id: 0, block } + InInstructionsEvent::Batch { + network: batch.batch.network, + id: batch.batch.id, + block: batch.batch.block + } ); break 'outer; } @@ -195,7 +197,43 @@ pub async fn batch( } } - // TODO: Verify the coordinator sends SubstrateBlock to all processors + // Verify the coordinator sends SubstrateBlock to all processors + for processor in processors.iter_mut() { + assert_eq!( + processor.recv_message().await, + messages::CoordinatorMessage::Substrate( + messages::substrate::CoordinatorMessage::SubstrateBlock { + context: SubstrateContext { + serai_time: serai + .get_block_by_number(last_serai_block) + .await + .unwrap() + .unwrap() + .time() + .unwrap() / + 1000, + network_latest_finalized_block: batch.batch.block, + }, + network: batch.batch.network, + block: last_serai_block, + key: (C::generator() * **network_key).to_bytes().as_ref().to_vec(), + burns: vec![], + batches: vec![batch.batch.id], + } + ) + ); + + // Send the ack as expected, though it shouldn't trigger any observable behavior + processor + .send_message(messages::ProcessorMessage::Coordinator( + messages::coordinator::ProcessorMessage::SubstrateBlockAck { + network: batch.batch.network, + block: last_serai_block, + plans: vec![], + }, + )) + .await; + } } #[tokio::test] @@ -220,8 +258,19 @@ async fn batch_test() { } let mut processors = new_processors; - let substrate_key = key_gen(&mut processors).await; - batch(&mut processors, &substrate_key).await; + let (substrate_key, network_key) = key_gen::(&mut processors).await; + batch::( + &mut processors, + &substrate_key, + &network_key, + Batch { + network: NetworkId::Bitcoin, + id: 0, + block: BlockHash([0x22; 32]), + instructions: vec![], + }, + ) + .await; }) .await; } diff --git a/tests/coordinator/src/tests/key_gen.rs b/tests/coordinator/src/tests/key_gen.rs index 1401e139..f650c14f 100644 --- a/tests/coordinator/src/tests/key_gen.rs +++ b/tests/coordinator/src/tests/key_gen.rs @@ -8,7 +8,7 @@ use rand_core::OsRng; use ciphersuite::{ group::{ff::Field, GroupEncoding}, - Ciphersuite, Ristretto, + Ciphersuite, Ristretto, Secp256k1, }; use dkg::{Participant, ThresholdParams}; @@ -21,7 +21,9 @@ use messages::{key_gen::KeyGenId, CoordinatorMessage}; use crate::{*, tests::*}; -pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing<::F> { +pub async fn key_gen( + processors: &mut [Processor], +) -> (Zeroizing<::F>, Zeroizing) { let participant_from_i = |i: usize| Participant::new(u16::try_from(i + 1).unwrap()).unwrap(); let set = ValidatorSet { session: Session(0), network: NetworkId::Bitcoin }; @@ -76,6 +78,9 @@ pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing<::F::random(&mut OsRng)); let substrate_key = (::generator() * *substrate_priv_key).to_bytes(); + let network_priv_key = Zeroizing::new(C::F::random(&mut OsRng)); + let network_key = (C::generator() * *network_priv_key).to_bytes().as_ref().to_vec(); + let serai = processors[0].serai().await; let mut last_serai_block = serai.get_latest_block().await.unwrap().number(); @@ -99,7 +104,7 @@ pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing< Zeroizing< panic!("coordinator didn't respond with ConfirmKeyPair"), } @@ -159,10 +164,10 @@ pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing<(&mut processors).await; }) .await; }