Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make user node declaration in config optional #6

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
220 changes: 180 additions & 40 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ tracing = "0.1.13"
tracing-subscriber = "0.2.3"
tracing-futures = "0.2.3"
warp = { version = "0.3.1", features = ["tls"] }
url = "2.4.1"
trust-dns-resolver = "0.23.2"

[features]
mock = []
Expand Down
5 changes: 2 additions & 3 deletions src/active_raft.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::configurations::NodeSpec;
use crate::db_utils::{SimpleDb, SimpleDbError};
use crate::raft::{
CommitReceiver, RaftCmd, RaftCmdSender, RaftCommit, RaftCommitData, RaftData,
Expand Down Expand Up @@ -39,7 +38,7 @@ impl ActiveRaft {
/// Create ActiveRaft, need to spawn the raft loop to use raft.
pub fn new(
node_idx: usize,
node_specs: &[NodeSpec],
node_specs: &[SocketAddr],
use_raft: bool,
tick_timeout_duration: Duration,
raft_db: SimpleDb,
Expand All @@ -50,7 +49,7 @@ impl ActiveRaft {
let peer_addr_vec: Vec<(u64, SocketAddr)> = peers
.iter()
.zip(node_specs.iter())
.map(|(idx, spec)| (*idx, spec.address))
.map(|(idx, spec)| (*idx, spec.clone()))
.filter(|(idx, _)| use_raft || *idx == peer_id)
.collect();

Expand Down
2 changes: 2 additions & 0 deletions src/bin/node/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ fn load_settings(matches: &clap::ArgMatches) -> config::Config {
.unwrap();
settings.set_default("compute_api_port", 3002).unwrap();
settings.set_default("compute_api_use_tls", true).unwrap();

settings.set_default("jurisdiction", "US").unwrap();
settings.set_default("compute_node_idx", 0).unwrap();
settings.set_default("compute_raft", 0).unwrap();
Expand All @@ -229,6 +230,7 @@ fn load_settings(matches: &clap::ArgMatches) -> config::Config {
settings
.merge(config::File::with_name(setting_file))
.unwrap();

settings
.merge(config::File::with_name(intial_block_setting_file))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/bin/node_settings_local_raft_1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ address = "127.0.0.1:12340"
[[user_nodes]]
address = "127.0.0.1:12360"

[[user_nodes]]
address = "127.0.0.1:12361"
#[[user_nodes]]
#"address"="127.0.0.1:12361"
24 changes: 17 additions & 7 deletions src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::raft::RaftCommit;
use crate::threaded_call::{ThreadedCallChannel, ThreadedCallSender};
use crate::tracked_utxo::TrackedUtxoSet;
use crate::utils::{
apply_mining_tx, check_druid_participants, create_item_asset_tx_from_sig,
apply_mining_tx, check_druid_participants, create_item_asset_tx_from_sig, create_socket_addr,
format_parition_pow_address, generate_pow_random_num, to_api_keys, to_route_pow_infos,
validate_pow_block, validate_pow_for_address, ApiKeys, LocalEvent, LocalEventChannel,
LocalEventSender, ResponseResult, RoutesPoWInfo, StringError,
Expand Down Expand Up @@ -175,16 +175,26 @@ impl ComputeNode {
/// * `config` - ComputeNodeConfig for the current compute node containing compute nodes and storage nodes
/// * `extra` - additional parameter for construction
pub async fn new(config: ComputeNodeConfig, mut extra: ExtraNodeParams) -> Result<Self> {
let addr = config
let raw_addr = config
.compute_nodes
.get(config.compute_node_idx)
.ok_or(ComputeError::ConfigError("Invalid compute index"))?
.address;
let storage_addr = config
.ok_or(ComputeError::ConfigError("Invalid compute index"))?;
let addr = create_socket_addr(&raw_addr.address).await.or_else(|_| {
Err(ComputeError::ConfigError(
"Invalid compute node address in config file",
))
})?;

let raw_storage_addr = config
.storage_nodes
.get(config.compute_node_idx)
.ok_or(ComputeError::ConfigError("Invalid storage index"))?
.address;
.ok_or(ComputeError::ConfigError("Invalid storage index"))?;
let storage_addr = create_socket_addr(&raw_storage_addr.address).await.or_else(|_| {
Err(ComputeError::ConfigError(
"Invalid storage node address in config file",
))
})?;

let tcp_tls_config = TcpTlsConfig::from_tls_spec(addr, &config.tls_config)?;
let api_addr = SocketAddr::new(addr.ip(), config.compute_api_port);
let api_tls_info = config
Expand Down
24 changes: 15 additions & 9 deletions src/compute_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::raft_util::{RaftContextKey, RaftInFlightProposals};
use crate::tracked_utxo::TrackedUtxoSet;
use crate::unicorn::{UnicornFixedParam, UnicornInfo};
use crate::utils::{
calculate_reward, get_total_coinbase_tokens, make_utxo_set_from_seed, BackupCheck,
UtxoReAlignCheck,
calculate_reward, create_socket_addr_for_list, get_total_coinbase_tokens,
make_utxo_set_from_seed, BackupCheck, UtxoReAlignCheck,
};
use a_block_chain::crypto::sha3_256;
use a_block_chain::primitives::asset::TokenAmount;
Expand Down Expand Up @@ -239,9 +239,15 @@ impl ComputeRaft {
if config.backup_restore.unwrap_or(false) {
db_utils::restore_file_backup(config.compute_db_mode, &DB_SPEC, None).unwrap();
}
let raw_node_ips = config
.compute_nodes
.clone()
.into_iter()
.map(|v| v.address.clone())
.collect::<Vec<String>>();
let raft_active = ActiveRaft::new(
config.compute_node_idx,
&config.compute_nodes,
&create_socket_addr_for_list(&raw_node_ips).await.unwrap_or_default(),
use_raft,
Duration::from_millis(config.compute_raft_tick_timeout as u64),
db_utils::new_db(config.compute_db_mode, &DB_SPEC, raft_db, None),
Expand Down Expand Up @@ -1593,7 +1599,7 @@ fn take_first_n<K: Clone + Ord, V>(n: usize, from: &mut BTreeMap<K, V>) -> BTree
mod test {
use super::*;
use crate::configurations::{DbMode, NodeSpec, TxOutSpec};
use crate::utils::{create_valid_transaction, get_test_common_unicorn};
use crate::utils::{create_socket_addr, create_valid_transaction, get_test_common_unicorn};
use a_block_chain::crypto::sign_ed25519 as sign;
use a_block_chain::primitives::asset::TokenAmount;
use rug::Integer;
Expand Down Expand Up @@ -1881,9 +1887,7 @@ mod test {
}

async fn new_test_node(seed_utxo: &[&str]) -> ComputeRaft {
let compute_node = NodeSpec {
address: "0.0.0.0:0".parse().unwrap(),
};
let compute_node = create_socket_addr("0.0.0.0").await.unwrap();
let tx_out = TxOutSpec {
public_key: "5371832122a8e804fa3520ec6861c3fa554a7f6fb617e6f0768452090207e07c"
.to_owned(),
Expand All @@ -1895,9 +1899,11 @@ mod test {
tls_config: Default::default(),
api_keys: Default::default(),
compute_unicorn_fixed_param: get_test_common_unicorn(),
compute_nodes: vec![compute_node],
compute_nodes: vec![NodeSpec {
address: compute_node.to_string(),
}],
storage_nodes: vec![],
user_nodes: vec![],
user_nodes: Some(vec![]),
compute_raft: 0,
compute_raft_tick_timeout: 10,
compute_mining_event_timeout: 500,
Expand Down
23 changes: 12 additions & 11 deletions src/configurations.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// use crate::comms_handler::Node;
use crate::compute_raft::MinerWhitelist;
use crate::db_utils::{CustomDbSpec, SimpleDb};
use crate::wallet::WalletDb;
Expand All @@ -9,12 +10,6 @@ use std::net::SocketAddr;

pub type UtxoSetSpec = BTreeMap<String, Vec<TxOutSpec>>;

/// Configuration info for a node
#[derive(Debug, Clone, Copy, Deserialize)]
pub struct NodeSpec {
pub address: SocketAddr,
}

/// Configuration info for TLS
#[derive(Default, Clone, Deserialize)]
pub struct TlsSpec {
Expand Down Expand Up @@ -95,6 +90,12 @@ pub enum DbMode {
InMemory,
}

/// Configuration info for a node
#[derive(Debug, Clone, Deserialize)]
pub struct NodeSpec {
pub address: String,
}

/// Configuration option for a compute node
#[derive(Debug, Clone, Deserialize)]
pub struct ComputeNodeConfig {
Expand All @@ -113,7 +114,7 @@ pub struct ComputeNodeConfig {
/// All storage nodes addresses: only use first
pub storage_nodes: Vec<NodeSpec>,
/// All user nodes addresses
pub user_nodes: Vec<NodeSpec>,
pub user_nodes: Option<Vec<NodeSpec>>,
/// Whether compute node will use raft or act independently (0)
pub compute_raft: usize,
/// API port
Expand Down Expand Up @@ -204,7 +205,7 @@ pub struct StorageNodeConfig {
#[derive(Debug, Clone, Deserialize)]
pub struct MinerNodeConfig {
/// Socket Address of this miner node
pub miner_address: SocketAddr,
pub miner_address: String,
/// Use specific database
pub miner_db_mode: DbMode,
/// Configuration for handling TLS
Expand Down Expand Up @@ -239,7 +240,7 @@ pub struct MinerNodeConfig {
#[derive(Debug, Clone, Deserialize)]
pub struct UserNodeConfig {
/// Socket Address of this User node
pub user_address: SocketAddr,
pub user_address: String,
/// Use specific database
pub user_db_mode: DbMode,
/// Configuration for handling TLS
Expand Down Expand Up @@ -287,9 +288,9 @@ pub struct PreLaunchNodeConfig {
/// Use specific database
pub storage_db_mode: DbMode,
/// All compute nodes addresses
pub compute_nodes: Vec<NodeSpec>,
pub compute_nodes: Vec<String>,
/// All storage nodes addresses: only use first
pub storage_nodes: Vec<NodeSpec>,
pub storage_nodes: Vec<String>,
/// Limit for the number of peers this node can have
pub peer_limit: usize,
}
Expand Down
17 changes: 11 additions & 6 deletions src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::interfaces::{
use crate::threaded_call::{ThreadedCallChannel, ThreadedCallSender};
use crate::transactor::Transactor;
use crate::utils::{
self, apply_mining_tx, construct_coinbase_tx, format_parition_pow_address,
self, apply_mining_tx, construct_coinbase_tx, create_socket_addr, format_parition_pow_address,
generate_pow_for_block, get_paiments_for_wallet, get_paiments_for_wallet_from_utxo,
to_api_keys, to_route_pow_infos, try_send_to_ui, ApiKeys, DeserializedBlockchainItem,
LocalEvent, LocalEventChannel, LocalEventSender, ResponseResult, RoutesPoWInfo,
Expand Down Expand Up @@ -176,11 +176,15 @@ impl MinerNode {
/// * `extra` - additional parameter for construction
pub async fn new(config: MinerNodeConfig, mut extra: ExtraNodeParams) -> Result<MinerNode> {
let addr = config.miner_address;
let compute_addr = config
let raw_compute_addr = config
.compute_nodes
.get(config.miner_compute_node_idx)
.ok_or(MinerError::ConfigError("Invalid compute index"))?
.address;
.ok_or(MinerError::ConfigError("Invalid compute index"))?;
let compute_addr = create_socket_addr(&raw_compute_addr.address).await.or_else(|_| {
Err(MinerError::ConfigError(
"Invalid compute node address in config file",
))
})?;

// Restore old keys if backup is present
if config.backup_restore.unwrap_or(false) {
Expand All @@ -194,8 +198,9 @@ impl MinerNode {
extra.custom_wallet_spec,
)?;
let disable_tcp_listener = extra.disable_tcp_listener;
let tcp_tls_config = TcpTlsConfig::from_tls_spec(addr, &config.tls_config)?;
let api_addr = SocketAddr::new(addr.ip(), config.miner_api_port);
let tls_addr = create_socket_addr(&addr).await.unwrap();
let tcp_tls_config = TcpTlsConfig::from_tls_spec(tls_addr, &config.tls_config)?;
let api_addr = SocketAddr::new(tls_addr.ip(), config.miner_api_port);
let api_tls_info = config
.miner_api_use_tls
.then(|| tcp_tls_config.clone_private_info());
Expand Down
37 changes: 24 additions & 13 deletions src/pre_launch.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::comms_handler::{CommsError, Event, Node, TcpTlsConfig};
use crate::configurations::{
DbMode, ExtraNodeParams, NodeSpec, PreLaunchNodeConfig, PreLaunchNodeType, TlsSpec,
DbMode, ExtraNodeParams, PreLaunchNodeConfig, PreLaunchNodeType, TlsSpec,
};
use crate::db_utils::{self, SimpleDb, SimpleDbSpec};
use crate::interfaces::{DbItem, NodeType, PreLaunchRequest, Response};
use crate::raft_store::{get_presistent_committed, CommittedIndex};
use crate::utils::{LocalEvent, LocalEventChannel, LocalEventSender, ResponseResult};
use crate::utils::{
create_socket_addr_for_list, LocalEvent, LocalEventChannel, LocalEventSender, ResponseResult,
};
use bincode::deserialize;
use bytes::Bytes;
use std::{collections::BTreeSet, error::Error, fmt, future::Future, net::SocketAddr};
Expand Down Expand Up @@ -74,7 +76,7 @@ struct PreLaunchNodeConfigSelected {
/// Configuration for handling TLS
pub tls_config: TlsSpec,
/// All nodes addresses
pub pre_launch_nodes: Vec<NodeSpec>,
pub pre_launch_nodes: Vec<SocketAddr>,
/// Db spec
pub db_spec: SimpleDbSpec,
/// Raft db spec
Expand All @@ -84,13 +86,14 @@ struct PreLaunchNodeConfigSelected {
}

impl PreLaunchNodeConfigSelected {
fn new(config: PreLaunchNodeConfig) -> Self {
async fn new(config: PreLaunchNodeConfig) -> Self {
match config.node_type {
PreLaunchNodeType::Compute => Self {
pre_launch_node_idx: config.compute_node_idx,
pre_launch_db_mode: config.compute_db_mode,
tls_config: config.tls_config,
pre_launch_nodes: config.compute_nodes,
pre_launch_nodes: create_socket_addr_for_list(&config.compute_nodes)
.await.unwrap_or_default(),
db_spec: crate::compute::DB_SPEC,
raft_db_spec: crate::compute_raft::DB_SPEC,
peer_limit: config.peer_limit,
Expand All @@ -99,7 +102,8 @@ impl PreLaunchNodeConfigSelected {
pre_launch_node_idx: config.storage_node_idx,
pre_launch_db_mode: config.storage_db_mode,
tls_config: config.tls_config,
pre_launch_nodes: config.storage_nodes,
pre_launch_nodes: create_socket_addr_for_list(&config.storage_nodes)
.await.unwrap_or_default(),
db_spec: crate::storage::DB_SPEC,
raft_db_spec: crate::storage_raft::DB_SPEC,
peer_limit: config.peer_limit,
Expand Down Expand Up @@ -131,13 +135,13 @@ impl PreLaunchNode {
config: PreLaunchNodeConfig,
mut extra: ExtraNodeParams,
) -> Result<PreLaunchNode> {
let config = PreLaunchNodeConfigSelected::new(config);
let config = PreLaunchNodeConfigSelected::new(config).await;
let addr = config
.pre_launch_nodes
.get(config.pre_launch_node_idx)
.ok_or(PreLaunchError::ConfigError("Invalid pre-launch index"))?
.address;
let tcp_tls_config = TcpTlsConfig::from_tls_spec(addr, &config.tls_config)?;
.ok_or(PreLaunchError::ConfigError("Invalid pre-launch index"))?;

let tcp_tls_config = TcpTlsConfig::from_tls_spec(addr.clone(), &config.tls_config)?;

let node = Node::new(
&tcp_tls_config,
Expand All @@ -155,9 +159,16 @@ impl PreLaunchNode {
db_utils::new_db(config.pre_launch_db_mode, spec, extra.raft_db.take(), None)
};

let pre_launch_nodes = config.pre_launch_nodes.iter().map(|s| s.address);
let shutdown_group: BTreeSet<SocketAddr> = pre_launch_nodes.clone().collect();
let pre_launch_nodes: Vec<_> = pre_launch_nodes.filter(|a| *a != addr).collect();
let pre_launch_nodes = config.pre_launch_nodes.iter().map(|s| s);
let shutdown_group: BTreeSet<SocketAddr> = pre_launch_nodes
.clone()
.into_iter()
.map(|v| v.clone())
.collect();
let pre_launch_nodes: Vec<_> = pre_launch_nodes
.filter(|a| *a != addr)
.map(|v| v.clone())
.collect();

let raft_db_send = if config.pre_launch_node_idx == 0 {
Some(PreLaunchRequest::SendDbItems {
Expand Down
Loading
Loading