diff --git a/alfis.cfg b/alfis.cfg index 771681d..5a2d805 100644 --- a/alfis.cfg +++ b/alfis.cfg @@ -1,6 +1,6 @@ { "chain_name": "test", - "origin": "", + "origin": "0000065DE954CEEAC1A313A3191E61F771ADFDC00F9B451FA4A1353A2955A19E", "version": 0, "key_file": "default.key", "listen": "127.0.0.1:4244", diff --git a/src/blockchain/blockchain.rs b/src/blockchain/blockchain.rs index 58f12c0..8737677 100644 --- a/src/blockchain/blockchain.rs +++ b/src/blockchain/blockchain.rs @@ -183,7 +183,7 @@ impl Blockchain { match self.last_block { None => { 0u64 } Some(ref block) => { - block.index + block.index + 1 } } } diff --git a/src/main.rs b/src/main.rs index 50fe737..4eb7b2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use rand::RngCore; use serde::{Deserialize}; use web_view::*; -use alfis::{Blockchain, Bytes, Context, Keystore, Settings, Transaction}; +use alfis::{Blockchain, Bytes, Context, Keystore, Settings, Transaction, Block}; use alfis::event::Event; use alfis::miner::Miner; use alfis::p2p::Network; @@ -31,6 +31,10 @@ fn main() { Some(keystore) => { keystore } }; let blockchain: Blockchain = Blockchain::new(&settings); + match blockchain.get_block(0) { + None => { println!("No blocks found in DB"); } + Some(block) => { println!("Loaded DB with origin {:?}", &block.hash); } + } let context: Arc> = Arc::new(Mutex::new(Context::new(settings, keystore, blockchain))); let mut miner_obj = Miner::new(context.clone()); diff --git a/src/p2p/network.rs b/src/p2p/network.rs index 34a331b..69eb62e 100644 --- a/src/p2p/network.rs +++ b/src/p2p/network.rs @@ -12,7 +12,7 @@ use mio::event::Event; use mio::net::{TcpListener, TcpStream}; use crate::{Context, Block, p2p::Message, p2p::State, p2p::Peer, p2p::Peers}; -use std::net::{SocketAddr, IpAddr, SocketAddrV4}; +use std::net::{SocketAddr, IpAddr, SocketAddrV4, Shutdown}; const SERVER: Token = Token(0); const POLL_TIMEOUT: Option = Some(Duration::from_millis(3000)); @@ -82,6 +82,7 @@ impl Network { } Err(_) => {} } + poll.registry().reregister(&mut server, SERVER, Interest::READABLE).expect("Error reregistering server"); } token => { match peers.get_mut_peer(&token) { @@ -89,7 +90,15 @@ impl Network { match handle_connection_event(context.clone(), &mut peers, &poll.registry(), &event) { Ok(result) => { if !result { - peers.remove_peer(&token); + match peers.remove_peer(&token) { + None => {} + Some(mut peer) => { + let stream = peer.get_stream(); + poll.registry().deregister(stream); + stream.shutdown(Shutdown::Both); + println!("Peer connection {:?} has shut down", &peer.get_addr()); + } + } } } Err(_err) => { @@ -102,6 +111,7 @@ impl Network { } } } + events.clear(); // Send pings to idle peers let height = { context.lock().unwrap().blockchain.height() }; @@ -258,6 +268,7 @@ fn handle_message(context: Arc>, message: Message, peers: &mut Pe peer.set_public(public); State::message(Message::shake(&origin, version, true, my_height)) } else { + println!("Handshake from unsupported chain or version"); State::Error } } @@ -267,7 +278,7 @@ fn handle_message(context: Arc>, message: Message, peers: &mut Pe } if ok { if height > my_height { - State::message(Message::GetBlock { index: my_height + 1u64 }) + State::message(Message::GetBlock { index: my_height }) } else { State::message(Message::GetPeers) } @@ -278,14 +289,14 @@ fn handle_message(context: Arc>, message: Message, peers: &mut Pe Message::Error => { State::Error } Message::Ping { height } => { if height > my_height { - State::message(Message::GetBlock { index: my_height + 1u64 }) + State::message(Message::GetBlock { index: my_height }) } else { State::message(Message::pong(my_height)) } } Message::Pong { height } => { if height > my_height { - State::message(Message::GetBlock { index: my_height + 1u64 }) + State::message(Message::GetBlock { index: my_height }) } else { State::idle() }