MINING
import hashlib
import requests
import time
# Free API Endpoint (Example: CoinGecko API for price data)
API_URL = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
# Mining Configuration
DIFFICULTY = 4 # Number of leading zeros required
MAX_NONCE = 1000000000 # Maximum attempts to find a valid hash
def fetch_block_data():
"""Fetch real-time blockchain data using a free API."""
try:
response = requests.get(API_URL)
data = response.json()
return data
except Exception as e:
print(f"API Error: {e}")
return None
def mine_block(block_data, previous_hash):
"""Mine a block using Proof-of-Work (SHA-256)."""
target = '0' * DIFFICULTY
for nonce in range(MAX_NONCE):
input_data = f"{block_data}{previous_hash}{nonce}".encode()
hash_attempt = hashlib.sha256(input_data).hexdigest()
if hash_attempt.startswith(target):
return nonce, hash_attempt
return None, None
if __name__ == "__main__":
print("Starting Miner...")
previous_hash = hashlib.sha256("genesis_block".encode()).hexdigest()
while True:
block_data = fetch_block_data()
if block_data:
start_time = time.time()
nonce, hash_result = mine_block(str(block_data), previous_hash)
if hash_result:
print(f"Block Mined! Nonce: {nonce}, Hash: {hash_result}")
previous_hash = hash_result
else:
print("Mining failed. Retrying...")
print(f"Time Elapsed: {time.time() - start_time:.2f}s")
time.sleep(10)
0 $type={blogger}:
Post a Comment