2024-11-25 07:26:16 +02:00
|
|
|
use rs_merkle::Hasher;
|
2024-10-10 14:09:31 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2024-11-25 07:26:16 +02:00
|
|
|
use crate::{merkle_tree_public::hasher::OwnHasher, transaction::Transaction};
|
2024-10-10 14:09:31 +03:00
|
|
|
|
|
|
|
|
pub type BlockHash = [u8; 32];
|
|
|
|
|
pub type Data = Vec<u8>;
|
|
|
|
|
pub type BlockId = u64;
|
|
|
|
|
|
2024-12-09 03:58:07 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2024-10-10 14:09:31 +03:00
|
|
|
pub struct Block {
|
|
|
|
|
pub block_id: BlockId,
|
2024-11-29 12:28:08 +02:00
|
|
|
pub prev_block_id: BlockId,
|
|
|
|
|
pub prev_block_hash: BlockHash,
|
2024-10-10 14:09:31 +03:00
|
|
|
pub hash: BlockHash,
|
|
|
|
|
pub transactions: Vec<Transaction>,
|
|
|
|
|
pub data: Data,
|
|
|
|
|
}
|
2024-11-25 07:26:16 +02:00
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct HashableBlockData {
|
|
|
|
|
pub block_id: BlockId,
|
2024-11-29 12:28:08 +02:00
|
|
|
pub prev_block_id: BlockId,
|
|
|
|
|
pub prev_block_hash: BlockHash,
|
2024-11-25 07:26:16 +02:00
|
|
|
pub transactions: Vec<Transaction>,
|
|
|
|
|
pub data: Data,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Block {
|
|
|
|
|
pub fn produce_block_from_hashable_data(hashable_data: HashableBlockData) -> Self {
|
|
|
|
|
let data = serde_json::to_vec(&hashable_data).unwrap();
|
|
|
|
|
|
|
|
|
|
let hash = OwnHasher::hash(&data);
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
block_id: hashable_data.block_id,
|
2024-11-29 12:28:08 +02:00
|
|
|
prev_block_id: hashable_data.prev_block_id,
|
2024-11-25 07:26:16 +02:00
|
|
|
hash,
|
|
|
|
|
transactions: hashable_data.transactions,
|
|
|
|
|
data: hashable_data.data,
|
2024-11-29 12:28:08 +02:00
|
|
|
prev_block_hash: hashable_data.prev_block_hash,
|
2024-11-25 07:26:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|