Skip to content

Commit

Permalink
Correct the coordinator tests
Browse files Browse the repository at this point in the history
They assumed processor 0 had keys `i = 1`. Under the new validator-set code,
the first key is the one with the highest amount, In case of tie, the key (or
as of the last commit, a Blake hash) decides order.

This commit kludges in a mapping from processor index to assigned key index, no
longer assuming its value.
  • Loading branch information
kayabaNerve committed Oct 10, 2023
1 parent ab5af57 commit 9bf8c92
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 70 deletions.
48 changes: 23 additions & 25 deletions tests/coordinator/src/tests/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{*, tests::*};

pub async fn batch(
processors: &mut [Processor],
processor_is: &[u8],
substrate_key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
batch: Batch,
) -> u64 {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub async fn batch(
.send_message(messages::coordinator::ProcessorMessage::BatchPreprocess {
id: id.clone(),
block: batch.block,
preprocess: [u8::try_from(i).unwrap(); 64].to_vec(),
preprocess: [processor_is[i]; 64].to_vec(),
})
.await;
}
Expand All @@ -73,7 +74,7 @@ pub async fn batch(
.send_message(messages::coordinator::ProcessorMessage::BatchPreprocess {
id: id.clone(),
block: batch.block,
preprocess: [u8::try_from(excluded_signer).unwrap(); 64].to_vec(),
preprocess: [processor_is[excluded_signer]; 64].to_vec(),
})
.await;

Expand All @@ -86,36 +87,32 @@ pub async fn batch(
) => {
assert_eq!(&id, &this_id);
assert_eq!(preprocesses.len(), THRESHOLD - 1);
assert!(!preprocesses
.contains_key(&Participant::new(u16::try_from(known_signer).unwrap() + 1).unwrap()));
let known_signer_i = Participant::new(u16::from(processor_is[known_signer])).unwrap();
assert!(!preprocesses.contains_key(&known_signer_i));

let mut participants =
preprocesses.keys().map(|p| usize::from(u16::from(*p)) - 1).collect::<HashSet<_>>();
let mut participants = preprocesses.keys().cloned().collect::<HashSet<_>>();
for (p, preprocess) in preprocesses {
assert_eq!(preprocess, vec![u8::try_from(u16::from(p)).unwrap() - 1; 64]);
assert_eq!(preprocess, vec![u8::try_from(u16::from(p)).unwrap(); 64]);
}
participants.insert(known_signer);
participants.insert(known_signer_i);
participants
}
_ => panic!("coordinator didn't send back BatchPreprocesses"),
};

for i in participants.clone() {
if i == known_signer {
if u16::from(i) == u16::from(processor_is[known_signer]) {
continue;
}
let processor = &mut processors[i];

let processor =
&mut processors[processor_is.iter().position(|p_i| u16::from(*p_i) == u16::from(i)).unwrap()];
let mut preprocesses = participants
.clone()
.into_iter()
.map(|i| {
(
Participant::new(u16::try_from(i + 1).unwrap()).unwrap(),
[u8::try_from(i).unwrap(); 64].to_vec(),
)
})
.map(|i| (i, [u8::try_from(u16::from(i)).unwrap(); 64].to_vec()))
.collect::<HashMap<_, _>>();
preprocesses.remove(&Participant::new(u16::try_from(i + 1).unwrap()).unwrap());
preprocesses.remove(&i);

assert_eq!(
processor.recv_message().await,
Expand All @@ -129,25 +126,25 @@ pub async fn batch(
}

for i in participants.clone() {
let processor = &mut processors[i];
let processor =
&mut processors[processor_is.iter().position(|p_i| u16::from(*p_i) == u16::from(i)).unwrap()];
processor
.send_message(messages::coordinator::ProcessorMessage::BatchShare {
id: id.clone(),
share: [u8::try_from(i).unwrap(); 32],
share: [u8::try_from(u16::from(i)).unwrap(); 32],
})
.await;
}
wait_for_tributary().await;
for i in participants.clone() {
let processor = &mut processors[i];
let processor =
&mut processors[processor_is.iter().position(|p_i| u16::from(*p_i) == u16::from(i)).unwrap()];
let mut shares = participants
.clone()
.into_iter()
.map(|i| {
(Participant::new(u16::try_from(i + 1).unwrap()).unwrap(), [u8::try_from(i).unwrap(); 32])
})
.map(|i| (i, [u8::try_from(u16::from(i)).unwrap(); 32]))
.collect::<HashMap<_, _>>();
shares.remove(&Participant::new(u16::try_from(i + 1).unwrap()).unwrap());
shares.remove(&i);

assert_eq!(
processor.recv_message().await,
Expand Down Expand Up @@ -271,9 +268,10 @@ async fn batch_test() {
}
let mut processors = new_processors;

let (substrate_key, _) = key_gen::<Secp256k1>(&mut processors).await;
let (processor_is, substrate_key, _) = key_gen::<Secp256k1>(&mut processors).await;
batch(
&mut processors,
&processor_is,
&substrate_key,
Batch {
network: NetworkId::Bitcoin,
Expand Down
65 changes: 50 additions & 15 deletions tests/coordinator/src/tests/key_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ciphersuite::{
group::{ff::Field, GroupEncoding},
Ciphersuite, Ristretto, Secp256k1,
};
use dkg::{Participant, ThresholdParams};
use dkg::ThresholdParams;

use serai_client::{
primitives::NetworkId,
Expand All @@ -24,21 +24,32 @@ use crate::{*, tests::*};

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();
) -> (Vec<u8>, Zeroizing<<Ristretto as Ciphersuite>::F>, Zeroizing<C::F>) {
let mut participant_is = vec![];

let set = ValidatorSet { session: Session(0), network: NetworkId::Bitcoin };
let id = KeyGenId { set, attempt: 0 };

for (i, processor) in processors.iter_mut().enumerate() {
let msg = processor.recv_message().await;
match &msg {
CoordinatorMessage::KeyGen(messages::key_gen::CoordinatorMessage::GenerateKey {
params,
..
}) => {
participant_is.push(params.i());
}
_ => panic!("unexpected message: {msg:?}"),
}

assert_eq!(
processor.recv_message().await,
msg,
CoordinatorMessage::KeyGen(messages::key_gen::CoordinatorMessage::GenerateKey {
id,
params: ThresholdParams::new(
u16::try_from(((COORDINATORS * 2) / 3) + 1).unwrap(),
u16::try_from(COORDINATORS).unwrap(),
participant_from_i(i),
participant_is[i],
)
.unwrap()
})
Expand All @@ -47,17 +58,22 @@ pub async fn key_gen<C: Ciphersuite>(
processor
.send_message(messages::key_gen::ProcessorMessage::Commitments {
id,
commitments: vec![u8::try_from(i).unwrap()],
commitments: vec![u8::try_from(u16::from(participant_is[i])).unwrap()],
})
.await;
}

wait_for_tributary().await;
for (i, processor) in processors.iter_mut().enumerate() {
let mut commitments = (0 .. u8::try_from(COORDINATORS).unwrap())
.map(|l| (participant_from_i(l.into()), vec![l]))
.map(|l| {
(
participant_is[usize::from(l)],
vec![u8::try_from(u16::from(participant_is[usize::from(l)])).unwrap()],
)
})
.collect::<HashMap<_, _>>();
commitments.remove(&participant_from_i(i));
commitments.remove(&participant_is[i]);
assert_eq!(
processor.recv_message().await,
CoordinatorMessage::KeyGen(messages::key_gen::CoordinatorMessage::Commitments {
Expand All @@ -66,13 +82,20 @@ pub async fn key_gen<C: Ciphersuite>(
})
);

// from (0 .. n), to (1 ..= n)
// Recipient it's for -> (Sender i, Recipient i)
let mut shares = (0 .. u8::try_from(COORDINATORS).unwrap())
.map(|l| (participant_from_i(l.into()), vec![u8::try_from(i).unwrap(), l + 1]))
.map(|l| {
(
participant_is[usize::from(l)],
vec![
u8::try_from(u16::try_from(participant_is[i]).unwrap()).unwrap(),
u8::try_from(u16::from(participant_is[usize::from(l)])).unwrap(),
],
)
})
.collect::<HashMap<_, _>>();

let i = participant_from_i(i);
shares.remove(&i);
shares.remove(&participant_is[i]);
processor.send_message(messages::key_gen::ProcessorMessage::Shares { id, shares }).await;
}

Expand All @@ -87,14 +110,22 @@ pub async fn key_gen<C: Ciphersuite>(

wait_for_tributary().await;
for (i, processor) in processors.iter_mut().enumerate() {
let i = participant_from_i(i);
let i = participant_is[i];
assert_eq!(
processor.recv_message().await,
CoordinatorMessage::KeyGen(messages::key_gen::CoordinatorMessage::Shares {
id,
shares: {
let mut shares = (0 .. u8::try_from(COORDINATORS).unwrap())
.map(|l| (participant_from_i(l.into()), vec![l, u8::try_from(u16::from(i)).unwrap()]))
.map(|l| {
(
participant_is[usize::from(l)],
vec![
u8::try_from(u16::from(participant_is[usize::from(l)])).unwrap(),
u8::try_from(u16::from(i)).unwrap(),
],
)
})
.collect::<HashMap<_, _>>();
shares.remove(&i);
shares
Expand Down Expand Up @@ -172,7 +203,11 @@ pub async fn key_gen<C: Ciphersuite>(
(Public(substrate_key), network_key.try_into().unwrap())
);

(substrate_priv_key, network_priv_key)
(
participant_is.into_iter().map(|i| u8::try_from(u16::from(i)).unwrap()).collect(),
substrate_priv_key,
network_priv_key,
)
}

#[tokio::test]
Expand Down
Loading

0 comments on commit 9bf8c92

Please sign in to comment.