Added app_version to handshake.

This commit is contained in:
Revertron
2021-03-21 00:48:32 +01:00
parent d23990c3e7
commit fdc5b8f233
5 changed files with 20 additions and 10 deletions
+7 -3
View File
@@ -7,7 +7,7 @@ use crate::Bytes;
#[derive(Debug, Serialize, Deserialize)]
pub enum Message {
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 },
Ping { 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 {
Message::Hand { origin: origin.to_owned(), version, public, rand: rand.to_owned() }
pub fn hand(app_version: &str, origin: &str, version: u32, public: bool, rand: &str) -> Self {
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 {
@@ -47,6 +47,10 @@ impl Message {
}
}
fn default_version() -> String {
String::from("0.0.0")
}
#[cfg(test)]
mod tests {
use crate::p2p::Message;
+3 -2
View File
@@ -187,7 +187,7 @@ fn handle_connection_event(context: Arc<Mutex<Context>>, peers: &mut Peers, regi
debug!("Sending hello to {}", &peer.get_addr());
let data: String = {
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()
};
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)
};
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) {
warn!("Detected loop connect");
State::Error
+1 -1
View File
@@ -234,7 +234,7 @@ impl Peers {
let addr = peer.get_addr();
match TcpStream::connect(addr.clone()) {
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();
peer.set_state(State::Connecting);
peer.set_stream(stream);