Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Introduce srml/im-online #3079

Merged
merged 45 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1e77e12
Fix grammar and typo
cmichi Jul 9, 2019
cfbec8c
Extend network service
cmichi Jul 9, 2019
5252851
Extend offchain API
cmichi Jul 9, 2019
1d5151b
Support creating unsigned UncheckedExtrinsic
cmichi Jul 9, 2019
2c89345
Introduce srml/im-online
cmichi Jul 9, 2019
e9a9028
Bump impl and spec version
cmichi Jul 10, 2019
dc531dc
Fix web-wasm test
cmichi Jul 10, 2019
da1679b
Merge branch 'master' into cmichi-srml-im-online
cmichi Jul 10, 2019
eeb4df5
Apply suggestions from code review
cmichi Jul 10, 2019
4895cad
Replace transmute with from_raw_parts
cmichi Jul 10, 2019
b8fe709
Replace PeerId.to_string() with .to_base58()
cmichi Jul 10, 2019
2256919
Merge branch 'cmichi-srml-im-online' of github.com:paritytech/substra…
cmichi Jul 10, 2019
0d1289b
Update Cargo.lock
cmichi Jul 10, 2019
44cd516
Bump impl and spec version (again)
cmichi Jul 10, 2019
73f2bb5
Merge branch 'master' into cmichi-srml-im-online
cmichi Jul 15, 2019
56d25bd
Apply suggestions from code review
cmichi Jul 15, 2019
5dc0298
Address comments
cmichi Jul 16, 2019
5f43208
Add public function is_online_in_current_session()
cmichi Jul 16, 2019
24adc6c
Bump spec_version
cmichi Jul 16, 2019
ec5f3e8
Merge branch 'master' into cmichi-srml-im-online
cmichi Jul 16, 2019
0d38749
Fix doc tests
cmichi Jul 16, 2019
fdbb2b4
Improve comments
cmichi Jul 16, 2019
39cd034
Remove superfluous line
cmichi Jul 16, 2019
d256a3c
Name parameters consistently
cmichi Jul 16, 2019
a7d9894
Implement comments
cmichi Jul 17, 2019
41091af
Switch From to TryFrom
cmichi Jul 17, 2019
9bee5bf
Use Vec instead of HashSet
cmichi Jul 17, 2019
f67d935
Fix tests
cmichi Jul 17, 2019
c7c0414
Merge branch 'master' into cmichi-srml-im-online
cmichi Jul 17, 2019
73fbc29
Revert me: local testing
cmichi Jul 9, 2019
2e80426
Merge branch 'cmichi-srml-im-online' of github.com:paritytech/substra…
cmichi Jul 18, 2019
e650c98
Fix check if already sent during session
cmichi Jul 18, 2019
f88d9ee
Fix typos
cmichi Jul 18, 2019
0fb8a6b
Consistent terminology
cmichi Jul 18, 2019
607df96
Revert "Revert me: local testing"
cmichi Jul 18, 2019
0210000
Introduce IsMember trait
cmichi Jul 19, 2019
7554808
Implement misc comments
cmichi Jul 19, 2019
f20a19b
Remove unused function
cmichi Jul 19, 2019
e4ab8da
Fix test
cmichi Jul 19, 2019
e3cea15
Fix external_addresses being written
cmichi Jul 19, 2019
c5828e8
Merge branch 'master' into cmichi-srml-im-online
cmichi Jul 19, 2019
3f88973
Fix test
cmichi Jul 19, 2019
537842d
Add necessary trait bound
cmichi Jul 19, 2019
1e50257
Do not increment version
cmichi Jul 19, 2019
e73329f
Update lib.rs
gavofyork Jul 19, 2019
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
54 changes: 36 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ members = [
"srml/system",
"srml/timestamp",
"srml/treasury",
"srml/im-online",
"node/cli",
"node/executor",
"node/primitives",
Expand Down
37 changes: 37 additions & 0 deletions core/executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use wasmi::{
};
use state_machine::{Externalities, ChildStorageKey};
use crate::error::{Error, Result};
use parity_codec::Encode;
use primitives::{blake2_128, blake2_256, twox_64, twox_128, twox_256, ed25519, sr25519, Pair};
use primitives::offchain;
use primitives::hexdisplay::HexDisplay;
Expand Down Expand Up @@ -767,6 +768,42 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,

Ok(offset)
},
ext_network_state(written_out: *mut u32) -> *mut u8 => {
let res = this.ext.offchain()
.map(|api| api.network_state())
.ok_or_else(|| "Calling unavailable API ext_network_state: wasm")?;

let encoded = res.encode();
let len = encoded.len() as u32;
let offset = this.heap.allocate(len)? as u32;
this.memory.set(offset, &encoded)
.map_err(|_| "Invalid attempt to set memory in ext_network_state")?;

this.memory.write_primitive(written_out, len)
.map_err(|_| "Invalid attempt to write written_out in ext_network_state")?;

Ok(offset)
},
ext_authority_pubkey(
kind: u32,
written_out: *mut u32
) -> *mut u8 => {
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_authority_pubkey: wasm")?;

let res = this.ext.offchain()
.map(|api| api.authority_pubkey(kind))
.ok_or_else(|| "Calling unavailable API ext_authority_pubkey: wasm")?;

let encoded = res.encode();
let len = encoded.len() as u32;
let offset = this.heap.allocate(len)? as u32;
this.memory.set(offset, &encoded)
.map_err(|_| "Invalid attempt to set memory in ext_authority_pubkey")?;
this.memory.write_primitive(written_out, len)
.map_err(|_| "Invalid attempt to write written_out in ext_authority_pubkey")?;
Ok(offset)
},
ext_decrypt(
key: u32,
kind: u32,
Expand Down
1 change: 1 addition & 0 deletions core/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub mod test;
pub use chain::{Client as ClientHandle, FinalityProofProvider};
pub use service::{
NetworkService, NetworkWorker, TransactionPool, ExHashT, ReportHandle,
NetworkStateInfo,
};
pub use protocol::{PeerInfo, Context, consensus_gossip, message, specialization};
pub use protocol::sync::SyncState;
Expand Down
50 changes: 49 additions & 1 deletion core/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use futures::{prelude::*, sync::mpsc};
use log::{warn, error, info};
use libp2p::core::{swarm::NetworkBehaviour, transport::boxed::Boxed, muxing::StreamMuxerBox};
use libp2p::{PeerId, Multiaddr, multihash::Multihash};
use parking_lot::Mutex;
use peerset::PeersetHandle;
use runtime_primitives::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId};

