Added more restriction by public key. Added alternative zones to restrictions.

This commit is contained in:
Revertron
2021-03-13 21:04:40 +01:00
parent b39f5fa7f0
commit 809256b955
8 changed files with 115 additions and 59 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ repository = "https://github.com/Revertron/Alfis"
[dependencies] [dependencies]
getopts = "0.2.21" getopts = "0.2.21"
log = "0.4.14" log = "0.4.14"
toml = "0.5.8"
simple_logger = "1.11.0" simple_logger = "1.11.0"
toml = "0.5.8"
rust-crypto = "^0.2" rust-crypto = "^0.2"
blakeout = "0.1.0" blakeout = "0.1.0"
num_cpus = "1.13.0" num_cpus = "1.13.0"
@@ -27,7 +27,7 @@ num-traits = "0.2"
bincode = "1.2.0" bincode = "1.2.0"
base64 = "0.11.0" base64 = "0.11.0"
chrono = { version = "0.4.13", features = ["serde"] } chrono = { version = "0.4.13", features = ["serde"] }
rand = "0.7.2" rand = "0.8.3"
sqlite = "0.25.3" sqlite = "0.25.3"
uuid = { version = "0.8.2", features = ["serde", "v4"] } uuid = { version = "0.8.2", features = ["serde", "v4"] }
mio = { version = "0.7", features = ["os-poll", "net"] } mio = { version = "0.7", features = ["os-poll", "net"] }
+32
View File
@@ -0,0 +1,32 @@
# OpenNIC zones
bbs
chan
cyb
dyn
geek
gopher
indy
libre
neo
null
o
oss
oz
parody
pirate
# Namecoin zone
bit
# Etherium
eth
# EmerCoin
emc
coin
lib
bazar
# Reserved
example
invalid
local
localhost
onion
test
+33 -24
View File
@@ -7,13 +7,12 @@ use log::{debug, error, info, trace, warn};
use sqlite::{Connection, State, Statement}; use sqlite::{Connection, State, Statement};
use crate::{Block, Bytes, Keystore, Transaction}; use crate::{Block, Bytes, Keystore, Transaction};
use crate::blockchain::constants::{BLOCK_DIFFICULTY, CHAIN_VERSION, LOCKER_BLOCK_COUNT, LOCKER_BLOCK_INTERVAL, LOCKER_BLOCK_START, LOCKER_DIFFICULTY}; use crate::blockchain::constants::*;
use crate::blockchain::enums::BlockQuality; use crate::blockchain::enums::BlockQuality;
use crate::blockchain::enums::BlockQuality::*; use crate::blockchain::enums::BlockQuality::*;
use crate::blockchain::hash_utils::*; use crate::blockchain::hash_utils::*;
use crate::settings::Settings; use crate::settings::Settings;
use crate::keys::check_public_key_strength; use crate::keys::check_public_key_strength;
use crate::blockchain::KEYSTORE_DIFFICULTY;
const DB_NAME: &str = "blockchain.db"; const DB_NAME: &str = "blockchain.db";
const SQL_CREATE_TABLES: &str = "CREATE TABLE blocks ( const SQL_CREATE_TABLES: &str = "CREATE TABLE blocks (
@@ -37,6 +36,7 @@ const SQL_GET_LAST_BLOCK: &str = "SELECT * FROM blocks ORDER BY id DESC LIMIT 1;
const SQL_ADD_TRANSACTION: &str = "INSERT INTO transactions (identity, confirmation, method, data, pub_key) VALUES (?, ?, ?, ?, ?)"; const SQL_ADD_TRANSACTION: &str = "INSERT INTO transactions (identity, confirmation, method, data, pub_key) VALUES (?, ?, ?, ?, ?)";
const SQL_GET_BLOCK_BY_ID: &str = "SELECT * FROM blocks WHERE id=? LIMIT 1;"; const SQL_GET_BLOCK_BY_ID: &str = "SELECT * FROM blocks WHERE id=? LIMIT 1;";
const SQL_GET_LAST_FULL_BLOCK: &str = "SELECT * FROM blocks WHERE `transaction`<>'' ORDER BY id DESC LIMIT 1;"; const SQL_GET_LAST_FULL_BLOCK: &str = "SELECT * FROM blocks WHERE `transaction`<>'' ORDER BY id DESC LIMIT 1;";
const SQL_GET_LAST_FULL_BLOCK_FOR_KEY: &str = "SELECT * FROM blocks WHERE `transaction`<>'' AND pub_key = ? ORDER BY id DESC LIMIT 1;";
const SQL_GET_PUBLIC_KEY_BY_ID: &str = "SELECT pub_key FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;"; const SQL_GET_PUBLIC_KEY_BY_ID: &str = "SELECT pub_key FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;";
const SQL_GET_ID_BY_ID: &str = "SELECT identity FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;"; const SQL_GET_ID_BY_ID: &str = "SELECT identity FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;";
const SQL_GET_TRANSACTION_BY_ID: &str = "SELECT * FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;"; const SQL_GET_TRANSACTION_BY_ID: &str = "SELECT * FROM transactions WHERE identity = ? ORDER BY id DESC LIMIT 1;";
@@ -112,7 +112,7 @@ impl Chain {
if block.transaction.is_some() { if block.transaction.is_some() {
self.last_full_block = Some(block); self.last_full_block = Some(block);
} else { } else {
self.last_full_block = self.get_last_full_block(); self.last_full_block = self.get_last_full_block(None);
} }
} }
} }
@@ -195,28 +195,30 @@ impl Chain {
} }
/// Gets last block that has a Transaction within /// Gets last block that has a Transaction within
pub fn get_last_full_block(&self) -> Option<Block> { pub fn get_last_full_block(&self, pub_key: Option<&[u8]>) -> Option<Block> {
match self.db.prepare(SQL_GET_LAST_FULL_BLOCK) { let mut statement = match pub_key {
Ok(mut statement) => { None => {
while statement.next().unwrap() == State::Row { self.db.prepare(SQL_GET_LAST_FULL_BLOCK).expect("Unable to prepare")
return match Self::get_block_from_statement(&mut statement) { }
None => { Some(pub_key) => {
error!("Something wrong with block in DB!"); let mut statement = self.db.prepare(SQL_GET_LAST_FULL_BLOCK_FOR_KEY).expect("Unable to prepare");
None statement.bind(1, pub_key).expect("Unable to bind");
} statement
Some(block) => { }
trace!("Got last full block: {:?}", &block); };
Some(block) while statement.next().unwrap() == State::Row {
} return match Self::get_block_from_statement(&mut statement) {
}; None => {
error!("Something wrong with block in DB!");
None
} }
None Some(block) => {
} trace!("Got last full block: {:?}", &block);
Err(e) => { Some(block)
warn!("Can't find any full blocks: {}", e); }
None };
}
} }
None
} }
/// Checks if any domain is available to mine for this client (pub_key) /// Checks if any domain is available to mine for this client (pub_key)
@@ -368,6 +370,13 @@ impl Chain {
warn!("Block {:?} is trying to spoof an identity!", &block); warn!("Block {:?} is trying to spoof an identity!", &block);
return Bad; return Bad;
} }
// TODO check only blocks with new identity
if let Some(last) = self.get_last_full_block(Some(&block.pub_key)) {
if last.timestamp + FULL_BLOCKS_INTERVAL > block.timestamp {
warn!("Block {:?} is mined too early!", &block);
return Bad;
}
}
} }
match &self.last_block { match &self.last_block {
None => { None => {
@@ -426,7 +435,7 @@ impl Chain {
if block.index < LOCKER_BLOCK_START { if block.index < LOCKER_BLOCK_START {
return None; return None;
} }
match self.get_last_full_block() { match self.get_last_full_block(None) {
Some(b) => { Some(b) => {
if b.index + LOCKER_BLOCK_COUNT <= block.index { if b.index + LOCKER_BLOCK_COUNT <= block.index {
trace!("Block {} is locked enough", b.index); trace!("Block {} is locked enough", b.index);
+2
View File
@@ -7,3 +7,5 @@ pub const KEYSTORE_DIFFICULTY: usize = 25;
pub const LOCKER_BLOCK_START: u64 = 10; pub const LOCKER_BLOCK_START: u64 = 10;
pub const LOCKER_BLOCK_COUNT: u64 = 3; pub const LOCKER_BLOCK_COUNT: u64 = 3;
pub const LOCKER_BLOCK_INTERVAL: i64 = 300; pub const LOCKER_BLOCK_INTERVAL: i64 = 300;
pub const FULL_BLOCKS_INTERVAL: i64 = 86400; // One day in seconds
+3 -3
View File
@@ -1,4 +1,4 @@
use crate::{Chain, Bus, Keystore, Settings, Iana}; use crate::{Chain, Bus, Keystore, Settings, ExternalZones};
use crate::event::Event; use crate::event::Event;
#[allow(unused_imports)] #[allow(unused_imports)]
use log::{trace, debug, info, warn, error}; use log::{trace, debug, info, warn, error};
@@ -7,14 +7,14 @@ pub struct Context {
pub settings: Settings, pub settings: Settings,
pub keystore: Keystore, pub keystore: Keystore,
pub chain: Chain, pub chain: Chain,
pub iana: Iana, pub iana: ExternalZones,
pub bus: Bus<Event>, pub bus: Bus<Event>,
} }
impl Context { impl Context {
/// Creating an essential context to work with /// Creating an essential context to work with
pub fn new(settings: Settings, keystore: Keystore, chain: Chain) -> Context { pub fn new(settings: Settings, keystore: Keystore, chain: Chain) -> Context {
Context { settings, keystore, chain, iana: Iana::new(), bus: Bus::new() } Context { settings, keystore, chain, iana: ExternalZones::new(), bus: Bus::new() }
} }
/// Load keystore and return Context /// Load keystore and return Context
-28
View File
@@ -1,28 +0,0 @@
use std::collections::HashSet;
pub struct Iana {
zones: HashSet<String>,
hashes: HashSet<String>
}
impl Iana {
pub fn new() -> Self {
let zones: HashSet<_> = include_str!("../iana-tlds.txt")
.split("\n")
.map(String::from)
.collect();
let hashes: HashSet<_> = include_str!("../iana-hashes.txt")
.split("\n")
.map(String::from)
.collect();
Self { zones, hashes }
}
pub fn has_zone(&self, zone: &str) -> bool {
self.zones.contains(zone)
}
pub fn has_hash(&self, hash: &str) -> bool {
self.hashes.contains(hash)
}
}
+2 -2
View File
@@ -8,7 +8,7 @@ pub use crate::p2p::Network;
pub use crate::settings::Settings; pub use crate::settings::Settings;
pub use crate::bytes::Bytes; pub use crate::bytes::Bytes;
pub use crate::keys::Keystore; pub use crate::keys::Keystore;
pub use crate::iana::Iana; pub use crate::x_zones::ExternalZones;
pub use crate::simplebus::*; pub use crate::simplebus::*;
pub use crate::utils::*; pub use crate::utils::*;
@@ -24,5 +24,5 @@ pub mod dns;
pub mod dns_utils; pub mod dns_utils;
pub mod settings; pub mod settings;
pub mod bytes; pub mod bytes;
pub mod iana; pub mod x_zones;
+41
View File
@@ -0,0 +1,41 @@
use std::collections::HashSet;
use crate::blockchain::hash_utils::hash_identity;
pub struct ExternalZones {
zones: HashSet<String>,
hashes: HashSet<String>
}
impl ExternalZones {
pub fn new() -> Self {
let mut zones: HashSet<_> = include_str!("../iana-tlds.txt")
.split("\n")
.map(String::from)
.collect();
let mut hashes: HashSet<_> = include_str!("../iana-hashes.txt")
.split("\n")
.map(String::from)
.collect();
let open_nic: HashSet<_> = include_str!("../other-tlds.txt")
.split("\n")
.map(String::from)
.collect();
for zone in open_nic.iter() {
if zone.is_empty() || zone.starts_with("#") {
continue;
}
zones.insert(zone.to_string());
hashes.insert(hash_identity(zone, None).to_string());
}
Self { zones, hashes }
}
pub fn has_zone(&self, zone: &str) -> bool {
self.zones.contains(zone)
}
pub fn has_hash(&self, hash: &str) -> bool {
self.hashes.contains(hash)
}
}