Fixed block exchange and server reconnection.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"chain_name": "test",
|
"chain_name": "test",
|
||||||
"origin": "",
|
"origin": "0000065DE954CEEAC1A313A3191E61F771ADFDC00F9B451FA4A1353A2955A19E",
|
||||||
"version": 0,
|
"version": 0,
|
||||||
"key_file": "default.key",
|
"key_file": "default.key",
|
||||||
"listen": "127.0.0.1:4244",
|
"listen": "127.0.0.1:4244",
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ impl Blockchain {
|
|||||||
match self.last_block {
|
match self.last_block {
|
||||||
None => { 0u64 }
|
None => { 0u64 }
|
||||||
Some(ref block) => {
|
Some(ref block) => {
|
||||||
block.index
|
block.index + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -9,7 +9,7 @@ use rand::RngCore;
|
|||||||
use serde::{Deserialize};
|
use serde::{Deserialize};
|
||||||
use web_view::*;
|
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::event::Event;
|
||||||
use alfis::miner::Miner;
|
use alfis::miner::Miner;
|
||||||
use alfis::p2p::Network;
|
use alfis::p2p::Network;
|
||||||
@@ -31,6 +31,10 @@ fn main() {
|
|||||||
Some(keystore) => { keystore }
|
Some(keystore) => { keystore }
|
||||||
};
|
};
|
||||||
let blockchain: Blockchain = Blockchain::new(&settings);
|
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<Mutex<Context>> = Arc::new(Mutex::new(Context::new(settings, keystore, blockchain)));
|
let context: Arc<Mutex<Context>> = Arc::new(Mutex::new(Context::new(settings, keystore, blockchain)));
|
||||||
|
|
||||||
let mut miner_obj = Miner::new(context.clone());
|
let mut miner_obj = Miner::new(context.clone());
|
||||||
|
|||||||
+16
-5
@@ -12,7 +12,7 @@ use mio::event::Event;
|
|||||||
use mio::net::{TcpListener, TcpStream};
|
use mio::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
use crate::{Context, Block, p2p::Message, p2p::State, p2p::Peer, p2p::Peers};
|
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 SERVER: Token = Token(0);
|
||||||
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(3000));
|
const POLL_TIMEOUT: Option<Duration> = Some(Duration::from_millis(3000));
|
||||||
@@ -82,6 +82,7 @@ impl Network {
|
|||||||
}
|
}
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
|
poll.registry().reregister(&mut server, SERVER, Interest::READABLE).expect("Error reregistering server");
|
||||||
}
|
}
|
||||||
token => {
|
token => {
|
||||||
match peers.get_mut_peer(&token) {
|
match peers.get_mut_peer(&token) {
|
||||||
@@ -89,7 +90,15 @@ impl Network {
|
|||||||
match handle_connection_event(context.clone(), &mut peers, &poll.registry(), &event) {
|
match handle_connection_event(context.clone(), &mut peers, &poll.registry(), &event) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if !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) => {
|
Err(_err) => {
|
||||||
@@ -102,6 +111,7 @@ impl Network {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
events.clear();
|
||||||
|
|
||||||
// Send pings to idle peers
|
// Send pings to idle peers
|
||||||
let height = { context.lock().unwrap().blockchain.height() };
|
let height = { context.lock().unwrap().blockchain.height() };
|
||||||
@@ -258,6 +268,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
|||||||
peer.set_public(public);
|
peer.set_public(public);
|
||||||
State::message(Message::shake(&origin, version, true, my_height))
|
State::message(Message::shake(&origin, version, true, my_height))
|
||||||
} else {
|
} else {
|
||||||
|
println!("Handshake from unsupported chain or version");
|
||||||
State::Error
|
State::Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -267,7 +278,7 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
|||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
if height > my_height {
|
if height > my_height {
|
||||||
State::message(Message::GetBlock { index: my_height + 1u64 })
|
State::message(Message::GetBlock { index: my_height })
|
||||||
} else {
|
} else {
|
||||||
State::message(Message::GetPeers)
|
State::message(Message::GetPeers)
|
||||||
}
|
}
|
||||||
@@ -278,14 +289,14 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
|
|||||||
Message::Error => { State::Error }
|
Message::Error => { State::Error }
|
||||||
Message::Ping { height } => {
|
Message::Ping { height } => {
|
||||||
if height > my_height {
|
if height > my_height {
|
||||||
State::message(Message::GetBlock { index: my_height + 1u64 })
|
State::message(Message::GetBlock { index: my_height })
|
||||||
} else {
|
} else {
|
||||||
State::message(Message::pong(my_height))
|
State::message(Message::pong(my_height))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::Pong { height } => {
|
Message::Pong { height } => {
|
||||||
if height > my_height {
|
if height > my_height {
|
||||||
State::message(Message::GetBlock { index: my_height + 1u64 })
|
State::message(Message::GetBlock { index: my_height })
|
||||||
} else {
|
} else {
|
||||||
State::idle()
|
State::idle()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user