lssa/storage/src/block.rs

40 lines
1005 B
Rust
Raw Normal View History

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;
//ToDo: Add fields to block when model is clear
#[derive(Debug, Serialize, Deserialize)]
pub struct Block {
pub block_id: BlockId,
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,
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,
hash,
transactions: hashable_data.transactions,
data: hashable_data.data,
}
}
}