| 33 | } |
| 34 | |
| 35 | pub fn run(&self, mut block: &mut Block) { |
| 36 | println!("Start mining .... "); |
| 37 | thread::sleep(Duration::from_secs(3)); |
| 38 | |
| 39 | let mut nonce: u32 = 0; |
| 40 | while nonce <= MAX_NONCE { |
| 41 | let header_ser = Self::prepare_data(&mut block, nonce); |
| 42 | let mut hash_u: [u8; 32] = [0; 32]; |
| 43 | hash_u8(&header_ser, &mut hash_u); |
| 44 | |
| 45 | let hash_int = U256::from(hash_u); |
| 46 | if hash_int <= self.target { |
| 47 | block.hash = hash_str(&header_ser); |
| 48 | println!("Produced a new block!"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | nonce += 1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn prepare_data(block: &mut Block, nonce: u32) -> Vec<u8> { |
| 57 | block.header.nonce = nonce; |