Skip to content

Commit

Permalink
Added more checks to resolve current forked state.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revertron committed Jun 12, 2021
1 parent 8facb69 commit 6101c40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "alfis"
version = "0.6.3"
version = "0.6.4"
authors = ["Revertron <[email protected]>"]
edition = "2018"
build = "build.rs"
Expand Down
34 changes: 30 additions & 4 deletions src/blockchain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use chrono::Utc;
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use sqlite::{Connection, State, Statement};
use lazy_static::lazy_static;

use crate::blockchain::hash_utils::*;
use crate::blockchain::transaction::DomainData;
Expand All @@ -18,7 +19,7 @@ use crate::blockchain::types::{BlockQuality, MineResult, Options, ZoneData};
use crate::commons::constants::*;
use crate::keystore::check_public_key_strength;
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;

const TEMP_DB_NAME: &str = ":memory:";
Expand All @@ -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_COUNT: &str = "SELECT count(DISTINCT identity) FROM domains;";
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;";

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
const MAX: u64 = i64::MAX as u64;

Expand Down Expand Up @@ -128,6 +136,19 @@ impl Chain {
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();
if self.check_block(&block, &last_block, &last_full_block) != BlockQuality::Good {
error!("Block {} is bad:\n{:?}", block.index, &block);
Expand Down Expand Up @@ -628,9 +649,10 @@ impl Chain {
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();
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() {
return statement.read::<i64>(0).unwrap();
}
Expand Down Expand Up @@ -736,6 +758,10 @@ impl Chain {
warn!("Ignoring block from unsupported version:\n{:?}", &block);
return Bad;
}
if WRONG_HASHES.contains(&block.hash) {
warn!("Got block with hash from wrong hashes.");
return Bad;
}
let timestamp = Utc::now().timestamp();
if block.timestamp > timestamp + 60 {
warn!("Ignoring block from the future:\n{:?}", &block);
Expand Down Expand Up @@ -974,7 +1000,7 @@ impl Chain {
while set.len() < BLOCK_SIGNERS_ALL as usize {
let index = (tail.wrapping_mul(count) % window) + 1; // We want it to start from 1
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 {
//debug!("Skipping public key {:?} from block {}, it has too little {} blocks", &b.pub_key, index, block_count);
count += 1;
Expand Down

0 comments on commit 6101c40

Please sign in to comment.