Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick 18536 #18541

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading