Skip to content

Commit

Permalink
Defer all possible errors from connect_lazy until request time (#18536)
Browse files Browse the repository at this point in the history
  • Loading branch information
mystenmark committed Jul 5, 2024
1 parent 3781a7e commit 8392d2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
50 changes: 30 additions & 20 deletions crates/sui-core/src/authority_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +85,7 @@ pub trait AuthorityAPI {

#[derive(Clone)]
pub struct NetworkAuthorityClient {
client: ValidatorClient<Channel>,
client: SuiResult<ValidatorClient<Channel>>,
}

impl NetworkAuthorityClient {
Expand All @@ -93,19 +96,26 @@ impl NetworkAuthorityClient {
Ok(Self::new(channel))
}

pub fn connect_lazy(address: &Multiaddr) -> anyhow::Result<Self> {
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<Channel>) -> Self {
Self {
client: client.map(ValidatorClient::new),
}
}

fn client(&self) -> ValidatorClient<Channel> {
fn client(&self) -> SuiResult<ValidatorClient<Channel>> {
self.client.clone()
}
}
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -167,7 +177,7 @@ impl AuthorityAPI for NetworkAuthorityClient {
&self,
request: ObjectInfoRequest,
) -> Result<ObjectInfoResponse, SuiError> {
self.client()
self.client()?
.object_info(request)
.await
.map(tonic::Response::into_inner)
Expand All @@ -179,7 +189,7 @@ impl AuthorityAPI for NetworkAuthorityClient {
&self,
request: TransactionInfoRequest,
) -> Result<TransactionInfoResponse, SuiError> {
self.client()
self.client()?
.transaction_info(request)
.await
.map(tonic::Response::into_inner)
Expand All @@ -191,7 +201,7 @@ impl AuthorityAPI for NetworkAuthorityClient {
&self,
request: CheckpointRequest,
) -> Result<CheckpointResponse, SuiError> {
self.client()
self.client()?
.checkpoint(request)
.await
.map(tonic::Response::into_inner)
Expand All @@ -203,7 +213,7 @@ impl AuthorityAPI for NetworkAuthorityClient {
&self,
request: CheckpointRequestV2,
) -> Result<CheckpointResponseV2, SuiError> {
self.client()
self.client()?
.checkpoint_v2(request)
.await
.map(tonic::Response::into_inner)
Expand All @@ -214,7 +224,7 @@ impl AuthorityAPI for NetworkAuthorityClient {
&self,
request: SystemStateRequest,
) -> Result<SuiSystemState, SuiError> {
self.client()
self.client()?
.get_system_state_object(request)
.await
.map(tonic::Response::into_inner)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,12 @@ impl From<&str> for SuiError {
}
}

impl From<String> for SuiError {
fn from(error: String) -> Self {
SuiError::GenericAuthorityError { error }
}
}

impl TryFrom<SuiError> for UserInputError {
type Error = anyhow::Error;

Expand Down

0 comments on commit 8392d2a

Please sign in to comment.