2
0
mirror of https://github.com/Revertron/Alfis synced 2024-11-15 06:12:52 +00:00

Added more checks to resolve current forked state.

This commit is contained in:
Revertron 2021-06-12 12:24:21 +02:00
parent 8facb69c3f
commit 6101c403d4
3 changed files with 32 additions and 6 deletions

2
Cargo.lock generated
View File

@ -65,7 +65,7 @@ dependencies = [
[[package]] [[package]]
name = "alfis" name = "alfis"
version = "0.6.3" version = "0.6.4"
dependencies = [ dependencies = [
"base64", "base64",
"bincode", "bincode",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "alfis" name = "alfis"
version = "0.6.3" version = "0.6.4"
authors = ["Revertron <alfis@revertron.com>"] authors = ["Revertron <alfis@revertron.com>"]
edition = "2018" edition = "2018"
build = "build.rs" build = "build.rs"

View File

@ -9,6 +9,7 @@ use chrono::Utc;
#[allow(unused_imports)] #[allow(unused_imports)]
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use sqlite::{Connection, State, Statement}; use sqlite::{Connection, State, Statement};
use lazy_static::lazy_static;
use crate::blockchain::hash_utils::*; use crate::blockchain::hash_utils::*;
use crate::blockchain::transaction::DomainData; use crate::blockchain::transaction::DomainData;
@ -18,7 +19,7 @@ use crate::blockchain::types::{BlockQuality, MineResult, Options, ZoneData};
use crate::commons::constants::*; use crate::commons::constants::*;
use crate::keystore::check_public_key_strength; use crate::keystore::check_public_key_strength;
use crate::settings::Settings; use crate::settings::Settings;
use crate::{check_domain, get_domain_zone, is_yggdrasil_record, Block, Bytes, Keystore, Transaction}; use crate::{check_domain, get_domain_zone, is_yggdrasil_record, Block, Bytes, Keystore, Transaction, from_hex};
use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
const TEMP_DB_NAME: &str = ":memory:"; const TEMP_DB_NAME: &str = ":memory:";
@ -39,10 +40,17 @@ const SQL_GET_DOMAIN_BY_ID: &str = "SELECT * FROM domains WHERE identity = ? ORD
const SQL_GET_DOMAINS_BY_KEY: &str = "SELECT * FROM domains WHERE signing = ? ORDER BY id;"; const SQL_GET_DOMAINS_BY_KEY: &str = "SELECT * FROM domains WHERE signing = ? ORDER BY id;";
const SQL_GET_DOMAINS_COUNT: &str = "SELECT count(DISTINCT identity) FROM domains;"; const SQL_GET_DOMAINS_COUNT: &str = "SELECT count(DISTINCT identity) FROM domains;";
const SQL_GET_USERS_COUNT: &str = "SELECT count(DISTINCT pub_key) FROM blocks;"; const SQL_GET_USERS_COUNT: &str = "SELECT count(DISTINCT pub_key) FROM blocks;";
const SQL_GET_USER_BLOCK_COUNT: &str = "SELECT count(pub_key) FROM blocks WHERE pub_key = ?"; const SQL_GET_USER_BLOCK_COUNT: &str = "SELECT count(pub_key) FROM blocks WHERE pub_key = ? AND id < ?";
const SQL_GET_OPTIONS: &str = "SELECT * FROM options;"; const SQL_GET_OPTIONS: &str = "SELECT * FROM options;";
lazy_static! {
static ref WRONG_HASHES: Vec<Bytes> = vec![
Bytes::from_bytes(&from_hex("5B2D63CD8BD854B23A34A49AD850BF520BDD8D4514F9B20A3DF01430A59F0000").unwrap()),
Bytes::from_bytes(&from_hex("4448E0582878FCB982C0DDAFEB441A03A30FB62FA6ECD1EA4D51C29A30980000").unwrap())
];
}
/// Max possible block index /// Max possible block index
const MAX: u64 = i64::MAX as u64; const MAX: u64 = i64::MAX as u64;
@ -128,6 +136,19 @@ impl Chain {
continue; continue;
} }
if WRONG_HASHES.contains(&block.hash) {
error!("Block {} has bad hash:\n{:?}", block.index, &block);
info!("Truncating database from block {}...", block.index);
match self.truncate_db_from_block(block.index) {
Ok(_) => {}
Err(e) => {
error!("{}", e);
panic!("Error truncating database! Please, delete 'blockchain.db' and restart.");
}
}
break;
}
//let last = self.last_block.clone().unwrap(); //let last = self.last_block.clone().unwrap();
if self.check_block(&block, &last_block, &last_full_block) != BlockQuality::Good { if self.check_block(&block, &last_block, &last_full_block) != BlockQuality::Good {
error!("Block {} is bad:\n{:?}", block.index, &block); error!("Block {} is bad:\n{:?}", block.index, &block);
@ -628,9 +649,10 @@ impl Chain {
0 0
} }
pub fn get_user_block_count(&self, pub_key: &Bytes) -> i64 { pub fn get_user_block_count(&self, pub_key: &Bytes, max_height: u64) -> i64 {
let mut statement = self.db.prepare(SQL_GET_USER_BLOCK_COUNT).unwrap(); let mut statement = self.db.prepare(SQL_GET_USER_BLOCK_COUNT).unwrap();
statement.bind(1, pub_key.as_slice()).expect("Error in bind"); statement.bind(1, pub_key.as_slice()).expect("Error in bind");
statement.bind(2, max_height as i64).expect("Error in bind");
if let State::Row = statement.next().unwrap() { if let State::Row = statement.next().unwrap() {
return statement.read::<i64>(0).unwrap(); return statement.read::<i64>(0).unwrap();
} }
@ -736,6 +758,10 @@ impl Chain {
warn!("Ignoring block from unsupported version:\n{:?}", &block); warn!("Ignoring block from unsupported version:\n{:?}", &block);
return Bad; return Bad;
} }
if WRONG_HASHES.contains(&block.hash) {
warn!("Got block with hash from wrong hashes.");
return Bad;
}
let timestamp = Utc::now().timestamp(); let timestamp = Utc::now().timestamp();
if block.timestamp > timestamp + 60 { if block.timestamp > timestamp + 60 {
warn!("Ignoring block from the future:\n{:?}", &block); warn!("Ignoring block from the future:\n{:?}", &block);
@ -974,7 +1000,7 @@ impl Chain {
while set.len() < BLOCK_SIGNERS_ALL as usize { while set.len() < BLOCK_SIGNERS_ALL as usize {
let index = (tail.wrapping_mul(count) % window) + 1; // We want it to start from 1 let index = (tail.wrapping_mul(count) % window) + 1; // We want it to start from 1
if let Some(b) = self.get_block(index) { if let Some(b) = self.get_block(index) {
let block_count = self.get_user_block_count(&b.pub_key); let block_count = self.get_user_block_count(&b.pub_key, block.index);
if block_count < minimum_block_count { if block_count < minimum_block_count {
//debug!("Skipping public key {:?} from block {}, it has too little {} blocks", &b.pub_key, index, block_count); //debug!("Skipping public key {:?} from block {}, it has too little {} blocks", &b.pub_key, index, block_count);
count += 1; count += 1;