Skip to main content

pot_o_core/
lib.rs

1//! Core types and utilities for PoT-O (Proof of Tensor Optimizations).
2//!
3//! Provides block and transaction types, error handling, and constants used across
4//! the validator, mining, and extensions crates.
5
6pub mod error;
7pub mod token_config;
8
9pub use error::{TribeError, TribeResult};
10
11use serde::{Deserialize, Serialize};
12use sha2::{Digest, Sha256};
13
14/// TribeChain version (from crate version).
15pub const VERSION: &str = env!("CARGO_PKG_VERSION");
16
17/// Block time target in seconds.
18pub const BLOCK_TIME_TARGET: u64 = 60;
19
20/// Maximum tensor dimensions for ESP-compatible challenges.
21pub const ESP_MAX_TENSOR_DIM: usize = 64;
22
23/// Token type identifier on the chain.
24#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
25pub enum TokenType {
26    /// Native chain token.
27    TribeChain,
28    /// Pumped TRIB€-test Coin (mining rewards).
29    PTtC,
30    /// Numerologic Master Coin.
31    NMTC,
32    /// STOMP token.
33    STOMP,
34    /// AUM token.
35    AUM,
36    /// AI3 token.
37    AI3,
38    /// RAVECOIN token.
39    RAVECOIN,
40}
41
42/// Minimal block representation aligned with .AI3 core::Block.
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct Block {
45    /// Block height (genesis = 0).
46    pub height: u64,
47    /// SHA-256 hash of the block header and transactions.
48    pub hash: String,
49    /// Hash of the previous block.
50    pub previous_hash: String,
51    /// Unix timestamp.
52    pub timestamp: u64,
53    /// Proof nonce.
54    pub nonce: u64,
55    /// Mining difficulty target.
56    pub difficulty: u32,
57    /// Miner address or identifier.
58    pub miner: String,
59    /// Transactions included in this block.
60    pub transactions: Vec<Transaction>,
61}
62
63impl Block {
64    /// Builds a new block with computed hash.
65    pub fn new(
66        height: u64,
67        previous_hash: String,
68        transactions: Vec<Transaction>,
69        miner: String,
70        difficulty: u32,
71    ) -> Self {
72        let mut block = Self {
73            height,
74            hash: String::new(),
75            previous_hash,
76            timestamp: chrono::Utc::now().timestamp() as u64,
77            nonce: 0,
78            difficulty,
79            miner,
80            transactions,
81        };
82        block.hash = block.calculate_hash();
83        block
84    }
85
86    /// Computes the block hash from header and transaction hashes.
87    pub fn calculate_hash(&self) -> String {
88        let mut hasher = Sha256::new();
89        hasher.update(self.height.to_le_bytes());
90        hasher.update(self.previous_hash.as_bytes());
91        hasher.update(self.timestamp.to_le_bytes());
92        hasher.update(self.nonce.to_le_bytes());
93        hasher.update(self.difficulty.to_le_bytes());
94        hasher.update(self.miner.as_bytes());
95        for tx in &self.transactions {
96            hasher.update(tx.hash.as_bytes());
97        }
98        hex::encode(hasher.finalize())
99    }
100}
101
102/// A single chain transaction.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct Transaction {
105    /// Transaction hash.
106    pub hash: String,
107    /// Sender address.
108    pub from: String,
109    /// Recipient address.
110    pub to: String,
111    /// Amount (in smallest unit).
112    pub amount: u64,
113    /// Fee paid.
114    pub fee: u64,
115    /// Unix timestamp.
116    pub timestamp: u64,
117    /// Sender nonce.
118    pub nonce: u64,
119    /// Transaction kind.
120    pub tx_type: TransactionType,
121}
122
123/// Kind of on-chain transaction.
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub enum TransactionType {
126    /// Simple transfer.
127    Transfer,
128    /// Staking operation.
129    Stake,
130    /// PoT-O tensor proof submission.
131    TensorProof,
132    /// Token creation.
133    TokenCreate,
134    /// AMM swap.
135    Swap,
136}