From 785b163ccfe43c83567a311a3103f3e0f6228c83 Mon Sep 17 00:00:00 2001 From: Mark Logan <103447440+mystenmark@users.noreply.github.com> Date: Fri, 5 Jul 2024 15:25:18 -0700 Subject: [PATCH] Defer all possible errors from connect_lazy until request time (#18536) --- crates/sui-core/src/authority_client.rs | 50 +++++++++++++++---------- crates/sui-types/src/error.rs | 6 +++ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/crates/sui-core/src/authority_client.rs b/crates/sui-core/src/authority_client.rs index 2b9f93ad588e5..bc62d8166c774 100644 --- a/crates/sui-core/src/authority_client.rs +++ b/crates/sui-core/src/authority_client.rs @@ -16,7 +16,10 @@ use sui_types::messages_checkpoint::{ }; use sui_types::multiaddr::Multiaddr; use sui_types::sui_system_state::SuiSystemState; -use sui_types::{error::SuiError, transaction::*}; +use sui_types::{ + error::{SuiError, SuiResult}, + transaction::*, +}; use crate::authority_client::tonic::IntoRequest; use sui_network::tonic::metadata::KeyAndValueRef; @@ -82,7 +85,7 @@ pub trait AuthorityAPI { #[derive(Clone)] pub struct NetworkAuthorityClient { - client: ValidatorClient, + client: SuiResult>, } impl NetworkAuthorityClient { @@ -93,19 +96,26 @@ impl NetworkAuthorityClient { Ok(Self::new(channel)) } - pub fn connect_lazy(address: &Multiaddr) -> anyhow::Result { - let channel = mysten_network::client::connect_lazy(address) - .map_err(|err| anyhow!(err.to_string()))?; - Ok(Self::new(channel)) + pub fn connect_lazy(address: &Multiaddr) -> Self { + let client: SuiResult<_> = mysten_network::client::connect_lazy(address) + .map(ValidatorClient::new) + .map_err(|err| err.to_string().into()); + Self { client } } pub fn new(channel: Channel) -> Self { Self { - client: ValidatorClient::new(channel), + client: Ok(ValidatorClient::new(channel)), + } + } + + fn new_lazy(client: SuiResult) -> Self { + Self { + client: client.map(ValidatorClient::new), } } - fn client(&self) -> ValidatorClient { + fn client(&self) -> SuiResult> { self.client.clone() } } @@ -121,7 +131,7 @@ impl AuthorityAPI for NetworkAuthorityClient { let mut request = transaction.into_request(); insert_metadata(&mut request, client_addr); - self.client() + self.client()? .transaction(request) .await .map(tonic::Response::into_inner) @@ -138,7 +148,7 @@ impl AuthorityAPI for NetworkAuthorityClient { insert_metadata(&mut request, client_addr); let response = self - .client() + .client()? .handle_certificate_v2(request) .await .map(tonic::Response::into_inner); @@ -155,7 +165,7 @@ impl AuthorityAPI for NetworkAuthorityClient { insert_metadata(&mut request, client_addr); let response = self - .client() + .client()? .handle_certificate_v3(request) .await .map(tonic::Response::into_inner); @@ -167,7 +177,7 @@ impl AuthorityAPI for NetworkAuthorityClient { &self, request: ObjectInfoRequest, ) -> Result { - self.client() + self.client()? .object_info(request) .await .map(tonic::Response::into_inner) @@ -179,7 +189,7 @@ impl AuthorityAPI for NetworkAuthorityClient { &self, request: TransactionInfoRequest, ) -> Result { - self.client() + self.client()? .transaction_info(request) .await .map(tonic::Response::into_inner) @@ -191,7 +201,7 @@ impl AuthorityAPI for NetworkAuthorityClient { &self, request: CheckpointRequest, ) -> Result { - self.client() + self.client()? .checkpoint(request) .await .map(tonic::Response::into_inner) @@ -203,7 +213,7 @@ impl AuthorityAPI for NetworkAuthorityClient { &self, request: CheckpointRequestV2, ) -> Result { - self.client() + self.client()? .checkpoint_v2(request) .await .map(tonic::Response::into_inner) @@ -214,7 +224,7 @@ impl AuthorityAPI for NetworkAuthorityClient { &self, request: SystemStateRequest, ) -> Result { - self.client() + self.client()? .get_system_state_object(request) .await .map(tonic::Response::into_inner) @@ -236,15 +246,15 @@ pub fn make_network_authority_clients_with_network_config( })? .network_address; let address = address.rewrite_udp_to_tcp(); - let channel = network_config.connect_lazy(&address).map_err(|e| { + let maybe_channel = network_config.connect_lazy(&address).map_err(|e| { tracing::error!( address = %address, name = %name, "unable to create authority client: {e}" ); - anyhow!(e.to_string()) - })?; - let client = NetworkAuthorityClient::new(channel); + e.to_string().into() + }); + let client = NetworkAuthorityClient::new_lazy(maybe_channel); authority_clients.insert(*name, client); } Ok(authority_clients) diff --git a/crates/sui-types/src/error.rs b/crates/sui-types/src/error.rs index 9b62a3bb2cbe7..e1df3c3db7912 100644 --- a/crates/sui-types/src/error.rs +++ b/crates/sui-types/src/error.rs @@ -749,6 +749,12 @@ impl From<&str> for SuiError { } } +impl From for SuiError { + fn from(error: String) -> Self { + SuiError::GenericAuthorityError { error } + } +} + impl TryFrom for UserInputError { type Error = anyhow::Error;