联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2019-03-14 10:53

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

COMP3221 Client-Server Blockchain

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 network

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 program.

When you submit your src.zip archive in PASTA, the system enqueues your submission

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.

Distributed Systems Page 2

COMP3221 Client-Server Blockchain

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.

Distributed Systems Page 3

COMP3221 Client-Server Blockchain

Task 1 Blockchain: Data Structure

In task 1, you are required to build the Blockchain’s 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|\n", 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 block’s 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|<sender>|<content> string format (E.g. tx|test0001|welcome to comp2121!) before

writing to DataOutputStream. If you do not know how to achieve this, please have a

look at Assignment 0 of this course. The hashing algorithm is shown as follows.

Distributed Systems Page 4

COMP3221 Client-Server Blockchain

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<Transaction> transactions;

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<Transaction> getTransactions() { return transactions; }

13 public void setPreviousBlock(Block previousBlock) { this.previousBlock = previousBlock; }

14 public void setPreviousHash(byte[] previousHash) { this.previousHash = previousHash; }

15 public void setTransactions(ArrayList<Transaction> transactions) {

16 this.transactions = transactions;

17 }

18

19 public String toString() {

20 String cutOffRule = new String(new char[81]).replace("\0", "-") + "\n";

21 String prevHashString = String.format("|PreviousHash:|%65s|\n",

22 Base64.getEncoder().encodeToString(previousHash));

23 String hashString = String.format("|CurrentHash:|%66s|\n",

24 Base64.getEncoder().encodeToString(calculateHash()));

25 String transactionsString = "";

26 for (Transaction tx : transactions) {

27 transactionsString += tx.toString();

28 }

29 return "Block:\n"

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|<sender>|<content> format. You need

to check each part of the txString following the standard as suggested before. Only valid

txString is converted into a transaction and then added to the pool. Adding invalid transaction

will cause this method to terminate and return 0. The pool should have an upper

limit of 3. Once the limit is reached, all uncommitted transactions in the pool are committed

to a new block, and that block is added to the blockchain (it becomes the new head of the

blockchain). If adding a new transaction causes a new block being generated, the length of

the blockchain should increase by 1 and this method should return 2. Otherwise, it returns

1. The root hash value is 0, this means the genesis block’s hash pointer has value 0.

Please use the skeleton code below to implement your Blockchain class.

1 public class Blockchain {

2

3 private Block head;

4 private ArrayList<Transaction> pool;

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<Transaction> getPool() { return pool; }

17 public int getLength() { return length; }

18 public void setHead(Block head) { this.head = head; }

19 public void setPool(ArrayList<Transaction> pool) { this.pool = pool; }

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("\0", "-") + "\n";

29 String poolString = "";

30 for (Transaction tx : pool) {

Distributed Systems Page 6

COMP3221 Client-Server Blockchain

31 poolString += tx.toString();

32 }

33

34 String blockString = "";

35 Block bl = head;

36 while (bl != null) {

37 blockString += bl.toString();

38 bl = bl.getPreviousBlock();

39 }

40

41 return "Pool:\n"

42 + cutOffRule

43 + poolString

44 + cutOffRule

45 + blockString;

46 }

47

48 // implement helper functions here if you need any.

49 }

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 <processID>". There are three types of requests.

tx request. "tx|<sender>|<content>"

pb request. "pb"

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\n\n". Otherwise, it should reply with "Rejected\n\n".

Note, since Mac, Windows and Linux all utilizes different newline characters, In this assignment,

if there is no special indication, print() method is used by default rather than

println() method, and we manually add "\n" when a new line is required.

For pb (Print Blockchain) request, the server should reply with string representation of the

blockchain with an additional "\n". (blockchain.toString() + "\n").

For cc (Close Connection) request, the server should not reply anything and close socket

gracefully. (Note, only end the connection, don’t terminate the server).

Distributed Systems Page 7

COMP3221 Client-Server Blockchain

For any other unrecognized request, the server should reply with "Error\n\n".

Please use the skeleton code below to implement your BlockchainServer class.

1 public class BlockchainServer {

2

3 private Blockchain blockchain;

4

5 public BlockchainServer() { blockchain = new Blockchain(); }

6

7 // getters and setters

8 public void setBlockchain(Blockchain blockchain) { this.blockchain = blockchain; }

9 public Blockchain getBlockchain() { return blockchain; }

10

11 public static void main(String[] args) {

12 if (args.length != 1) {

13 return;

14 }

15 int portNumber = Integer.parseInt(args[0]);

16 BlockchainServer bcs = new BlockchainServer();

17

18 // implement your code here.

19 }

20

21 public void serverHandler(InputStream clientInputStream, OutputStream clientOutputStream) {

22

23 BufferedReader inputReader = new BufferedReader(

24 new InputStreamReader(clientInputStream));

25 PrintWriter outWriter = new PrintWriter(clientOutputStream, true);

26

27 // implement your code here.

28

29 }

30

31 // implement helper functions here if you need any.

32 }

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 command

"javac -d bin src/*.java" for compilation.

To start up your server, please issue command "java -cp bin BlockchainServer 8333 &",

and then start a connection to your server with command "telnet localhost 8333" in the

terminal.

Make sure you can reproduce the transcript below.

1 Trying ::1...

2 Connected to localhost.

3 Escape character is '^]'.

Distributed Systems Page 8

COMP3221 Client-Server Blockchain

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 ---------------------------------------------------------------------------------

Distributed Systems Page 9

COMP3221 Client-Server Blockchain

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=|

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 ---------------------------------------------------------------------------------

Distributed Systems Page 10

COMP3221 Client-Server Blockchain

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 ---------------------------------------------------------------------------------

Distributed Systems Page 11

COMP3221 Client-Server Blockchain

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 socket’s 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.

1 public class BlockchainClient {

2

3 public static void main(String[] args) {

4

5 if (args.length != 2) {

6 return;

7 }

8 String serverName = args[0];

9 int portNumber = Integer.parseInt(args[1]);

10 BlockchainClient bcc = new BlockchainClient();

11

12 // implement your code here.

Distributed Systems Page 12

COMP3221 Client-Server Blockchain

13 }

14

15 public void clientHandler(InputStream serverInputStream, OutputStream serverOutputStream) {

16 BufferedReader inputReader = new BufferedReader(

17 new InputStreamReader(serverInputStream));

18 PrintWriter outWriter = new PrintWriter(serverOutputStream, true);

19

20 Scanner sc = new Scanner(System.in);

21 while (sc.hasNextLine()) {

22 // implement your code here

23 }

24 }

25

26 // implement helper functions here if you need any.

27 }

While no code should be shared, note that you are allowed to test with others after you have

finished your assignment.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp