Learn

Let’s review the concepts of nonce and proof of work. In this exercise, we will implement an example that demonstrates the difficulty of the math problem that helps protect the blockchain from potential attackers.

Instructions

1.

Import the sha256 hash function from the Python hashlib library

2.

Create a variable called difficulty and assign it a value of 2. This sets the number of leading zeros that the hash we find must have.

Create another variable called nonce and assign it to a value of 0. This will be our default starting value.

3.

Using the .str() method, cast nonce and new_transactions into strings. Pass the two strings into the sha256 function.

Store the resulting hash value into a variable called proof and print it out!

Note: Use the .hexdigest() method over the resulting sha256 function to properly store the hash value.

4.

Come up with some code that increments the nonce value until the generated hash has difficulty number of leading zeros. Once the desired proof has been found, store it in a variable called final_proof and print it out to see the correct hash!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?