yedad_common 0.3.3

Shared types, constants, and utilities for Yedad network
Documentation
//! yedad_common
//! تنظیمات هندسه شش‌ضلعی و ساختارهای دقیقاً تراز شده حافظه

use serde::{Deserialize, Serialize};
use thiserror::Error;

pub const ADDRESS_SIZE: usize = 12;
pub const HASH_SIZE: usize = 32;
pub const SLOT_SIZE: usize = 64;
pub const NODE_ACTIVITY_SIZE: usize = 16;
pub const HISTORY_SIZE: usize = 29;

pub const PULSE_INTERVAL_MS: u64 = 1500;
pub const DEFAULT_LADDER_HEIGHT: usize = 100_000;
pub const ROLE_SHUFFLE_PULSES: u64 = 4800;

pub const DIAMOND_REWARD_PERC: u64 = 60;
pub const GOLD_REWARD_PERC: u64 = 30;
pub const REGULAR_REWARD_PERC: u64 = 10;
pub const GOLD_TO_DIAMOND_RATIO: usize = 6;
pub const REGULAR_TO_GOLD_RATIO: usize = 6;

pub const COIN_DECIMALS: u32 = 8;
pub const ONE_YD: u64 = 100_000_000;
pub const MAX_SUPPLY: u64 = 19_000_000 * ONE_YD;
pub const INITIAL_PULSE_REWARD: u64 = 12_500_000;
pub const HALVING_PULSES: u64 = 84_000_000;

pub type Address = [u8; ADDRESS_SIZE];
pub type Hash = [u8; HASH_SIZE];

#[derive(Debug, Clone, PartialEq)]
pub struct Slot {
    pub pulse_id: u32,         // 4 بایت
    pub user_address: Address, // 12 بایت
    pub step_number: u32,      // 4 بایت
    pub ladder_id: u32,        // 4 بایت
    pub balance: u64,          // 8 بایت
    pub last_hash: Hash,       // 32 بایت (منطق Ladder شما)
} // مجموع: 64 بایت

impl Slot {
    pub fn new(user_address: Address, ladder_id: u32, step_number: u32, last_hash: Hash) -> Self {
        Self {
            pulse_id: 1,
            user_address,
            step_number,
            ladder_id,
            balance: 0,
            last_hash,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SlotKey {
    pub user_address: String,
    pub ladder_id: u32,
    pub last_hash: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Transaction {
    pub timestamp: u64,
    pub from: Address,
    pub to: Address,
    pub amount: u64,
    pub proof: Hash,
}

pub struct HistoryRecordRaw {
    pub tx_type: u8, // 1: Send, 0: Receive
    pub peer_address: Address,
    pub amount: u64,
    pub timestamp: u64,
}

#[derive(Error, Debug, Clone, Serialize, Deserialize)]
pub enum YedadError {
    #[error("Account not found")]
    AccountNotFound,
    #[error("Insufficient balance")]
    InsufficientBalance,
    #[error("Invalid proof")]
    InvalidProof,
    #[error("Already exists")]
    AlreadyExists,
    #[error("Ladder exhausted")]
    LadderExhausted,
    #[error("Arithmetic overflow")]
    ArithmeticOverflow,
    #[error("IO error: {0}")]
    Io(String),
}