-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalloonCoinDaemon
371 lines (325 loc) · 13.2 KB
/
BalloonCoinDaemon
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Instant, Duration};
use std::collections::HashMap;
use sha2::{Sha256, Digest};
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
// Define the maximum supply of coins
const MAX_SUPPLY: u64 = 21_000_000;
// Define the block reward for each block
const BLOCK_REWARD: u64 = 50;
// Define the halving interval in blocks
const HALVING_INTERVAL: u64 = 210_000;
// Define the target block time in seconds
const BLOCK_TIME: u64 = 60;
// Define the maximum number of transactions per block
const MAX_TRANSACTIONS_PER_BLOCK: usize = 100;
// Define the maximum number of connections per node
const MAX_CONNECTIONS: usize = 10;
// Define the maximum number of unconfirmed transactions in the pool
const MAX_UNCONFIRMED_TRANSACTIONS: usize = 1000;
// Define the port number to listen for incoming connections
const LISTEN_PORT: u16 = 8333;
// Define the version number of the protocol
const PROTOCOL_VERSION: u32 = 70015;
// Define the magic bytes for the Ballooncoin network
const MAGIC_BYTES: [u8; 4] = [0xFA, 0xBF, 0xB5, 0xDA];
// Define the struct for a Ballooncoin transaction
#[derive(Clone, Serialize, Deserialize)]
struct Transaction {
inputs: Vec<Input>,
outputs: Vec<Output>,
timestamp: DateTime<Utc>,
signature: Vec<u8>,
}
// Define the struct for a Ballooncoin transaction input
#[derive(Clone, Serialize, Deserialize)]
struct Input {
transaction_hash: Vec<u8>,
output_index: usize,
signature: Vec<u8>,
}
// Define the struct for a Ballooncoin transaction output
#[derive(Clone, Serialize, Deserialize)]
struct Output {
address: Vec<u8>,
value: u64,
}
// Define the struct for a Ballooncoin block header
#[derive(Clone, Serialize, Deserialize)]
struct BlockHeader {
version: u32,
prev_block_hash: Vec<u8>,
merkle_root: Vec<u8>,
timestamp: u32,
nonce: u32,
}
// Define the struct for a Ballooncoin block
#[derive(Clone, Serialize, Deserialize)]
struct Block {
header: BlockHeader,
transactions: Vec<Transaction>,
}
// Define the struct for a Ballooncoin node
struct Node {
address: String,
peers: Vec<String>,
mempool: Arc<Mutex<HashMap<Vec<u8>, Transaction>>>,
blockchain: Arc<Mutex<Vec<Block>>>,
}
impl Node {
// Start the node and listen for incoming connections
fn start(&self) {
// Spawn a new thread to handle incoming connections
let mempool = self.mempool.clone();
let blockchain = self.blockchain.clone();
let listen_thread = thread::spawn(move || {
// TODO: implement incoming connection handling
});
// Spawn a new thread to mine new blocks
let mining_thread = self.start_mining();
// Wait for both threads to exit
listen_thread.join().unwrap();
mining_thread.join().unwrap();
}
// Start mining new blocks
fn start_mining(&self) -> thread::JoinHandle<()> {
let mempool = self.mempool.clone();
let blockchain = self.block
let block_time = Duration::from_secs(BLOCK_TIME);
let mining_thread = thread::spawn(move || {
let mut rng = rand::thread_rng();
let mut nonce = rng.gen_range(0, u32::MAX);
let mut last_block_time = Instant::now();
loop {
// Check if the block time has elapsed
if Instant::now().duration_since(last_block_time) >= block_time {
// Generate a new block if there are any transactions in the mempool
let mut mempool = mempool.lock().unwrap();
let mut blockchain = blockchain.lock().unwrap();
if !mempool.is_empty() {
// Create a new block and add the coinbase transaction
let mut block = Block {
header: BlockHeader {
version: PROTOCOL_VERSION,
prev_block_hash: blockchain.last().unwrap().hash(),
merkle_root: vec![],
timestamp: Utc::now().timestamp() as u32,
nonce: nonce,
},
transactions: vec![],
};
let coinbase = Transaction {
inputs: vec![],
outputs: vec![Output {
address: vec![],
value: BLOCK_REWARD,
}],
timestamp: Utc::now(),
signature: vec![],
};
block.transactions.push(coinbase);
// Add as many transactions from the mempool as possible
let mut transaction_count = 0;
for (_, transaction) in mempool.iter() {
if transaction_count >= MAX_TRANSACTIONS_PER_BLOCK {
break;
}
if self.validate_transaction(&transaction) {
block.transactions.push(transaction.clone());
transaction_count += 1;
}
}
// Update the merkle root and hash the block header
block.header.merkle_root = self.calculate_merkle_root(&block.transactions);
let block_hash = self.hash_block_header(&block.header);
// Check if the block meets the difficulty target
if self.check_difficulty(&block_hash) {
// Add the block to the blockchain and remove the transactions from the mempool
blockchain.push(block);
for transaction in block.transactions.iter().skip(1) {
mempool.remove(&self.hash_transaction(&transaction));
}
// Update the nonce and reset the last block time
nonce = rng.gen_range(0, u32::MAX);
last_block_time = Instant::now();
// Check if a halving event has occurred
if blockchain.len() as u64 % HALVING_INTERVAL == 0 {
// Calculate the new block reward and check if it's below the minimum
let block_reward = BLOCK_REWARD >> (blockchain.len() / HALVING_INTERVAL);
if block_reward > 0 {
// Add the coinbase output for the new block reward
let coinbase_output = Output {
address: vec![],
value: block_reward,
};
blockchain.last_mut().unwrap().transactions[0].outputs.push(coinbase_output);
}
}
// Check if the maximum supply has been reached
if blockchain.len() as u64 * BLOCK_REWARD > MAX_SUPPLY {
break;
}
}
}
}
}
});
mining_thread
}
// Calculate the merkle root of a list of transactions
fn calculate_merkle_root(&self, transactions: &Vec<Transaction>) -> Vec<u8> {
let mut hashes = transactions.iter().map(|transaction| self.hash_transaction(&transaction)).collect::<Vec<_>>();
while hashes.len() > 1 {
if hashes.len() % 2 == 1 {
hashes.push(hashes.last
.unwrap()
.clone();
}
let mut new_hashes = Vec::new();
for chunk in hashes.chunks(2) {
let mut hasher = Sha256::new();
let (left, right) = (chunk[0].clone(), chunk[1].clone());
hasher.update(&left);
hasher.update(&right);
new_hashes.push(hasher.finalize().to_vec());
}
hashes = new_hashes;
}
hashes[0].clone()
}
// Calculate the hash of a transaction
fn hash_transaction(&self, transaction: &Transaction) -> Vec<u8> {
let mut hasher = Sha256::new();
hasher.update(&bincode::serialize(&transaction).unwrap());
hasher.finalize().to_vec()
}
// Hash the header of a block
fn hash_block_header(&self, header: &BlockHeader) -> Vec<u8> {
let mut hasher = Sha256::new();
hasher.update(&bincode::serialize(&header).unwrap());
hasher.finalize().to_vec()
}
// Check if a block hash meets the difficulty target
fn check_difficulty(&self, hash: &Vec<u8>) -> bool {
let target = self.difficulty_target();
let hash_int = BigUint::from_bytes_be(hash);
let target_int = BigUint::from_bytes_be(&target);
hash_int <= target_int
}
// Get the difficulty target for the current block height
fn difficulty_target(&self) -> Vec<u8> {
let blockchain = self.blockchain.lock().unwrap();
let last_block = blockchain.last().unwrap();
let last_difficulty = &last_block.header.difficulty;
// Calculate the new difficulty based on the last block and the block time
let time_difference = last_block.timestamp - blockchain[blockchain.len() - BLOCK_TIME_INTERVAL as usize].timestamp;
let expected_time_difference = BLOCK_TIME * BLOCK_TIME_INTERVAL;
let mut new_difficulty = last_difficulty.clone();
if time_difference > expected_time_difference {
let adjustment = last_difficulty * BigUint::from(expected_time_difference) / BigUint::from(time_difference);
new_difficulty = last_difficulty + adjustment;
} else if time_difference < expected_time_difference {
let adjustment = last_difficulty * BigUint::from(time_difference) / BigUint::from(expected_time_difference);
new_difficulty = last_difficulty - adjustment;
}
// Make sure the new difficulty is within the acceptable range
let max_difficulty = BigUint::from_bytes_be(&[0xff; 32]);
if new_difficulty > max_difficulty {
new_difficulty = max_difficulty;
} else if new_difficulty.is_zero() {
new_difficulty = BigUint::from(1u32);
}
new_difficulty.to_bytes_be()
}
}
fn main() {
// Create the blockchain, mempool, and node
let blockchain = Arc::new(Mutex::new(vec![Block::genesis()]));
let mempool = Arc::new(Mutex::new(HashMap::new()));
let node = Node::new(blockchain.clone(), mempool.clone());
// Start the node and mining threads
node.start();
let mining_thread = node.start_mining();
// Listen for incoming connections
let listener = TcpListener::bind("0.0.0.0:8333").unwrap();
for stream in listener.incoming() {
let mut node_clone = node.clone();
thread::spawn(move || {
let mut stream = stream.unwrap();
loop {
let message = read_message
use std::collections::HashMap;
// Define the blockchain struct
pub struct Blockchain {
pub chain: Vec<Block>,
pub current_transactions: Vec<Transaction>,
pub nodes: HashSet<String>,
pub difficulty: u32,
pub max_coinbase: u64,
pub total_coinbase: u64,
pub halving_period: u32,
}
impl Blockchain {
// Implement the mining method
pub fn mine_block(&mut self, miner_address: String) -> Block {
let previous_hash = self.last_block().hash();
let mut nonce = 0;
loop {
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let block = Block::new(
timestamp,
self.current_transactions.clone(),
self.difficulty,
previous_hash.clone(),
nonce,
);
if block.hash().starts_with(&"0".repeat(self.difficulty as usize)) {
let mut coinbase = Transaction {
sender: String::from("COINBASE"),
recipient: miner_address.clone(),
amount: self.calculate_coinbase(),
};
coinbase.sign(&String::from("COINBASE"));
self.current_transactions.push(coinbase);
self.chain.push(block.clone());
self.total_coinbase += block.transactions[0].amount;
if self.total_coinbase >= self.max_coinbase {
panic!("Maximum coinbase reached");
}
return block;
}
nonce += 1;
}
}
// Implement the calculate coinbase method
fn calculate_coinbase(&self) -> u64 {
let total_blocks = self.chain.len() as u32;
let halvings = (total_blocks / self.halving_period) as u64;
let coinbase = 50u64 * (10u64).pow(8) / (2u64).pow(halvings);
coinbase
}
}
fn main() {
let mut blockchain = Blockchain {
chain: vec![Block::genesis()],
current_transactions: vec![],
nodes: HashSet::new(),
difficulty: 2,
max_coinbase: 21_000_000_0000,
total_coinbase: 0,
halving_period: 210_000,
};
// Add nodes to the blockchain
blockchain.register_node(String::from("http://localhost:5000"));
blockchain.register_node(String::from("http://localhost:5001"));
// Mine a block
let block = blockchain.mine_block(String::from("miner_address"));
println!("Block mined: {:?}", block);
// Print the blockchain
println!("Blockchain: {:?}", blockchain.chain);
}