acme_chains/actors/validators/
mod.rs1pub use utils::*;
10
11mod utils {
12 use crate::{calculate_block_hash, convert_hash_into_binary, Block, DIFFICULTY_PREFIX};
13
14 pub fn determine_block_validity(block: &Block, pblock: &Block) -> bool {
15 if block.previous != pblock.hash {
16 log::warn!("block with id: {} has wrong previous hash", block.id);
17 return false;
18 } else if !convert_hash_into_binary(
19 &hex::decode(&block.hash).expect("Decoding Error: failed to decode the BlockHash"),
20 )
21 .starts_with(DIFFICULTY_PREFIX.as_ref())
22 {
23 log::warn!("block with id: {} has invalid difficulty", block.id);
24 return false;
25 } else if block.id != pblock.id + 1 {
26 log::warn!(
27 "block with id: {} is not the next block after the latest: {}",
28 block.id,
29 pblock.id
30 );
31 return false;
32 } else if hex::encode(calculate_block_hash(
33 block.id,
34 block.data.clone(),
35 block.nonce,
36 block.previous.clone(),
37 block.timestamp,
38 )) != block.hash
39 {
40 log::warn!("block with id: {} has invalid hash", block.id);
41 return false;
42 }
43 true
44 }
45
46 pub fn determine_chain_validity(chain: &[Block]) -> bool {
47 for i in 0..chain.len() {
48 if i == 0 {
49 continue;
50 }
51 let first = chain.get(i - 1).expect("has to exist");
52 let second = chain.get(i).expect("has to exist");
53 if !determine_block_validity(second, first) {
54 return false;
55 }
56 }
57 true
58 }
59}