Added app_version to handshake.
This commit is contained in:
+3
-2
@@ -4,6 +4,7 @@ use crate::event::Event;
|
|||||||
use log::{trace, debug, info, warn, error};
|
use log::{trace, debug, info, warn, error};
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
|
pub app_version: String,
|
||||||
pub settings: Settings,
|
pub settings: Settings,
|
||||||
pub keystore: Keystore,
|
pub keystore: Keystore,
|
||||||
pub chain: Chain,
|
pub chain: Chain,
|
||||||
@@ -13,8 +14,8 @@ pub struct Context {
|
|||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
/// Creating an essential context to work with
|
/// Creating an essential context to work with
|
||||||
pub fn new(settings: Settings, keystore: Keystore, chain: Chain) -> Context {
|
pub fn new(app_version: String, settings: Settings, keystore: Keystore, chain: Chain) -> Context {
|
||||||
Context { settings, keystore, chain, x_zones: ExternalZones::new(), bus: Bus::new() }
|
Context { app_version, settings, keystore, chain, x_zones: ExternalZones::new(), bus: Bus::new() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load keystore and return Context
|
/// Load keystore and return Context
|
||||||
|
|||||||
+6
-2
@@ -71,7 +71,11 @@ fn main() {
|
|||||||
None => { SETTINGS_FILENAME.to_owned() }
|
None => { SETTINGS_FILENAME.to_owned() }
|
||||||
Some(path) => { path }
|
Some(path) => { path }
|
||||||
};
|
};
|
||||||
SimpleLogger::new().with_level(level).with_module_level("mio::poll", LevelFilter::Warn).init().unwrap();
|
SimpleLogger::new()
|
||||||
|
.with_level(level)
|
||||||
|
.with_module_level("mio::poll", LevelFilter::Warn)
|
||||||
|
.init()
|
||||||
|
.unwrap();
|
||||||
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
info!(target: LOG_TARGET_MAIN, "Starting ALFIS {}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
let settings = Settings::load(&config_name);
|
let settings = Settings::load(&config_name);
|
||||||
@@ -98,7 +102,7 @@ fn main() {
|
|||||||
Some(block) => { trace!(target: LOG_TARGET_MAIN, "Loaded DB with origin {:?}", &block.hash); }
|
Some(block) => { trace!(target: LOG_TARGET_MAIN, "Loaded DB with origin {:?}", &block.hash); }
|
||||||
}
|
}
|
||||||
let settings_copy = settings.clone();
|
let settings_copy = settings.clone();
|
||||||
let context: Arc<Mutex<Context>> = Arc::new(Mutex::new(Context::new(settings, keystore, chain)));
|
let context: Arc<Mutex<Context>> = Arc::new(Mutex::new(Context::new(env!("CARGO_PKG_VERSION").to_owned(), settings, keystore, chain)));
|
||||||
dns_utils::start_dns_server(&context, &settings_copy);
|
dns_utils::start_dns_server(&context, &settings_copy);
|
||||||
|
|
||||||
let mut miner_obj = Miner::new(context.clone());
|
let mut miner_obj = Miner::new(context.clone());
|
||||||
|
|||||||
+7
-3
@@ -7,7 +7,7 @@ use crate::Bytes;
|
|||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
Error,
|
Error,
|
||||||
Hand { origin: String, version: u32, public: bool, #[serde(default)] rand: String },
|
Hand { #[serde(default = "default_version")] app_version: String, origin: String, version: u32, public: bool, #[serde(default)] rand: String },
|
||||||
Shake { origin: String, version: u32, ok: bool, height: u64 },
|
Shake { origin: String, version: u32, ok: bool, height: u64 },
|
||||||
Ping { height: u64, hash: Bytes },
|
Ping { height: u64, hash: Bytes },
|
||||||
Pong { height: u64, hash: Bytes },
|
Pong { height: u64, hash: Bytes },
|
||||||
@@ -26,8 +26,8 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hand(origin: &str, version: u32, public: bool, rand: &str) -> Self {
|
pub fn hand(app_version: &str, origin: &str, version: u32, public: bool, rand: &str) -> Self {
|
||||||
Message::Hand { origin: origin.to_owned(), version, public, rand: rand.to_owned() }
|
Message::Hand { app_version: app_version.to_owned(), origin: origin.to_owned(), version, public, rand: rand.to_owned() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shake(origin: &str, version: u32, ok: bool, height: u64) -> Self {
|
pub fn shake(origin: &str, version: u32, ok: bool, height: u64) -> Self {
|
||||||
@@ -47,6 +47,10 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_version() -> String {
|
||||||
|
String::from("0.0.0")
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::p2p::Message;
|
use crate::p2p::Message;
|
||||||
|
|||||||
+3
-2
@@ -187,7 +187,7 @@ fn handle_connection_event(context: Arc<Mutex<Context>>, peers: &mut Peers, regi
|
|||||||
debug!("Sending hello to {}", &peer.get_addr());
|
debug!("Sending hello to {}", &peer.get_addr());
|
||||||
let data: String = {
|
let data: String = {
|
||||||
let c = context.lock().unwrap();
|
let c = context.lock().unwrap();
|
||||||
let message = Message::hand(&c.settings.origin, CHAIN_VERSION, c.settings.public, peer.get_rand());
|
let message = Message::hand(&c.app_version, &c.settings.origin, CHAIN_VERSION, c.settings.public, peer.get_rand());
|
||||||
serde_json::to_string(&message).unwrap()
|
serde_json::to_string(&message).unwrap()
|
||||||
};
|
};
|
||||||
send_message(peer.get_stream(), &data.into_bytes()).unwrap_or_else(|e| warn!("Error sending hello {}", e));
|
send_message(peer.get_stream(), &data.into_bytes()).unwrap_or_else(|e| warn!("Error sending hello {}", e));
|
||||||
@@ -279,7 +279,8 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
|||||||
(context.chain.height(), context.chain.last_hash(), &context.settings.origin.clone(), CHAIN_VERSION)
|
(context.chain.height(), context.chain.last_hash(), &context.settings.origin.clone(), CHAIN_VERSION)
|
||||||
};
|
};
|
||||||
match message {
|
match message {
|
||||||
Message::Hand { origin, version, public, rand} => {
|
Message::Hand { app_version, origin, version, public, rand} => {
|
||||||
|
debug!("Hello from v{}", &app_version);
|
||||||
if peers.is_our_own_connect(&rand) {
|
if peers.is_our_own_connect(&rand) {
|
||||||
warn!("Detected loop connect");
|
warn!("Detected loop connect");
|
||||||
State::Error
|
State::Error
|
||||||
|
|||||||
+1
-1
@@ -234,7 +234,7 @@ impl Peers {
|
|||||||
let addr = peer.get_addr();
|
let addr = peer.get_addr();
|
||||||
match TcpStream::connect(addr.clone()) {
|
match TcpStream::connect(addr.clone()) {
|
||||||
Ok(mut stream) => {
|
Ok(mut stream) => {
|
||||||
info!("Created connection to peer {}, state = {:?}", &addr, peer.get_state());
|
info!("Created connection to peer {}", &addr);
|
||||||
registry.register(&mut stream, token.clone(), Interest::WRITABLE).unwrap();
|
registry.register(&mut stream, token.clone(), Interest::WRITABLE).unwrap();
|
||||||
peer.set_state(State::Connecting);
|
peer.set_state(State::Connecting);
|
||||||
peer.set_stream(stream);
|
peer.set_stream(stream);
|
||||||
|
|||||||
Reference in New Issue
Block a user