In this lab, we are going to write a program to mine a cryptocurrency, Troncoin. It uses the blockchain technology. For each block mined, you will receive 1 Troncoin (TNC).

Overview

Input transactions

Troncoin is used to validate and record financial transactions. Throughout this lab, we will use the transactions (strictly speaking, we should verify the transactions with digital signatures but we will skip that part in the lab, and assume that the transactions have already been verified).

Block format

A Troncoin block input consists of:
  • PrevHash
  • Exactly 5 transactions
  • Nonce
Each block in the chain should consist of 7 lines:
Line 1  : PrevHash
Line 2-6: 5 transactions 
Line 7  : Nonce
Carefully inspect the sample data on the right to see how the 7 lines in each block are written.

Calculating hash of a block

The hash of a given block is calculated as follows:
SHA-256((PrevHash+transactions+Nonce).encode())
  • Use a hexstring for PrevHash. So, PrevHash should be of string type.
  • The new line character '\n' at the end of each line should be stripped off.
  • Nonce can be any arbitrary string.
  • Use encode() function to change the concatenated string into bytes.
  

Sample data

Here is an example block chain.
  • It was mined with the alpha 999999 as the input into the first block.
  • The block chain was created with difficulty level of 4 hex letters (16 bits of 0s). Note all PrevHash (except the first one) starts with 0000.

Difficulty level

The difficulty level is set to be the number of leading 0s (in a hex letter, see the sample below) in the block hash.

PrevHash for the first block

PrevHash of into the first block is: Just your alpha.

[40pts] Part 1: Verifying a Blockchain

Write a program verify.py.
$ ./verify.py blockchain.txt 4
Verifying Troncoin Block Chain...

Checking Block 1
 PrevHash entry : 999999
 Hash of Block 0: NA
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Good
Checking Block 2
 PrevHash entry : 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Hash of Block 2: 0000e4303b192450bcc4362c14bd7476dfd2ecbca09a6e2d0794b6d6bfe06114
 Good
Checking Block 3
 PrevHash entry : 0000e4303b192450bcc4362c14bd7476dfd2ecbca09a6e2d0794b6d6bfe06114
 Hash of Block 2: 0000e4303b192450bcc4362c14bd7476dfd2ecbca09a6e2d0794b6d6bfe06114
 Hash of Block 3: 00000de88262dfb48a723db88cf44664773289c30ccfe145315b6235138567cb
 Good
Checking Block 4
 PrevHash entry : 00000de88262dfb48a723db88cf44664773289c30ccfe145315b6235138567cb
 Hash of Block 3: 00000de88262dfb48a723db88cf44664773289c30ccfe145315b6235138567cb
 Hash of Block 4: 0000513470f65fce04521bb0b7260f5bd02efc04ca4be0a64f9fe813931b5586
 Good
Checking Block 5
 PrevHash entry : 0000513470f65fce04521bb0b7260f5bd02efc04ca4be0a64f9fe813931b5586
 Hash of Block 4: 0000513470f65fce04521bb0b7260f5bd02efc04ca4be0a64f9fe813931b5586
 Hash of Block 5: 00004215691def6c560b66986e0acb4f2bd32e033e855f4566e4860e674b808c
 Good
Checking Block 6
 PrevHash entry : 00004215691def6c560b66986e0acb4f2bd32e033e855f4566e4860e674b808c
 Hash of Block 5: 00004215691def6c560b66986e0acb4f2bd32e033e855f4566e4860e674b808c
 Hash of Block 6: 000017dc0d8544d85cdf7efde27b9de4f3b5e8cd51f3557fb0cdde010e7a438d
 Good
Checking Block 7
 PrevHash entry : 000017dc0d8544d85cdf7efde27b9de4f3b5e8cd51f3557fb0cdde010e7a438d
 Hash of Block 6: 000017dc0d8544d85cdf7efde27b9de4f3b5e8cd51f3557fb0cdde010e7a438d
 Hash of Block 7: 0000051f3b5e78c4b720a1e3e2d6c24298d9110d0f9ffe8c8a36f864056a1df8
 Good
Checking Block 8
 PrevHash entry : 0000051f3b5e78c4b720a1e3e2d6c24298d9110d0f9ffe8c8a36f864056a1df8
 Hash of Block 7: 0000051f3b5e78c4b720a1e3e2d6c24298d9110d0f9ffe8c8a36f864056a1df8
 Hash of Block 8: 00000eed512fe29d61fe689d705f82208f7498cba120c91a838ac602b5802f4d
 Good
Checking Block 9
 PrevHash entry : 00000eed512fe29d61fe689d705f82208f7498cba120c91a838ac602b5802f4d
 Hash of Block 8: 00000eed512fe29d61fe689d705f82208f7498cba120c91a838ac602b5802f4d
 Hash of Block 9: 0000da6622813dced26952db8b908b79e153cf5d0f1e45cc6c073df9623909f6
 Good
Checking Block 10
 PrevHash entry : 0000da6622813dced26952db8b908b79e153cf5d0f1e45cc6c073df9623909f6
 Hash of Block 9: 0000da6622813dced26952db8b908b79e153cf5d0f1e45cc6c073df9623909f6
 Hash of Block 10: 00004be6d7cc5a1ae661a1f0f29208bc0460eb8c5943bd09130b194c0fbe8370
 Good

Check the prevHash

Your verification program should the correctnes of prevHash. For example, In blockchain-bad-prevhash.txt, I slightly changed the prevHash of Block2.
$ ./verify.py blockchain-bad-prevhash.txt 4
Verifying Troncoin Block Chain...

Checking Block 1
 PrevHash entry : 999999
 Hash of Block 0: NA
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Good
Checking Block 2
 PrevHash entry : 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e96
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Hash of Block 2: 50b508d84c8361e33f808e17dc29d3397165129743dd3a2b0a74726acdce54b6
 Error: prevHash mismatch.

Check the proof of work

Your verification program should the correctnes of nonce. For example, In blockchain-bad-nonce.txt, I slightly changed the nonce of Block2.
$ ./verify.py blockchain-bad-nonce.txt 4
Verifying Troncoin Block Chain...

Checking Block 1
 PrevHash entry : 999999
 Hash of Block 0: NA
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Good
Checking Block 2
 PrevHash entry : 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Hash of Block 2: 90fc0c2807de005e7cc413f5a27b82c40c4f556e3fced8cadadb6f7dab81637c
 Error: invalid proof of work
Note, if you increase the difficulty level, even the correct file blockchain.txt will be having all invalid blocks.
$  ./verify.py blockchain.txt 5
Verifying Troncoin Block Chain...

Verifying Troncoin Block Chain...

Checking Block 1
 PrevHash entry : 999999
 Hash of Block 0: NA
 Hash of Block 1: 0000bb57def78d233229d89eed5532aa93509691afb1aecbc05cc36461820e95
 Error: invalid proof of work

Note

You must pass the first 4 test cases in the submit server.

[60pts] Part 2: Mining Troncoins!

Write a program mine.py that creates a blockchain file.

Submit

~/bin/submit -c=IT432 -p=lab06 verify.py mine.py blockchain4.txt blockchain6.txt