Rename SubstrateSigner to BatchSigner

This commit is contained in:
Luke Parker 2023-11-20 22:20:58 -05:00
parent 05d8c32be8
commit 4b85d4b03b
No known key found for this signature in database
4 changed files with 32 additions and 37 deletions

View file

@ -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 = <AlgorithmSignMachine<Ristretto, Schnorrkel> as SignMachin
<Schnorrkel as Algorithm<Ristretto>>::Signature,
>>::SignatureShare;
// TODO: Rename BatchSigner
pub struct SubstrateSigner<D: Db> {
pub struct BatchSigner<D: Db> {
db: PhantomData<D>,
network: NetworkId,
@ -61,20 +60,20 @@ pub struct SubstrateSigner<D: Db> {
HashMap<[u8; 5], (AlgorithmSignatureMachine<Ristretto, Schnorrkel>, Vec<SignatureShare>)>,
}
impl<D: Db> fmt::Debug for SubstrateSigner<D> {
impl<D: Db> fmt::Debug for BatchSigner<D> {
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<D: Db> SubstrateSigner<D> {
pub fn new(network: NetworkId, keys: Vec<ThresholdKeys<Ristretto>>) -> SubstrateSigner<D> {
impl<D: Db> BatchSigner<D> {
pub fn new(network: NetworkId, keys: Vec<ThresholdKeys<Ristretto>>) -> BatchSigner<D> {
assert!(!keys.is_empty());
SubstrateSigner {
BatchSigner {
db: PhantomData,
network,
@ -89,7 +88,7 @@ impl<D: Db> SubstrateSigner<D> {
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<D: Db> SubstrateSigner<D> {
) -> Option<messages::ProcessorMessage> {
match msg {
CoordinatorMessage::CosignSubstrateBlock { .. } => {
panic!("SubstrateSigner passed CosignSubstrateBlock")
panic!("BatchSigner passed CosignSubstrateBlock")
}
CoordinatorMessage::SubstratePreprocesses { id, preprocesses } => {

View file

@ -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<N: Network, D: Db> {
// 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<SubstrateSigner<D>>,
// There should only be one BatchSigner at a time (see #277)
batch_signer: Option<BatchSigner<D>>,
// Solely mutated by the tributary.
cosigner: Option<Cosigner>,
@ -201,7 +201,7 @@ async fn handle_coordinator_msg<D: Db, N: Network, Co: Coordinator>(
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<D: Db, N: Network, Co: Coordinator>(
};
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<D: Db, N: Network, Co: Coordinator>(
// 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<D: Db, N: Network, Co: Coordinator>(
}
// 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<N: Network, D: Db, Co: Coordinator>(
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::<N, _>::new(raw_db.clone());
@ -494,16 +494,16 @@ async fn boot<N: Network, D: Db, Co: Coordinator>(
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<N: Network, D: Db, Co: Coordinator>(
// This hedges against being dropped due to full mempools, temporarily too low of a fee...
tokio::spawn(Signer::<N, D>::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<N: Network, D: Db, Co: Coordinator>(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<N: Network, D: Db, Co: Coordinator>(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));
}
}
},

View file

@ -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::<MemDb>::new(NetworkId::Monero, vec![keys]);
let mut signer = BatchSigner::<MemDb>::new(NetworkId::Monero, vec![keys]);
let mut db = MemDb::new();
let mut txn = db.txn();

View file

@ -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;