use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Transaction {
pub from: String,
pub kind: String,
pub payload: String,
}
impl Transaction {
pub fn new(
from: impl Into<String>,
kind: impl Into<String>,
payload: impl Into<String>,
) -> Self {
Self {
from: from.into(),
kind: kind.into(),
payload: payload.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
pub index: u64,
pub timestamp: u64,
pub prev_hash: String,
pub beacon: String,
pub transactions: Vec<Transaction>,
pub proposer_id: String,
pub proposer_pubkey: String,
pub hash: String,
pub signature: String,
}
pub fn block_digest(
index: u64,
timestamp: u64,
prev_hash: &str,
beacon: &str,
transactions: &[Transaction],
proposer_id: &str,
) -> [u8; 32] {
let mut hasher = blake3::Hasher::new();
hasher.update(&index.to_be_bytes());
hasher.update(×tamp.to_be_bytes());
hasher.update(prev_hash.as_bytes());
hasher.update(beacon.as_bytes());
hasher.update(proposer_id.as_bytes());
let tx_bytes = serde_json::to_vec(transactions).expect("transactions serialize");
hasher.update(&tx_bytes);
*hasher.finalize().as_bytes()
}