Added restriction by public key, made some refactoring.

This commit is contained in:
Revertron
2021-03-12 01:36:54 +01:00
parent 186f9cb05b
commit b39f5fa7f0
5 changed files with 66 additions and 35 deletions
+21 -15
View File
@@ -12,6 +12,8 @@ use crate::blockchain::enums::BlockQuality;
use crate::blockchain::enums::BlockQuality::*;
use crate::blockchain::hash_utils::*;
use crate::settings::Settings;
use crate::keys::check_public_key_strength;
use crate::blockchain::KEYSTORE_DIFFICULTY;
const DB_NAME: &str = "blockchain.db";
const SQL_CREATE_TABLES: &str = "CREATE TABLE blocks (
@@ -149,21 +151,21 @@ impl Chain {
statement.bind(7, transaction.to_string().as_str())?;
}
}
statement.bind(8, block.prev_block_hash.as_slice())?;
statement.bind(9, block.hash.as_slice())?;
statement.bind(10, block.pub_key.as_slice())?;
statement.bind(11, block.signature.as_slice())?;
statement.bind(8, &**block.prev_block_hash)?;
statement.bind(9, &**block.hash)?;
statement.bind(10, &**block.pub_key)?;
statement.bind(11, &**block.signature)?;
statement.next()
}
/// Adds transaction to transactions table
fn add_transaction_to_table(&mut self, t: &Transaction) -> sqlite::Result<State> {
let mut statement = self.db.prepare(SQL_ADD_TRANSACTION)?;
statement.bind(1, t.identity.as_slice())?;
statement.bind(2, t.confirmation.as_slice())?;
statement.bind(1, &**t.identity)?;
statement.bind(2, &**t.confirmation)?;
statement.bind(3, t.method.as_ref() as &str)?;
statement.bind(4, t.data.as_ref() as &str)?;
statement.bind(5, t.pub_key.as_slice())?;
statement.bind(5, &**t.pub_key)?;
statement.next()
}
@@ -241,9 +243,9 @@ impl Chain {
/// Checks if this identity is free or is owned by the same pub_key
pub fn is_id_available(&self, identity: &Bytes, public_key: &Bytes) -> bool {
let mut statement = self.db.prepare(SQL_GET_PUBLIC_KEY_BY_ID).unwrap();
statement.bind(1, identity.as_slice()).expect("Error in bind");
statement.bind(1, &***identity).expect("Error in bind");
while let State::Row = statement.next().unwrap() {
let pub_key = Bytes::from_bytes(statement.read::<Vec<u8>>(0).unwrap().as_slice());
let pub_key = Bytes::from_bytes(&statement.read::<Vec<u8>>(0).unwrap());
if !pub_key.eq(public_key) {
return false;
}
@@ -260,7 +262,7 @@ impl Chain {
// Checking for existing zone in DB
let identity_hash = hash_identity(zone, None);
let mut statement = self.db.prepare(SQL_GET_ID_BY_ID).unwrap();
statement.bind(1, identity_hash.as_slice()).expect("Error in bind");
statement.bind(1, &**identity_hash).expect("Error in bind");
while let State::Row = statement.next().unwrap() {
// If there is such a zone
self.zones.borrow_mut().insert(zone.to_owned());
@@ -277,13 +279,13 @@ impl Chain {
let identity_hash = hash_identity(domain, None);
let mut statement = self.db.prepare(SQL_GET_TRANSACTION_BY_ID).unwrap();
statement.bind(1, identity_hash.as_slice()).expect("Error in bind");
statement.bind(1, &**identity_hash).expect("Error in bind");
while let State::Row = statement.next().unwrap() {
let identity = Bytes::from_bytes(statement.read::<Vec<u8>>(1).unwrap().as_slice());
let confirmation = Bytes::from_bytes(statement.read::<Vec<u8>>(2).unwrap().as_slice());
let identity = Bytes::from_bytes(&statement.read::<Vec<u8>>(1).unwrap());
let confirmation = Bytes::from_bytes(&statement.read::<Vec<u8>>(2).unwrap());
let method = statement.read::<String>(3).unwrap();
let data = statement.read::<String>(4).unwrap();
let pub_key = Bytes::from_bytes(statement.read::<Vec<u8>>(5).unwrap().as_slice());
let pub_key = Bytes::from_bytes(&statement.read::<Vec<u8>>(5).unwrap());
let transaction = Transaction { identity, confirmation, method, data, pub_key };
debug!("Found transaction for domain {}: {:?}", domain, &transaction);
if transaction.check_identity(domain) {
@@ -337,6 +339,10 @@ impl Chain {
warn!("Ignoring block from the future:\n{:?}", &block);
return Bad;
}
if !check_public_key_strength(&block.pub_key, KEYSTORE_DIFFICULTY) {
warn!("Ignoring block with weak public key:\n{:?}", &block);
return Bad;
}
let difficulty = match block.transaction {
None => { LOCKER_DIFFICULTY }
Some(_) => { BLOCK_DIFFICULTY }
@@ -345,7 +351,7 @@ impl Chain {
warn!("Block difficulty is lower than needed");
return Bad;
}
if !hash_is_good(block.hash.as_slice(), block.difficulty as usize) {
if !hash_is_good(&block.hash, block.difficulty as usize) {
warn!("Ignoring block with low difficulty:\n{:?}", &block);
return Bad;
}
+2 -2
View File
@@ -7,7 +7,7 @@ use num_traits::One;
use crate::{Block, Bytes, Keystore};
/// Creates needed hasher by current blockchain version
fn get_hasher_for_version(version: u32) -> Box<dyn Digest> {
pub(crate) fn get_hasher_for_version(version: u32) -> Box<dyn Digest> {
match version {
2 => Box::new(Blakeout::default()),
_ => Box::new(Sha256::new())
@@ -41,7 +41,7 @@ pub fn hash_data(digest: &mut dyn Digest, 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.as_slice(), block.signature.as_slice())
Keystore::check(&copy.as_bytes(), &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.