Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a94e7e7
Switch from devp2p to libp2p
tomaka May 22, 2018
a514ed3
Move the keys in the network state
tomaka Jun 30, 2018
1cfc18e
Properly load, store or generate private key
tomaka Jun 30, 2018
0e30b5d
Some robustness
tomaka Jun 30, 2018
536edd6
Merge remote-tracking branch 'upstream/master' into tka-libp2p
tomaka Jul 2, 2018
21ab784
Update for latest libp2p
tomaka Jul 2, 2018
fb17f77
Allow secio
tomaka Jul 2, 2018
329b7dc
Don't open a new Kademlia connec all the time
tomaka Jul 2, 2018
abe756c
Handle Kademlia disconnection
tomaka Jul 2, 2018
1131498
Set correct permissions on key file
tomaka Jul 2, 2018
d1d38d7
Improvements to secret key storage
tomaka Jul 2, 2018
2277a5f
Flush the peer store at Kademlia requests
tomaka Jul 2, 2018
224f6b1
Use RAII guards for disconnection
tomaka Jul 2, 2018
77092a3
Some misc work
tomaka Jul 3, 2018
18ee06f
Set informations about peers
tomaka Jul 3, 2018
4ad5da7
Fix tests and external URL
tomaka Jul 3, 2018
fdc5fbf
Merge remote-tracking branch 'upstream/master' into tka-libp2p
tomaka Jul 3, 2018
9e06c31
Fix some style
tomaka Jul 3, 2018
5e74817
Split obtain_private_key into multiple function
tomaka Jul 4, 2018
293cbdf
Split start_kademlia_discovery in multiple functions
tomaka Jul 4, 2018
95c79fc
More style fixes
tomaka Jul 4, 2018
e8f743c
More style fixes
tomaka Jul 6, 2018
61e3e57
Merge remote-tracking branch 'upstream/master' into tka-libp2p
tomaka Jul 6, 2018
533f53d
Fix some concerns
tomaka Jul 6, 2018
fc5a342
Turn // into ///
tomaka Jul 7, 2018
e5390ab
More style fixes
tomaka Jul 8, 2018
2316ebb
Merge remote-tracking branch 'upstream/master' into tka-libp2p
tomaka Jul 10, 2018
ce3b819
More style fixes
tomaka Jul 10, 2018
b9b54b3
Merge remote-tracking branch 'upstream/master' into tka-libp2p
tomaka Jul 11, 2018
dfa92bc
Merge remote-tracking branch 'origin/master' into tka-libp2p
gavofyork Jul 12, 2018
e9757a4
Add annotations to unreachable!
tomaka Jul 13, 2018
6731f05
Fix style again
tomaka Jul 13, 2018
06b5498
Remove commented out code
tomaka Jul 13, 2018
ab89ef9
Fix test year
tomaka Jul 13, 2018
1a942a2
More concerns
tomaka Jul 13, 2018
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
Prev Previous commit
Next Next commit
Move the keys in the network state
  • Loading branch information
tomaka committed Jun 30, 2018
commit a514ed350c48acb9604f377679c6bbabcc0e15ec
26 changes: 25 additions & 1 deletion substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
use bytes::Bytes;
use fnv::{FnvHashMap, FnvHashSet};
use futures::sync::mpsc;
use libp2p::core::{Multiaddr, AddrComponent, Endpoint, PeerId as PeerstorePeerId};
use libp2p::core::{Multiaddr, AddrComponent, Endpoint, PeerId as PeerstorePeerId, PublicKey};
use libp2p::peerstore::{Peerstore, PeerAccess};
use libp2p::peerstore::json_peerstore::JsonPeerstore;
use libp2p::peerstore::memory_peerstore::MemoryPeerstore;
use libp2p::secio;
use network::{Error, ErrorKind, NetworkConfiguration, NonReservedPeerMode};
use network::{PeerId, ProtocolId, SessionInfo};
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -56,6 +57,11 @@ pub struct NetworkState {

// List of the IDs of the disabled peers. These peers will see their connections refused.
disabled_peers: RwLock<FnvHashSet<PeerstorePeerId>>,

// Local private key.
local_private_key: secio::SecioKeyPair,
// Local public key.
local_public_key: PublicKey,
}