Expand Down Expand Up @@ -86,6 +87,8 @@ impl ReportHandle {
pub struct NetworkService<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> {
/// Number of peers we're connected to.
num_connected: Arc<AtomicUsize>,
/// The local external addresses.
external_addresses: Arc<Mutex<Vec<Multiaddr>>>,
cmichi marked this conversation as resolved.
Show resolved Hide resolved
/// Are we actively catching up with the chain?
is_major_syncing: Arc<AtomicBool>,
/// Local copy of the `PeerId` of the local node.
Expand Down Expand Up @@ -217,6 +220,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker

let service = Arc::new(NetworkService {
bandwidth,
external_addresses: Arc::new(Mutex::new(Vec::new())),
num_connected: num_connected.clone(),
is_major_syncing: is_major_syncing.clone(),
peerset: peerset_handle,
Expand All @@ -226,6 +230,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
});

Ok(NetworkWorker {
external_addresses: Arc::new(Mutex::new(Vec::new())),
num_connected,
is_major_syncing,
network_service: swarm,
Expand All @@ -251,6 +256,12 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
self.network_service.user_protocol().num_connected_peers()
}

/// Returns the local external addresses.
pub fn external_addresses(&self) -> Vec<Multiaddr> {
let swarm = &self.network_service;
cmichi marked this conversation as resolved.
Show resolved Hide resolved
Swarm::<B, S, H>::external_addresses(&swarm).cloned().collect()
}

/// Returns the number of peers we're connected to and that are being queried.
pub fn num_active_peers(&self) -> usize {
self.network_service.user_protocol().num_active_peers()
Expand Down Expand Up @@ -295,7 +306,7 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkWorker
/// Get network state.
///
/// **Note**: Use this only for debugging. This API is unstable. There are warnings literaly
/// everywhere about this. Please don't use this function to retreive actual information.
/// everywhere about this. Please don't use this function to retrieve actual information.
pub fn network_state(&mut self) -> NetworkState {
let swarm = &mut self.network_service;
let open = swarm.user_protocol().open_peers().cloned().collect::<Vec<_>>();
Expand Down Expand Up @@ -487,6 +498,11 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkServic
pub fn num_connected(&self) -> usize {
self.num_connected.load(Ordering::Relaxed)
}

/// Returns the local external addresses.
pub fn external_addresses(&self) -> Vec<Multiaddr> {
self.external_addresses.lock().clone()
}
}

impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
Expand All @@ -500,6 +516,32 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
}
}

/// Trait for providing information about the local network state
pub trait NetworkStateInfo {
/// Returns the local external addresses.
fn external_addresses(&self) -> Vec<Multiaddr>;

/// Returns the local Peer ID.
fn peer_id(&self) -> PeerId;
}

impl<B, S, H> NetworkStateInfo for NetworkService<B, S, H>
where
B: runtime_primitives::traits::Block,
S: NetworkSpecialization<B>,
H: ExHashT,
{
/// Returns the local external addresses.
fn external_addresses(&self) -> Vec<Multiaddr> {
self.external_addresses.lock().clone()
}

/// Returns the local Peer ID.
fn peer_id(&self) -> PeerId {
self.local_peer_id.clone()
}
}

/// Messages sent from the `NetworkService` to the `NetworkWorker`.
///
/// Each entry corresponds to a method of `NetworkService`.
Expand All @@ -520,6 +562,8 @@ enum ServerToWorkerMsg<B: BlockT, S: NetworkSpecialization<B>> {
/// You are encouraged to poll this in a separate background thread or task.
#[must_use = "The NetworkWorker must be polled in order for the network to work"]
pub struct NetworkWorker<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> {
/// Updated by the `NetworkWorker` and loaded by the `NetworkService`.
Copy link
Member

Choose a reason for hiding this comment

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

Where is this loaded by the networkservice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here: https://github.com/paritytech/substrate/pull/3079/files#diff-a045635f3eec17c06b3c7ac7944acceaR500, do you have concerns regarding the name "loading" being associated with the method load()?

external_addresses: Arc<Mutex<Vec<Multiaddr>>>,
/// Updated by the `NetworkWorker` and loaded by the `NetworkService`.
num_connected: Arc<AtomicUsize>,
/// Updated by the `NetworkWorker` and loaded by the `NetworkService`.
Expand Down Expand Up @@ -621,6 +665,10 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> Future for Ne

// Update the variables shared with the `NetworkService`.
self.num_connected.store(self.network_service.user_protocol_mut().num_connected_peers(), Ordering::Relaxed);
{
let external_addresses = Swarm::<B, S, H>::external_addresses(&self.network_service).cloned().collect();
*self.external_addresses.lock() = external_addresses;
}
self.is_major_syncing.store(match self.network_service.user_protocol_mut().sync_state() {
SyncState::Idle => false,
SyncState::Downloading => true,
Expand Down
Loading