-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu
More file actions
54 lines (49 loc) · 1.43 KB
/
cpu
File metadata and controls
54 lines (49 loc) · 1.43 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
import hashlib
# Define a function to compute the hash of a block.
def compute_hash(block):
# Encode the block data as a bytes object.
block_bytes = str(block).encode()
# Compute the SHA-256 hash of the block data.
return hashlib.sha256(block_bytes).hexdigest()
# Define a function to mine a new block.
def mine_block(last_block, data):
# Start with a random nonce value.
nonce = 0
# Compute the hash of the last block.
last_hash = compute_hash(last_block)
# Keep trying new nonce values until we find one that works.
while True:
# Compute the hash of the new block candidate.
block_hash = compute_hash({
'last_hash': last_hash,
'data': data,
'nonce': nonce
})
# Check if the block hash meets the difficulty requirement.
if block_hash[:4] == '0000':
# If it does, we've found a valid block.
return {
'last_hash': last_hash,
'data': data,
'nonce': nonce,
'hash': block_hash
}
# If it doesn't, try the next nonce value.
nonce += 1
# Define the starting block of the chain.
genesis_block = {
'last_hash': '',
'data': 'Genesis block',
'nonce': 0,
'hash': compute_hash({
'last_hash': '',
'data': 'Genesis block',
'nonce': 0
})
}
# Define the blockchain.
blockchain = [genesis_block]
# Mine some new blocks and add them to the chain.
for i in range(1, 10):
new_block = mine_block(blockchain[-1], f'Block {i}')
blockchain