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

[DO NOT MERGE] Command peer connections from runtime #6863

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c908c1c
new node permission pallet
kaichaosun Aug 1, 2020
f44a970
runtime api for node ides.
kaichaosun Aug 1, 2020
1c06b8e
refresh node allowlist when import block
kaichaosun Aug 4, 2020
686dd0b
fix more
kaichaosun Aug 4, 2020
e12784b
more
kaichaosun Aug 5, 2020
06207c5
use well known keys
kaichaosun Aug 5, 2020
18639a5
rm runtime api
kaichaosun Aug 5, 2020
24a61a2
refactor
kaichaosun Aug 5, 2020
4a87e28
improve node permission module
kaichaosun Aug 6, 2020
eb67330
more
kaichaosun Aug 6, 2020
c652e07
fix build
kaichaosun Aug 9, 2020
8047858
apply allowlist when init discovery
kaichaosun Aug 9, 2020
bc9ef19
fix init peerset
kaichaosun Aug 10, 2020
ad07ce6
remove node permission key id
kaichaosun Aug 10, 2020
6d69444
more dispatchable calls, clean logs
kaichaosun Aug 13, 2020
89e7d4c
init in client service
kaichaosun Aug 13, 2020
f06d2fe
rm allowlist in peerset
kaichaosun Aug 13, 2020
ec778a9
revert backend constraint for client network.
kaichaosun Aug 13, 2020
60a2477
revert more
kaichaosun Aug 13, 2020
b32041b
remove local peer ids from list
kaichaosun Aug 13, 2020
04609bc
refactor
kaichaosun Aug 13, 2020
dba3175
test node permission pallet
kaichaosun Aug 13, 2020
d48636e
Update client/service/src/lib.rs
kaichaosun Aug 13, 2020
6e71cd9
Update client/service/Cargo.toml
kaichaosun Aug 13, 2020
b4fb0f3
Update client/service/Cargo.toml
kaichaosun Aug 13, 2020
a1080e4
use decode_all
kaichaosun Aug 13, 2020
e7eb421
comments and weight
kaichaosun Aug 13, 2020
cf86c58
remove node permission from node runtime.
kaichaosun Aug 13, 2020
1f57ed2
WeightInfo
kaichaosun Aug 14, 2020
29bbbb4
fix build
kaichaosun Aug 14, 2020
385876b
Update client/service/src/lib.rs
kaichaosun Aug 14, 2020
00e2aff
format
kaichaosun Aug 14, 2020
6b94f45
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
0ea0d05
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
6c8210f
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
b2626c3
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
477ca98
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
5822801
Update frame/node-permission/src/lib.rs
kaichaosun Aug 15, 2020
d389d49
no mut in call param
kaichaosun Aug 15, 2020
6397176
Merge branch 'master' of https://github.com/paritytech/substrate into…
kaichaosun Aug 16, 2020
fda845c
rename to node-authorization
kaichaosun Aug 16, 2020
c0912c3
refactor more
kaichaosun Aug 16, 2020
46cd901
rename to allow list
kaichaosun Aug 18, 2020
1a81a51
Add node id in events.
kaichaosun Aug 18, 2020
386029e
rename to NodePublicKey
kaichaosun Aug 19, 2020
1f1cace
Merge branch 'master' of https://github.com/paritytech/substrate into…
kaichaosun Aug 20, 2020
fdd5afe
primitive type NodePublicKey.
kaichaosun Aug 21, 2020
4aa9ef6
fix tests.
kaichaosun Aug 21, 2020
537210a
Merge branch 'master' of https://github.com/paritytech/substrate into…
kaichaosun Aug 21, 2020
1072e42
update to rc6
kaichaosun Aug 21, 2020
847e4c7
re-export NodePublicKey
kaichaosun Aug 21, 2020
c830199
update codec
kaichaosun Aug 21, 2020
14c2389
increase spec version
kaichaosun Aug 21, 2020
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
15 changes: 15 additions & 0 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 @@ -88,6 +88,7 @@ members = [
"frame/metadata",
"frame/multisig",
"frame/nicks",
"frame/node-authorization",
"frame/offences",
"frame/proxy",
"frame/randomness-collective-flip",
Expand Down
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 257,
spec_version: 258,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
6 changes: 6 additions & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,12 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
Ok(())
}

/// Sets reserved peers to the new peers
pub fn set_reserved_peers(&self, peer_ids: HashSet<PeerId>, reserved_only: bool) {
self.peerset.set_reserved_peers(peer_ids);
self.peerset.set_reserved_only(reserved_only);
}

