mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 12:49:23 +00:00
Correct processor flow to have the coordinator decide signing set/re-attempts
The signing set should be the first group to submit preprocesses to Tributary. Re-attempts shouldn't be once every 30s, yet n blocks since the last relevant message. Removes the use of an async task/channel in the signer (and Substrate signer). Also removes the need to be able to get the time from a coin's block, which was a fragile system marked with a TODO already.
This commit is contained in:
@@ -52,7 +52,7 @@ async fn spend<C: Coin, D: Db>(
|
||||
coin.mine_block().await;
|
||||
}
|
||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||
ScannerEvent::Block { key: this_key, block: _, time: _, batch: this_batch, outputs } => {
|
||||
ScannerEvent::Block { key: this_key, block: _, batch: this_batch, outputs } => {
|
||||
assert_eq!(this_key, key);
|
||||
assert_eq!(this_batch, batch);
|
||||
assert_eq!(outputs.len(), 1);
|
||||
@@ -89,7 +89,7 @@ pub async fn test_addresses<C: Coin>(coin: C) {
|
||||
// Verify the Scanner picked them up
|
||||
let outputs =
|
||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||
ScannerEvent::Block { key: this_key, block, time: _, batch, outputs } => {
|
||||
ScannerEvent::Block { key: this_key, block, batch, outputs } => {
|
||||
assert_eq!(this_key, key);
|
||||
assert_eq!(block, block_id);
|
||||
assert_eq!(batch, 0);
|
||||
|
||||
@@ -122,7 +122,7 @@ pub async fn test_key_gen<C: Coin>() {
|
||||
let key_gen = key_gens.get_mut(&i).unwrap();
|
||||
if let KeyGenEvent::KeyConfirmed { activation_block, substrate_keys, coin_keys } = key_gen
|
||||
.handle(CoordinatorMessage::ConfirmKeyPair {
|
||||
context: SubstrateContext { time: 0, coin_latest_finalized_block: BlockHash([0x11; 32]) },
|
||||
context: SubstrateContext { coin_latest_finalized_block: BlockHash([0x11; 32]) },
|
||||
id: ID,
|
||||
})
|
||||
.await
|
||||
|
||||
@@ -43,16 +43,14 @@ pub async fn test_scanner<C: Coin>(coin: C) {
|
||||
// Receive funds
|
||||
let block = coin.test_send(C::address(keys.group_key())).await;
|
||||
let block_id = block.id();
|
||||
let block_time = block.time();
|
||||
|
||||
// Verify the Scanner picked them up
|
||||
let verify_event = |mut scanner: ScannerHandle<C, MemDb>| async {
|
||||
let outputs =
|
||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||
ScannerEvent::Block { key, block, time, batch, outputs } => {
|
||||
ScannerEvent::Block { key, block, batch, outputs } => {
|
||||
assert_eq!(key, keys.group_key());
|
||||
assert_eq!(block, block_id);
|
||||
assert_eq!(time, block_time);
|
||||
assert_eq!(batch, 0);
|
||||
assert_eq!(outputs.len(), 1);
|
||||
assert_eq!(outputs[0].kind(), OutputType::External);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use std::{
|
||||
time::{Duration, SystemTime},
|
||||
collections::HashMap,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::OsRng;
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use frost::{
|
||||
@@ -11,8 +8,6 @@ use frost::{
|
||||
dkg::tests::{key_gen, clone_without},
|
||||
};
|
||||
|
||||
use tokio::time::timeout;
|
||||
|
||||
use serai_db::MemDb;
|
||||
|
||||
use messages::sign::*;
|
||||
@@ -36,35 +31,52 @@ pub async fn sign<C: Coin>(
|
||||
attempt: 0,
|
||||
};
|
||||
|
||||
let signing_set = actual_id.signing_set(&keys_txs[&Participant::new(1).unwrap()].0.params());
|
||||
let mut keys = HashMap::new();
|
||||
let mut txs = HashMap::new();
|
||||
for (i, (these_keys, this_tx)) in keys_txs.drain() {
|
||||
assert_eq!(actual_id.signing_set(&these_keys.params()), signing_set);
|
||||
keys.insert(i, these_keys);
|
||||
txs.insert(i, this_tx);
|
||||
}
|
||||
|
||||
let mut signers = HashMap::new();
|
||||
let mut t = 0;
|
||||
for i in 1 ..= keys.len() {
|
||||
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||
signers.insert(i, Signer::new(MemDb::new(), coin.clone(), keys.remove(&i).unwrap()));
|
||||
let keys = keys.remove(&i).unwrap();
|
||||
t = keys.params().t();
|
||||
signers.insert(i, Signer::new(MemDb::new(), coin.clone(), keys));
|
||||
}
|
||||
drop(keys);
|
||||
|
||||
let start = SystemTime::now();
|
||||
for i in 1 ..= signers.len() {
|
||||
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||
let (tx, eventuality) = txs.remove(&i).unwrap();
|
||||
signers[&i].sign_transaction(actual_id.id, start, tx, eventuality).await;
|
||||
signers.get_mut(&i).unwrap().sign_transaction(actual_id.id, tx, eventuality).await;
|
||||
}
|
||||
|
||||
let mut signing_set = vec![];
|
||||
while signing_set.len() < usize::from(t) {
|
||||
let candidate = Participant::new(
|
||||
u16::try_from((OsRng.next_u64() % u64::try_from(signers.len()).unwrap()) + 1).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
if signing_set.contains(&candidate) {
|
||||
continue;
|
||||
}
|
||||
signing_set.push(candidate);
|
||||
}
|
||||
|
||||
// All participants should emit a preprocess
|
||||
let mut preprocesses = HashMap::new();
|
||||
for i in &signing_set {
|
||||
if let Some(SignerEvent::ProcessorMessage(ProcessorMessage::Preprocess { id, preprocess })) =
|
||||
signers.get_mut(i).unwrap().events.recv().await
|
||||
for i in 1 ..= signers.len() {
|
||||
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||
if let SignerEvent::ProcessorMessage(ProcessorMessage::Preprocess { id, preprocess }) =
|
||||
signers.get_mut(&i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(id, actual_id);
|
||||
preprocesses.insert(*i, preprocess);
|
||||
if signing_set.contains(&i) {
|
||||
preprocesses.insert(i, preprocess);
|
||||
}
|
||||
} else {
|
||||
panic!("didn't get preprocess back");
|
||||
}
|
||||
@@ -72,14 +84,16 @@ pub async fn sign<C: Coin>(
|
||||
|
||||
let mut shares = HashMap::new();
|
||||
for i in &signing_set {
|
||||
signers[i]
|
||||
signers
|
||||
.get_mut(i)
|
||||
.unwrap()
|
||||
.handle(CoordinatorMessage::Preprocesses {
|
||||
id: actual_id.clone(),
|
||||
preprocesses: clone_without(&preprocesses, i),
|
||||
})
|
||||
.await;
|
||||
if let Some(SignerEvent::ProcessorMessage(ProcessorMessage::Share { id, share })) =
|
||||
signers.get_mut(i).unwrap().events.recv().await
|
||||
if let SignerEvent::ProcessorMessage(ProcessorMessage::Share { id, share }) =
|
||||
signers.get_mut(i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(id, actual_id);
|
||||
shares.insert(*i, share);
|
||||
@@ -90,14 +104,16 @@ pub async fn sign<C: Coin>(
|
||||
|
||||
let mut tx_id = None;
|
||||
for i in &signing_set {
|
||||
signers[i]
|
||||
signers
|
||||
.get_mut(i)
|
||||
.unwrap()
|
||||
.handle(CoordinatorMessage::Shares {
|
||||
id: actual_id.clone(),
|
||||
shares: clone_without(&shares, i),
|
||||
})
|
||||
.await;
|
||||
if let Some(SignerEvent::SignedTransaction { id, tx }) =
|
||||
signers.get_mut(i).unwrap().events.recv().await
|
||||
if let SignerEvent::SignedTransaction { id, tx } =
|
||||
signers.get_mut(i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(id, actual_id.id);
|
||||
if tx_id.is_none() {
|
||||
@@ -109,20 +125,9 @@ pub async fn sign<C: Coin>(
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the signers not included didn't do anything
|
||||
let mut excluded = (1 ..= signers.len())
|
||||
.map(|i| Participant::new(u16::try_from(i).unwrap()).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
for i in signing_set {
|
||||
excluded.remove(excluded.binary_search(&i).unwrap());
|
||||
}
|
||||
for i in excluded {
|
||||
assert!(timeout(
|
||||
Duration::from_secs(5),
|
||||
signers.get_mut(&Participant::new(u16::try_from(i).unwrap()).unwrap()).unwrap().events.recv()
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
// Make sure there's no events left
|
||||
for (_, mut signer) in signers.drain() {
|
||||
assert!(signer.events.pop_front().is_none());
|
||||
}
|
||||
|
||||
tx_id.unwrap()
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use std::{
|
||||
time::{Duration, SystemTime},
|
||||
collections::HashMap,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand_core::OsRng;
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use frost::{
|
||||
@@ -12,8 +9,6 @@ use frost::{
|
||||
dkg::tests::{key_gen, clone_without},
|
||||
};
|
||||
|
||||
use tokio::time::timeout;
|
||||
|
||||
use scale::Encode;
|
||||
use sp_application_crypto::{RuntimePublic, sr25519::Public};
|
||||
|
||||
@@ -53,29 +48,43 @@ async fn test_substrate_signer() {
|
||||
],
|
||||
};
|
||||
|
||||
let signing_set = actual_id.signing_set(&keys[&participant_one].params());
|
||||
for these_keys in keys.values() {
|
||||
assert_eq!(actual_id.signing_set(&these_keys.params()), signing_set);
|
||||
}
|
||||
|
||||
let start = SystemTime::now();
|
||||
let mut signers = HashMap::new();
|
||||
let mut t = 0;
|
||||
for i in 1 ..= keys.len() {
|
||||
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||
let signer = SubstrateSigner::new(MemDb::new(), keys.remove(&i).unwrap());
|
||||
signer.sign(start, batch.clone()).await;
|
||||
let keys = keys.remove(&i).unwrap();
|
||||
t = keys.params().t();
|
||||
let mut signer = SubstrateSigner::new(MemDb::new(), keys);
|
||||
signer.sign(batch.clone()).await;
|
||||
signers.insert(i, signer);
|
||||
}
|
||||
drop(keys);
|
||||
|
||||
let mut signing_set = vec![];
|
||||
while signing_set.len() < usize::from(t) {
|
||||
let candidate = Participant::new(
|
||||
u16::try_from((OsRng.next_u64() % u64::try_from(signers.len()).unwrap()) + 1).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
if signing_set.contains(&candidate) {
|
||||
continue;
|
||||
}
|
||||
signing_set.push(candidate);
|
||||
}
|
||||
|
||||
// All participants should emit a preprocess
|
||||
let mut preprocesses = HashMap::new();
|
||||
for i in &signing_set {
|
||||
if let Some(SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchPreprocess {
|
||||
for i in 1 ..= signers.len() {
|
||||
let i = Participant::new(u16::try_from(i).unwrap()).unwrap();
|
||||
if let SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchPreprocess {
|
||||
id,
|
||||
preprocess,
|
||||
})) = signers.get_mut(i).unwrap().events.recv().await
|
||||
}) = signers.get_mut(&i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(id, actual_id);
|
||||
preprocesses.insert(*i, preprocess);
|
||||
if signing_set.contains(&i) {
|
||||
preprocesses.insert(i, preprocess);
|
||||
}
|
||||
} else {
|
||||
panic!("didn't get preprocess back");
|
||||
}
|
||||
@@ -83,16 +92,16 @@ async fn test_substrate_signer() {
|
||||
|
||||
let mut shares = HashMap::new();
|
||||
for i in &signing_set {
|
||||
signers[i]
|
||||
signers
|
||||
.get_mut(i)
|
||||
.unwrap()
|
||||
.handle(CoordinatorMessage::BatchPreprocesses {
|
||||
id: actual_id.clone(),
|
||||
preprocesses: clone_without(&preprocesses, i),
|
||||
})
|
||||
.await;
|
||||
if let Some(SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchShare {
|
||||
id,
|
||||
share,
|
||||
})) = signers.get_mut(i).unwrap().events.recv().await
|
||||
if let SubstrateSignerEvent::ProcessorMessage(ProcessorMessage::BatchShare { id, share }) =
|
||||
signers.get_mut(i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(id, actual_id);
|
||||
shares.insert(*i, share);
|
||||
@@ -102,15 +111,17 @@ async fn test_substrate_signer() {
|
||||
}
|
||||
|
||||
for i in &signing_set {
|
||||
signers[i]
|
||||
signers
|
||||
.get_mut(i)
|
||||
.unwrap()
|
||||
.handle(CoordinatorMessage::BatchShares {
|
||||
id: actual_id.clone(),
|
||||
shares: clone_without(&shares, i),
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Some(SubstrateSignerEvent::SignedBatch(signed_batch)) =
|
||||
signers.get_mut(i).unwrap().events.recv().await
|
||||
if let SubstrateSignerEvent::SignedBatch(signed_batch) =
|
||||
signers.get_mut(i).unwrap().events.pop_front().unwrap()
|
||||
{
|
||||
assert_eq!(signed_batch.batch, batch);
|
||||
assert!(Public::from_raw(actual_id.key.clone().try_into().unwrap())
|
||||
@@ -120,19 +131,8 @@ async fn test_substrate_signer() {
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure the signers not included didn't do anything
|
||||
let mut excluded = (1 ..= signers.len())
|
||||
.map(|i| Participant::new(u16::try_from(i).unwrap()).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
for i in signing_set {
|
||||
excluded.remove(excluded.binary_search(&i).unwrap());
|
||||
}
|
||||
for i in excluded {
|
||||
assert!(timeout(
|
||||
Duration::from_secs(5),
|
||||
signers.get_mut(&Participant::new(u16::try_from(i).unwrap()).unwrap()).unwrap().events.recv()
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
// Make sure there's no events left
|
||||
for (_, mut signer) in signers.drain() {
|
||||
assert!(signer.events.pop_front().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,13 +31,11 @@ pub async fn test_wallet<C: Coin>(coin: C) {
|
||||
|
||||
let block = coin.test_send(C::address(key)).await;
|
||||
let block_id = block.id();
|
||||
let block_time = block.time();
|
||||
|
||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||
ScannerEvent::Block { key: this_key, block, time, batch, outputs } => {
|
||||
ScannerEvent::Block { key: this_key, block, batch, outputs } => {
|
||||
assert_eq!(this_key, key);
|
||||
assert_eq!(block, block_id);
|
||||
assert_eq!(time, block_time);
|
||||
assert_eq!(batch, 0);
|
||||
assert_eq!(outputs.len(), 1);
|
||||
(block_id, outputs)
|
||||
@@ -104,10 +102,9 @@ pub async fn test_wallet<C: Coin>(coin: C) {
|
||||
}
|
||||
|
||||
match timeout(Duration::from_secs(30), scanner.events.recv()).await.unwrap().unwrap() {
|
||||
ScannerEvent::Block { key: this_key, block: block_id, time, batch, outputs: these_outputs } => {
|
||||
ScannerEvent::Block { key: this_key, block: block_id, batch, outputs: these_outputs } => {
|
||||
assert_eq!(this_key, key);
|
||||
assert_eq!(block_id, block.id());
|
||||
assert_eq!(time, block.time());
|
||||
assert_eq!(batch, 1);
|
||||
assert_eq!(these_outputs, outputs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user