@@ -16,6 +16,7 @@ use serde::{de::DeserializeOwned, Serialize};
1616use sp1_core_machine:: io:: SP1Stdin ;
1717use sp1_prover:: { HashableKey , SP1VerifyingKey } ;
1818use tonic:: { transport:: Channel , Code } ;
19+ use tokio:: sync:: OnceCell ;
1920
2021use super :: {
2122 grpc,
@@ -47,6 +48,7 @@ pub struct NetworkClient {
4748 pub ( crate ) signer : NetworkSigner ,
4849 pub ( crate ) http : HttpClientWithMiddleware ,
4950 pub ( crate ) rpc_url : String ,
51+ pub ( crate ) channel : OnceCell < Channel > ,
5052}
5153
5254#[ async_trait]
@@ -85,7 +87,7 @@ impl NetworkClient {
8587 . pool_idle_timeout ( Duration :: from_secs ( 240 ) )
8688 . build ( )
8789 . unwrap ( ) ;
88- Self { signer, http : client. into ( ) , rpc_url : rpc_url. into ( ) }
90+ Self { signer, http : client. into ( ) , rpc_url : rpc_url. into ( ) , channel : OnceCell :: new ( ) }
8991 }
9092
9193 /// Get the latest nonce for this account's address.
@@ -475,26 +477,20 @@ impl NetworkClient {
475477 . await
476478 }
477479
480+ pub ( crate ) async fn get_channel ( & self ) -> Result < Channel > {
481+ Ok ( self . channel . get_or_try_init ( || async {
482+ let channel = grpc:: configure_endpoint ( & self . rpc_url ) ?. connect ( ) . await ?;
483+ Ok ( channel)
484+ } )
485+ . await . map_err ( |e| anyhow:: anyhow!( "Failed to connect: {:?}" , e) ) ?. clone ( ) )
486+ }
487+
478488 pub ( crate ) async fn prover_network_client ( & self ) -> Result < ProverNetworkClient < Channel > > {
479- self . with_retry (
480- || async {
481- let channel = grpc:: configure_endpoint ( & self . rpc_url ) ?. connect ( ) . await ?;
482- Ok ( ProverNetworkClient :: new ( channel) )
483- } ,
484- "creating network client" ,
485- )
486- . await
489+ Ok ( ProverNetworkClient :: new ( self . get_channel ( ) . await ?) )
487490 }
488491
489492 pub ( crate ) async fn artifact_store_client ( & self ) -> Result < ArtifactStoreClient < Channel > > {
490- self . with_retry (
491- || async {
492- let channel = grpc:: configure_endpoint ( & self . rpc_url ) ?. connect ( ) . await ?;
493- Ok ( ArtifactStoreClient :: new ( channel) )
494- } ,
495- "creating artifact client" ,
496- )
497- . await
493+ Ok ( ArtifactStoreClient :: new ( self . get_channel ( ) . await ?) )
498494 }
499495
500496 pub ( crate ) async fn create_artifact_with_content < T : Serialize + Send + Sync > (
0 commit comments