use serde::{Serialize};
use super::*;
use crate::transaction::Transaction;
use std::{
cell::{Cell},
fmt::{self, Debug, Formatter},
rc::Rc,
};
#[derive(Serialize)]
pub struct Block {
pub index: u32,
pub timestamp: u128,
pub hash: Hash,
pub prev_block_hash: Hash,
pub nonce: u64,
pub transactions: Vec<Transaction> ,
pub difficulty: u128,
}
impl Debug for Block {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Block[{}]: {} at: {} with: {} trx, nonce: {}",
&self.index,
&hex::encode(&self.hash),
&self.timestamp,
&self.transactions.len(),
&self.nonce,
)
}
}
impl Block {
pub fn new(
index: u32,
timestamp: u128,
prev_block_hash: Hash,
transactions: &mut Rc<Cell<Vec<Transaction>>>,
difficulty: u128,
) -> Self {
Block {
index,
timestamp,
hash: vec![0; 32],
prev_block_hash,
nonce: 0,
transactions: transactions.take().to_vec(),
difficulty,
}
}
pub fn blockchain_mine(&mut self) -> Result<Hash, CustomError> {
for nonce_attempt in 0..(u64::max_value()) {
self.nonce = nonce_attempt;
let hash = self.hash();
if blockchain_check_difficulty(&hash, self.difficulty) {
self.hash = hash;
return Ok(self.hash.clone())
}
}
return Err(CustomError::BlockValidation(BlockValidationError::NonceAttemptFailed));
}
}
impl Hashable for Block {
fn bytes(&self) -> Vec<u8> {
let mut bytes = vec![];
bytes.extend(&lib_block_u32_bytes(&self.index));
bytes.extend(&lib_block_u128_bytes(&self.timestamp));
bytes.extend(&self.prev_block_hash);
bytes.extend(&lib_block_u64_bytes(&self.nonce));
bytes.extend(
self.transactions
.iter()
.flat_map(|transaction| transaction.bytes())
.collect::<Vec<u8>>(),
);
bytes.extend(&lib_block_u128_bytes(&self.difficulty));
bytes
}
}
pub fn blockchain_check_difficulty(hash: &Hash, difficulty: u128) -> bool {
let result = lib_block_difficulty_bytes_as_u128(&hash);
difficulty > result
}