Skip to content

Commit

Permalink
refactor(network): immutable chain id (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape authored Aug 24, 2023
1 parent 8ff7658 commit 1f0ddfa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
34 changes: 25 additions & 9 deletions core/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub struct NetworkConfig {

// rpc
pub rpc_timeout: Duration,

// consensus
pub chain_id: u64,
}

impl NetworkConfig {
Expand Down Expand Up @@ -88,24 +91,32 @@ impl NetworkConfig {
ping_timeout: Duration::from_secs(DEFAULT_PING_TIMEOUT),

rpc_timeout: Duration::from_secs(DEFAULT_RPC_TIMEOUT),

chain_id: Default::default(),
}
}

pub fn from_config(config: &Config) -> ProtocolResult<Self> {
let mut network_config = Self::new()
pub fn from_config(config: &Config, chain_id: u64) -> ProtocolResult<Self> {
Self::new()
.peer_store_dir(config.data_path.clone().join("peer_store"))
.ping_interval(config.network.ping_interval)
.max_frame_length(config.network.max_frame_length)
.send_buffer_size(config.network.send_buffer_size)
.recv_buffer_size(config.network.recv_buffer_size)
.bootstraps(config.network.bootstraps.clone().unwrap_or_default().iter().map(|addr| addr.multi_address.clone()).collect())
// .allowlist(allowlist)?
.bootstraps(
config
.network
.bootstraps
.clone()
.unwrap_or_default()
.iter()
.map(|addr| addr.multi_address.clone())
.collect(),
)
.listen_addr(config.network.listening_address.clone())
.secio_keypair(&config.privkey.as_string_trim0x());

network_config = network_config.max_connections(config.network.max_connected_peers)?;

Ok(network_config)
.secio_keypair(&config.privkey.as_string_trim0x())
.chain_id(chain_id)
.max_connections(config.network.max_connected_peers)
}

pub fn max_connections(mut self, max: Option<usize>) -> ProtocolResult<Self> {
Expand Down Expand Up @@ -180,6 +191,11 @@ impl NetworkConfig {
self.peer_store_path = path;
self
}

pub fn chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = chain_id;
self
}
}

impl Default for NetworkConfig {
Expand Down
12 changes: 4 additions & 8 deletions core/network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use parking_lot::{Mutex, RwLock};
use parking_lot::RwLock;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
Expand All @@ -21,7 +21,7 @@ pub struct PeerManager {
online: RwLock<Online>,
peer_store: RwLock<PeerStore>,
bootstraps: HashMap<PeerId, Multiaddr>,
chain_id: Mutex<String>,
chain_id: String,
pub public_addrs: RwLock<HashSet<Multiaddr>>,
config: Arc<NetworkConfig>,

Expand All @@ -46,7 +46,7 @@ impl PeerManager {
peer_store: RwLock::new(PeerStore::load_from_dir_or_default(
config.peer_store_path.clone(),
)),
chain_id: Mutex::new(String::new()),
chain_id: config.chain_id.to_string(),
bootstraps,
public_addrs: RwLock::new(HashSet::new()),
config,
Expand Down Expand Up @@ -180,12 +180,8 @@ impl PeerManager {
}
}

pub fn set_chain_id(&self, chain_id: String) {
*self.chain_id.lock() = chain_id;
}

pub fn chain_id(&self) -> String {
self.chain_id.lock().clone()
self.chain_id.clone()
}

pub fn local_peer_id(&self) -> PeerId {
Expand Down
4 changes: 0 additions & 4 deletions core/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ where
self.config.secio_keypair.peer_id()
}

pub fn set_chain_id(&self, chain_id: String) {
self.peer_mgr_handle.set_chain_id(chain_id);
}

/// Dial just feeler protocol
pub async fn dial_feeler(&mut self, addr: Multiaddr) {
let peer_id = extract_peer_id(&addr).unwrap();
Expand Down
7 changes: 3 additions & 4 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,14 @@ impl Axon {
&self,
key_provider: Option<K>,
) -> NetworkService<KeyP<K>> {
let network_config = NetworkConfig::from_config(&self.config).unwrap();
let network_config =
NetworkConfig::from_config(&self.config, self.genesis.block.header.chain_id).unwrap();

let key = key_provider
.map(KeyP::Custom)
.unwrap_or(KeyP::Default(network_config.secio_keypair.clone()));

let network_service = NetworkService::new(network_config, key);
network_service.set_chain_id(self.genesis.block.header.chain_id.to_string());
network_service
NetworkService::new(network_config, key)
}

fn init_evm_trie_db(&self, inner_db: Arc<RocksDB>) -> Arc<RocksTrieDB> {
Expand Down

0 comments on commit 1f0ddfa

Please sign in to comment.