Skip to content

Commit

Permalink
update condition of loading network from chain, if epoch > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
lanvidr committed Jun 30, 2022
1 parent c7f87e7 commit 5f8b1e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
104 changes: 54 additions & 50 deletions crates/sui-core/src/epoch/reconfiguration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use sui_types::crypto::PublicKeyBytes;
use sui_types::error::{SuiError, SuiResult};
use sui_types::messages::{ConfirmationTransaction, SignedTransaction};
use sui_types::messages_checkpoint::CheckpointSequenceNumber;
use sui_types::sui_system_state::SuiSystemState;
use typed_store::Map;

#[async_trait]
Expand Down Expand Up @@ -108,63 +109,15 @@ where

// Reconnect the network if we have an type of AuthorityClient that has a network.
if A::needs_network_recreation() {
let mut new_clients = BTreeMap::new();
let sui_system_state = self
.state
.get_sui_system_state_object()
.await
.map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
})
.unwrap();
let next_epoch_validators = sui_system_state.validators.next_epoch_validators;

let mut net_config = mysten_network::config::Config::new();
net_config.connect_timeout = Some(Duration::from_secs(5));
net_config.request_timeout = Some(Duration::from_secs(5));
net_config.http2_keepalive_interval = Some(Duration::from_secs(5));

for validator in next_epoch_validators {
let net_addr: &[u8] = &validator.net_address.clone();
let str_addr =
std::str::from_utf8(net_addr).map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
});
let address: Multiaddr = str_addr
.unwrap()
.parse()
.map_err(|e: multiaddr::Error| SuiError::GenericAuthorityError {
error: e.to_string(),
})
.unwrap();

let channel = net_config
.connect_lazy(&address)
.map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
})
.unwrap();
let client: A = A::recreate(channel);
let name: &[u8] = &validator.name;
let public_key_bytes = PublicKeyBytes::try_from(name)?;
new_clients.insert(public_key_bytes, client);
}

// Replace the clients in the authority aggregator with new clients.
let new_net = Arc::new(AuthorityAggregator::new(
new_committee,
new_clients,
self.gateway_metrics.clone(),
));
self.net.store(new_net);
self.recreate_network(sui_system_state, new_committee)?;
} else {
// update the authorities with the new committee
let new_net = Arc::new(AuthorityAggregator::new(
new_committee,
self.net.load().clone_inner_clients(),
self.gateway_metrics.clone(),
));
self.net.store(new_net.clone());
self.net.store(new_net);
}
// TODO: Update all committee in all components safely,
// potentially restart narwhal committee/consensus adapter,
Expand Down Expand Up @@ -214,4 +167,55 @@ where
pub fn is_second_last_checkpoint_epoch(checkpoint: CheckpointSequenceNumber) -> bool {
(checkpoint + 1) % CHECKPOINT_COUNT_PER_EPOCH == 0
}

/// Recreates the network if the client is a type of client that has a network, and swap the new
/// clients onto the authority aggregator with the new committee.
pub fn recreate_network(
&self,
sui_system_state: SuiSystemState,
new_committee: Committee,
) -> SuiResult {
let mut new_clients = BTreeMap::new();
let next_epoch_validators = sui_system_state.validators.next_epoch_validators;

let mut net_config = mysten_network::config::Config::new();
net_config.connect_timeout = Some(Duration::from_secs(5));
net_config.request_timeout = Some(Duration::from_secs(5));
net_config.http2_keepalive_interval = Some(Duration::from_secs(5));

for validator in next_epoch_validators {
let net_addr: &[u8] = &validator.net_address.clone();
let str_addr =
std::str::from_utf8(net_addr).map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
});
let address: Multiaddr = str_addr
.unwrap()
.parse()
.map_err(|e: multiaddr::Error| SuiError::GenericAuthorityError {
error: e.to_string(),
})
.unwrap();

let channel = net_config
.connect_lazy(&address)
.map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
})
.unwrap();
let client: A = A::recreate(channel);
let name: &[u8] = &validator.name;
let public_key_bytes = PublicKeyBytes::try_from(name)?;
new_clients.insert(public_key_bytes, client);
}

// Replace the clients in the authority aggregator with new clients.
let new_net = Arc::new(AuthorityAggregator::new(
new_committee,
new_clients,
self.gateway_metrics.clone(),
));
self.net.store(new_net);
Ok(())
}
}
9 changes: 4 additions & 5 deletions crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use anyhow::Result;
use futures::TryFutureExt;
use jsonrpsee::http_server::HttpServerHandle;
use jsonrpsee::ws_server::WsServerBuilder;
use jsonrpsee::ws_server::WsServerHandle;
use multiaddr::Multiaddr;
use parking_lot::Mutex;
Expand Down Expand Up @@ -35,7 +34,6 @@ use sui_json_rpc::event_api::EventReadApiImpl;
use sui_json_rpc::event_api::EventStreamingApiImpl;
use sui_json_rpc::read_api::FullNodeApi;
use sui_json_rpc::read_api::ReadApi;
use sui_json_rpc_api::EventApiServer;
use sui_types::crypto::PublicKeyBytes;

pub mod metrics;
Expand Down Expand Up @@ -131,15 +129,16 @@ impl SuiNode {

let mut authority_clients = BTreeMap::new();

if config.enable_reconfig {
let sui_system_state = state.get_sui_system_state_object().await?;

if config.enable_reconfig && sui_system_state.epoch > 0 {
// Create NetworkAuthorityClient with this epoch's network information
let sui_system_state = state.get_sui_system_state_object().await?;
let epoch_validators = &sui_system_state.validators.active_validators;

for validator in epoch_validators {
let net_addr: &[u8] = &validator.metadata.net_address.clone();
let str_addr = std::str::from_utf8(net_addr)?;
let address: Multiaddr = str_addr.parse().unwrap();
let address: Multiaddr = str_addr.parse()?;
//let address = Multiaddr::try_from(net_addr)?;
let channel = net_config.connect_lazy(&address)?;
let client = NetworkAuthorityClient::new(channel);
Expand Down

0 comments on commit 5f8b1e6

Please sign in to comment.