Set reserved nodes with offchain worker. (#6996)

* add offchain worker api to set reserved nodes.

* new offchain api to get node public key.

* node public key from converter

* refactor set reserved nodes ocw api.

* new ndoe authorization pallet

* remove unnecessary clone and more.

* more

* tests for node authorization pallet

* remove dependency

* fix build

* more tests.

* refactor

* Update primitives/core/src/offchain/testing.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update frame/node-authorization/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update frame/node-authorization/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update frame/node-authorization/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* format code

* expose NetworkService

* remove NetworkStateInfo in offchain

* replace NodePublicKey with PeerId.

* set max length of peer id.

* clear more

* use BTreeSet for set of peers.

* decode opaque peer id.

* extract NetworkProvider for client offchain.

* use OpaquePeerId in node authorization pallet.

* fix test

* better documentation

* fix test

* doc

* more fix

* Update primitives/core/src/offchain/mod.rs

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>

* Update client/offchain/src/api.rs

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>

* derive serialize and deserialize

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
kaichao
2020-09-10 23:26:09 +08:00
committed by GitHub
parent 56ce689be2
commit 91c0213413
15 changed files with 1080 additions and 44 deletions
+14 -1
View File
@@ -32,6 +32,7 @@ macro_rules! map {
);
}
use sp_runtime_interface::pass_by::{PassByEnum, PassByInner};
use sp_std::prelude::*;
use sp_std::ops::Deref;
#[cfg(feature = "std")]
@@ -176,6 +177,18 @@ impl sp_std::ops::Deref for OpaqueMetadata {
}
}
/// Simple blob to hold a `PeerId` without committing to its format.
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, PassByInner)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct OpaquePeerId(pub Vec<u8>);
impl OpaquePeerId {
/// Create new `OpaquePeerId`
pub fn new(vec: Vec<u8>) -> Self {
OpaquePeerId(vec)
}
}
/// Something that is either a native or an encoded value.
#[cfg(feature = "std")]
pub enum NativeOrEncoded<R> {
@@ -257,7 +270,7 @@ pub trait TypeId {
/// A log level matching the one from `log` crate.
///
/// Used internally by `sp_io::log` method.
#[derive(Encode, Decode, sp_runtime_interface::pass_by::PassByEnum, Copy, Clone)]
#[derive(Encode, Decode, PassByEnum, Copy, Clone)]
pub enum LogLevel {
/// `Error` log level.
Error = 1,
+25 -13
View File
@@ -19,7 +19,7 @@
use codec::{Encode, Decode};
use sp_std::{prelude::{Vec, Box}, convert::TryFrom};
use crate::RuntimeDebug;
use crate::{OpaquePeerId, RuntimeDebug};
use sp_runtime_interface::pass_by::{PassByCodec, PassByInner, PassByEnum};
pub use crate::crypto::KeyTypeId;
@@ -184,23 +184,12 @@ impl TryFrom<u32> for HttpRequestStatus {
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, PassByCodec)]
#[cfg_attr(feature = "std", derive(Default))]
pub struct OpaqueNetworkState {
/// PeerId of the local node.
/// PeerId of the local node in SCALE encoded.
pub peer_id: OpaquePeerId,
/// List of addresses the node knows it can be reached as.
pub external_addresses: Vec<OpaqueMultiaddr>,
}
/// Simple blob to hold a `PeerId` without committing to its format.
#[derive(Default, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, PassByInner)]
pub struct OpaquePeerId(pub Vec<u8>);
impl OpaquePeerId {
/// Create new `OpaquePeerId`
pub fn new(vec: Vec<u8>) -> Self {
OpaquePeerId(vec)
}
}
/// Simple blob to hold a `Multiaddr` without committing to its format.
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, PassByInner)]
pub struct OpaqueMultiaddr(pub Vec<u8>);
@@ -277,6 +266,8 @@ pub enum Capability {
OffchainWorkerDbRead = 32,
/// Access to offchain worker DB (writes).
OffchainWorkerDbWrite = 64,
/// Manage the authorized nodes
NodeAuthorization = 128,
}
/// A set of capabilities
@@ -495,6 +486,18 @@ pub trait Externalities: Send {
buffer: &mut [u8],
deadline: Option<Timestamp>
) -> Result<usize, HttpError>;
/// Set the authorized nodes from runtime.
///
/// In a permissioned network, the connections between nodes need to reach a
/// consensus between participants.
///
/// - `nodes`: a set of nodes which are allowed to connect for the local node.
/// each one is identified with an `OpaquePeerId`, here it just use plain bytes
/// without any encoding. Invalid `OpaquePeerId`s are silently ignored.
/// - `authorized_only`: if true, only the authorized nodes are allowed to connect,
/// otherwise unauthorized nodes can also be connected through other mechanism.
fn set_authorized_nodes(&mut self, nodes: Vec<OpaquePeerId>, authorized_only: bool);
}
impl<T: Externalities + ?Sized> Externalities for Box<T> {
@@ -573,6 +576,10 @@ impl<T: Externalities + ?Sized> Externalities for Box<T> {
) -> Result<usize, HttpError> {
(&mut **self).http_response_read_body(request_id, buffer, deadline)
}
fn set_authorized_nodes(&mut self, nodes: Vec<OpaquePeerId>, authorized_only: bool) {
(&mut **self).set_authorized_nodes(nodes, authorized_only)
}
}
/// An `OffchainExternalities` implementation with limited capabilities.
@@ -691,6 +698,11 @@ impl<T: Externalities> Externalities for LimitedExternalities<T> {
self.check(Capability::Http, "http_response_read_body");
self.externalities.http_response_read_body(request_id, buffer, deadline)
}
fn set_authorized_nodes(&mut self, nodes: Vec<OpaquePeerId>, authorized_only: bool) {
self.check(Capability::NodeAuthorization, "set_authorized_nodes");
self.externalities.set_authorized_nodes(nodes, authorized_only)
}
}
#[cfg(feature = "std")]
@@ -24,6 +24,7 @@ use std::{
collections::{BTreeMap, VecDeque},
sync::Arc,
};
use crate::OpaquePeerId;
use crate::offchain::{
self,
storage::{InMemOffchainStorage, OffchainOverlayedChange, OffchainOverlayedChanges},
@@ -375,6 +376,10 @@ impl offchain::Externalities for TestOffchainExt {
Err(HttpError::IoError)
}
}
fn set_authorized_nodes(&mut self, _nodes: Vec<OpaquePeerId>, _authorized_only: bool) {
unimplemented!()
}
}
/// The internal state of the fake transaction pool.