Skip to content

Commit

Permalink
Use [u8; 64] where possible in the processor messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Nov 25, 2023
1 parent 229dfbb commit fd7966b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 27 deletions.
6 changes: 3 additions & 3 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ async fn handle_processor_message<D: Db, P: P2p>(
vec![Transaction::SubstratePreprocess(SignData {
plan: id.id,
attempt: id.attempt,
data: preprocesses,
data: preprocesses.into_iter().map(Into::into).collect(),
signed: Transaction::empty_signed(),
})]
}
Expand All @@ -612,7 +612,7 @@ async fn handle_processor_message<D: Db, P: P2p>(
};
id.encode()
},
preprocesses,
preprocesses.into_iter().map(Into::into).collect(),
);

let intended = Transaction::Batch(
Expand Down Expand Up @@ -681,7 +681,7 @@ async fn handle_processor_message<D: Db, P: P2p>(
vec![Transaction::SubstratePreprocess(SignData {
plan: id.id,
attempt: id.attempt,
data: preprocesses,
data: preprocesses.into_iter().map(Into::into).collect(),
signed: Transaction::empty_signed(),
})]
}
Expand Down
14 changes: 12 additions & 2 deletions coordinator/src/tributary/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,16 @@ pub(crate) async fn handle_application_tx<
}

