-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlib.rs
More file actions
210 lines (182 loc) · 6.16 KB
/
lib.rs
File metadata and controls
210 lines (182 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! ZHTP Blockchain Package
//!
//! Core blockchain implementation with zero-knowledge transactions
//! and quantum-resistant consensus integration. Focuses on blockchain
//! fundamentals while delegating specialized functionality to other packages.
// External dependencies
extern crate lib_crypto;
extern crate lib_proofs;
extern crate lib_identity;
extern crate lib_economy;
pub mod types;
pub mod transaction;
pub mod block;
pub mod blockchain;
pub mod mempool;
pub mod integration;
pub mod utils;
pub mod edge_node_state;
pub mod dht_index;
// Smart contracts submodule (feature-gated)
#[cfg(feature = "contracts")]
pub mod contracts;
// Re-export core types for convenience
pub use types::*;
pub use transaction::{*, WalletReference, WalletPrivateData};
pub use block::*;
pub use blockchain::{Blockchain, BlockchainImport, BlockchainBroadcastMessage, EconomicsTransaction, ValidatorInfo};
pub use mempool::*;
pub use utils::*;
pub use dht_index::*;
// Re-export enhanced integrations
pub use integration::enhanced_zk_crypto::{
EnhancedTransactionValidator,
EnhancedTransactionCreator,
EnhancedConsensusValidator,
TransactionSpec,
};
// Re-export economic integration
pub use integration::economic_integration::{
EconomicTransactionProcessor,
TreasuryStats,
create_economic_processor,
create_welfare_funding_transactions,
validate_dao_fee_calculation,
calculate_minimum_blockchain_fee,
convert_economy_amount_to_blockchain,
convert_blockchain_amount_to_economy,
};
// Re-export consensus integration
pub use integration::consensus_integration::{
BlockchainConsensusCoordinator,
ConsensusStatus,
initialize_consensus_integration,
initialize_consensus_integration_with_difficulty_config,
create_dao_proposal_transaction,
create_dao_vote_transaction,
};
// Re-export difficulty types from lib-consensus for convenience
pub use lib_consensus::{DifficultyConfig, DifficultyManager, DifficultyError, DifficultyResult};
// Re-export contracts when feature is enabled
#[cfg(feature = "contracts")]
pub use contracts::*;
/// ZHTP blockchain protocol version
pub const BLOCKCHAIN_VERSION: u32 = 1;
/// Maximum block size in bytes (1MB)
pub const MAX_BLOCK_SIZE: usize = 1_048_576;
/// Target block time in seconds (10 seconds)
pub const TARGET_BLOCK_TIME: u64 = 10;
/// Maximum transactions per block
pub const MAX_TRANSACTIONS_PER_BLOCK: usize = 4096;
/// Genesis block timestamp (January 1, 2022 00:00:00 UTC)
pub const GENESIS_TIMESTAMP: u64 = 1640995200;
/// Initial difficulty for proof of work
pub const INITIAL_DIFFICULTY: u32 = 0x1d00ffff;
/// Difficulty adjustment interval (blocks)
pub const DIFFICULTY_ADJUSTMENT_INTERVAL: u64 = 2016;
/// Target timespan for difficulty adjustment (2 weeks)
pub const TARGET_TIMESPAN: u64 = 14 * 24 * 60 * 60;
/// Maximum nullifier cache size
pub const MAX_NULLIFIER_CACHE: usize = 1_000_000;
/// Maximum UTXO cache size
pub const MAX_UTXO_CACHE: usize = 10_000_000;
/// Genesis block message
pub const GENESIS_MESSAGE: &[u8] = b"In the beginning was the Word, and the Word was ZHTP";
/// Get blockchain health information for monitoring
pub fn get_blockchain_health() -> Result<BlockchainHealth, String> {
Ok(BlockchainHealth {
is_synced: true,
current_height: 12345,
peer_count: 8,
mempool_size: 42,
last_block_time: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
difficulty: INITIAL_DIFFICULTY,
network_hashrate: 1000000, // Example hashrate
})
}
/// Get comprehensive blockchain information
pub fn get_blockchain_info() -> Result<BlockchainInfo, String> {
Ok(BlockchainInfo {
version: BLOCKCHAIN_VERSION,
protocol_version: 1,
blocks: 12345,
timeoffset: 0,
connections: 8,
proxy: None,
difficulty: INITIAL_DIFFICULTY as f64,
testnet: false,
keypoololdest: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
keypoolsize: 100,
paytxfee: 0.0001,
mininput: 0.0001,
errors: None,
})
}
/// Get current blockchain height asynchronously
pub async fn get_current_block_height() -> Result<u64, String> {
// In production, this would query the actual blockchain state
Ok(12345)
}
/// Get treasury balance for economic calculations
pub fn get_treasury_balance() -> Result<u64, String> {
// Return a default treasury balance
// In production, this would query the actual treasury state
Ok(1_000_000_000) // 1 billion tokens
}
/// Blockchain health status structure
#[derive(Debug, Clone)]
pub struct BlockchainHealth {
/// Whether the blockchain is fully synced
pub is_synced: bool,
/// Current blockchain height
pub current_height: u64,
/// Number of connected peers
pub peer_count: u32,
/// Number of transactions in mempool
pub mempool_size: u32,
/// Timestamp of last block
pub last_block_time: u64,
/// Current network difficulty
pub difficulty: u32,
/// Network hash rate estimate
pub network_hashrate: u64,
}
/// Comprehensive blockchain information structure
#[derive(Debug, Clone)]
pub struct BlockchainInfo {
/// Blockchain software version
pub version: u32,
/// Protocol version
pub protocol_version: u32,
/// Current block count
pub blocks: u64,
/// Time offset from system clock
pub timeoffset: i64,
/// Number of peer connections
pub connections: u32,
/// Proxy configuration
pub proxy: Option<String>,
/// Current network difficulty
pub difficulty: f64,
/// Whether running on testnet
pub testnet: bool,
/// Oldest key in keypool
pub keypoololdest: u64,
/// Size of keypool
pub keypoolsize: u32,
/// Transaction fee per byte
pub paytxfee: f64,
/// Minimum input value
pub mininput: f64,
/// Any blockchain errors
pub errors: Option<String>,
}
// NOTE: Shared blockchain provider has been removed.
// Use zhtp::runtime::blockchain_provider::get_global_blockchain() instead.
// This provides better control over blockchain initialization and lifecycle.