|
| 1 | +use blockchaintree::block::Block as _; |
| 2 | +use blockchaintree::tools; |
| 3 | +use blockchaintree::{blockchaintree::BlockChainTree, static_values}; |
| 4 | +use primitive_types::U256; |
| 5 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 9 | + |
| 10 | + let mut tree = BlockChainTree::new().unwrap(); |
| 11 | + |
| 12 | + let wallet = [1u8; 33]; |
| 13 | + |
| 14 | + let chain = tree.get_derivative_chain(&wallet).unwrap(); |
| 15 | + |
| 16 | + loop { |
| 17 | + println!("Current height: {}", chain.get_height()); |
| 18 | + println!( |
| 19 | + "Current miner gas amount: {}", |
| 20 | + tree.get_gas(&wallet).unwrap() |
| 21 | + ); |
| 22 | + let mut nonce = U256::zero(); |
| 23 | + let (prev_hash, difficulty, _prev_timestamp, _height) = |
| 24 | + if let Some(block) = chain.get_last_block().unwrap() { |
| 25 | + ( |
| 26 | + block.hash().unwrap(), |
| 27 | + block.get_info().difficulty, |
| 28 | + block.get_info().timestamp, |
| 29 | + block.get_info().height, |
| 30 | + ) |
| 31 | + } else { |
| 32 | + let block = tree |
| 33 | + .get_main_chain() |
| 34 | + .find_by_hash(&chain.genesis_hash) |
| 35 | + .unwrap() |
| 36 | + .unwrap(); |
| 37 | + ( |
| 38 | + block.hash().unwrap(), |
| 39 | + static_values::BEGINNING_DIFFICULTY, |
| 40 | + block.get_info().timestamp, |
| 41 | + U256::zero(), |
| 42 | + ) |
| 43 | + }; |
| 44 | + println!( |
| 45 | + "Current difficulty: {}", |
| 46 | + tools::count_leading_zeros(&difficulty) |
| 47 | + ); |
| 48 | + while nonce < U256::MAX { |
| 49 | + let mut pow = [0u8; 32]; |
| 50 | + nonce.to_big_endian(&mut pow); |
| 51 | + if tools::check_pow(&prev_hash, &difficulty, &pow) { |
| 52 | + let timestamp = SystemTime::now() |
| 53 | + .duration_since(UNIX_EPOCH) |
| 54 | + .unwrap() |
| 55 | + .as_secs(); |
| 56 | + |
| 57 | + println!("Found nonce! {}", nonce); |
| 58 | + |
| 59 | + let block = rt |
| 60 | + .block_on(tree.emmit_new_derivative_block(&pow, &wallet, timestamp)) |
| 61 | + .unwrap(); |
| 62 | + |
| 63 | + tree.add_gas(&wallet, *static_values::MAIN_CHAIN_PAYMENT) |
| 64 | + .unwrap(); |
| 65 | + |
| 66 | + println!("Added new block! {:?}\n", block.hash().unwrap()); |
| 67 | + |
| 68 | + rt.block_on(chain.flush()).unwrap(); |
| 69 | + break; |
| 70 | + } |
| 71 | + nonce += U256::one(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments