1pub mod error;
7pub mod token_config;
8
9pub use error::{TribeError, TribeResult};
10
11use serde::{Deserialize, Serialize};
12use sha2::{Digest, Sha256};
13
14pub const VERSION: &str = env!("CARGO_PKG_VERSION");
16
17pub const BLOCK_TIME_TARGET: u64 = 60;
19
20pub const ESP_MAX_TENSOR_DIM: usize = 64;
22
23#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
25pub enum TokenType {
26 TribeChain,
28 PTtC,
30 NMTC,
32 STOMP,
34 AUM,
36 AI3,
38 RAVECOIN,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct Block {
45 pub height: u64,
47 pub hash: String,
49 pub previous_hash: String,
51 pub timestamp: u64,
53 pub nonce: u64,
55 pub difficulty: u32,
57 pub miner: String,
59 pub transactions: Vec<Transaction>,
61}
62
63impl Block {
64 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 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#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct Transaction {
105 pub hash: String,
107 pub from: String,
109 pub to: String,
111 pub amount: u64,
113 pub fee: u64,
115 pub timestamp: u64,
117 pub nonce: u64,
119 pub tx_type: TransactionType,
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize)]
125pub enum TransactionType {
126 Transfer,
128 Stake,
130 TensorProof,
132 TokenCreate,
134 Swap,
136}