MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / run

Method run

code/chapter09/blockchain/blockchain5/core/src/pow.rs:35–54  ·  view source on GitHub ↗
(&self, mut block: &mut Block)

Source from the content-addressed store, hash-verified

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;

Callers 1

newMethod · 0.45

Calls 2

hash_u8Function · 0.50
hash_strFunction · 0.50

Tested by

no test coverage detected