/// Configure an explicit fork sync request.
/// Note that this function should not be used for recent blocks.
/// Sync should be able to download all the recent forks normally.
Expand Down
12 changes: 12 additions & 0 deletions client/peerset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const FORGET_AFTER: Duration = Duration::from_secs(3600);
enum Action {
AddReservedPeer(PeerId),
RemoveReservedPeer(PeerId),
SetReservedPeers(HashSet<PeerId>),
SetReservedOnly(bool),
ReportPeer(PeerId, ReputationChange),
SetPriorityGroup(String, HashSet<PeerId>),
Expand Down Expand Up @@ -98,6 +99,11 @@ impl PeersetHandle {
let _ = self.tx.unbounded_send(Action::RemoveReservedPeer(peer_id));
}

/// Set reserved peers to the new peers.
pub fn set_reserved_peers(&self, peer_ids: HashSet<PeerId>) {
let _ = self.tx.unbounded_send(Action::SetReservedPeers(peer_ids));
}

/// Sets whether or not the peerset only has connections .
pub fn set_reserved_only(&self, reserved: bool) {
let _ = self.tx.unbounded_send(Action::SetReservedOnly(reserved));
Expand Down Expand Up @@ -247,6 +253,10 @@ impl Peerset {
self.on_remove_from_priority_group(RESERVED_NODES, peer_id);
}

fn on_set_reserved_peers(&mut self, peer_ids: HashSet<PeerId>) {
self.on_set_priority_group(RESERVED_NODES, peer_ids);
}

fn on_set_reserved_only(&mut self, reserved_only: bool) {
self.reserved_only = reserved_only;

Expand Down Expand Up @@ -655,6 +665,8 @@ impl Stream for Peerset {
self.on_add_reserved_peer(peer_id),
Action::RemoveReservedPeer(peer_id) =>
self.on_remove_reserved_peer(peer_id),
Action::SetReservedPeers(peer_ids) =>
self.on_set_reserved_peers(peer_ids),
Action::SetReservedOnly(reserved) =>
self.on_set_reserved_only(reserved),
Action::ReportPeer(peer_id, score_diff) =>
Expand Down
7 changes: 4 additions & 3 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use sp_core::traits::{CodeExecutor, SpawnNamed};
use sp_runtime::BuildStorage;
use sc_client_api::{
BlockBackend, BlockchainEvents,
backend::StorageProvider,
backend::{StorageProvider, Backend as BackendT},
proof_provider::ProofProvider,
execution_extensions::ExecutionExtensions
};
Expand Down Expand Up @@ -815,7 +815,7 @@ pub struct BuildNetworkParams<'a, TBl: BlockT, TExPool, TImpQu, TCl> {
}

/// Build the network service, the network status sinks and an RPC sender.
pub fn build_network<TBl, TExPool, TImpQu, TCl>(
pub fn build_network<TBl, TBE, TExPool, TImpQu, TCl>(
params: BuildNetworkParams<TBl, TExPool, TImpQu, TCl>
) -> Result<
(
Expand All @@ -828,9 +828,10 @@ pub fn build_network<TBl, TExPool, TImpQu, TCl>(
>
where
TBl: BlockT,
TBE: BackendT<TBl> + 'static,
TCl: ProvideRuntimeApi<TBl> + HeaderMetadata<TBl, Error=sp_blockchain::Error> + Chain<TBl> +
BlockBackend<TBl> + BlockIdTo<TBl, Error=sp_blockchain::Error> + ProofProvider<TBl> +
HeaderBackend<TBl> + BlockchainEvents<TBl> + 'static,
HeaderBackend<TBl> + BlockchainEvents<TBl> + StorageProvider<TBl, TBE> + 'static,
TExPool: MaintainedTransactionPool<Block=TBl, Hash = <TBl as BlockT>::Hash> + 'static,
TImpQu: ImportQueue<TBl> + 'static,
{
Expand Down
59 changes: 54 additions & 5 deletions client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,28 @@ mod task_manager;

use std::{io, pin::Pin};
use std::net::SocketAddr;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use std::task::Poll;
use sc_network::config::identity::{
ed25519::PublicKey as Ed25519PublicKey,
PublicKey
};
use parking_lot::Mutex;

use futures::{Future, FutureExt, Stream, StreamExt, stream, compat::*};
use sc_network::{NetworkStatus, network_state::NetworkState, PeerId};
use log::{warn, debug, error};
use codec::{Encode, Decode};
use codec::{Encode, Decode, DecodeAll};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use parity_util_mem::MallocSizeOf;
use sp_utils::{status_sinks, mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}};
use sp_core::storage::{StorageKey, well_known_keys};
use sp_core::NodePublicKey;
use sc_client_api::backend::{Backend as BackendT, StorageProvider};
use sc_client_api::blockchain::HeaderBackend;
use sc_client_api::BlockchainEvents;

pub use self::error::Error;
pub use self::builder::{
Expand Down Expand Up @@ -80,7 +89,6 @@ pub use sc_tracing::TracingReceiver;
pub use task_manager::SpawnTaskHandle;
pub use task_manager::TaskManager;
pub use sp_consensus::import_queue::ImportQueue;
use sc_client_api::BlockchainEvents;
pub use sc_keystore::KeyStorePtr as KeyStore;

const DEFAULT_PROTOCOL_ID: &str = "sup";
Expand Down Expand Up @@ -186,7 +194,8 @@ pub struct PartialComponents<Client, Backend, SelectChain, ImportQueue, Transact
/// The `status_sink` contain a list of senders to send a periodic network status to.
async fn build_network_future<
B: BlockT,
C: BlockchainEvents<B>,
BE: BackendT<B>,
C: BlockchainEvents<B> + StorageProvider<B, BE> + HeaderBackend<B>,
H: sc_network::ExHashT
> (
role: Role,
Expand All @@ -197,6 +206,8 @@ async fn build_network_future<
should_have_peers: bool,
announce_imported_blocks: bool,
) {
check_node_allow_list(&network, &client);

let mut imported_blocks_stream = client.import_notification_stream().fuse();

// Stream of finalized blocks reported by the client.
Expand Down Expand Up @@ -239,6 +250,8 @@ async fn build_network_future<
notification.header.number().clone(),
);
}

check_node_allow_list(&network, &client);
}

// List of blocks that the client has finalized.
Expand Down Expand Up @@ -338,6 +351,42 @@ async fn build_network_future<
}
}

/// Set storage `NODE_ALLOW_LIST` means it's a permissioned network,
/// then only connect to these well known peers.
fn check_node_allow_list<
B: BlockT,
BE: BackendT<B>,
C: BlockchainEvents<B> + StorageProvider<B, BE> + HeaderBackend<B>,
H: sc_network::ExHashT
> (
network: &sc_network::NetworkWorker<B, H>,
client: &Arc<C>,
) {
let id = BlockId::hash(client.info().best_hash);
let allow_list_storage = client.storage(&id, &StorageKey(well_known_keys::NODE_ALLOW_LIST.to_vec()));
if let Ok(Some(raw_allow_list)) = allow_list_storage {
let node_allow_list = Vec::<NodePublicKey>::decode_all(&mut &raw_allow_list.0[..]);

if let Ok(node_allow_list) = node_allow_list {
let mut peer_ids: HashSet<PeerId> = node_allow_list.iter()
.filter_map(|node_public_key| {
match node_public_key {
NodePublicKey::Ed25519(pubkey) => Ed25519PublicKey::decode(&pubkey.0).ok()
}
})
.map(|pubkey| PublicKey::Ed25519(pubkey).into_peer_id())
.collect();
peer_ids.remove(network.local_peer_id());

// Set only reserved peers are allowed to connect.
network.service().set_reserved_peers(peer_ids, true);
}
} else {
// Note that the situation where the storage entry previously existed but no longer
// does isn't handled at the moment.
}
kaichaosun marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(not(target_os = "unknown"))]
// Wrapper for HTTP and WS servers that makes sure they are properly shut down.
mod waiting {
Expand Down Expand Up @@ -488,9 +537,9 @@ where
impl<B, H, C, Pool, E> sc_network::config::TransactionPool<H, B> for
TransactionPoolAdapter<C, Pool>
where
B: BlockT,
C: sc_network::config::Client<B> + Send + Sync,
Pool: 'static + TransactionPool<Block=B, Hash=H, Error=E>,
B: BlockT,
H: std::hash::Hash + Eq + sp_runtime::traits::Member + sp_runtime::traits::MaybeSerialize,
E: 'static + IntoPoolError + From<sp_transaction_pool::error::Error>,
{
Expand Down
38 changes: 38 additions & 0 deletions frame/node-authorization/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "pallet-node-authorization"
version = "2.0.0-rc6"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet for node authorization"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }
frame-support = { version = "2.0.0-rc6", default-features = false, path = "../support" }
frame-system = { version = "2.0.0-rc6", default-features = false, path = "../system" }
sp-core = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/core" }
sp-io = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
hex-literal = "0.2.1"

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
Loading