From 4b85d4b03b764eab68514fca58b390b054db7e40 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 20 Nov 2023 22:20:58 -0500 Subject: [PATCH] Rename SubstrateSigner to BatchSigner --- .../{substrate_signer.rs => batch_signer.rs} | 19 ++++----- processor/src/main.rs | 42 +++++++++---------- .../{substrate_signer.rs => batch_signer.rs} | 6 +-- processor/src/tests/mod.rs | 2 +- 4 files changed, 32 insertions(+), 37 deletions(-) rename processor/src/{substrate_signer.rs => batch_signer.rs} (97%) rename processor/src/tests/{substrate_signer.rs => batch_signer.rs} (96%) diff --git a/processor/src/substrate_signer.rs b/processor/src/batch_signer.rs similarity index 97% rename from processor/src/substrate_signer.rs rename to processor/src/batch_signer.rs index 645603af..28b094a3 100644 --- a/processor/src/substrate_signer.rs +++ b/processor/src/batch_signer.rs @@ -32,7 +32,7 @@ fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] { } create_db!( - SubstrateSignerDb { + BatchSignerDb { CompletedDb: (id: [u8; 5]) -> (), AttemptDb: (id: [u8; 5], attempt: u32) -> (), BatchDb: (block: BlockHash) -> SignedBatch @@ -44,8 +44,7 @@ type SignatureShare = as SignMachin >::Signature, >>::SignatureShare; -// TODO: Rename BatchSigner -pub struct SubstrateSigner { +pub struct BatchSigner { db: PhantomData, network: NetworkId, @@ -61,20 +60,20 @@ pub struct SubstrateSigner { HashMap<[u8; 5], (AlgorithmSignatureMachine, Vec)>, } -impl fmt::Debug for SubstrateSigner { +impl fmt::Debug for BatchSigner { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt - .debug_struct("SubstrateSigner") + .debug_struct("BatchSigner") .field("signable", &self.signable) .field("attempt", &self.attempt) .finish_non_exhaustive() } } -impl SubstrateSigner { - pub fn new(network: NetworkId, keys: Vec>) -> SubstrateSigner { +impl BatchSigner { + pub fn new(network: NetworkId, keys: Vec>) -> BatchSigner { assert!(!keys.is_empty()); - SubstrateSigner { + BatchSigner { db: PhantomData, network, @@ -89,7 +88,7 @@ impl SubstrateSigner { fn verify_id(&self, id: &SubstrateSignId) -> Result<([u8; 32], [u8; 5], u32), ()> { let SubstrateSignId { key, id, attempt } = id; - let SubstrateSignableId::Batch(id) = id else { panic!("SubstrateSigner handed non-Batch") }; + let SubstrateSignableId::Batch(id) = id else { panic!("BatchSigner handed non-Batch") }; assert_eq!(key, &self.keys[0].group_key().to_bytes()); @@ -233,7 +232,7 @@ impl SubstrateSigner { ) -> Option { match msg { CoordinatorMessage::CosignSubstrateBlock { .. } => { - panic!("SubstrateSigner passed CosignSubstrateBlock") + panic!("BatchSigner passed CosignSubstrateBlock") } CoordinatorMessage::SubstratePreprocesses { id, preprocesses } => { diff --git a/processor/src/main.rs b/processor/src/main.rs index abf53165..54e2e675 100644 --- a/processor/src/main.rs +++ b/processor/src/main.rs @@ -52,8 +52,8 @@ use signer::Signer; mod cosigner; use cosigner::Cosigner; -mod substrate_signer; -use substrate_signer::SubstrateSigner; +mod batch_signer; +use batch_signer::BatchSigner; mod multisigs; use multisigs::{MultisigEvent, MultisigManager}; @@ -92,8 +92,8 @@ struct TributaryMutable { // Substrate may mark tasks as completed, invalidating any existing mutable borrows. // The safety of this follows as written above. - // There should only be one SubstrateSigner at a time (see #277) - substrate_signer: Option>, + // There should only be one BatchSigner at a time (see #277) + batch_signer: Option>, // Solely mutated by the tributary. cosigner: Option, @@ -201,7 +201,7 @@ async fn handle_coordinator_msg( let KeyConfirmed { substrate_keys, network_keys } = tributary_mutable.key_gen.confirm(txn, set, key_pair.clone()).await; if set.session.0 == 0 { - tributary_mutable.substrate_signer = Some(SubstrateSigner::new(N::NETWORK, substrate_keys)); + tributary_mutable.batch_signer = Some(BatchSigner::new(N::NETWORK, substrate_keys)); } tributary_mutable .signers @@ -241,7 +241,7 @@ async fn handle_coordinator_msg( }; if is_batch { if let Some(msg) = tributary_mutable - .substrate_signer + .batch_signer .as_mut() .expect( "coordinator told us to sign a batch when we don't currently have a Substrate signer", @@ -293,7 +293,7 @@ async fn handle_coordinator_msg( // TODO: Use an Option instead of a magic? if context.network_latest_finalized_block.0 == [0; 32] { assert!(tributary_mutable.signers.is_empty()); - assert!(tributary_mutable.substrate_signer.is_none()); + assert!(tributary_mutable.batch_signer.is_none()); assert!(tributary_mutable.cosigner.is_none()); // We can't check this as existing is no longer pub // assert!(substrate_mutable.existing.as_ref().is_none()); @@ -393,9 +393,9 @@ async fn handle_coordinator_msg( } // Since this block was acknowledged, we no longer have to sign the batches within it - if let Some(substrate_signer) = tributary_mutable.substrate_signer.as_mut() { + if let Some(batch_signer) = tributary_mutable.batch_signer.as_mut() { for batch_id in batches { - substrate_signer.batch_signed(txn, batch_id); + batch_signer.batch_signed(txn, batch_id); } } @@ -485,7 +485,7 @@ async fn boot( let (multisig_manager, current_keys, actively_signing) = MultisigManager::new(raw_db, network).await; - let mut substrate_signer = None; + let mut batch_signer = None; let mut signers = HashMap::new(); let main_db = MainDb::::new(raw_db.clone()); @@ -494,16 +494,16 @@ async fn boot( let Some((substrate_keys, network_keys)) = key_gen.keys(key) else { continue }; let network_key = network_keys[0].group_key(); - // If this is the oldest key, load the SubstrateSigner for it as the active SubstrateSigner + // If this is the oldest key, load the BatchSigner for it as the active BatchSigner // The new key only takes responsibility once the old key is fully deprecated // // We don't have to load any state for this since the Scanner will re-fire any events // necessary, only no longer scanning old blocks once Substrate acks them if i == 0 { - substrate_signer = Some(SubstrateSigner::new(N::NETWORK, substrate_keys)); + batch_signer = Some(BatchSigner::new(N::NETWORK, substrate_keys)); } - // The Scanner re-fires events as needed for substrate_signer yet not signer + // The Scanner re-fires events as needed for batch_signer yet not signer // This is due to the transactions which we start signing from due to a block not being // guaranteed to be signed before we stop scanning the block on reboot // We could simplify the Signer flow by delaying when it acks a block, yet that'd: @@ -535,11 +535,7 @@ async fn boot( // This hedges against being dropped due to full mempools, temporarily too low of a fee... tokio::spawn(Signer::::rebroadcast_task(raw_db.clone(), network.clone())); - ( - main_db, - TributaryMutable { key_gen, substrate_signer, cosigner: None, signers }, - multisig_manager, - ) + (main_db, TributaryMutable { key_gen, batch_signer, cosigner: None, signers }, multisig_manager) } #[allow(clippy::await_holding_lock)] // Needed for txn, unfortunately can't be down-scoped @@ -617,8 +613,8 @@ async fn run(mut raw_db: D, network: N, mut messages::substrate::ProcessorMessage::Batch { batch: batch.clone() } ).await; - if let Some(substrate_signer) = tributary_mutable.substrate_signer.as_mut() { - if let Some(msg) = substrate_signer.sign(&mut txn, batch).await { + if let Some(batch_signer) = tributary_mutable.batch_signer.as_mut() { + if let Some(msg) = batch_signer.sign(&mut txn, batch).await { coordinator.send(msg).await; } } @@ -627,10 +623,10 @@ async fn run(mut raw_db: D, network: N, mut if let Some((retired_key, new_key)) = retired_key_new_key { // Safe to mutate since all signing operations are done and no more will be added tributary_mutable.signers.remove(retired_key.to_bytes().as_ref()); - tributary_mutable.substrate_signer.take(); + tributary_mutable.batch_signer.take(); if let Some((substrate_keys, _)) = tributary_mutable.key_gen.keys(&new_key) { - tributary_mutable.substrate_signer = - Some(SubstrateSigner::new(N::NETWORK, substrate_keys)); + tributary_mutable.batch_signer = + Some(BatchSigner::new(N::NETWORK, substrate_keys)); } } }, diff --git a/processor/src/tests/substrate_signer.rs b/processor/src/tests/batch_signer.rs similarity index 96% rename from processor/src/tests/substrate_signer.rs rename to processor/src/tests/batch_signer.rs index b47fb9c7..379ff9aa 100644 --- a/processor/src/tests/substrate_signer.rs +++ b/processor/src/tests/batch_signer.rs @@ -21,10 +21,10 @@ use messages::{ coordinator::{self, SubstrateSignableId, SubstrateSignId, CoordinatorMessage}, ProcessorMessage, }; -use crate::substrate_signer::SubstrateSigner; +use crate::batch_signer::BatchSigner; #[tokio::test] -async fn test_substrate_signer() { +async fn test_batch_signer() { let keys = key_gen::<_, Ristretto>(&mut OsRng); let participant_one = Participant::new(1).unwrap(); @@ -73,7 +73,7 @@ async fn test_substrate_signer() { let i = Participant::new(u16::try_from(i).unwrap()).unwrap(); let keys = keys.get(&i).unwrap().clone(); - let mut signer = SubstrateSigner::::new(NetworkId::Monero, vec![keys]); + let mut signer = BatchSigner::::new(NetworkId::Monero, vec![keys]); let mut db = MemDb::new(); let mut txn = db.txn(); diff --git a/processor/src/tests/mod.rs b/processor/src/tests/mod.rs index 37d68719..9a68b7bc 100644 --- a/processor/src/tests/mod.rs +++ b/processor/src/tests/mod.rs @@ -8,7 +8,7 @@ mod signer; pub(crate) use signer::{sign, test_signer}; mod cosigner; -mod substrate_signer; +mod batch_signer; mod wallet; pub(crate) use wallet::test_wallet;