Made it possible to run ALFIS without settings file. It will be created if not found.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
# Settings
|
||||
origin = "0000002CE5D1D28A93B89ED426A70F337D6FB3A222C64585FB1159A397E9722F"
|
||||
version = 1
|
||||
key_file = "default.key"
|
||||
listen = "[::]:4244"
|
||||
public = false
|
||||
|
||||
@@ -11,3 +11,5 @@ pub const LOCKER_BLOCK_TIME: i64 = 300;
|
||||
pub const LOCKER_BLOCK_INTERVAL: u64 = 50;
|
||||
|
||||
pub const FULL_BLOCKS_INTERVAL: i64 = 86400; // One day in seconds
|
||||
|
||||
pub const ZONE_MAX_LENGTH: usize = 10;
|
||||
+2
-1
@@ -68,7 +68,8 @@ fn main() {
|
||||
SimpleLogger::new().with_level(level).init().unwrap();
|
||||
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let settings = Settings::load(&config_name).expect("Error loading settings");
|
||||
let settings = Settings::load(&config_name);
|
||||
info!("Loaded settings: {:?}", &settings);
|
||||
let keystore: Keystore = match Keystore::from_file(&settings.key_file, "") {
|
||||
None => {
|
||||
warn!(target: LOG_TARGET_MAIN, "Generated temporary keystore. Please, generate full-privileged keys.");
|
||||
|
||||
+1
-1
@@ -407,7 +407,7 @@ fn mine_locker_block(context: Arc<Mutex<Context>>) {
|
||||
if lockers.contains(&context.keystore.get_public()) {
|
||||
info!("We have an honor to mine locker block!");
|
||||
context.bus.post(crate::event::Event::ActionMineLocker { index: block.index + 1, hash: block.hash });
|
||||
} else {
|
||||
} else if !lockers.is_empty() {
|
||||
info!("Locker block must be mined by other nodes");
|
||||
}
|
||||
}
|
||||
|
||||
+45
-11
@@ -1,19 +1,26 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[allow(unused_imports)]
|
||||
use log::{debug, error, info, LevelFilter, trace, warn};
|
||||
|
||||
use crate::Bytes;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
#[serde(default)]
|
||||
pub origin: String,
|
||||
#[serde(default)]
|
||||
pub key_file: String,
|
||||
#[serde(default = "default_listen")]
|
||||
pub listen: String,
|
||||
#[serde(default)]
|
||||
pub public: bool,
|
||||
#[serde(default)]
|
||||
pub peers: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub dns: Dns
|
||||
pub dns: Dns,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
@@ -21,17 +28,27 @@ impl Settings {
|
||||
serde_json::from_str(&settings.into())
|
||||
}
|
||||
|
||||
pub fn load(file_name: &str) -> Option<Settings> {
|
||||
match File::open(file_name) {
|
||||
pub fn load(filename: &str) -> Settings {
|
||||
match File::open(filename) {
|
||||
Ok(mut file) => {
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text).unwrap();
|
||||
if let Ok(settings) = toml::from_str(&text) {
|
||||
return Some(settings);
|
||||
return settings;
|
||||
}
|
||||
None
|
||||
},
|
||||
Err(..) => None
|
||||
Settings::default()
|
||||
}
|
||||
Err(..) => {
|
||||
let settings = Settings::default();
|
||||
let string = toml::to_string(&settings).unwrap();
|
||||
match File::create(filename) {
|
||||
Ok(mut f) => {
|
||||
f.write_all(string.as_bytes()).expect("Error saving settings!");
|
||||
}
|
||||
Err(_) => { error!("Error saving settings file!"); }
|
||||
}
|
||||
settings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,22 +61,39 @@ impl Settings {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
origin: "".to_string(),
|
||||
key_file: "".to_string(),
|
||||
listen: String::from("[::]:4244"),
|
||||
public: false,
|
||||
peers: vec![],
|
||||
dns: Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Dns {
|
||||
#[serde(default = "default_listen")]
|
||||
#[serde(default = "default_listen_dns")]
|
||||
pub listen: String,
|
||||
#[serde(default = "default_threads")]
|
||||
pub threads: usize,
|
||||
pub forwarders: Vec<String>
|
||||
pub forwarders: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for Dns {
|
||||
fn default() -> Self {
|
||||
Dns { listen: String::from("0.0.0.0:53"), threads: 20, forwarders: Vec::new() }
|
||||
Dns { listen: String::from("0.0.0.0:53"), threads: 20, forwarders: vec!["94.140.14.14:53".to_owned(), "94.140.15.15:53".to_owned()] }
|
||||
}
|
||||
}
|
||||
|
||||
fn default_listen() -> String {
|
||||
String::from("[::]:4244")
|
||||
}
|
||||
|
||||
fn default_listen_dns() -> String {
|
||||
String::from("0.0.0.0:53")
|
||||
}
|
||||
|
||||
|
||||
+12
-11
@@ -17,6 +17,7 @@ use alfis::miner::Miner;
|
||||
use alfis::{keys, check_domain};
|
||||
use alfis::event::Event;
|
||||
use alfis::dns::protocol::DnsRecord;
|
||||
use alfis::blockchain::ZONE_MAX_LENGTH;
|
||||
use Cmd::*;
|
||||
|
||||
pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
@@ -164,11 +165,11 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
};
|
||||
match transaction {
|
||||
None => {
|
||||
create_domain(context.clone(), miner.clone(), name, records, &keystore);
|
||||
create_domain(context.clone(), miner.clone(), &name, &records, &keystore);
|
||||
}
|
||||
Some(transaction) => {
|
||||
if transaction.pub_key == keystore.get_public() {
|
||||
create_domain(context.clone(), miner.clone(), name, records, &keystore);
|
||||
create_domain(context.clone(), miner.clone(), &name, &records, &keystore);
|
||||
} else {
|
||||
warn!("Tried to mine not owned domain!");
|
||||
let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!"));
|
||||
@@ -185,7 +186,7 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
TransferDomain { .. } => {}
|
||||
CheckZone { name } => {
|
||||
let name = name.to_lowercase();
|
||||
if name.len() > 10 || !check_domain(&name, false) || context.lock().unwrap().x_zones.has_zone(&name) {
|
||||
if name.len() > ZONE_MAX_LENGTH || !check_domain(&name, false) || context.lock().unwrap().x_zones.has_zone(&name) {
|
||||
web_view.eval("zoneAvailable(false)").expect("Error evaluating!");
|
||||
} else {
|
||||
let c = context.lock().unwrap();
|
||||
@@ -195,7 +196,7 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
}
|
||||
CreateZone { name, data } => {
|
||||
let name = name.to_lowercase();
|
||||
if name.len() > 10 || !check_domain(&name, false) || context.lock().unwrap().x_zones.has_zone(&name) {
|
||||
if name.len() > ZONE_MAX_LENGTH || !check_domain(&name, false) || context.lock().unwrap().x_zones.has_zone(&name) {
|
||||
warn!("This zone is unavailable for mining!");
|
||||
let _ = web_view.eval(&format!("showWarning('{}');", "This zone is unavailable for mining!"));
|
||||
return Ok(());
|
||||
@@ -207,11 +208,11 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
};
|
||||
match transaction {
|
||||
None => {
|
||||
create_domain(context.clone(), miner.clone(), name, data, &keystore);
|
||||
create_domain(context.clone(), miner.clone(), &name, &data, &keystore);
|
||||
}
|
||||
Some(transaction) => {
|
||||
if transaction.pub_key == keystore.get_public() {
|
||||
create_domain(context.clone(), miner.clone(), name, data, &keystore);
|
||||
create_domain(context.clone(), miner.clone(), &name, &data, &keystore);
|
||||
} else {
|
||||
warn!("Tried to mine not owned domain!");
|
||||
let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!"));
|
||||
@@ -258,15 +259,15 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
|
||||
interface.exit();
|
||||
}
|
||||
|
||||
fn create_domain<S: Into<String>>(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>, name: S, data: S, keystore: &Keystore) {
|
||||
let name = name.into();
|
||||
info!("Generating domain or zone {}", name);
|
||||
fn create_domain(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>, name: &str, data: &str, keystore: &Keystore) {
|
||||
let name = name.to_owned();
|
||||
info!("Generating domain or zone {}", &name);
|
||||
if context.lock().unwrap().x_zones.has_zone(&name) {
|
||||
error!("Unable to mine IANA zone {}!", &name);
|
||||
error!("Unable to mine IANA/OpenNIC/etc zone {}!", &name);
|
||||
return;
|
||||
}
|
||||
//let tags_vector: Vec<String> = tags.into().trim().split(",").map(|s| s.trim()).map(String::from).collect();
|
||||
let transaction = Transaction::from_str(name.into(), "dns".into(), data.into(), keystore.get_public().clone());
|
||||
let transaction = Transaction::from_str(name, "dns".to_owned(), data.to_owned(), keystore.get_public().clone());
|
||||
let block = Block::new(Some(transaction), keystore.get_public(), Bytes::default());
|
||||
miner.lock().unwrap().add_block(block);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user