mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-18 08:45:00 +00:00
Rename SubstrateSigner to BatchSigner
This commit is contained in:
parent
05d8c32be8
commit
4b85d4b03b
4 changed files with 32 additions and 37 deletions
|
@ -32,7 +32,7 @@ fn batch_sign_id(network: NetworkId, id: u32) -> [u8; 5] {
|
||||||
}
|
}
|
||||||
|
|
||||||
create_db!(
|
create_db!(
|
||||||
SubstrateSignerDb {
|
BatchSignerDb {
|
||||||
CompletedDb: (id: [u8; 5]) -> (),
|
CompletedDb: (id: [u8; 5]) -> (),
|
||||||
AttemptDb: (id: [u8; 5], attempt: u32) -> (),
|
AttemptDb: (id: [u8; 5], attempt: u32) -> (),
|
||||||
BatchDb: (block: BlockHash) -> SignedBatch
|
BatchDb: (block: BlockHash) -> SignedBatch
|
||||||
|
@ -44,8 +44,7 @@ type SignatureShare = <AlgorithmSignMachine<Ristretto, Schnorrkel> as SignMachin
|
||||||
<Schnorrkel as Algorithm<Ristretto>>::Signature,
|
<Schnorrkel as Algorithm<Ristretto>>::Signature,
|
||||||
>>::SignatureShare;
|
>>::SignatureShare;
|
||||||
|
|
||||||
// TODO: Rename BatchSigner
|
pub struct BatchSigner<D: Db> {
|
||||||
pub struct SubstrateSigner<D: Db> {
|
|
||||||
db: PhantomData<D>,
|
db: PhantomData<D>,
|
||||||
|
|
||||||
network: NetworkId,
|
network: NetworkId,
|
||||||
|
@ -61,20 +60,20 @@ pub struct SubstrateSigner<D: Db> {
|
||||||
HashMap<[u8; 5], (AlgorithmSignatureMachine<Ristretto, Schnorrkel>, Vec<SignatureShare>)>,
|
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 {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
fmt
|
fmt
|
||||||
.debug_struct("SubstrateSigner")
|
.debug_struct("BatchSigner")
|
||||||
.field("signable", &self.signable)
|
.field("signable", &self.signable)
|
||||||
.field("attempt", &self.attempt)
|
.field("attempt", &self.attempt)
|
||||||
.finish_non_exhaustive()
|
.finish_non_exhaustive()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D: Db> SubstrateSigner<D> {
|
impl<D: Db> BatchSigner<D> {
|
||||||
pub fn new(network: NetworkId, keys: Vec<ThresholdKeys<Ristretto>>) -> SubstrateSigner<D> {
|
pub fn new(network: NetworkId, keys: Vec<ThresholdKeys<Ristretto>>) -> BatchSigner<D> {
|
||||||
assert!(!keys.is_empty());
|
assert!(!keys.is_empty());
|
||||||
SubstrateSigner {
|
BatchSigner {
|
||||||
db: PhantomData,
|
db: PhantomData,
|
||||||
|
|
||||||
network,
|
network,
|
||||||
|
@ -89,7 +88,7 @@ impl<D: Db> SubstrateSigner<D> {
|
||||||
|
|
||||||
fn verify_id(&self, id: &SubstrateSignId) -> Result<([u8; 32], [u8; 5], u32), ()> {
|
fn verify_id(&self, id: &SubstrateSignId) -> Result<([u8; 32], [u8; 5], u32), ()> {
|
||||||
let SubstrateSignId { key, id, attempt } = id;
|
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());
|
assert_eq!(key, &self.keys[0].group_key().to_bytes());
|
||||||
|
|
||||||
|
@ -233,7 +232,7 @@ impl<D: Db> SubstrateSigner<D> {
|
||||||
) -> Option<messages::ProcessorMessage> {
|
) -> Option<messages::ProcessorMessage> {
|
||||||
match msg {
|
match msg {
|
||||||
CoordinatorMessage::CosignSubstrateBlock { .. } => {
|
CoordinatorMessage::CosignSubstrateBlock { .. } => {
|
||||||
panic!("SubstrateSigner passed CosignSubstrateBlock")
|
panic!("BatchSigner passed CosignSubstrateBlock")
|
||||||
}
|
}
|
||||||
|
|
||||||
CoordinatorMessage::SubstratePreprocesses { id, preprocesses } => {
|
CoordinatorMessage::SubstratePreprocesses { id, preprocesses } => {
|
|
@ -52,8 +52,8 @@ use signer::Signer;
|
||||||
mod cosigner;
|
mod cosigner;
|
||||||
use cosigner::Cosigner;
|
use cosigner::Cosigner;
|
||||||
|
|
||||||
mod substrate_signer;
|
mod batch_signer;
|
||||||
use substrate_signer::SubstrateSigner;
|
use batch_signer::BatchSigner;
|
||||||
|
|
||||||
mod multisigs;
|
mod multisigs;
|
||||||
use multisigs::{MultisigEvent, MultisigManager};
|
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.
|
// Substrate may mark tasks as completed, invalidating any existing mutable borrows.
|
||||||
// The safety of this follows as written above.
|
// The safety of this follows as written above.
|
||||||
|
|
||||||
// There should only be one SubstrateSigner at a time (see #277)
|
// There should only be one BatchSigner at a time (see #277)
|
||||||
substrate_signer: Option<SubstrateSigner<D>>,
|
batch_signer: Option<BatchSigner<D>>,
|
||||||
|
|
||||||
// Solely mutated by the tributary.
|
// Solely mutated by the tributary.
|
||||||
cosigner: Option<Cosigner>,
|
cosigner: Option<Cosigner>,
|
||||||
|
@ -201,7 +201,7 @@ async fn handle_coordinator_msg<D: Db, N: Network, Co: Coordinator>(
|
||||||
let KeyConfirmed { substrate_keys, network_keys } =
|
let KeyConfirmed { substrate_keys, network_keys } =
|
||||||
tributary_mutable.key_gen.confirm(txn, set, key_pair.clone()).await;
|
tributary_mutable.key_gen.confirm(txn, set, key_pair.clone()).await;
|
||||||
if set.session.0 == 0 {
|
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
|
tributary_mutable
|
||||||
.signers
|
.signers
|
||||||
|
@ -241,7 +241,7 @@ async fn handle_coordinator_msg<D: Db, N: Network, Co: Coordinator>(
|
||||||
};
|
};
|
||||||
if is_batch {
|
if is_batch {
|
||||||
if let Some(msg) = tributary_mutable
|
if let Some(msg) = tributary_mutable
|
||||||
.substrate_signer
|
.batch_signer
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.expect(
|
.expect(
|
||||||
"coordinator told us to sign a batch when we don't currently have a Substrate signer",
|
"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?
|
// TODO: Use an Option instead of a magic?
|
||||||
if context.network_latest_finalized_block.0 == [0; 32] {
|
if context.network_latest_finalized_block.0 == [0; 32] {
|
||||||
assert!(tributary_mutable.signers.is_empty());
|
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());
|
assert!(tributary_mutable.cosigner.is_none());
|
||||||
// We can't check this as existing is no longer pub
|
// We can't check this as existing is no longer pub
|
||||||
// assert!(substrate_mutable.existing.as_ref().is_none());
|
// 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
|
// 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 {
|
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) =
|
let (multisig_manager, current_keys, actively_signing) =
|
||||||
MultisigManager::new(raw_db, network).await;
|
MultisigManager::new(raw_db, network).await;
|
||||||
|
|
||||||
let mut substrate_signer = None;
|
let mut batch_signer = None;
|
||||||
let mut signers = HashMap::new();
|
let mut signers = HashMap::new();
|
||||||
|
|
||||||
let main_db = MainDb::<N, _>::new(raw_db.clone());
|
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 Some((substrate_keys, network_keys)) = key_gen.keys(key) else { continue };
|
||||||
let network_key = network_keys[0].group_key();
|
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
|
// 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
|
// 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
|
// necessary, only no longer scanning old blocks once Substrate acks them
|
||||||
if i == 0 {
|
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
|
// 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
|
// 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:
|
// 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...
|
// 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()));
|
tokio::spawn(Signer::<N, D>::rebroadcast_task(raw_db.clone(), network.clone()));
|
||||||
|
|
||||||
(
|
(main_db, TributaryMutable { key_gen, batch_signer, cosigner: None, signers }, multisig_manager)
|
||||||
main_db,
|
|
||||||
TributaryMutable { key_gen, substrate_signer, cosigner: None, signers },
|
|
||||||
multisig_manager,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::await_holding_lock)] // Needed for txn, unfortunately can't be down-scoped
|
#[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() }
|
messages::substrate::ProcessorMessage::Batch { batch: batch.clone() }
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
if let Some(substrate_signer) = tributary_mutable.substrate_signer.as_mut() {
|
if let Some(batch_signer) = tributary_mutable.batch_signer.as_mut() {
|
||||||
if let Some(msg) = substrate_signer.sign(&mut txn, batch).await {
|
if let Some(msg) = batch_signer.sign(&mut txn, batch).await {
|
||||||
coordinator.send(msg).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 {
|
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
|
// 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.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) {
|
if let Some((substrate_keys, _)) = tributary_mutable.key_gen.keys(&new_key) {
|
||||||
tributary_mutable.substrate_signer =
|
tributary_mutable.batch_signer =
|
||||||
Some(SubstrateSigner::new(N::NETWORK, substrate_keys));
|
Some(BatchSigner::new(N::NETWORK, substrate_keys));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -21,10 +21,10 @@ use messages::{
|
||||||
coordinator::{self, SubstrateSignableId, SubstrateSignId, CoordinatorMessage},
|
coordinator::{self, SubstrateSignableId, SubstrateSignId, CoordinatorMessage},
|
||||||
ProcessorMessage,
|
ProcessorMessage,
|
||||||
};
|
};
|
||||||
use crate::substrate_signer::SubstrateSigner;
|
use crate::batch_signer::BatchSigner;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_substrate_signer() {
|
async fn test_batch_signer() {
|
||||||
let keys = key_gen::<_, Ristretto>(&mut OsRng);
|
let keys = key_gen::<_, Ristretto>(&mut OsRng);
|
||||||
|
|
||||||
let participant_one = Participant::new(1).unwrap();
|
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 i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||||
let keys = keys.get(&i).unwrap().clone();
|
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 db = MemDb::new();
|
||||||
|
|
||||||
let mut txn = db.txn();
|
let mut txn = db.txn();
|
|
@ -8,7 +8,7 @@ mod signer;
|
||||||
pub(crate) use signer::{sign, test_signer};
|
pub(crate) use signer::{sign, test_signer};
|
||||||
|
|
||||||
mod cosigner;
|
mod cosigner;
|
||||||
mod substrate_signer;
|
mod batch_signer;
|
||||||
|
|
||||||
mod wallet;
|
mod wallet;
|
||||||
pub(crate) use wallet::test_wallet;
|
pub(crate) use wallet::test_wallet;
|
||||||
|
|
Loading…
Reference in a new issue