Implemented right way to constrain zone difficulty.

This commit is contained in:
Revertron
2021-03-17 14:55:05 +01:00
parent d8ac1e3c32
commit 1d9833db0f
10 changed files with 95 additions and 36 deletions
+2 -2
View File
@@ -29,12 +29,12 @@ pub struct Block {
}
impl Block {
pub fn new(transaction: Option<Transaction>, pub_key: Bytes, prev_block_hash: Bytes) -> Self {
pub fn new(transaction: Option<Transaction>, pub_key: Bytes, prev_block_hash: Bytes, difficulty: u32) -> Self {
Block {
index: 0,
timestamp: 0,
version: 0,
difficulty: 0,
difficulty,
random: 0,
nonce: 0,
transaction,
+22
View File
@@ -14,6 +14,7 @@ use crate::blockchain::hash_utils::*;
use crate::settings::Settings;
use crate::keys::check_public_key_strength;
use std::cmp::{min, max};
use crate::blockchain::transaction::{ZoneData, DomainData};
const DB_NAME: &str = "blockchain.db";
const SQL_CREATE_TABLES: &str = "CREATE TABLE blocks (
@@ -309,6 +310,21 @@ impl Chain {
}
}
pub fn get_zone_difficulty(&self, zone: &str) -> u32 {
match self.get_domain_transaction(zone) {
None => { u32::max_value() }
Some(transaction) => {
match serde_json::from_str::<ZoneData>(&transaction.data) {
Ok(data) => { data.difficulty }
Err(_) => {
warn!("Wrong data for zone {}!", zone);
u32::max_value()
}
}
}
}
}
pub fn last_block(&self) -> Option<Block> {
self.last_block.clone()
}
@@ -395,6 +411,12 @@ impl Chain {
return Bad;
}
}
if let Ok(data) = serde_json::from_str::<DomainData>(&transaction.data) {
if self.get_zone_difficulty(&data.zone) > block.difficulty {
warn!("Block {:?} is mined with too low difficulty!", &block);
return Bad;
}
}
}
match &self.last_block {
None => {
+1
View File
@@ -1,5 +1,6 @@
pub const CHAIN_VERSION: u32 = 1;
pub const ZONE_DIFFICULTY: u32 = 28;
pub const BLOCK_DIFFICULTY: u32 = 24;
pub const LOCKER_DIFFICULTY: u32 = 18;
pub const KEYSTORE_DIFFICULTY: usize = 25;
+5 -4
View File
@@ -4,6 +4,7 @@ use crate::dns::filter::DnsFilter;
use crate::dns::protocol::{DnsPacket, QueryType, DnsRecord, DnsQuestion, ResultCode};
#[allow(unused_imports)]
use log::{trace, debug, info, warn, error};
use crate::blockchain::transaction::DomainData;
pub struct BlockchainFilter {
context: Arc<Mutex<Context>>
@@ -48,12 +49,12 @@ impl DnsFilter for BlockchainFilter {
}
Some(data) => {
info!("Found data for domain {}", &search);
let mut records: Vec<DnsRecord> = match serde_json::from_str(&data) {
let mut data: DomainData = match serde_json::from_str(&data) {
Err(_) => { return None; }
Ok(records) => { records }
Ok(data) => { data }
};
let mut answers: Vec<DnsRecord> = Vec::new();
for mut record in records.iter_mut() {
for mut record in data.records.iter_mut() {
if record.get_querytype() == qtype {
match &mut record {
DnsRecord::A { domain, .. }
@@ -98,7 +99,7 @@ impl DnsFilter for BlockchainFilter {
}
if answers.is_empty() {
// If there are no records found we search for *.domain.ltd record
for mut record in records {
for mut record in data.records {
if record.get_querytype() == qtype {
match record.get_domain() {
None => {}
+19
View File
@@ -5,6 +5,7 @@ use serde::ser::SerializeStruct;
use crate::blockchain::hash_utils::*;
use crate::bytes::Bytes;
use crate::dns::protocol::DnsRecord;
extern crate serde;
extern crate serde_json;
@@ -75,4 +76,22 @@ impl Serialize for Transaction {
structure.serialize_field("pub_key", &self.pub_key)?;
structure.end()
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq)]
pub struct DomainData {
pub zone: String,
pub records: Vec<DnsRecord>
}
impl DomainData {
pub fn new(zone: String, records: Vec<DnsRecord>) -> Self {
Self { zone, records }
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq)]
pub struct ZoneData {
pub name: String,
pub difficulty: u32
}