From c03fb6c71b8bd1fa89690ea2c110de2fa5abd942 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 6 Nov 2023 19:50:32 -0500 Subject: [PATCH] Add dedicated BatchSignId --- coins/bitcoin/src/wallet/send.rs | 2 +- coordinator/src/db.rs | 6 +-- coordinator/src/main.rs | 14 +++---- coordinator/src/tests/tributary/mod.rs | 11 ++++-- coordinator/src/tributary/db.rs | 2 +- coordinator/src/tributary/handle.rs | 14 +++---- coordinator/src/tributary/mod.rs | 20 +++++----- coordinator/src/tributary/nonce_decider.rs | 34 +++++++--------- coordinator/src/tributary/scanner.rs | 4 +- processor/messages/src/lib.rs | 21 ++++++---- processor/src/networks/monero.rs | 2 +- processor/src/substrate_signer.rs | 45 +++++++++------------- processor/src/tests/substrate_signer.rs | 17 ++++---- tests/coordinator/src/tests/batch.rs | 8 ++-- tests/coordinator/src/tests/key_gen.rs | 2 +- tests/processor/src/networks.rs | 3 +- tests/processor/src/tests/batch.rs | 23 ++++++----- 17 files changed, 112 insertions(+), 116 deletions(-) diff --git a/coins/bitcoin/src/wallet/send.rs b/coins/bitcoin/src/wallet/send.rs index 81adbda8..e6997c5b 100644 --- a/coins/bitcoin/src/wallet/send.rs +++ b/coins/bitcoin/src/wallet/send.rs @@ -94,7 +94,7 @@ impl SignableTransaction { // the value is fixed size (so any value could be used here) tx.output.push(TxOut { value: Amount::ZERO, script_pubkey: change.script_pubkey() }); } - u64::try_from(tx.weight()).unwrap() + u64::from(tx.weight()) } /// Returns the fee necessary for this transaction to achieve the fee rate specified at diff --git a/coordinator/src/db.rs b/coordinator/src/db.rs index c93ce56b..7be798b6 100644 --- a/coordinator/src/db.rs +++ b/coordinator/src/db.rs @@ -106,14 +106,14 @@ impl MainDb { res } - fn first_preprocess_key(network: NetworkId, id_type: RecognizedIdType, id: [u8; 32]) -> Vec { + fn first_preprocess_key(network: NetworkId, id_type: RecognizedIdType, id: &[u8]) -> Vec { Self::main_key(b"first_preprocess", (network, id_type, id).encode()) } pub fn save_first_preprocess( txn: &mut D::Transaction<'_>, network: NetworkId, id_type: RecognizedIdType, - id: [u8; 32], + id: &[u8], preprocess: Vec>, ) { let preprocess = preprocess.encode(); @@ -128,7 +128,7 @@ impl MainDb { getter: &G, network: NetworkId, id_type: RecognizedIdType, - id: [u8; 32], + id: &[u8], ) -> Option>> { getter .get(Self::first_preprocess_key(network, id_type, id)) diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index ab00e945..905fc4e5 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -488,7 +488,7 @@ async fn handle_processor_message( &mut txn, network, RecognizedIdType::Plan, - id.id, + &id.id, preprocesses, ); @@ -547,7 +547,7 @@ async fn handle_processor_message( &mut txn, spec.set().network, RecognizedIdType::Batch, - id.id, + &id.id, preprocesses, ); @@ -877,7 +877,7 @@ pub async fn run( } }); - move |set: ValidatorSet, genesis, id_type, id, nonce| { + move |set: ValidatorSet, genesis, id_type, id: Vec, nonce| { let mut raw_db = raw_db.clone(); let key = key.clone(); let tributaries = tributaries.clone(); @@ -899,16 +899,16 @@ pub async fn run( let mut tx = match id_type { RecognizedIdType::Batch => Transaction::BatchPreprocess(SignData { - plan: id, + data: get_preprocess(&raw_db, id_type, &id).await, + plan: id.try_into().unwrap(), attempt: 0, - data: get_preprocess(&raw_db, id_type, id).await, signed: Transaction::empty_signed(), }), RecognizedIdType::Plan => Transaction::SignPreprocess(SignData { - plan: id, + data: get_preprocess(&raw_db, id_type, &id).await, + plan: id.try_into().unwrap(), attempt: 0, - data: get_preprocess(&raw_db, id_type, id).await, signed: Transaction::empty_signed(), }), }; diff --git a/coordinator/src/tests/tributary/mod.rs b/coordinator/src/tests/tributary/mod.rs index 4a67d18d..b8061d34 100644 --- a/coordinator/src/tests/tributary/mod.rs +++ b/coordinator/src/tests/tributary/mod.rs @@ -28,8 +28,8 @@ fn random_vec(rng: &mut R, limit: usize) -> Vec { res } -fn random_sign_data(rng: &mut R) -> SignData { - let mut plan = [0; 32]; +fn random_sign_data(rng: &mut R) -> SignData { + let mut plan = [0; N]; rng.fill_bytes(&mut plan); SignData { @@ -80,7 +80,10 @@ fn tx_size_limit() { #[test] fn serialize_sign_data() { - test_read_write(random_sign_data(&mut OsRng)); + test_read_write(random_sign_data::<_, 3>(&mut OsRng)); + test_read_write(random_sign_data::<_, 8>(&mut OsRng)); + test_read_write(random_sign_data::<_, 16>(&mut OsRng)); + test_read_write(random_sign_data::<_, 24>(&mut OsRng)); } #[test] @@ -143,7 +146,7 @@ fn serialize_transaction() { { let mut block = [0; 32]; OsRng.fill_bytes(&mut block); - let mut batch = [0; 32]; + let mut batch = [0; 5]; OsRng.fill_bytes(&mut batch); test_read_write(Transaction::Batch(block, batch)); } diff --git a/coordinator/src/tributary/db.rs b/coordinator/src/tributary/db.rs index aedd37e0..0fb26375 100644 --- a/coordinator/src/tributary/db.rs +++ b/coordinator/src/tributary/db.rs @@ -16,7 +16,7 @@ use crate::tributary::TributarySpec; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Topic { Dkg, - Batch([u8; 32]), + Batch([u8; 5]), Sign([u8; 32]), } diff --git a/coordinator/src/tributary/handle.rs b/coordinator/src/tributary/handle.rs index ebc44248..25334062 100644 --- a/coordinator/src/tributary/handle.rs +++ b/coordinator/src/tributary/handle.rs @@ -18,7 +18,7 @@ use tributary::Signed; use processor_messages::{ key_gen::{self, KeyGenId}, - coordinator, + coordinator::{self, BatchSignId}, sign::{self, SignId}, }; @@ -370,7 +370,7 @@ pub(crate) async fn handle_application_tx< // Because this Batch has achieved synchrony, its batch ID should be authorized TributaryDb::::recognize_topic(txn, genesis, Topic::Batch(batch)); let nonce = NonceDecider::::handle_batch(txn, genesis, batch); - recognized_id(spec.set(), genesis, RecognizedIdType::Batch, batch, nonce).await; + recognized_id(spec.set(), genesis, RecognizedIdType::Batch, batch.to_vec(), nonce).await; } Transaction::SubstrateBlock(block) => { @@ -382,7 +382,7 @@ pub(crate) async fn handle_application_tx< let nonces = NonceDecider::::handle_substrate_block(txn, genesis, &plan_ids); for (nonce, id) in nonces.into_iter().zip(plan_ids.into_iter()) { TributaryDb::::recognize_topic(txn, genesis, Topic::Sign(id)); - recognized_id(spec.set(), genesis, RecognizedIdType::Plan, id, nonce).await; + recognized_id(spec.set(), genesis, RecognizedIdType::Plan, id.to_vec(), nonce).await; } } @@ -403,12 +403,12 @@ pub(crate) async fn handle_application_tx< Accumulation::Ready(DataSet::Participating(mut preprocesses)) => { unflatten(spec, &mut preprocesses); NonceDecider::::selected_for_signing_batch(txn, genesis, data.plan); - let key = TributaryDb::::key_pair(txn, spec.set()).unwrap().0 .0.to_vec(); + let key = TributaryDb::::key_pair(txn, spec.set()).unwrap().0 .0; processors .send( spec.set().network, coordinator::CoordinatorMessage::BatchPreprocesses { - id: SignId { key, id: data.plan, attempt: data.attempt }, + id: BatchSignId { key, id: data.plan, attempt: data.attempt }, preprocesses, }, ) @@ -434,12 +434,12 @@ pub(crate) async fn handle_application_tx< ) { Accumulation::Ready(DataSet::Participating(mut shares)) => { unflatten(spec, &mut shares); - let key = TributaryDb::::key_pair(txn, spec.set()).unwrap().0 .0.to_vec(); + let key = TributaryDb::::key_pair(txn, spec.set()).unwrap().0 .0; processors .send( spec.set().network, coordinator::CoordinatorMessage::BatchShares { - id: SignId { key, id: data.plan, attempt: data.attempt }, + id: BatchSignId { key, id: data.plan, attempt: data.attempt }, shares: shares .into_iter() .map(|(validator, share)| (validator, share.try_into().unwrap())) diff --git a/coordinator/src/tributary/mod.rs b/coordinator/src/tributary/mod.rs index 9d8c5a0b..2284d77b 100644 --- a/coordinator/src/tributary/mod.rs +++ b/coordinator/src/tributary/mod.rs @@ -167,8 +167,8 @@ impl TributarySpec { } #[derive(Clone, PartialEq, Eq, Debug)] -pub struct SignData { - pub plan: [u8; 32], +pub struct SignData { + pub plan: [u8; N], pub attempt: u32, pub data: Vec>, @@ -176,9 +176,9 @@ pub struct SignData { pub signed: Signed, } -impl ReadWrite for SignData { +impl ReadWrite for SignData { fn read(reader: &mut R) -> io::Result { - let mut plan = [0; 32]; + let mut plan = [0; N]; reader.read_exact(&mut plan)?; let mut attempt = [0; 4]; @@ -249,16 +249,16 @@ pub enum Transaction { // which would be binding over the block hash and automatically achieve synchrony on all // relevant batches. ExternalBlock was removed for this due to complexity around the pipeline // with the current processor, yet it would still be an improvement. - Batch([u8; 32], [u8; 32]), + Batch([u8; 32], [u8; 5]), // When a Serai block is finalized, with the contained batches, we can allow the associated plan // IDs SubstrateBlock(u64), - BatchPreprocess(SignData), - BatchShare(SignData), + BatchPreprocess(SignData<5>), + BatchShare(SignData<5>), - SignPreprocess(SignData), - SignShare(SignData), + SignPreprocess(SignData<32>), + SignShare(SignData<32>), // This is defined as an Unsigned transaction in order to de-duplicate SignCompleted amongst // reporters (who should all report the same thing) // We do still track the signer in order to prevent a single signer from publishing arbitrarily @@ -367,7 +367,7 @@ impl ReadWrite for Transaction { 3 => { let mut block = [0; 32]; reader.read_exact(&mut block)?; - let mut batch = [0; 32]; + let mut batch = [0; 5]; reader.read_exact(&mut batch)?; Ok(Transaction::Batch(block, batch)) } diff --git a/coordinator/src/tributary/nonce_decider.rs b/coordinator/src/tributary/nonce_decider.rs index 55cd6295..d2f1a918 100644 --- a/coordinator/src/tributary/nonce_decider.rs +++ b/coordinator/src/tributary/nonce_decider.rs @@ -27,31 +27,25 @@ impl NonceDecider { next } - fn item_nonce_key(genesis: [u8; 32], code: u8, id: [u8; 32]) -> Vec { + fn item_nonce_key(genesis: [u8; 32], code: u8, id: &[u8]) -> Vec { D::key( b"coordinator_tributary_nonce", b"item", - [genesis.as_slice(), [code].as_ref(), id.as_ref()].concat(), + [genesis.as_slice(), [code].as_ref(), id].concat(), ) } - fn set_nonce( - txn: &mut D::Transaction<'_>, - genesis: [u8; 32], - code: u8, - id: [u8; 32], - nonce: u32, - ) { + fn set_nonce(txn: &mut D::Transaction<'_>, genesis: [u8; 32], code: u8, id: &[u8], nonce: u32) { txn.put(Self::item_nonce_key(genesis, code, id), nonce.to_le_bytes()) } - fn db_nonce(getter: &G, genesis: [u8; 32], code: u8, id: [u8; 32]) -> Option { + fn db_nonce(getter: &G, genesis: [u8; 32], code: u8, id: &[u8]) -> Option { getter .get(Self::item_nonce_key(genesis, code, id)) .map(|bytes| u32::from_le_bytes(bytes.try_into().unwrap())) } - pub fn handle_batch(txn: &mut D::Transaction<'_>, genesis: [u8; 32], batch: [u8; 32]) -> u32 { + pub fn handle_batch(txn: &mut D::Transaction<'_>, genesis: [u8; 32], batch: [u8; 5]) -> u32 { let nonce_for = Self::allocate_nonce(txn, genesis); - Self::set_nonce(txn, genesis, BATCH_CODE, batch, nonce_for); + Self::set_nonce(txn, genesis, BATCH_CODE, &batch, nonce_for); nonce_for } // TODO: The processor won't yield shares for this if the signing protocol aborts. We need to @@ -60,10 +54,10 @@ impl NonceDecider { pub fn selected_for_signing_batch( txn: &mut D::Transaction<'_>, genesis: [u8; 32], - batch: [u8; 32], + batch: [u8; 5], ) { let nonce_for = Self::allocate_nonce(txn, genesis); - Self::set_nonce(txn, genesis, BATCH_SIGNING_CODE, batch, nonce_for); + Self::set_nonce(txn, genesis, BATCH_SIGNING_CODE, &batch, nonce_for); } pub fn handle_substrate_block( @@ -74,7 +68,7 @@ impl NonceDecider { let mut res = Vec::with_capacity(plans.len()); for plan in plans { let nonce_for = Self::allocate_nonce(txn, genesis); - Self::set_nonce(txn, genesis, PLAN_CODE, *plan, nonce_for); + Self::set_nonce(txn, genesis, PLAN_CODE, plan, nonce_for); res.push(nonce_for); } res @@ -86,7 +80,7 @@ impl NonceDecider { plan: [u8; 32], ) { let nonce_for = Self::allocate_nonce(txn, genesis); - Self::set_nonce(txn, genesis, PLAN_SIGNING_CODE, plan, nonce_for); + Self::set_nonce(txn, genesis, PLAN_SIGNING_CODE, &plan, nonce_for); } pub fn nonce(getter: &G, genesis: [u8; 32], tx: &Transaction) -> Option> { @@ -109,20 +103,20 @@ impl NonceDecider { Transaction::BatchPreprocess(data) => { assert_eq!(data.attempt, 0); - Some(Self::db_nonce(getter, genesis, BATCH_CODE, data.plan)) + Some(Self::db_nonce(getter, genesis, BATCH_CODE, &data.plan)) } Transaction::BatchShare(data) => { assert_eq!(data.attempt, 0); - Some(Self::db_nonce(getter, genesis, BATCH_SIGNING_CODE, data.plan)) + Some(Self::db_nonce(getter, genesis, BATCH_SIGNING_CODE, &data.plan)) } Transaction::SignPreprocess(data) => { assert_eq!(data.attempt, 0); - Some(Self::db_nonce(getter, genesis, PLAN_CODE, data.plan)) + Some(Self::db_nonce(getter, genesis, PLAN_CODE, &data.plan)) } Transaction::SignShare(data) => { assert_eq!(data.attempt, 0); - Some(Self::db_nonce(getter, genesis, PLAN_SIGNING_CODE, data.plan)) + Some(Self::db_nonce(getter, genesis, PLAN_SIGNING_CODE, &data.plan)) } Transaction::SignCompleted { .. } => None, diff --git a/coordinator/src/tributary/scanner.rs b/coordinator/src/tributary/scanner.rs index 78e71920..895c7754 100644 --- a/coordinator/src/tributary/scanner.rs +++ b/coordinator/src/tributary/scanner.rs @@ -35,10 +35,10 @@ pub enum RecognizedIdType { } pub(crate) trait RIDTrait: - Clone + Fn(ValidatorSet, [u8; 32], RecognizedIdType, [u8; 32], u32) -> FRid + Clone + Fn(ValidatorSet, [u8; 32], RecognizedIdType, Vec, u32) -> FRid { } -impl FRid> +impl, u32) -> FRid> RIDTrait for F { } diff --git a/processor/messages/src/lib.rs b/processor/messages/src/lib.rs index 5b118434..317ad6ae 100644 --- a/processor/messages/src/lib.rs +++ b/processor/messages/src/lib.rs @@ -106,15 +106,22 @@ pub mod sign { } pub mod coordinator { - use super::{sign::SignId, *}; + use super::*; + + #[derive(Clone, PartialEq, Eq, Hash, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)] + pub struct BatchSignId { + pub key: [u8; 32], + pub id: [u8; 5], + pub attempt: u32, + } #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum CoordinatorMessage { // Uses Vec instead of [u8; 64] since serde Deserialize isn't implemented for [u8; 64] - BatchPreprocesses { id: SignId, preprocesses: HashMap> }, - BatchShares { id: SignId, shares: HashMap }, + BatchPreprocesses { id: BatchSignId, preprocesses: HashMap> }, + BatchShares { id: BatchSignId, shares: HashMap }, // Re-attempt a batch signing protocol. - BatchReattempt { id: SignId }, + BatchReattempt { id: BatchSignId }, } impl CoordinatorMessage { @@ -148,8 +155,8 @@ pub mod coordinator { #[derive(Clone, PartialEq, Eq, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)] pub enum ProcessorMessage { SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec }, - BatchPreprocess { id: SignId, block: BlockHash, preprocesses: Vec> }, - BatchShare { id: SignId, shares: Vec<[u8; 32]> }, + BatchPreprocess { id: BatchSignId, block: BlockHash, preprocesses: Vec> }, + BatchShare { id: BatchSignId, shares: Vec<[u8; 32]> }, } } @@ -359,7 +366,7 @@ impl ProcessorMessage { coordinator::ProcessorMessage::SubstrateBlockAck { network, block, .. } => { (0, (network, block).encode()) } - // Unique since SignId + // Unique since BatchSignId coordinator::ProcessorMessage::BatchPreprocess { id, .. } => (1, id.encode()), coordinator::ProcessorMessage::BatchShare { id, .. } => (2, id.encode()), }; diff --git a/processor/src/networks/monero.rs b/processor/src/networks/monero.rs index 5bedf5ef..f919b195 100644 --- a/processor/src/networks/monero.rs +++ b/processor/src/networks/monero.rs @@ -226,7 +226,7 @@ impl Monero { // This isn't entirely accurate as Bulletproof TXs will have a higher weight than their // serialization length // It's likely 'good enough' - // TODO: Improve + // TODO2: Improve fees.push(tx.rct_signatures.base.fee / u64::try_from(tx.serialize().len()).unwrap()); } fees.sort(); diff --git a/processor/src/substrate_signer.rs b/processor/src/substrate_signer.rs index f46fb7fe..2a93f42b 100644 --- a/processor/src/substrate_signer.rs +++ b/processor/src/substrate_signer.rs @@ -3,7 +3,6 @@ use std::collections::{VecDeque, HashMap}; use rand_core::OsRng; -use transcript::{Transcript, RecommendedTranscript}; use ciphersuite::group::GroupEncoding; use frost::{ curve::Ristretto, @@ -24,20 +23,12 @@ use serai_client::{ in_instructions::primitives::{Batch, SignedBatch, batch_message}, }; -use messages::{sign::SignId, coordinator::*}; +use messages::coordinator::*; use crate::{Get, DbTxn, Db}; // Generate an ID unique to a Batch -// TODO: Fork SignId to BatchSignId in order to just use the 5-byte encoding, not the hash of the -// 5-byte encoding -fn sign_id(network: NetworkId, id: u32) -> [u8; 32] { - let mut transcript = RecommendedTranscript::new(b"Serai Processor Batch Sign ID"); - transcript.append_message(b"network", network.encode()); - transcript.append_message(b"id", id.to_le_bytes()); - - let mut res = [0; 32]; - res.copy_from_slice(&transcript.challenge(b"id")[.. 32]); - res +fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] { + (network, id).encode().try_into().unwrap() } #[derive(Debug)] @@ -53,23 +44,23 @@ impl SubstrateSignerDb { D::key(b"SUBSTRATE_SIGNER", dst, key) } - fn completed_key(id: [u8; 32]) -> Vec { + fn completed_key(id: [u8; 5]) -> Vec { Self::sign_key(b"completed", id) } - fn complete(txn: &mut D::Transaction<'_>, id: [u8; 32]) { + fn complete(txn: &mut D::Transaction<'_>, id: [u8; 5]) { txn.put(Self::completed_key(id), []); } - fn completed(getter: &G, id: [u8; 32]) -> bool { + fn completed(getter: &G, id: [u8; 5]) -> bool { getter.get(Self::completed_key(id)).is_some() } - fn attempt_key(id: &SignId) -> Vec { + fn attempt_key(id: &BatchSignId) -> Vec { Self::sign_key(b"attempt", id.encode()) } - fn attempt(txn: &mut D::Transaction<'_>, id: &SignId) { + fn attempt(txn: &mut D::Transaction<'_>, id: &BatchSignId) { txn.put(Self::attempt_key(id), []); } - fn has_attempt(getter: &G, id: &SignId) -> bool { + fn has_attempt(getter: &G, id: &BatchSignId) -> bool { getter.get(Self::attempt_key(id)).is_some() } @@ -89,14 +80,14 @@ pub struct SubstrateSigner { network: NetworkId, keys: Vec>, - signable: HashMap<[u8; 32], Batch>, - attempt: HashMap<[u8; 32], u32>, + signable: HashMap<[u8; 5], Batch>, + attempt: HashMap<[u8; 5], u32>, #[allow(clippy::type_complexity)] preprocessing: - HashMap<[u8; 32], (Vec>, Vec)>, + HashMap<[u8; 5], (Vec>, Vec)>, #[allow(clippy::type_complexity)] signing: - HashMap<[u8; 32], (AlgorithmSignatureMachine, Vec)>, + HashMap<[u8; 5], (AlgorithmSignatureMachine, Vec)>, pub events: VecDeque, } @@ -129,7 +120,7 @@ impl SubstrateSigner { } } - fn verify_id(&self, id: &SignId) -> Result<(), ()> { + fn verify_id(&self, id: &BatchSignId) -> Result<(), ()> { // Check the attempt lines up match self.attempt.get(&id.id) { // If we don't have an attempt logged, it's because the coordinator is faulty OR because we @@ -155,7 +146,7 @@ impl SubstrateSigner { Ok(()) } - async fn attempt(&mut self, txn: &mut D::Transaction<'_>, id: [u8; 32], attempt: u32) { + async fn attempt(&mut self, txn: &mut D::Transaction<'_>, id: [u8; 5], attempt: u32) { // See above commentary for why this doesn't emit SignedBatch if SubstrateSignerDb::::completed(txn, id) { return; @@ -189,7 +180,7 @@ impl SubstrateSigner { // Update the attempt number self.attempt.insert(id, attempt); - let id = SignId { key: self.keys[0].group_key().to_bytes().to_vec(), id, attempt }; + let id = BatchSignId { key: self.keys[0].group_key().to_bytes(), id, attempt }; info!("signing batch {} #{}", hex::encode(id.id), id.attempt); // If we reboot mid-sign, the current design has us abort all signs and wait for latter @@ -241,7 +232,7 @@ impl SubstrateSigner { pub async fn sign(&mut self, txn: &mut D::Transaction<'_>, batch: Batch) { debug_assert_eq!(self.network, batch.network); - let id = sign_id(batch.network, batch.id); + let id = batch_sign_id(batch.network, batch.id); if SubstrateSignerDb::::completed(txn, id) { debug!("Sign batch order for ID we've already completed signing"); // See batch_signed for commentary on why this simply returns @@ -400,7 +391,7 @@ impl SubstrateSigner { // block behind it, which will trigger starting the Batch // TODO: There is a race condition between the Scanner recognizing the block and the Batch // having signing started - let sign_id = sign_id(self.network, id); + let sign_id = batch_sign_id(self.network, id); // Stop trying to sign for this batch SubstrateSignerDb::::complete(txn, sign_id); diff --git a/processor/src/tests/substrate_signer.rs b/processor/src/tests/substrate_signer.rs index 9dd96791..418a5a90 100644 --- a/processor/src/tests/substrate_signer.rs +++ b/processor/src/tests/substrate_signer.rs @@ -13,9 +13,10 @@ use sp_application_crypto::{RuntimePublic, sr25519::Public}; use serai_db::{DbTxn, Db, MemDb}; +use scale::Encode; use serai_client::{primitives::*, in_instructions::primitives::*}; -use messages::{sign::SignId, coordinator::*}; +use messages::coordinator::*; use crate::substrate_signer::{SubstrateSignerEvent, SubstrateSigner}; #[tokio::test] @@ -26,11 +27,6 @@ async fn test_substrate_signer() { let id: u32 = 5; let block = BlockHash([0xaa; 32]); - let mut actual_id = SignId { - key: keys.values().next().unwrap().group_key().to_bytes().to_vec(), - id: [0; 32], - attempt: 0, - }; let batch = Batch { network: NetworkId::Monero, @@ -48,6 +44,12 @@ async fn test_substrate_signer() { ], }; + let actual_id = BatchSignId { + key: keys.values().next().unwrap().group_key().to_bytes(), + id: (batch.network, batch.id).encode().try_into().unwrap(), + attempt: 0, + }; + let mut signers = HashMap::new(); let mut dbs = HashMap::new(); let mut t = 0; @@ -88,9 +90,6 @@ async fn test_substrate_signer() { preprocesses: mut these_preprocesses, }) = signers.get_mut(&i).unwrap().events.pop_front().unwrap() { - if actual_id.id == [0; 32] { - actual_id.id = id.id; - } assert_eq!(id, actual_id); assert_eq!(batch_block, block); assert_eq!(these_preprocesses.len(), 1); diff --git a/tests/coordinator/src/tests/batch.rs b/tests/coordinator/src/tests/batch.rs index ac7a6165..49ceb20b 100644 --- a/tests/coordinator/src/tests/batch.rs +++ b/tests/coordinator/src/tests/batch.rs @@ -23,7 +23,7 @@ use serai_client::{ InInstructionsEvent, }, }; -use messages::{sign::SignId, SubstrateContext, CoordinatorMessage}; +use messages::{coordinator::BatchSignId, SubstrateContext, CoordinatorMessage}; use crate::{*, tests::*}; @@ -33,10 +33,10 @@ pub async fn batch( substrate_key: &Zeroizing<::F>, batch: Batch, ) -> u64 { - let mut id = [0; 32]; + let mut id = [0; 5]; OsRng.fill_bytes(&mut id); - let id = SignId { - key: (::generator() * **substrate_key).to_bytes().to_vec(), + let id = BatchSignId { + key: (::generator() * **substrate_key).to_bytes(), id, attempt: 0, }; diff --git a/tests/coordinator/src/tests/key_gen.rs b/tests/coordinator/src/tests/key_gen.rs index ed18258c..122c6c7f 100644 --- a/tests/coordinator/src/tests/key_gen.rs +++ b/tests/coordinator/src/tests/key_gen.rs @@ -89,7 +89,7 @@ pub async fn key_gen( ( participant_is[usize::from(l)], vec![ - u8::try_from(u16::try_from(participant_is[i]).unwrap()).unwrap(), + u8::try_from(u16::from(participant_is[i])).unwrap(), u8::try_from(u16::from(participant_is[usize::from(l)])).unwrap(), ], ) diff --git a/tests/processor/src/networks.rs b/tests/processor/src/networks.rs index 755f546d..9db4fb1d 100644 --- a/tests/processor/src/networks.rs +++ b/tests/processor/src/networks.rs @@ -402,8 +402,7 @@ impl Wallet { view_pair.address(Network::Mainnet, AddressSpec::Standard), ) .unwrap() - .try_into() - .unwrap(), + .into(), ) .unwrap() } diff --git a/tests/processor/src/tests/batch.rs b/tests/processor/src/tests/batch.rs index d41b767a..cdc09402 100644 --- a/tests/processor/src/tests/batch.rs +++ b/tests/processor/src/tests/batch.rs @@ -5,8 +5,9 @@ use std::{ use dkg::{Participant, tests::clone_without}; -use messages::{coordinator::PlanMeta, sign::SignId, SubstrateContext}; +use messages::{coordinator::*, SubstrateContext}; +use scale::Encode; use serai_client::{ primitives::{ BlockHash, Amount, Balance, crypto::RuntimePublic, PublicKey, SeraiAddress, NetworkId, @@ -25,8 +26,13 @@ pub(crate) async fn recv_batch_preprocesses( substrate_key: &[u8; 32], batch: &Batch, attempt: u32, -) -> (SignId, HashMap>) { - let mut id = None; +) -> (BatchSignId, HashMap>) { + let id = BatchSignId { + key: *substrate_key, + id: (batch.network, batch.id).encode().try_into().unwrap(), + attempt, + }; + let mut block = None; let mut preprocesses = HashMap::new(); for (i, coordinator) in coordinators.iter_mut().enumerate() { @@ -51,13 +57,10 @@ pub(crate) async fn recv_batch_preprocesses( preprocesses: mut these_preprocesses, }, ) => { - if id.is_none() { - assert_eq!(&this_id.key, substrate_key); - assert_eq!(this_id.attempt, attempt); - id = Some(this_id.clone()); + assert_eq!(this_id, id); + if block.is_none() { block = Some(this_block); } - assert_eq!(&this_id, id.as_ref().unwrap()); assert_eq!(&this_block, block.as_ref().unwrap()); assert_eq!(these_preprocesses.len(), 1); @@ -77,13 +80,13 @@ pub(crate) async fn recv_batch_preprocesses( ); } - (id.unwrap(), preprocesses) + (id, preprocesses) } pub(crate) async fn sign_batch( coordinators: &mut [Coordinator], key: [u8; 32], - id: SignId, + id: BatchSignId, preprocesses: HashMap>, ) -> SignedBatch { assert_eq!(preprocesses.len(), THRESHOLD);