pub use crate::chain::{Client, FinalityProofProvider};
pub use crate::on_demand_layer::OnDemand;
pub use crate::service::{TransactionPool, EmptyTransactionPool};
pub use libp2p::{identity, core::PublicKey, wasm_ext::ExtTransport, build_multiaddr};
#[doc(hidden)]
pub use crate::protocol::ProtocolConfig;
use crate::service::ExHashT;
use bitflags::bitflags;
use sp_consensus::{block_validation::BlockAnnounceValidator, import_queue::ImportQueue};
use sp_runtime::traits::{Block as BlockT};
use libp2p::identity::{Keypair, ed25519};
use libp2p::wasm_ext;
use libp2p::{PeerId, Multiaddr, multiaddr};
use core::{fmt, iter};
use std::{future::Future, pin::Pin};
use std::{error::Error, fs, io::{self, Write}, net::Ipv4Addr, path::{Path, PathBuf}, sync::Arc};
use zeroize::Zeroize;
use prometheus_endpoint::Registry;
pub struct Params<B: BlockT, H: ExHashT> {
pub roles: Roles,
pub executor: Option<Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>>,
pub network_config: NetworkConfiguration,
pub chain: Arc<dyn Client<B>>,
pub finality_proof_provider: Option<Arc<dyn FinalityProofProvider<B>>>,
pub finality_proof_request_builder: Option<BoxFinalityProofRequestBuilder<B>>,
pub on_demand: Option<Arc<OnDemand<B>>>,
pub transaction_pool: Arc<dyn TransactionPool<H, B>>,
pub protocol_id: ProtocolId,
pub import_queue: Box<dyn ImportQueue<B>>,
pub block_announce_validator: Box<dyn BlockAnnounceValidator<B> + Send>,
pub metrics_registry: Option<Registry>,
}
bitflags! {
pub struct Roles: u8 {
const NONE = 0b00000000;
const FULL = 0b00000001;
const LIGHT = 0b00000010;
const AUTHORITY = 0b00000100;
}
}
impl Roles {
pub fn is_full(&self) -> bool {
self.intersects(Roles::FULL | Roles::AUTHORITY)
}
pub fn is_authority(&self) -> bool {
*self == Roles::AUTHORITY
}
pub fn is_light(&self) -> bool {
!self.is_full()
}
}
impl fmt::Display for Roles {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl codec::Encode for Roles {
fn encode_to<T: codec::Output>(&self, dest: &mut T) {
dest.push_byte(self.bits())
}
}
impl codec::EncodeLike for Roles {}
impl codec::Decode for Roles {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
Self::from_bits(input.read_byte()?).ok_or_else(|| codec::Error::from("Invalid bytes"))
}
}
pub trait FinalityProofRequestBuilder<B: BlockT>: Send {
fn build_request_data(&mut self, hash: &B::Hash) -> Vec<u8>;
}
#[derive(Debug, Default)]
pub struct DummyFinalityProofRequestBuilder;
impl<B: BlockT> FinalityProofRequestBuilder<B> for DummyFinalityProofRequestBuilder {
fn build_request_data(&mut self, _: &B::Hash) -> Vec<u8> {
Vec::new()
}
}
pub type BoxFinalityProofRequestBuilder<B> = Box<dyn FinalityProofRequestBuilder<B> + Send + Sync>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProtocolId(smallvec::SmallVec<[u8; 6]>);
impl<'a> From<&'a [u8]> for ProtocolId {
fn from(bytes: &'a [u8]) -> ProtocolId {
ProtocolId(bytes.into())
}
}
impl ProtocolId {
pub fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
}
}
pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {
let addr: Multiaddr = addr_str.parse()?;
parse_addr(addr)
}
pub fn parse_addr(mut addr: Multiaddr)-> Result<(PeerId, Multiaddr), ParseErr> {
let who = match addr.pop() {
Some(multiaddr::Protocol::P2p(key)) => PeerId::from_multihash(key)
.map_err(|_| ParseErr::InvalidPeerId)?,
_ => return Err(ParseErr::PeerIdMissing),
};
Ok((who, addr))
}
#[derive(Debug)]
pub enum ParseErr {
MultiaddrParse(multiaddr::Error),
InvalidPeerId,
PeerIdMissing,
}
impl fmt::Display for ParseErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseErr::MultiaddrParse(err) => write!(f, "{}", err),
ParseErr::InvalidPeerId => write!(f, "Peer id at the end of the address is invalid"),
ParseErr::PeerIdMissing => write!(f, "Peer id is missing from the address"),
}
}
}
impl std::error::Error for ParseErr {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ParseErr::MultiaddrParse(err) => Some(err),
ParseErr::InvalidPeerId => None,
ParseErr::PeerIdMissing => None,
}
}
}
impl From<multiaddr::Error> for ParseErr {
fn from(err: multiaddr::Error) -> ParseErr {
ParseErr::MultiaddrParse(err)
}
}
#[derive(Clone, Debug)]
pub struct NetworkConfiguration {
pub config_path: Option<PathBuf>,
pub net_config_path: Option<PathBuf>,
pub listen_addresses: Vec<Multiaddr>,
pub public_addresses: Vec<Multiaddr>,
pub boot_nodes: Vec<String>,
pub node_key: NodeKeyConfig,
pub in_peers: u32,
pub out_peers: u32,
pub reserved_nodes: Vec<String>,
pub non_reserved_mode: NonReservedPeerMode,
pub sentry_nodes: Vec<String>,
pub client_version: String,
pub node_name: String,
pub transport: TransportConfig,
pub max_parallel_downloads: u32,
}
impl Default for NetworkConfiguration {
fn default() -> Self {
NetworkConfiguration {
config_path: None,
net_config_path: None,
listen_addresses: Vec::new(),
public_addresses: Vec::new(),
boot_nodes: Vec::new(),
node_key: NodeKeyConfig::Ed25519(Secret::New),
in_peers: 25,
out_peers: 75,
reserved_nodes: Vec::new(),
non_reserved_mode: NonReservedPeerMode::Accept,
sentry_nodes: Vec::new(),
client_version: "unknown".into(),
node_name: "unknown".into(),
transport: TransportConfig::Normal {
enable_mdns: false,
allow_private_ipv4: true,
wasm_external_transport: None,
use_yamux_flow_control: false,
},
max_parallel_downloads: 5,
}
}
}
impl NetworkConfiguration {
pub fn new() -> Self {
Self::default()
}
pub fn new_local() -> NetworkConfiguration {
let mut config = NetworkConfiguration::new();
config.listen_addresses = vec![
iter::once(multiaddr::Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(multiaddr::Protocol::Tcp(0)))
.collect()
];
config
}
pub fn new_memory() -> NetworkConfiguration {
let mut config = NetworkConfiguration::new();
config.listen_addresses = vec![
iter::once(multiaddr::Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(multiaddr::Protocol::Tcp(0)))
.collect()
];
config
}
}
#[derive(Clone, Debug)]
pub enum TransportConfig {
Normal {
enable_mdns: bool,
allow_private_ipv4: bool,
wasm_external_transport: Option<wasm_ext::ExtTransport>,
use_yamux_flow_control: bool,
},
MemoryOnly,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NonReservedPeerMode {
Accept,
Deny,
}
impl NonReservedPeerMode {
pub fn parse(s: &str) -> Option<Self> {
match s {
"accept" => Some(NonReservedPeerMode::Accept),
"deny" => Some(NonReservedPeerMode::Deny),
_ => None,
}
}
}
#[derive(Clone, Debug)]
pub enum NodeKeyConfig {
Ed25519(Secret<ed25519::SecretKey>)
}
pub type Ed25519Secret = Secret<ed25519::SecretKey>;
#[derive(Clone)]
pub enum Secret<K> {
Input(K),
File(PathBuf),
New
}
impl<K> fmt::Debug for Secret<K> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Secret::Input(_) => f.debug_tuple("Secret::Input").finish(),
Secret::File(path) => f.debug_tuple("Secret::File").field(path).finish(),
Secret::New => f.debug_tuple("Secret::New").finish(),
}
}
}
impl NodeKeyConfig {
pub fn into_keypair(self) -> io::Result<Keypair> {
use NodeKeyConfig::*;
match self {
Ed25519(Secret::New) =>
Ok(Keypair::generate_ed25519()),
Ed25519(Secret::Input(k)) =>
Ok(Keypair::Ed25519(k.into())),
Ed25519(Secret::File(f)) =>
get_secret(f,
|mut b| ed25519::SecretKey::from_bytes(&mut b),
ed25519::SecretKey::generate,
|b| b.as_ref().to_vec())
.map(ed25519::Keypair::from)
.map(Keypair::Ed25519),
}
}
}
fn get_secret<P, F, G, E, W, K>(file: P, parse: F, generate: G, serialize: W) -> io::Result<K>
where
P: AsRef<Path>,
F: for<'r> FnOnce(&'r mut [u8]) -> Result<K, E>,
G: FnOnce() -> K,
E: Error + Send + Sync + 'static,
W: Fn(&K) -> Vec<u8>,
{
std::fs::read(&file)
.and_then(|mut sk_bytes|
parse(&mut sk_bytes)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)))
.or_else(|e| {
if e.kind() == io::ErrorKind::NotFound {
file.as_ref().parent().map_or(Ok(()), fs::create_dir_all)?;
let sk = generate();
let mut sk_vec = serialize(&sk);
write_secret_file(file, &sk_vec)?;
sk_vec.zeroize();
Ok(sk)
} else {
Err(e)
}
})
}
fn write_secret_file<P>(path: P, sk_bytes: &[u8]) -> io::Result<()>
where
P: AsRef<Path>
{
let mut file = open_secret_file(&path)?;
file.write_all(sk_bytes)
}
#[cfg(unix)]
fn open_secret_file<P>(path: P) -> io::Result<fs::File>
where
P: AsRef<Path>
{
use std::os::unix::fs::OpenOptionsExt;
fs::OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(path)
}
#[cfg(not(unix))]
fn open_secret_file<P>(path: P) -> Result<fs::File, io::Error>
where
P: AsRef<Path>
{
fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn tempdir_with_prefix(prefix: &str) -> TempDir {
tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
}
fn secret_bytes(kp: &Keypair) -> Vec<u8> {
match kp {
Keypair::Ed25519(p) => p.secret().as_ref().iter().cloned().collect(),
Keypair::Secp256k1(p) => p.secret().to_bytes().to_vec(),
_ => panic!("Unexpected keypair.")
}
}
#[test]
fn test_secret_file() {
let tmp = tempdir_with_prefix("x");
std::fs::remove_dir(tmp.path()).unwrap();
let file = tmp.path().join("x").to_path_buf();
let kp1 = NodeKeyConfig::Ed25519(Secret::File(file.clone())).into_keypair().unwrap();
let kp2 = NodeKeyConfig::Ed25519(Secret::File(file.clone())).into_keypair().unwrap();
assert!(file.is_file() && secret_bytes(&kp1) == secret_bytes(&kp2))
}
#[test]
fn test_secret_input() {
let sk = ed25519::SecretKey::generate();
let kp1 = NodeKeyConfig::Ed25519(Secret::Input(sk.clone())).into_keypair().unwrap();
let kp2 = NodeKeyConfig::Ed25519(Secret::Input(sk)).into_keypair().unwrap();
assert!(secret_bytes(&kp1) == secret_bytes(&kp2));
}
#[test]
fn test_secret_new() {
let kp1 = NodeKeyConfig::Ed25519(Secret::New).into_keypair().unwrap();
let kp2 = NodeKeyConfig::Ed25519(Secret::New).into_keypair().unwrap();
assert!(secret_bytes(&kp1) != secret_bytes(&kp2));
}
}