Background
Blockchain, a cryptographic tool, is extensively used in Fintech applications nowadays for
protecting chronological order of transactions.
The blocks are “linked” together by enforcing a specific relationship between blocks. That is,
a block must contain a “fingerprint”, which is a hash value, of the data of the previous block.
A hash function can condense arbitrary message (the block information) to a fixed size (e.g.,
160 bits) and produces a fingerprint of the message.
Hash of Block 1 Hash of Block 2 Hash of Block 3
COMP1011 Programming Fundamentals 2017/18 Semester 2
After a block is added to the end of the chain, it can neither be removed or modified. If there
are malicious changes to the transaction data, it can be detected by checking the hash value
stored in the next block.
In short, blockchain is essentially a linked list data structure, which links the transaction data
stored in blocks. And the integrity of the blocks can be verified by using hash values.
What you have to do
Implement a Blockchain using linked list as the data structure. The following template files
have been given to you (available in Blackboard). Import the files in a new C++ project and
use them as the starting point of your code:
BlockData.h
This file should contain a structure of a transaction information, which contains a c-string
with a maximum of 100 characters. You have to implement this file.
Block.h
This file contains the declaration of the Block class. You DO NOT have to modify this file.
Block.cpp
This file contains the definition of the Block class. You have to implement the constructor.
Blockchain.h
This file contains the declaration of the Blockchain class. You DO NOT have to modify this
file.
Blockchain.cpp
This file contains the definition of the Blockchain class. You have to implement the
constructor, destructor and functions.
SHA1.cpp
This file contains the core logic of generating a 160-bit value hash value based on Secure
Hash Algorithm version 1 (SHA-1). You DO NOT have to modify this file.
SHA1.hpp
This file contains the declarations of sha1. You DO NOT have to modify this file.
COMP1011 Programming Fundamentals 2017/18 Semester 2
Department of Computing
The Hong Kong Polytechnic University
SHA1_Demo.cpp
This file contains the sample logic of how to generate a 160-bit hash value based on a string.
The output is represented as a 40-character string, with each character representing a
hexadecimal value. Each hexadecimal value is equivalent to 4 bits. For example, ‘7’ means
0111 and ‘B’ means 1011. You DO NOT have to modify this file.
Assignment_2.cpp
This file contains the main() program. Use it as a testing driver of your code. You DO NOT
have to modify this file.
Read carefully the details of each file first before coding. The existing coding may shed you
some light on how to code the missing parts.
The following is a visual representation of the blockchain, to be implemented by your
program:
B lo c k
B lo c k Da t a
t r a n s a c t i o n
N U L L
h a s h o f p r e v i o u s
t r a n s a c t i o n
B lo c k
B lo c k Da t a
t r a n s a c t i o n
B lo c k c h a in
B lo c k
B lo c k Da t a
t r a n s a c t i o n
h a s h o f p r e v i o u s
t r a n s a c t i o n
h a s h o f p r e v i o u s
t r a n s a c t i o n
In the real world, the hash value has to be digitally signed by the creator of the block so
changes on the hash value is not possible by the attacker. In this assignment, we just assume
the hash value cannot be changed after the block is created.
COMP1011 Programming Fundamentals 2017/18 Semester 2
Department of Computing
The Hong Kong Polytechnic University Below shows the output of Assignment_2.cpp if the implementation is successful:
Assessment
This assignment is divided into multiple levels of difficulty. Try to accomplish the
assignment from the easiest level first and proceed to the next level. It helps you break down
the problem into smaller pieces and build up the program progressively. You will be awarded
a maximum mark for each level. Only if you complete all requirement of this assignment,
you will get full marks.
Level 1 (30 marks)
➢ Implement BlockData.h.
➢ Implement Block.cpp.
➢ Implement the constructor, destructor and isEmpty() function in Blockchain.cpp.
COMP1011 Programming Fundamentals 2017/18 Semester 2
Department of Computing
The Hong Kong Polytechnic University
Level 2 (20 marks)
➢ Implement the print() in Blockchain.cpp.
Level 3 (50 marks)
➢ Implement the add() and verify() functions in Blockchain.cpp.
At the beginning of Assignment_2.cpp, type your information using the following C++
comment template:
//============================================================================
// Author :
// Student No. :
// Description : COMP1011 Assignment 2
//============================================================================
Warning: Any compilation error will be awarded ZERO mark, regardless of what you
have coded. Therefore, if you are unable to complete the whole program, try to accomplish
some of the implementations. In this case, you may edit Assignment_2.cpp as well as other
files so that the project can be compiled and the program can be run successfully.
Submission
Follow the steps below:
1. Create a folder and name it as _.
E.g., 12345678d_CHANTaiMan
2. Put the following .h and .cpp files into the folder:
BlockData.h
Block.h
Block.cpp
Blockchain.h
Blockchain.cpp
Assignment_2.cpp
3. Compress the folder (.zip, .7z, .rar, or .jar) and submit the compressed file to
Blackboard.
COMP1011 Programming Fundamentals 2017/18 Semester 2
Department of Computing
The Hong Kong Polytechnic University
Any wrong file naming and submission will be given ZERO mark in this assignment. If
you are using Windows, the file extension may be hidden by the operating system. Follow the
steps of below links to make sure the file extension is not hidden:
Windows XP, 7 & 8: https://www.howtohaven.com/system/show-file-extensions-in-
windows-explorer.shtml
Windows 10: There are a maximum of 5 attempts for submission. Only the last attempt will be assessed.
The deadline of this assignment is 23:59:00 Wed 25th April 2018. No late submission is
allowed.
This assignment is an individual work. All work must be done on your own. Plagiarism
is serious offence. The Moss (https://theory.stanford.edu/~aiken/moss/) system will be
adopted for plagiarism checking. Submissions with high similarity, in terms of code
patterns and structures, in addition to direct-copy-and-paste, will be treated as
plagiarism. Copying code from web resources is prohibited as well. Any plagiarism
cases (both copier and copiee) will be given ZERO mark in this assignment.