enum PeersStorage {
Expand Down Expand Up @@ -105,6 +111,12 @@ struct PeerConnectionInfo {

impl NetworkState {
pub fn new(config: &NetworkConfiguration) -> Result<NetworkState, Error> {
// Private and public keys configuration.
// TODO: use key from the config ; however that requires supporting secp256k1 in libp2p
// see https://github.com/libp2p/rust-libp2p/issues/228
let local_private_key = secio::SecioKeyPair::ed25519_generated().unwrap();
let local_public_key = local_private_key.to_public_key();

// Build the storage for peers, including the bootstrap nodes.
let peerstore = if let Some(ref path) = config.net_config_path {
let path = Path::new(path).join(NODES_FILE);
Expand Down Expand Up @@ -148,9 +160,21 @@ impl NetworkState {
reserved_peers,
next_peer_id: atomic::AtomicUsize::new(0),
disabled_peers: RwLock::new(Default::default()),
local_private_key,
local_public_key,
})
}

/// Returns the private key of the local node.
pub fn local_private_key(&self) -> &secio::SecioKeyPair {
&self.local_private_key
}

/// Returns the public key of the local node.
pub fn local_public_key(&self) -> &PublicKey {
&self.local_public_key
}

/// Returns all the IDs of the peer we have knowledge of.
///
/// This includes peers we are not connected to.
Expand Down
11 changes: 3 additions & 8 deletions substrate/network-libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,7 @@ fn init_thread(core: Handle, shared: Arc<Shared>,
timeouts_register_rx: mpsc::UnboundedReceiver<(Instant, (Arc<NetworkProtocolHandler + Send + Sync + 'static>, ProtocolId, TimerToken))>,
close_rx: oneshot::Receiver<()>) -> Result<impl Future<Item = (), Error = IoError>, Error>
{
// TODO: use key from the config ; however that requires supporting secp256k1 in libp2p
// see https://github.com/libp2p/rust-libp2p/issues/228
let local_private_key = secio::SecioKeyPair::ed25519_generated().unwrap();
let local_public_key = local_private_key.to_public_key();
let local_peer_id = local_public_key.clone().into_peer_id();
let local_peer_id = shared.network_state.local_public_key().clone().into_peer_id();
info!(target: "sub-libp2p", "Local node id = {:?}", local_peer_id); // TODO: debug! instead?

// Configuration for Kademlia DHT.
Expand Down Expand Up @@ -429,7 +425,7 @@ fn init_thread(core: Handle, shared: Arc<Shared>,
libp2p::core::swarm(
upgraded_transport,
move |(upgrade, endpoint), client_addr| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneeded {

listener_handle(shared.clone(), upgrade, endpoint, local_public_key.clone(), client_addr)
listener_handle(shared.clone(), upgrade, endpoint, client_addr)
},
)
};
Expand Down Expand Up @@ -497,7 +493,6 @@ enum FinalUpgrade<C> {

// Called whenever we successfully open a multistream with a remote.
fn listener_handle<'a, C>(shared: Arc<Shared>, upgrade: FinalUpgrade<C>, endpoint: Endpoint,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function formatting

local_public_key: libp2p::core::PublicKey,
client_addr: impl Future<Item = Multiaddr, Error = IoError> + 'a,
/*listener_upgrade: impl ConnectionUpgrade<C, Box<Future<Item = Multiaddr, Error = IoError>>>*/)
-> Box<Future<Item = (), Error = IoError> + 'a>
Expand Down Expand Up @@ -540,7 +535,7 @@ where C: AsyncRead + AsyncWrite + 'a
.collect();*/
sender.send(
IdentifyInfo {
public_key: local_public_key.clone(),
public_key: shared.network_state.local_public_key().clone(),
protocol_version: concat!("substrate/", env!("CARGO_PKG_VERSION")).to_owned(),
agent_version: "rust-libp2p/1.0.0".to_owned(),
listen_addrs: shared.listened_addrs.read().clone(),
Expand Down