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, tensor network utilities,
4//! and constants used across the validator, mining, and extensions crates.
5//!
6//! # Tensor Network (REALMS Part IV)
7//!
8//! This crate implements quantum-inspired tensor network models where:
9//! - Vertices represent quantum subsystems (miners, pools)
10//! - Edges represent entanglement links with bond dimension d
11//! - Entropy S(A) = |γ_A| * log(d) quantifies entanglement
12//! - Mutual information I(A:B) measures region coupling
13//! - Effective distance d_eff recovers geometric structure
14
15pub mod error;
16pub mod types;
17pub mod tensor;
18pub mod math;
19
20pub use error::{TribeError, TribeResult};
21pub use types::*;
22pub use tensor::{
23 constants::*,
24 entropy::{
25 entropy_from_cut,
26 mutual_information,
27 effective_distance,
28 total_network_entropy,
29 approximate_minimal_cut,
30 coherence_probability,
31 },
32};
33pub use math::portable::*;
34
35use serde::{Deserialize, Serialize};
36use sha2::{Digest, Sha256};
37
38/// TribeChain version (from crate version).
39pub const VERSION: &str = env!("CARGO_PKG_VERSION");
40
41/// Block time target in seconds.
42pub const BLOCK_TIME_TARGET: u64 = 60;
43
44/// Maximum tensor dimensions for ESP-compatible challenges.
45pub const ESP_MAX_TENSOR_DIM: usize = 64;
46
47/// Token type identifier on the chain.
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
49pub enum TokenType {
50 /// Native chain token.
51 TribeChain,
52 /// Pumped TRIB€-test Coin (mining rewards).
53 PTtC,
54 /// Numerologic Master Coin.
55 NMTC,
56 /// STOMP token.
57 STOMP,
58 /// AUM token.
59 AUM,
60 /// AI3 token.
61 AI3,
62}
63
64/// Minimal block representation aligned with .AI3 core::Block.
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct Block {
67 /// Block height (genesis = 0).
68 pub height: u64,
69 /// SHA-256 hash of the block header and transactions.
70 pub hash: String,
71 /// Hash of the previous block.
72 pub previous_hash: String,
73 /// Unix timestamp.
74 pub timestamp: u64,
75 /// Proof nonce.
76 pub nonce: u64,
77 /// Mining difficulty target.
78 pub difficulty: u32,
79 /// Miner address or identifier.
80 pub miner: String,
81 /// Transactions included in this block.
82 pub transactions: Vec<Transaction>,
83}
84
85impl Block {
86 /// Builds a new block with computed hash.
87 pub fn new(
88 height: u64,
89 previous_hash: String,
90 transactions: Vec<Transaction>,
91 miner: String,
92 difficulty: u32,
93 ) -> Self {
94 let mut block = Self {
95 height,
96 hash: String::new(),
97 previous_hash,
98 timestamp: chrono::Utc::now().timestamp() as u64,
99 nonce: 0,
100 difficulty,
101 miner,
102 transactions,
103 };
104 block.hash = block.calculate_hash();
105 block
106 }
107
108 /// Computes the block hash from header and transaction hashes.
109 pub fn calculate_hash(&self) -> String {
110 let mut hasher = Sha256::new();
111 hasher.update(self.height.to_le_bytes());
112 hasher.update(self.previous_hash.as_bytes());
113 hasher.update(self.timestamp.to_le_bytes());
114 hasher.update(self.nonce.to_le_bytes());
115 hasher.update(self.difficulty.to_le_bytes());
116 hasher.update(self.miner.as_bytes());
117 for tx in &self.transactions {
118 hasher.update(tx.hash.as_bytes());
119 }
120 hex::encode(hasher.finalize())
121 }
122}
123
124/// A single chain transaction.
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct Transaction {
127 /// Transaction hash.
128 pub hash: String,
129 /// Sender address.
130 pub from: String,
131 /// Recipient address.
132 pub to: String,
133 /// Amount (in smallest unit).
134 pub amount: u64,
135 /// Fee paid.
136 pub fee: u64,
137 /// Unix timestamp.
138 pub timestamp: u64,
139 /// Sender nonce.
140 pub nonce: u64,
141 /// Transaction kind.
142 pub tx_type: TransactionType,
143}
144
145/// Kind of on-chain transaction.
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub enum TransactionType {
148 /// Simple transfer.
149 Transfer,
150 /// Staking operation.
151 Stake,
152 /// PoT-O tensor proof submission.
153 TensorProof,
154 /// Token creation.
155 TokenCreate,
156 /// AMM swap.
157 Swap,
158}