Refactored block and transaction structure. Introduced a 'confirmation' entity to avoid block interceptions (at least make them pointless).

This commit is contained in:
Revertron
2021-02-13 23:37:44 +01:00
parent 279b3e87c3
commit 1331f44b0e
9 changed files with 97 additions and 85 deletions
+6 -6
View File
@@ -7,8 +7,8 @@ use crate::p2p::peer::Peer;
#[derive(Debug, Serialize, Deserialize)]
pub enum Message {
Error,
Hand { chain: String, version: u32, public: bool },
Shake { ok: bool, height: u64 },
Hand { origin: String, version: u32, public: bool },
Shake { origin: String, version: u32, ok: bool, height: u64 },
Ping { height: u64 },
Pong { height: u64 },
GetPeers,
@@ -26,12 +26,12 @@ impl Message {
}
}
pub fn hand(chain: &str, version: u32, public: bool) -> Self {
Message::Hand { chain: chain.to_owned(), version, public }
pub fn hand(origin: &str, version: u32, public: bool) -> Self {
Message::Hand { origin: origin.to_owned(), version, public }
}
pub fn shake(ok: bool, height: u64) -> Self {
Message::Shake { ok, height }
pub fn shake(origin: &str, version: u32, ok: bool, height: u64) -> Self {
Message::Shake { origin: origin.to_owned(), version, ok, height }
}
pub fn ping(height: u64) -> Self {
+6 -5
View File
@@ -176,7 +176,7 @@ fn handle_connection_event(context: Arc<Mutex<Context>>, peers: &mut Peers, regi
println!("Sending hello to socket {}", event.token().0);
let data: String = {
let mut c = context.lock().unwrap();
let message = Message::hand(&c.settings.chain_name, c.settings.version_flags, c.settings.public);
let message = Message::hand(&c.settings.origin, c.settings.version, c.settings.public);
serde_json::to_string(&message).unwrap()
};
send_message(peer.get_stream(), &data.into_bytes());
@@ -255,17 +255,18 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
context.blockchain.height()
};
match message {
Message::Hand { chain, version, public } => {
Message::Hand { origin: origin, version, public } => {
let context = context.lock().unwrap();
if chain == context.settings.chain_name && version == context.settings.version_flags {
if origin == context.settings.origin && version == context.settings.version {
let mut peer = peers.get_mut_peer(token).unwrap();
peer.set_public(public);
State::message(Message::shake(true, context.blockchain.height()))
State::message(Message::shake(&context.settings.origin, context.settings.version, true, context.blockchain.height()))
} else {
State::Error
}
}
Message::Shake { ok, height } => {
Message::Shake { origin, version, ok, height } => {
// TODO check origin and version for compatibility
if ok {
if height > my_height {
State::message(Message::GetBlock { index: my_height + 1u64 })