Extend Batch test with SubstrateBlock/arb Batch signing

This commit is contained in:
Luke Parker 2023-08-25 21:37:36 -04:00
parent f249e20028
commit c245bcdc9b
No known key found for this signature in database
3 changed files with 75 additions and 21 deletions
tests/coordinator

View file

@ -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"] }

View file

@ -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<C: Ciphersuite>(
processors: &mut [Processor],
substrate_key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
network_key: &Zeroizing<C::F>,
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::<Secp256k1>(&mut processors).await;
batch::<Secp256k1>(
&mut processors,
&substrate_key,
&network_key,
Batch {
network: NetworkId::Bitcoin,
id: 0,
block: BlockHash([0x22; 32]),
instructions: vec![],
},
)
.await;
})
.await;
}

View file

@ -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<<Ristretto as Ciphersuite>::F> {
pub async fn key_gen<C: Ciphersuite>(
processors: &mut [Processor],
) -> (Zeroizing<<Ristretto as Ciphersuite>::F>, Zeroizing<C::F>) {
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<<Ristretto as Ci
let substrate_priv_key = Zeroizing::new(<Ristretto as Ciphersuite>::F::random(&mut OsRng));
let substrate_key = (<Ristretto as Ciphersuite>::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<<Ristretto as Ci
.send_message(messages::key_gen::ProcessorMessage::GeneratedKeyPair {
id,
substrate_key,
network_key: b"network_key".to_vec(),
network_key: network_key.clone(),
})
.await;
}
@ -148,7 +153,7 @@ pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing<<Ristretto as Ci
assert_eq!(context.network_latest_finalized_block.0, [0; 32]);
assert_eq!(set, this_set);
assert_eq!(key_pair.0 .0, substrate_key);
assert_eq!(key_pair.1.to_vec(), b"network_key".to_vec());
assert_eq!(&key_pair.1, &network_key);
}
_ => panic!("coordinator didn't respond with ConfirmKeyPair"),
}
@ -159,10 +164,10 @@ pub async fn key_gen(processors: &mut [Processor]) -> Zeroizing<<Ristretto as Ci
}
assert_eq!(
serai.get_keys(set).await.unwrap().unwrap(),
(Public(substrate_key), b"network_key".to_vec().try_into().unwrap())
(Public(substrate_key), network_key.try_into().unwrap())
);
substrate_priv_key
(substrate_priv_key, network_priv_key)
}
#[tokio::test]
@ -187,7 +192,7 @@ async fn key_gen_test() {
}
let mut processors = new_processors;
key_gen(&mut processors).await;
key_gen::<Secp256k1>(&mut processors).await;
})
.await;
}