Due: 19 March 2019(Tuesday Week 4) by 11:59 PM
COMP3221 Assignment 1: CS Blockchain
The goal of this project is to implement a Client-Server (CS) Blockchain application in Java which could be able to store messages and detect tampering.
1 Submission Details
The assignment comprises 3 tasks, each can be submitted separately. The final version of your assignment should be submitted electronically via PASTA by 11:59PM on the Tuesday of Week 4 (Hard Deadline). The project is an individual project, and each student has to submit his/her own version.
1.1 Program structure
For task 1, three java files must be submitted. It is recommended to submit by 11:59 PM on the Friday of Week 2 (Soft Deadline).
A Blockchain.java file. A Block.java file.
A Transaction.java file.
For task 2, four java files must be submitted. It is recommended to submit by 11:59 PM on the Sunday of Week 2 (Soft Deadline).
A Blockchain.java file.
A Block.java file.
A Transaction.java file.
A BlockchainServer.java file.
For task 3, five java files must be submitted. It is recommended to submit by 11:59 PM on the Sunday of Week 3 (Soft Deadline).
A Blockchain.java file. A Block.java file.
1
A Transaction.java file.
A BlockchainServer.java file. A BlockchainClient.java file.
All classes will be stored in the same default package (no package header in java files), and all files should be located in the same src folder with no subfolders. All present .java files should be correct and do not forget to remove any dummy files that do not count as source files (E.g. junit test cases, class files). Please zip the src folder and submit the resulting archive src.zip by the deadline given above. The program should compile with Java 8. No optional packages that are not part of the default Java 8 JDK can be used.
1.2 Submission system
PASTA will be used to assess that your program correctly implements the Client-Server Blockchain protocol. The archive src.zip should be submitted at https://pastpd01191.srv. sydney.edu.au/PASTA. To access this website, you will have to be connected to the net- work of the University of Sydney (physically on campus or through VPN).
PASTA stands for Programming Assessment Submission and Testing Application and is a web-based application that automates the compilation, execution and testing of your pro- gram. When you submit your src.zip archive in PASTA, the system enqueues your submis- sion in the shared queue of all assignments to be tested. It may take more time close to the deadline as many students will try to submit at the same time.
1.3 Academic Honesty / Plagiarism
By uploading your submission to PASTA you implicitly agree to abide by the University policies regarding academic honesty, and in particular that all the work is original and not plagiarised from the work of others. If you believe that part of your submission is not your work you must bring this to the attention of your tutor or lecturer immediately. See the policy slides released in Week 1 for further details.
In assessing a piece of submitted work, the School of IT may reproduce it entirely, may provide a copy to another member of faculty, and/or communicate a copy of this assignment to a plagiarism checking service or in-house computer program. A copy of the assignment may be maintained by the service or the School of IT for the purpose of future plagiarism checking.
2 Marking
This assignment is worth 10% of your final grade for this unit of study.
The first category of tasks, called Blockchain: Data Structure, assesses the quality of the message being stored in the block chain. If you could pass all tests, 4 marks are given.
COMP3221 Client-Server Blockchain
Distributed Systems Page 2
Maximum 1 mark for rejecting invalid transactions.
Maximum 1 mark for storing transaction into pool properly.
Maximum 2 mark for committing transactions from pool to blockchain correctly.
The second category, called Blockchain: Server, assesses the behaviour of the server with respect to protocol requirements. If you could pass all tests, 3 marks are given.
Maximum 1 mark for server listening to connections continuously.
Maximum 1 mark for server replying to add transaction request correctly. Maximum 1 mark for server replying to print blockchain request correctly.
The third category, called Blockchain: Client, assesses the behaviour of the client with respect to protocol requirements. If you could pass all tests, 3 marks are given.
Maximum 1 mark for client connecting to the server properly.
Maximum 1 mark for client forwarding requests to the server correctly. Maximum 1 mark for client printing replies correctly.
Please make sure previous tasks are implemented correctly, before moving to next task. You may face cascading failures due to improper implementation of previous tasks.
2.1 Feedback
PASTA provides feedback about each individual submission, one at a time. It will output a list of tests and outcomes indicating whether your program passed each visible test successfully or failed. The feedback provided is indicative, intentionally high-level and will not precisely identify the nature of any bug of your program. Please write your personal test cases and thoroughly test all your code before submission.
3 Functionalities of a Client-Server Blockchain
The goal of this project is to implement a Client-Server (CS) Blockchain application. While the original blockchain operates in a distributed and P2P mode, your current CS Blockchain program will only require one client and one server running at the same time. The following tasks indicate the features that should be implemented, one solution for each task is expected to be submitted.
COMP3221 Client-Server Blockchain
Distributed Systems Page 3
Task 1 Blockchain: Data Structure
In task 1, you are required to build the Blockchains core data structure, which is a linked list of blocks of transactions. If you would like to have a better view of what we are trying to build, please have a look at Assignment 0.
Transaction. A transaction is defined as the container to store a single message (sender + content).
Message sender, e.g., test0001
Message content, e.g., welcome to comp2121!
You should perform some checks before you consider a message as a valid transaction. The message sender must present and should match a unikey-like form (regex: [a-z]{4}[0-9]{4}). The message content cannot have more than 70 English characters or contain a | character. (| is used as delimiter later). If a transaction violates those rules, it should be considered as an invalid transaction.
Please use the skeleton code below to implement your Transaction class.
1 public class Transaction {
2 private String sender;
3 private String content;
4
5 // getters and setters
6 public void setSender(String sender) { this.sender = sender; }
7 public void setContent(String content) { this.content = content; }
8 public String getSender() { return sender; }
9 public String getContent() { return content; }
10
11 public String toString() {
12 return String.format(|%s|%70s|
, sender, content);
13 }
14
15 // implement helper functions here if you need any
16 }
Block. A block contains a list of transactions and a hash value of the previous block as the pointer:
a list of transactions contains the messages;
a hash pointer stores the hash value of the previous block.
The important thing here is to calculate the hash precisely. The hash function is SHA- 256. The first thing gets hashed is the previous blocks hash value using dos.write() method, then each transaction in the list gets hashed using the dos.writeUTF() method. writeUTF() method expects string as input. Therefore, you should convert each transaction to tx|
COMP3221 Client-Server Blockchain
Distributed Systems Page 4
1 H = SHA-256(H,tx1,tx2,tx3)
Please use the skeleton code below to implement your Block class.
1 public class Block { 2
3 private Block previousBlock;
4 private byte[] previousHash;
5 private ArrayList
6
7 public Block() { transactions = new ArrayList<>(); } 8
9 // getters and setters
10 public Block getPreviousBlock() { return previousBlock; }
11 public byte[] getPreviousHash() { return previousHash; }
12 public ArrayList
13 public void setPreviousBlock(Block previousBlock) { this.previousBlock = previousBlock; }
14 public void setPreviousHash(byte[] previousHash) { this.previousHash = previousHash; }
15 public void setTransactions(ArrayList
16 this.transactions = transactions;
17 }
18
19 public String toString() {
20 String cutOffRule = new String(new char[81]).replace( , -) +
;
21 String prevHashString = String.format(|PreviousHash:|%65s|
,
22 Base64.getEncoder().encodeToString(previousHash));
23 String hashString = String.format(|CurrentHash:|%66s|
,
24 Base64.getEncoder().encodeToString(calculateHash()));
25 String transactionsString = ;
26 for (Transaction tx : transactions) {
27 transactionsString += tx.toString();
28 }
29 return Block:
30 + cutOffRule
31 + hashString
32 + cutOffRule
33 + transactionsString
34 + cutOffRule
35 + prevHashString
36 + cutOffRule;
37 }
38
39 // to calculate the hash of current block.
40 public byte[] calculateHash() {
41 // implement your code here.
42 }
43
44 // implement helper functions here if you need any.
45 }
Blockchain. A blockchain contains the blockchain itself to store committed transactions and Distributed Systems Page 5
COMP3221 Client-Server Blockchain
a pool to store uncommitted transactions. More precisely, it contains:
a list of uncommitted transactions as a pool; the head (latest block) of the blockchain;
the length of the blockchain.
The blockchain has a public int addTransaction(String txString) method for the user to add a transaction. The txString is in tx|
Please use the skeleton code below to implement your Blockchain class.
1 public class Blockchain { 2
3 private Block head;
4 private ArrayList
5 private int length;
6
7 private final int poolLimit = 3; 8
9 public Blockchain() {
10 pool = new ArrayList<>();
11 length = 0;
12 }
13
14 // getters and setters
15 public Block getHead() { return head; }
16 public ArrayList
17 public int getLength() { return length; }
18 public void setHead(Block head) { this.head = head; }
19 public void setPool(ArrayList
20 public void setLength(int length) { this.length = length; }
21
22 // add a transaction
23 public int addTransaction(String txString) {
24 // implement you code here.
25 }
26
27 public String toString() {
28 String cutOffRule = new String(new char[81]).replace( , -) +
;
29 String poolString = ;
30 for (Transaction tx : pool) {
COMP3221 Client-Server Blockchain
Distributed Systems
Page 6
Task 2
Blockchain: Server
The Blockchain server should build a ServerSocket that keeps listening to a specific port provided by the user. Once a client connects, the server should accept the connection, and pass the InputStream and OutputStream of the accepted socket to the serverHandler() function. serverHandler() method implements the core logic about how to interact with user requests. Your server should keep listening for connections unless you terminated the process explicitly like kill -9
tx request. tx|
cc request. cc
For tx (Transaction) request, the server should try to add the transaction message to the blockchain and reply whether the transaction is in correct format. If the transaction is valid, server should reply with Accepted
. Otherwise, it should reply with Rejected
. Note, since Mac, Windows and Linux all utilizes different newline characters, In this as- signment, if there is no special indication, print() method is used by default rather than println() method, and we manually add
when a new line is required.
For pb (Print Blockchain) request, the server should reply with string representation of the blockchain with an additional
. (blockchain.toString() +
).
For cc (Close Connection) request, the server should not reply anything and close socket gracefully. (Note, only end the connection, dont terminate the server).
COMP3221 Client-Server Blockchain
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
poolString += tx.toString(); }
String blockString = ; Block bl = head;
while (bl != null) {
blockString += bl.toString();
bl = bl.getPreviousBlock(); }
return Pool:
+ cutOffRule
+ poolString
+ cutOffRule
+ blockString; }
// implement helper functions here if you need any.
}
Distributed Systems Page 7
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
public class BlockchainServer {
private Blockchain blockchain;
public BlockchainServer() { blockchain = new Blockchain(); }
// getters and setters
public void setBlockchain(Blockchain blockchain) { this.blockchain = blockchain; } public Blockchain getBlockchain() { return blockchain; }
public static void main(String[] args) { if (args.length != 1) {
return; }
int portNumber = Integer.parseInt(args[0]); BlockchainServer bcs = new BlockchainServer();
// implement your code here.
}
public void serverHandler(InputStream clientInputStream, OutputStream clientOutputStream) {
BufferedReader inputReader = new BufferedReader( new InputStreamReader(clientInputStream));
PrintWriter outWriter = new PrintWriter(clientOutputStream, true);
// implement your code here.
}
// implement helper functions here if you need any.
}
You should test this part using Linux telnet program as taught in the tutorial before.
For example, if you choose to use command line to compile your code, please issue com-
mand javac -d bin src/*.java for compilation.
Tostartupyourserver,pleaseissuecommandjava -cp bin BlockchainServer 8333 &, andthenstartaconnectiontoyourserverwithcommandtelnet localhost 8333inthe terminal.
Make sure you can reproduce the transcript below.
1 Trying ::1
2 Connected to localhost.
3 Escape character is ^].
COMP3221 Client-Server Blockchain
For any other unrecognized request, the server should reply with Error
. Please use the skeleton code below to implement your BlockchainServer class.
Distributed Systems
Page 8
4 pb
5 Pool:
6
7
8
9 tx|test0000|1
10 Accepted
11
12 pb
13 Pool:
14
15 |test0000| 1|
16
17
18 tx|test0000|2
19 Accepted
20
21 pb
22 Pool:
23
24 |test0000| 1|
25 |test0000| 2|
26
27
28 tx|test0000|3
29 Accepted
30
31 pb
32 Pool:
33
34
35 Block:
36
37 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
38
39 |test0000| 1|
40 |test0000| 2|
41 |test0000| 3|
42
43 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
44
45
46 tx|test0000|4
47 Accepted
48
49 pb
50 Pool:
51
52 |test0000| 4|
53
COMP3221 Client-Server Blockchain
Distributed Systems Page 9
54 Block:
55
56 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
57
58 |test0000| 1|
59 |test0000| 2|
60 |test0000| 3|
61
62 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
63
64
65 tx|test0000|5
66 Accepted
67
68 pb
69 Pool:
70
71 |test0000| 4|
72 |test0000| 5|
73
74 Block:
75
76 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
77
78 |test0000| 1|
79 |test0000| 2|
80 |test0000| 3|
81
82 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
83
84
85 tx|test0000|6
86 Accepted
87
88 pb
89 Pool:
90
91
92 Block:
93
94 |CurrentHash:| Lakir/jIQFUGnf+UUnRbiuYsNDOcGXekM+2cKXVmyRw=|
95
96 |test0000| 4|
97 |test0000| 5|
98 |test0000| 6|
99
100 |PreviousHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
101
102 Block:
103
COMP3221 Client-Server Blockchain
Distributed Systems Page 10
104 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
105
106 |test0000| 1|
107 |test0000| 2|
108 |test0000| 3|
109
110 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
111
112
113 tx|test0000|7
114 Accepted
115
116 pb
117 Pool:
118
119 |test0000| 7|
120
121 Block:
122
123 |CurrentHash:| Lakir/jIQFUGnf+UUnRbiuYsNDOcGXekM+2cKXVmyRw=|
124
125 |test0000| 4|
126 |test0000| 5|
127 |test0000| 6|
128
129 |PreviousHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
130
131 Block:
132
133 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
134
135 |test0000| 1|
136 |test0000| 2|
137 |test0000| 3|
138
139 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
140
141
142 tx|wrong|wrong
143 Rejected
144
145 pb
146 Pool:
147
148 |test0000| 7|
149
150 Block:
151
152 |CurrentHash:| Lakir/jIQFUGnf+UUnRbiuYsNDOcGXekM+2cKXVmyRw=|
153
COMP3221 Client-Server Blockchain
Distributed Systems Page 11
154 |test0000| 4|
155 |test0000| 5|
156 |test0000| 6|
157
158 |PreviousHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
159
160 Block:
161
162 |CurrentHash:| jWuaYc5TOawKJew+B+tYuLZT0NDsTo6NDKEJdmgJyBk=|
163
164 |test0000| 1|
165 |test0000| 2|
166 |test0000| 3|
167
168 |PreviousHash:| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=|
169
170
171 cc
172 Connection closed by foreign host.
Task 3 Blockchain: Client
The last task is to build the client program. It should try to create a socket to connect to the specific server and port number, and pass sockets InputStream and OutputStream to the clientHandler() method. The clientHandler() method implements the core logic of the client, which uses a scanner to listen to user inputs, and forward user requests to the server. If the request is cc, the client should terminate. Otherwise, it keeps waiting for new user inputs. All server replies should be correctly printed on the screen. The client program should work exactly the same as the telnet program does, except for lines like
1 Trying ::1
2 Connected to localhost.
3 Escape character is ^].
4 Connection closed by foreign host.
Please use the skeleton code below to implement your BlockchainClient class.
public class BlockchainClient {
public static void main(String[] args) {
1
2
3
4
5
6 7}
8 String serverName = args[0];
9 int portNumber = Integer.parseInt(args[1]);
if (args.length != 2) { return;
10 BlockchainClient bcc = new BlockchainClient(); 11
12 // implement your code here.
COMP3221 Client-Server Blockchain
Distributed Systems
Page 12
While no code should be shared, note that you are allowed to test with others after you have finished your assignment.
COMP3221 Client-Server Blockchain
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
}
public void clientHandler(InputStream serverInputStream, OutputStream serverOutputStream) { BufferedReader inputReader = new BufferedReader(
new InputStreamReader(serverInputStream));
PrintWriter outWriter = new PrintWriter(serverOutputStream, true);
Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) {
// implement your code here
} }
// implement helper functions here if you need any.
}
Distributed Systems Page 13
Reviews
There are no reviews yet.