Transaction::SubstratePreprocess(data) => {
let Ok(_) = check_sign_data_len::<D>(txn, spec, data.signed.signer, data.data.len()) else {
let signer = data.signed.signer;
let Ok(_) = check_sign_data_len::<D>(txn, spec, signer, data.data.len()) else {
return;
};
for data in &data.data {
if data.len() != 64 {
fatal_slash::<D>(txn, genesis, signer.to_bytes(), "non-64-byte Substrate preprocess");
return;
}
}
match handle(
txn,
&DataSpecification {
Expand All @@ -578,7 +585,10 @@ pub(crate) async fn handle_application_tx<
spec.set().network,
coordinator::CoordinatorMessage::SubstratePreprocesses {
id: SubstrateSignId { key, id: data.plan, attempt: data.attempt },
preprocesses,
preprocesses: preprocesses
.into_iter()
.map(|(k, v)| (k, v.try_into().unwrap()))
.collect(),
},
)
.await;
Expand Down
2 changes: 1 addition & 1 deletion message-queue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ serai-db = { path = "../common/db", optional = true }

serai-env = { path = "../common/env" }

serai-primitives = { path = "../substrate/primitives" }
serai-primitives = { path = "../substrate/primitives", features = ["borsh", "serde"] }

jsonrpsee = { version = "0.16", default-features = false, features = ["server"], optional = true }
simple-request = { path = "../common/request", default-features = false }
Expand Down
7 changes: 3 additions & 4 deletions processor/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ pub mod coordinator {
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub enum CoordinatorMessage {
CosignSubstrateBlock { id: SubstrateSignId, block_number: u64 },
// Uses Vec<u8> instead of [u8; 64] since serde Deserialize isn't implemented for [u8; 64]
SubstratePreprocesses { id: SubstrateSignId, preprocesses: HashMap<Participant, Vec<u8>> },
SubstratePreprocesses { id: SubstrateSignId, preprocesses: HashMap<Participant, [u8; 64]> },
SubstrateShares { id: SubstrateSignId, shares: HashMap<Participant, [u8; 32]> },
// Re-attempt a batch signing protocol.
BatchReattempt { id: SubstrateSignId },
Expand Down Expand Up @@ -252,8 +251,8 @@ pub mod coordinator {
pub enum ProcessorMessage {
SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec<PlanMeta> },
InvalidParticipant { id: SubstrateSignId, participant: Participant },
CosignPreprocess { id: SubstrateSignId, preprocesses: Vec<Vec<u8>> },
BatchPreprocess { id: SubstrateSignId, block: BlockHash, preprocesses: Vec<Vec<u8>> },
CosignPreprocess { id: SubstrateSignId, preprocesses: Vec<[u8; 64]> },
BatchPreprocess { id: SubstrateSignId, block: BlockHash, preprocesses: Vec<[u8; 64]> },
SubstrateShare { id: SubstrateSignId, shares: Vec<[u8; 32]> },
CosignedBlock { block_number: u64, block: [u8; 32], signature: Vec<u8> },
}
Expand Down
2 changes: 1 addition & 1 deletion processor/src/batch_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<D: Db> BatchSigner<D> {

let (machine, preprocess) = machine.preprocess(&mut OsRng);
machines.push(machine);
serialized_preprocesses.push(preprocess.serialize());
serialized_preprocesses.push(preprocess.serialize().try_into().unwrap());
preprocesses.push(preprocess);
}
self.preprocessing.insert(id, (machines, preprocesses));
Expand Down
2 changes: 1 addition & 1 deletion processor/src/cosigner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Cosigner {

let (machine, preprocess) = machine.preprocess(&mut OsRng);
machines.push(machine);
serialized_preprocesses.push(preprocess.serialize());
serialized_preprocesses.push(preprocess.serialize().try_into().unwrap());
preprocesses.push(preprocess);
}
let preprocessing = Some((machines, preprocesses));
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub const MAX_ADDRESS_LEN: u32 = 196;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExternalAddress(
#[cfg_attr(
feature = "std",
feature = "borsh",
borsh(
serialize_with = "borsh_serialize_bounded_vec",
deserialize_with = "borsh_deserialize_bounded_vec"
Expand Down Expand Up @@ -102,7 +102,7 @@ pub const MAX_DATA_LEN: u32 = 512;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Data(
#[cfg_attr(
feature = "std",
feature = "borsh",
borsh(
serialize_with = "borsh_serialize_bounded_vec",
deserialize_with = "borsh_deserialize_bounded_vec"
Expand Down
4 changes: 2 additions & 2 deletions substrate/primitives/src/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Serialize, Deserialize};

use sp_core::{ConstU32, bounded::BoundedVec};

#[cfg(feature = "std")]
#[cfg(feature = "borsh")]
use crate::{borsh_serialize_bounded_vec, borsh_deserialize_bounded_vec};

/// The type used to identify networks.
Expand Down Expand Up @@ -112,7 +112,7 @@ pub const MAX_COINS_PER_NETWORK: u32 = 8;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Network {
#[cfg_attr(
feature = "std",
feature = "borsh",
borsh(
serialize_with = "borsh_serialize_bounded_vec",
deserialize_with = "borsh_deserialize_bounded_vec"
Expand Down
2 changes: 1 addition & 1 deletion tests/coordinator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Processor {
res
.send_message(messages::coordinator::ProcessorMessage::CosignPreprocess {
id: id.clone(),
preprocesses: vec![vec![raw_i; 64]],
preprocesses: vec![[raw_i; 64]],
})
.await;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/coordinator/src/tests/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn batch(
.send_message(messages::coordinator::ProcessorMessage::BatchPreprocess {
id: id.clone(),
block: batch.block,
preprocesses: vec![[processor_is[i]; 64].to_vec()],
preprocesses: vec![[processor_is[i]; 64]],
})
.await;
}
Expand All @@ -77,7 +77,7 @@ pub async fn batch(
.send_message(messages::coordinator::ProcessorMessage::BatchPreprocess {
id: id.clone(),
block: batch.block,
preprocesses: vec![[processor_is[excluded_signer]; 64].to_vec()],
preprocesses: vec![[processor_is[excluded_signer]; 64]],
})
.await;

Expand All @@ -98,7 +98,7 @@ pub async fn batch(

let mut participants = preprocesses.keys().cloned().collect::<HashSet<_>>();
for (p, preprocess) in preprocesses {
assert_eq!(preprocess, vec![u8::try_from(u16::from(p)).unwrap(); 64]);
assert_eq!(preprocess, [u8::try_from(u16::from(p)).unwrap(); 64]);
}
participants.insert(known_signer_i);
participants
Expand All @@ -116,7 +116,7 @@ pub async fn batch(
let mut preprocesses = participants
.clone()
.into_iter()
.map(|i| (i, [u8::try_from(u16::from(i)).unwrap(); 64].to_vec()))
.map(|i| (i, [u8::try_from(u16::from(i)).unwrap(); 64]))
.collect::<HashMap<_, _>>();
preprocesses.remove(&i);

Expand Down
8 changes: 4 additions & 4 deletions tests/coordinator/src/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn sign<C: Ciphersuite>(
processor
.send_message(messages::sign::ProcessorMessage::Preprocess {
id: id.clone(),
preprocesses: vec![[processor_is[i]; 64].to_vec()],
preprocesses: vec![vec![processor_is[i]; 128]],
})
.await;
}
Expand All @@ -65,7 +65,7 @@ pub async fn sign<C: Ciphersuite>(
processors[excluded_signer]
.send_message(messages::sign::ProcessorMessage::Preprocess {
id: id.clone(),
preprocesses: vec![[processor_is[excluded_signer]; 64].to_vec()],
preprocesses: vec![vec![processor_is[excluded_signer]; 128]],
})
.await;

Expand All @@ -83,7 +83,7 @@ pub async fn sign<C: Ciphersuite>(

let mut participants = preprocesses.keys().cloned().collect::<HashSet<_>>();
for (p, preprocess) in preprocesses {
assert_eq!(preprocess, vec![u8::try_from(u16::from(p)).unwrap(); 64]);
assert_eq!(preprocess, vec![u8::try_from(u16::from(p)).unwrap(); 128]);
}
participants.insert(known_signer_i);
participants
Expand All @@ -101,7 +101,7 @@ pub async fn sign<C: Ciphersuite>(
let mut preprocesses = participants
.clone()
.into_iter()
.map(|i| (i, [u8::try_from(u16::from(i)).unwrap(); 64].to_vec()))
.map(|i| (i, vec![u8::try_from(u16::from(i)).unwrap(); 128]))
.collect::<HashMap<_, _>>();
preprocesses.remove(&i);

Expand Down
4 changes: 2 additions & 2 deletions tests/processor/src/tests/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) async fn recv_batch_preprocesses(
substrate_key: &[u8; 32],
batch: &Batch,
attempt: u32,
) -> (SubstrateSignId, HashMap<Participant, Vec<u8>>) {
) -> (SubstrateSignId, HashMap<Participant, [u8; 64]>) {
let id = SubstrateSignId {
key: *substrate_key,
id: SubstrateSignableId::Batch((batch.network, batch.id).encode().try_into().unwrap()),
Expand Down Expand Up @@ -87,7 +87,7 @@ pub(crate) async fn sign_batch(
coordinators: &mut [Coordinator],
key: [u8; 32],
id: SubstrateSignId,
preprocesses: HashMap<Participant, Vec<u8>>,
preprocesses: HashMap<Participant, [u8; 64]>,
) -> SignedBatch {
assert_eq!(preprocesses.len(), THRESHOLD);

Expand Down

0 comments on commit fd7966b

Please sign in to comment.