Implemented P2P traffic encryption.

Changed serialization format of P2P messages.
Refactored P2P network code.
This commit is contained in:
Revertron
2021-05-30 00:33:13 +02:00
parent 5398410d8d
commit 319051edbd
15 changed files with 857 additions and 493 deletions
+10
View File
@@ -61,13 +61,23 @@ impl Block {
}
}
pub fn from_bytes(data: &[u8]) -> serde_cbor::Result<Self> {
serde_cbor::from_slice(data)
}
pub fn is_genesis(&self) -> bool {
self.index == 1 &&
matches!(Transaction::get_type(&self.transaction), TransactionType::Origin) &&
self.prev_block_hash == Bytes::default()
}
/// Serializes block to CBOR for network
pub fn as_bytes(&self) -> Vec<u8> {
serde_cbor::to_vec(&self).unwrap()
}
/// Serializes block to bincode format for hashing.
pub fn as_bytes_compact(&self) -> Vec<u8> {
bincode::serialize(&self).unwrap()
}
+27 -1
View File
@@ -1010,8 +1010,10 @@ impl SignersCache {
pub mod tests {
use log::LevelFilter;
use simplelog::{ColorChoice, ConfigBuilder, TerminalMode, TermLogger};
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use crate::{Chain, Settings};
use crate::{Chain, Settings, Block};
fn init_logger() {
let config = ConfigBuilder::new()
@@ -1035,4 +1037,28 @@ pub mod tests {
chain.check_chain(u64::MAX);
assert_eq!(chain.get_height(), 149);
}
#[test]
pub fn check_serde() {
let settings = Settings::default();
let chain = Chain::new(&settings, "./tests/blockchain.db");
// Check the first block, its transaction doesn't have identity
let block = chain.get_block(1).unwrap();
let buf = serde_cbor::to_vec(&block).unwrap();
let block2: Block = serde_cbor::from_slice(&buf[..]).unwrap();
assert_eq!(block, block2);
// Check second block, it is common "full" block with domain
let block = chain.get_block(2).unwrap();
let buf = serde_cbor::to_vec(&block).unwrap();
let block2: Block = serde_cbor::from_slice(&buf[..]).unwrap();
assert_eq!(block, block2);
// Check block 36, it is an "empty" block, used to sign full blocks
let block = chain.get_block(36).unwrap();
let buf = serde_cbor::to_vec(&block).unwrap();
let block2: Block = serde_cbor::from_slice(&buf[..]).unwrap();
assert_eq!(block, block2);
}
}
+2 -2
View File
@@ -9,7 +9,7 @@ pub fn check_block_hash(block: &Block) -> bool {
let mut copy: Block = block.clone();
copy.hash = Bytes::default();
copy.signature = Bytes::default();
blakeout_data(&copy.as_bytes()) == block.hash
blakeout_data(&copy.as_bytes_compact()) == block.hash
}
/// Hashes data by given hasher
@@ -23,7 +23,7 @@ pub fn blakeout_data(data: &[u8]) -> Bytes {
pub fn check_block_signature(block: &Block) -> bool {
let mut copy = block.clone();
copy.signature = Bytes::default();
Keystore::check(&copy.as_bytes(), &copy.pub_key, &block.signature)
Keystore::check(&copy.as_bytes_compact(), &copy.pub_key, &block.signature)
}
/// Hashes some identity (domain in case of DNS). If you give it a public key, it will hash with it as well.
-5
View File
@@ -49,11 +49,6 @@ impl Transaction {
}
}
pub fn get_bytes(&self) -> Vec<u8> {
// Let it panic if something is not okay
serde_json::to_vec(&self).unwrap()
}
pub fn to_string(&self) -> String {
// Let it panic if something is not okay
serde_json::to_string(&self).unwrap()