Skip to content

Commit 16ca00d

Browse files
committed
Optimize distributed accounts signing waiting times
1 parent 3f59b79 commit 16ca00d

3 files changed

Lines changed: 48 additions & 26 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/signer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ prost.workspace = true
2121

2222
# async / threads
2323
tokio.workspace = true
24+
futures.workspace = true
2425

2526
# telemetry
2627
tracing.workspace = true

crates/signer/src/manager/dirk.rs

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use cb_common::{
1313
types::{Chain, ModuleId},
1414
};
1515
use eyre::bail;
16+
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
1617
use rand::Rng;
1718
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity};
1819
use tracing::{debug, error, warn};
@@ -21,7 +22,7 @@ use tree_hash::TreeHash;
2122
use crate::{
2223
error::SignerModuleError,
2324
proto::v1::{
24-
account_manager_client::AccountManagerClient, lister_client::ListerClient,
25+
account_manager_client::AccountManagerClient, lister_client::ListerClient, sign_request,
2526
signer_client::SignerClient, Endpoint, GenerateRequest, ListAccountsRequest, ResponseState,
2627
SignRequest,
2728
},
@@ -311,18 +312,16 @@ impl DirkManager {
311312
.sign(SignRequest {
312313
data: object_root.to_vec(),
313314
domain: domain.to_vec(),
314-
id: Some(crate::proto::v1::sign_request::Id::PublicKey(
315-
account.public_key.to_vec(),
316-
)),
315+
id: Some(sign_request::Id::PublicKey(account.public_key.to_vec())),
317316
})
318317
.await
319-
.map_err(|_| {
320-
SignerModuleError::DirkCommunicationError("Failed to sign object".to_string())
318+
.map_err(|e| {
319+
SignerModuleError::DirkCommunicationError(format!("Failed to sign object: {e}"))
321320
})?;
322321

323322
if response.get_ref().state() != ResponseState::Succeeded {
324323
return Err(SignerModuleError::DirkCommunicationError(
325-
"Failed to sign object".to_string(),
324+
"Failed to sign object, server responded error".to_string(),
326325
));
327326
}
328327

@@ -331,47 +330,68 @@ impl DirkManager {
331330
})
332331
}
333332

334-
// TODO: Improve await times
335333
async fn request_distributed_signature(
336334
&self,
337335
account: &DistributedAccount,
338336
object_root: [u8; 32],
339337
) -> Result<BlsSignature, SignerModuleError> {
340338
let mut partials = Vec::with_capacity(account.participants.len());
339+
let mut requests = Vec::with_capacity(account.participants.len());
341340

342341
for (id, endpoint) in account.participants.iter() {
343342
let Some(channel) = self.connections.get(endpoint) else {
344343
warn!("Couldn't find server {endpoint}");
345344
continue;
346345
};
347346

348-
let Ok(response) = SignerClient::new(channel.clone())
349-
.sign(SignRequest {
350-
data: object_root.to_vec(),
351-
domain: compute_domain(self.chain, COMMIT_BOOST_DOMAIN).to_vec(),
352-
id: Some(crate::proto::v1::sign_request::Id::Account(format!(
353-
"{}/{}",
354-
account.wallet, account.name
355-
))),
356-
})
357-
.await
358-
else {
359-
warn!("Failed to sign object with server {endpoint}");
360-
continue;
347+
let request = async move {
348+
SignerClient::new(channel.clone())
349+
.sign(SignRequest {
350+
data: object_root.to_vec(),
351+
domain: compute_domain(self.chain, COMMIT_BOOST_DOMAIN).to_vec(),
352+
id: Some(sign_request::Id::Account(format!(
353+
"{}/{}",
354+
account.wallet, account.name
355+
))),
356+
})
357+
.map(|res| (res, (*id, endpoint.clone())))
358+
.await
359+
};
360+
requests.push(request);
361+
}
362+
363+
let mut requests = requests.into_iter().collect::<FuturesUnordered<_>>();
364+
365+
while let Some((response, participant)) = requests.next().await {
366+
let (id, endpoint) = participant;
367+
368+
let response = match response {
369+
Ok(res) => res,
370+
Err(e) => {
371+
warn!("Failed to sign object with server {endpoint}: {e}");
372+
continue;
373+
}
361374
};
362375

363376
if response.get_ref().state() != ResponseState::Succeeded {
364377
warn!("Failed to sign object with server {endpoint}");
365378
continue;
366379
}
367380

368-
let Ok(signature) = BlsSignature::try_from(response.into_inner().signature.as_slice())
369-
else {
370-
warn!("Failed to parse signature from server {endpoint}");
371-
continue;
381+
let signature = match BlsSignature::try_from(response.into_inner().signature.as_slice())
382+
{
383+
Ok(sig) => sig,
384+
Err(e) => {
385+
warn!("Failed to parse signature from server {endpoint}: {e}");
386+
continue;
387+
}
372388
};
373389

374-
partials.push((signature, *id));
390+
partials.push((signature, id));
391+
392+
if partials.len() >= account.threshold as usize {
393+
break;
394+
}
375395
}
376396

377397
if partials.len() < account.threshold as usize {

0 commit comments

Comments
 (0)