C-20200219

Consensus Intermediate February 19, 2020

Back to All Tasks

Problem Statement

Design and implement a simple Proof of Work (PoW) algorithm for a blockchain network. Your task is to create a Python function that simulates the mining process by finding a nonce such that the hash of a block's data, combined with this nonce, results in a hash value that meets certain difficulty criteria (e.g., starts with at least two zeros). The function should take as input the block's transaction data and return the nonce along with the resulting valid hash. Ensure your solution demonstrates an understanding of how increasing the number of leading zeros affects mining time.

Concepts

  • Proof of Work
  • Blockchain Basics
  • Hash Functions

Constraints

  • Use SHA-256 hashing algorithm
  • The difficulty level (number of leading zeros) is adjustable via a parameter
  • Implement the solution in Python

Security Notes

  • Be aware that real-world PoW systems are more complex and involve additional security measures to prevent attacks such as brute force attacks and double-spending.
  • Ensure your code does not perform excessive computations that could lead to significant power consumption or system slowdowns.

Solutions

Javascript Solution

const crypto = require('crypto');

// Function to calculate SHA-256 hash
function calculateHash(data) {
	return crypto.createHash('sha256').update(data).digest('hex');
}

// Proof of Work function that finds the nonce
function proofOfWork(transactions, difficulty) {
	let nonce = 0;
	let prefixZeros = '0'.repeat(difficulty);	// Create a string with the required number of leading zeros
	let hash;
	const data = transactions + nonce;
	do {
		nonce++; // Increment nonce to find a valid hash
		hash = calculateHash(transactions + nonce); // Calculate new hash with the updated nonce
	} while (hash.substring(0, difficulty) !== prefixZeros);	// Check if the hash meets the difficulty criteria
	return {
		nonce: nonce,
		hash: hash
	};
}

// Example usage of proofOfWork function
const transactions = "Transaction data";
const difficulty = 2; // Adjust this value to change difficulty
const result = proofOfWork(transactions, difficulty);
console.log(`Nonce: ${result.nonce}, Hash: ${result.hash}`);

This JavaScript code implements a simple Proof of Work (PoW) algorithm for simulating the mining process in a blockchain network. The core function is `proofOfWork`, which takes transaction data and a difficulty level as inputs.

The function calculates the SHA-256 hash of the combined transaction data and a nonce value, incrementing the nonce until it finds a hash that meets the specified difficulty criteria (i.e., starts with a certain number of leading zeros).

A helper function `calculateHash` is used to compute the SHA-256 hash of given data. The main loop in `proofOfWork` continuously updates the nonce, recalculates the hash, and checks if it meets the required difficulty until a valid hash is found.

The example usage at the end demonstrates how to use this PoW function with a sample transaction string and an adjustable difficulty level. This implementation serves as a basic model for understanding PoW but lacks many security measures present in real-world blockchain systems.