mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 04:09:23 +00:00
* Remove NetworkId from processor-messages Because intent binds to the sender/receiver, it's not needed for intent. The processor knows what the network is. The coordinator knows which to use because it's sending this message to the processor for that network. Also removes the unused zeroize. * ProcessorMessage::Completed use Session instead of key * Move SubstrateSignId to Session * Finish replacing key with session
This commit is contained in:
@@ -22,6 +22,7 @@ use serai_client::{
|
||||
primitives::{Batch, SignedBatch, batch_message},
|
||||
InInstructionsEvent,
|
||||
},
|
||||
validator_sets::primitives::Session,
|
||||
};
|
||||
use messages::{
|
||||
coordinator::{SubstrateSignableId, SubstrateSignId},
|
||||
@@ -33,16 +34,13 @@ use crate::{*, tests::*};
|
||||
pub async fn batch(
|
||||
processors: &mut [Processor],
|
||||
processor_is: &[u8],
|
||||
session: Session,
|
||||
substrate_key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
||||
batch: Batch,
|
||||
) -> u64 {
|
||||
let mut id = [0; 5];
|
||||
OsRng.fill_bytes(&mut id);
|
||||
let id = SubstrateSignId {
|
||||
key: (<Ristretto as Ciphersuite>::generator() * **substrate_key).to_bytes(),
|
||||
id: SubstrateSignableId::Batch(id),
|
||||
attempt: 0,
|
||||
};
|
||||
let id = SubstrateSignId { session, id: SubstrateSignableId::Batch(id), attempt: 0 };
|
||||
|
||||
for processor in processors.iter_mut() {
|
||||
processor
|
||||
@@ -232,7 +230,6 @@ pub async fn batch(
|
||||
serai_time: last_block.time().unwrap() / 1000,
|
||||
network_latest_finalized_block: batch.batch.block,
|
||||
},
|
||||
network: batch.batch.network,
|
||||
block: last_serai_block,
|
||||
burns: vec![],
|
||||
batches: vec![batch.batch.id],
|
||||
@@ -244,7 +241,6 @@ pub async fn batch(
|
||||
processor
|
||||
.send_message(messages::ProcessorMessage::Coordinator(
|
||||
messages::coordinator::ProcessorMessage::SubstrateBlockAck {
|
||||
network: batch.batch.network,
|
||||
block: last_serai_block,
|
||||
plans: vec![],
|
||||
},
|
||||
@@ -283,6 +279,7 @@ async fn batch_test() {
|
||||
batch(
|
||||
&mut processors,
|
||||
&processor_is,
|
||||
Session(0),
|
||||
&substrate_key,
|
||||
Batch {
|
||||
network: NetworkId::Bitcoin,
|
||||
|
||||
@@ -28,7 +28,7 @@ pub async fn key_gen<C: Ciphersuite>(
|
||||
let mut participant_is = vec![];
|
||||
|
||||
let set = ValidatorSet { session: Session(0), network: NetworkId::Bitcoin };
|
||||
let id = KeyGenId { set, attempt: 0 };
|
||||
let id = KeyGenId { session: set.session, attempt: 0 };
|
||||
|
||||
for (i, processor) in processors.iter_mut().enumerate() {
|
||||
let msg = processor.recv_message().await;
|
||||
@@ -173,7 +173,7 @@ pub async fn key_gen<C: Ciphersuite>(
|
||||
CoordinatorMessage::Substrate(
|
||||
messages::substrate::CoordinatorMessage::ConfirmKeyPair {
|
||||
context,
|
||||
set: this_set,
|
||||
session,
|
||||
ref key_pair,
|
||||
},
|
||||
) => {
|
||||
@@ -186,7 +186,7 @@ pub async fn key_gen<C: Ciphersuite>(
|
||||
70
|
||||
);
|
||||
assert_eq!(context.network_latest_finalized_block.0, [0; 32]);
|
||||
assert_eq!(set, this_set);
|
||||
assert_eq!(set.session, session);
|
||||
assert_eq!(key_pair.0 .0, substrate_key);
|
||||
assert_eq!(&key_pair.1, &network_key);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,9 @@ use std::{
|
||||
collections::{HashSet, HashMap},
|
||||
};
|
||||
|
||||
use zeroize::Zeroizing;
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use ciphersuite::{group::GroupEncoding, Ciphersuite, Secp256k1};
|
||||
use ciphersuite::Secp256k1;
|
||||
|
||||
use dkg::Participant;
|
||||
|
||||
@@ -22,23 +21,20 @@ use serai_client::{
|
||||
CoinsEvent,
|
||||
},
|
||||
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
|
||||
validator_sets::primitives::Session,
|
||||
SeraiCoins,
|
||||
};
|
||||
use messages::{coordinator::PlanMeta, sign::SignId, SubstrateContext, CoordinatorMessage};
|
||||
|
||||
use crate::tests::*;
|
||||
|
||||
pub async fn sign<C: Ciphersuite>(
|
||||
pub async fn sign(
|
||||
processors: &mut [Processor],
|
||||
processor_is: &[u8],
|
||||
network_key: &Zeroizing<C::F>,
|
||||
session: Session,
|
||||
plan_id: [u8; 32],
|
||||
) {
|
||||
let id = SignId {
|
||||
key: (C::generator() * **network_key).to_bytes().as_ref().to_vec(),
|
||||
id: plan_id,
|
||||
attempt: 0,
|
||||
};
|
||||
let id = SignId { session, id: plan_id, attempt: 0 };
|
||||
|
||||
// Select a random participant to exclude, so we know for sure who *is* participating
|
||||
assert_eq!(COORDINATORS - THRESHOLD, 1);
|
||||
@@ -150,7 +146,7 @@ pub async fn sign<C: Ciphersuite>(
|
||||
&mut processors[processor_is.iter().position(|p_i| u16::from(*p_i) == u16::from(i)).unwrap()];
|
||||
processor
|
||||
.send_message(messages::sign::ProcessorMessage::Completed {
|
||||
key: id.key.clone(),
|
||||
session,
|
||||
id: id.id,
|
||||
tx: b"signed_tx".to_vec(),
|
||||
})
|
||||
@@ -163,7 +159,7 @@ pub async fn sign<C: Ciphersuite>(
|
||||
assert_eq!(
|
||||
processor.recv_message().await,
|
||||
CoordinatorMessage::Sign(messages::sign::CoordinatorMessage::Completed {
|
||||
key: id.key.clone(),
|
||||
session,
|
||||
id: id.id,
|
||||
tx: b"signed_tx".to_vec()
|
||||
})
|
||||
@@ -196,8 +192,7 @@ async fn sign_test() {
|
||||
}
|
||||
let mut processors = new_processors;
|
||||
|
||||
let (participant_is, substrate_key, network_key) =
|
||||
key_gen::<Secp256k1>(&mut processors).await;
|
||||
let (participant_is, substrate_key, _) = key_gen::<Secp256k1>(&mut processors).await;
|
||||
|
||||
// 'Send' external coins into Serai
|
||||
let serai = processors[0].serai().await;
|
||||
@@ -230,6 +225,7 @@ async fn sign_test() {
|
||||
let block_included_in = batch(
|
||||
&mut processors,
|
||||
&participant_is,
|
||||
Session(0),
|
||||
&substrate_key,
|
||||
Batch {
|
||||
network: NetworkId::Bitcoin,
|
||||
@@ -331,7 +327,6 @@ async fn sign_test() {
|
||||
serai_time: last_serai_block.time().unwrap() / 1000,
|
||||
network_latest_finalized_block: coin_block,
|
||||
},
|
||||
network: NetworkId::Bitcoin,
|
||||
block: last_serai_block.number(),
|
||||
burns: vec![out_instruction.clone()],
|
||||
batches: vec![],
|
||||
@@ -343,18 +338,14 @@ async fn sign_test() {
|
||||
processor
|
||||
.send_message(messages::ProcessorMessage::Coordinator(
|
||||
messages::coordinator::ProcessorMessage::SubstrateBlockAck {
|
||||
network: NetworkId::Bitcoin,
|
||||
block: last_serai_block.number(),
|
||||
plans: vec![PlanMeta {
|
||||
key: (Secp256k1::generator() * *network_key).to_bytes().to_vec(),
|
||||
id: plan_id,
|
||||
}],
|
||||
plans: vec![PlanMeta { session: Session(0), id: plan_id }],
|
||||
},
|
||||
))
|
||||
.await;
|
||||
}
|
||||
|
||||
sign::<Secp256k1>(&mut processors, &participant_is, &network_key, plan_id).await;
|
||||
sign(&mut processors, &participant_is, Session(0), plan_id).await;
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ use serai_client::{
|
||||
in_instructions::primitives::{
|
||||
InInstruction, InInstructionWithBalance, Batch, SignedBatch, batch_message,
|
||||
},
|
||||
validator_sets::primitives::Session,
|
||||
};
|
||||
|
||||
use processor::networks::{Network, Bitcoin, Monero};
|
||||
@@ -23,12 +24,12 @@ use crate::{*, tests::*};
|
||||
|
||||
pub(crate) async fn recv_batch_preprocesses(
|
||||
coordinators: &mut [Coordinator],
|
||||
substrate_key: &[u8; 32],
|
||||
session: Session,
|
||||
batch: &Batch,
|
||||
attempt: u32,
|
||||
) -> (SubstrateSignId, HashMap<Participant, [u8; 64]>) {
|
||||
let id = SubstrateSignId {
|
||||
key: *substrate_key,
|
||||
session,
|
||||
id: SubstrateSignableId::Batch((batch.network, batch.id).encode().try_into().unwrap()),
|
||||
attempt,
|
||||
};
|
||||
@@ -171,7 +172,6 @@ pub(crate) async fn substrate_block(
|
||||
match block.clone() {
|
||||
messages::substrate::CoordinatorMessage::SubstrateBlock {
|
||||
context: _,
|
||||
network: sent_network,
|
||||
block: sent_block,
|
||||
burns: _,
|
||||
batches: _,
|
||||
@@ -179,13 +179,8 @@ pub(crate) async fn substrate_block(
|
||||
coordinator.send_message(block).await;
|
||||
match coordinator.recv_message().await {
|
||||
messages::ProcessorMessage::Coordinator(
|
||||
messages::coordinator::ProcessorMessage::SubstrateBlockAck {
|
||||
network: recvd_network,
|
||||
block: recvd_block,
|
||||
plans,
|
||||
},
|
||||
messages::coordinator::ProcessorMessage::SubstrateBlockAck { block: recvd_block, plans },
|
||||
) => {
|
||||
assert_eq!(recvd_network, sent_network);
|
||||
assert_eq!(recvd_block, sent_block);
|
||||
plans
|
||||
}
|
||||
@@ -214,7 +209,7 @@ fn batch_test() {
|
||||
coordinators[0].sync(&ops, &coordinators[1 ..]).await;
|
||||
|
||||
// Generate keys
|
||||
let key_pair = key_gen(&mut coordinators, network).await;
|
||||
let key_pair = key_gen(&mut coordinators).await;
|
||||
|
||||
// Now we we have to mine blocks to activate the key
|
||||
// (the first key is activated when the network's time as of a block exceeds the Serai time
|
||||
@@ -284,7 +279,7 @@ fn batch_test() {
|
||||
|
||||
// Make sure the processors picked it up by checking they're trying to sign a batch for it
|
||||
let (mut id, mut preprocesses) =
|
||||
recv_batch_preprocesses(&mut coordinators, &key_pair.0 .0, &expected_batch, 0).await;
|
||||
recv_batch_preprocesses(&mut coordinators, Session(0), &expected_batch, 0).await;
|
||||
// Trigger a random amount of re-attempts
|
||||
for attempt in 1 ..= u32::try_from(OsRng.next_u64() % 4).unwrap() {
|
||||
// TODO: Double check how the processor handles this ID field
|
||||
@@ -298,8 +293,7 @@ fn batch_test() {
|
||||
.await;
|
||||
}
|
||||
(id, preprocesses) =
|
||||
recv_batch_preprocesses(&mut coordinators, &key_pair.0 .0, &expected_batch, attempt)
|
||||
.await;
|
||||
recv_batch_preprocesses(&mut coordinators, Session(0), &expected_batch, attempt).await;
|
||||
}
|
||||
|
||||
// Continue with signing the batch
|
||||
@@ -319,7 +313,6 @@ fn batch_test() {
|
||||
serai_time,
|
||||
network_latest_finalized_block: batch.batch.block,
|
||||
},
|
||||
network,
|
||||
block: substrate_block_num + u64::from(i),
|
||||
burns: vec![],
|
||||
batches: vec![batch.batch.id],
|
||||
|
||||
@@ -4,14 +4,14 @@ use dkg::{Participant, ThresholdParams, tests::clone_without};
|
||||
|
||||
use serai_client::{
|
||||
primitives::{NetworkId, BlockHash, PublicKey},
|
||||
validator_sets::primitives::{Session, ValidatorSet, KeyPair},
|
||||
validator_sets::primitives::{Session, KeyPair},
|
||||
};
|
||||
|
||||
use messages::{SubstrateContext, key_gen::KeyGenId, CoordinatorMessage, ProcessorMessage};
|
||||
|
||||
use crate::{*, tests::*};
|
||||
|
||||
pub(crate) async fn key_gen(coordinators: &mut [Coordinator], network: NetworkId) -> KeyPair {
|
||||
pub(crate) async fn key_gen(coordinators: &mut [Coordinator]) -> KeyPair {
|
||||
// Perform an interaction with all processors via their coordinators
|
||||
async fn interact_with_all<
|
||||
FS: Fn(Participant) -> messages::key_gen::CoordinatorMessage,
|
||||
@@ -33,7 +33,7 @@ pub(crate) async fn key_gen(coordinators: &mut [Coordinator], network: NetworkId
|
||||
}
|
||||
|
||||
// Order a key gen
|
||||
let id = KeyGenId { set: ValidatorSet { session: Session(0), network }, attempt: 0 };
|
||||
let id = KeyGenId { session: Session(0), attempt: 0 };
|
||||
|
||||
let mut commitments = HashMap::new();
|
||||
interact_with_all(
|
||||
@@ -132,7 +132,7 @@ pub(crate) async fn key_gen(coordinators: &mut [Coordinator], network: NetworkId
|
||||
.send_message(CoordinatorMessage::Substrate(
|
||||
messages::substrate::CoordinatorMessage::ConfirmKeyPair {
|
||||
context,
|
||||
set: id.set,
|
||||
session: id.session,
|
||||
key_pair: key_pair.clone(),
|
||||
},
|
||||
))
|
||||
@@ -158,7 +158,7 @@ fn key_gen_test() {
|
||||
.map(|(handles, key)| Coordinator::new(network, &ops, handles, key))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
key_gen(&mut coordinators, network).await;
|
||||
key_gen(&mut coordinators).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ use messages::{sign::SignId, SubstrateContext};
|
||||
|
||||
use serai_client::{
|
||||
primitives::{BlockHash, NetworkId},
|
||||
in_instructions::primitives::Batch,
|
||||
coins::primitives::{OutInstruction, OutInstructionWithBalance},
|
||||
in_instructions::primitives::Batch,
|
||||
validator_sets::primitives::Session,
|
||||
};
|
||||
|
||||
use crate::{*, tests::*};
|
||||
@@ -18,7 +19,7 @@ use crate::{*, tests::*};
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn recv_sign_preprocesses(
|
||||
coordinators: &mut [Coordinator],
|
||||
key: Vec<u8>,
|
||||
session: Session,
|
||||
attempt: u32,
|
||||
) -> (SignId, HashMap<Participant, Vec<u8>>) {
|
||||
let mut id = None;
|
||||
@@ -33,7 +34,7 @@ pub(crate) async fn recv_sign_preprocesses(
|
||||
preprocesses: mut these_preprocesses,
|
||||
}) => {
|
||||
if id.is_none() {
|
||||
assert_eq!(&this_id.key, &key);
|
||||
assert_eq!(&this_id.session, &session);
|
||||
assert_eq!(this_id.attempt, attempt);
|
||||
id = Some(this_id.clone());
|
||||
}
|
||||
@@ -62,6 +63,7 @@ pub(crate) async fn recv_sign_preprocesses(
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn sign_tx(
|
||||
coordinators: &mut [Coordinator],
|
||||
session: Session,
|
||||
id: SignId,
|
||||
preprocesses: HashMap<Participant, Vec<u8>>,
|
||||
) -> Vec<u8> {
|
||||
@@ -120,11 +122,11 @@ pub(crate) async fn sign_tx(
|
||||
if preprocesses.contains_key(&i) {
|
||||
match coordinator.recv_message().await {
|
||||
messages::ProcessorMessage::Sign(messages::sign::ProcessorMessage::Completed {
|
||||
key,
|
||||
session: this_session,
|
||||
id: this_id,
|
||||
tx: this_tx,
|
||||
}) => {
|
||||
assert_eq!(&key, &id.key);
|
||||
assert_eq!(session, this_session);
|
||||
assert_eq!(&this_id, &id.id);
|
||||
|
||||
if tx.is_none() {
|
||||
@@ -158,7 +160,7 @@ fn send_test() {
|
||||
coordinators[0].sync(&ops, &coordinators[1 ..]).await;
|
||||
|
||||
// Generate keys
|
||||
let key_pair = key_gen(&mut coordinators, network).await;
|
||||
let key_pair = key_gen(&mut coordinators).await;
|
||||
|
||||
// Now we we have to mine blocks to activate the key
|
||||
// (the first key is activated when the network's time as of a block exceeds the Serai time
|
||||
@@ -195,7 +197,7 @@ fn send_test() {
|
||||
|
||||
// Make sure the proceessors picked it up by checking they're trying to sign a batch for it
|
||||
let (id, preprocesses) =
|
||||
recv_batch_preprocesses(&mut coordinators, &key_pair.0 .0, &expected_batch, 0).await;
|
||||
recv_batch_preprocesses(&mut coordinators, Session(0), &expected_batch, 0).await;
|
||||
|
||||
// Continue with signing the batch
|
||||
let batch = sign_batch(&mut coordinators, key_pair.0 .0, id, preprocesses).await;
|
||||
@@ -216,7 +218,6 @@ fn send_test() {
|
||||
serai_time,
|
||||
network_latest_finalized_block: batch.batch.block,
|
||||
},
|
||||
network,
|
||||
block: substrate_block_num,
|
||||
burns: vec![OutInstructionWithBalance {
|
||||
instruction: OutInstruction { address: wallet.address(), data: None },
|
||||
@@ -237,9 +238,8 @@ fn send_test() {
|
||||
|
||||
// Start signing the TX
|
||||
let (mut id, mut preprocesses) =
|
||||
recv_sign_preprocesses(&mut coordinators, key_pair.1.to_vec(), 0).await;
|
||||
// TODO: Should this use the Substrate key?
|
||||
assert_eq!(id, SignId { key: key_pair.1.to_vec(), id: plans[0].id, attempt: 0 });
|
||||
recv_sign_preprocesses(&mut coordinators, Session(0), 0).await;
|
||||
assert_eq!(id, SignId { session: Session(0), id: plans[0].id, attempt: 0 });
|
||||
|
||||
// Trigger a random amount of re-attempts
|
||||
for attempt in 1 ..= u32::try_from(OsRng.next_u64() % 4).unwrap() {
|
||||
@@ -251,12 +251,11 @@ fn send_test() {
|
||||
.send_message(messages::sign::CoordinatorMessage::Reattempt { id: id.clone() })
|
||||
.await;
|
||||
}
|
||||
(id, preprocesses) =
|
||||
recv_sign_preprocesses(&mut coordinators, key_pair.1.to_vec(), attempt).await;
|
||||
(id, preprocesses) = recv_sign_preprocesses(&mut coordinators, Session(0), attempt).await;
|
||||
}
|
||||
let participating = preprocesses.keys().cloned().collect::<Vec<_>>();
|
||||
|
||||
let tx_id = sign_tx(&mut coordinators, id.clone(), preprocesses).await;
|
||||
let tx_id = sign_tx(&mut coordinators, Session(0), id.clone(), preprocesses).await;
|
||||
|
||||
// Make sure all participating nodes published the TX
|
||||
let participating =
|
||||
@@ -276,7 +275,7 @@ fn send_test() {
|
||||
// Tell them of it as a completion of the relevant signing nodess
|
||||
coordinator
|
||||
.send_message(messages::sign::CoordinatorMessage::Completed {
|
||||
key: key_pair.1.to_vec(),
|
||||
session: Session(0),
|
||||
id: id.id,
|
||||
tx: tx_id.clone(),
|
||||
})
|
||||
@@ -284,11 +283,11 @@ fn send_test() {
|
||||
// Verify they send Completed back
|
||||
match coordinator.recv_message().await {
|
||||
messages::ProcessorMessage::Sign(messages::sign::ProcessorMessage::Completed {
|
||||
key,
|
||||
session,
|
||||
id: this_id,
|
||||
tx: this_tx,
|
||||
}) => {
|
||||
assert_eq!(&key, &id.key);
|
||||
assert_eq!(session, Session(0));
|
||||
assert_eq!(&this_id, &id.id);
|
||||
assert_eq!(this_tx, tx_id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user