Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in Java The task is to implement a simplified consensus algorithm between a number of nodes. We assume there is one leader node

Please write in Java The task is to implement a simplified consensus algorithm between a number of nodes. We assume there is one leader node which all the other nodes can talk to. We will skip the part where each node can potentially talk to all the other nodes, so no peer to peer network here. Basic structure: Client sends a request to the leader node, the leader then asks the other nodes for consent, receives the answers and handles accordingly. You do not start off with starter code for this task. Your client (command line) should communicate only with the leader node. The client can ask for a credit (loan) of a specific amount or pay back some or all of their existing credit. We skip things like the banks making more money and getting percentages when giving credit to simplify things. As always you should make sure that your code is as robust as possible, does not crash, the data is persistent and the user interface is easy to use. The protocol you use for your communication is totally up to you but it has to be well defined. Requirements 1. We want to run the leader node through "gradle leader" with some default port that is set for us, so the client can easily connect to the leader. 2. We want to start at least two separate nodes through "gradle node1", "gradle node2" which should then connect to the leader correctly (let us know in your README.md in which order we will have to start what). It is up to you to treat the nodes as servers or as clients to the leader there are pros and cons to each. You can choose what you like best. 3. Nodes need to start with initial money, include this in your gradle task with a default value but also let us call it through "gradle node1 -Pmoney=1000", which would then set the start money of the bank to 1000. 4. The client should start through "gradle client", connecting to the leader node with which the client can talk. 5. Leader will ask the Client for their clientID, which should be provided by the client. 6. Client will then have the choice between requesting a credit or paying part of the credit. This should be a client side choice followed by the amount of "credit" or "payback". 7. The leader receives the request ("credit", "payback" or however you want to call this) with the amount. See below for details on each operation. 8. Credit: a) If the client wants credit, then the leader sends all known nodes the information that a specific client wants a credit of that amount. b) Nodes will check if that client already has credit with them and if the node (representing a bank) has enough money available. If the client with that ID does not have credit with them yet and the bank has at least 150% of what the client wants available (yes we want the bank to have more than what the client wants), they respond with a "yes". If that is not the case, they respond with a "no". c) If the majority of nodes (so if two nodes are connected, both have to answer yes) say "yes", then the credit is granted to the client. d) The leader will split up the amount as evenly as possible between the nodes and notify them that this client now has this amount of credit with them. The nodes will have to persistently save that amount and clientID. The client is informed that they get the credit and the leader stores the amount persistently as well. e) Nodes will decrease their available money. f) If the majority vote "no", the client will be informed that they do not get any credit and the nodes will not decrease their available money. 9. Pay back: a) If the client wants to pay money back, then the leader informs the nodes that this client wants to pay money back. b) Nodes will check how much the client owes and return how much that clients owes to them to the leader. c) If the client wants to pay back more than is owed, they will just get an error message which the leader will send to the client. d) If the client pays back partially or all of their existing credit, the leader will split up the amount to each node. You can split it up 2/3 1/3 if you like. You should of course not pay back more than is owed to a node. The nodes will update their records, the leader will update its records and the client will be informed about how much debt is still owed. 10. You should make sure that when a node crashes the whole system does not go down. If the leader crashes then of course a restart might be needed but the data should be persistent. 11. If a restart is needed, the first thing the leader should do is check in with the nodes and verify their records, e.g. client=1 owes=100 based on leader, node1 says client=1 owes=50, node2 says client=1 owes=20. In this case something went wrong and you might want to check what happened. Maybe have the leader keep track of all transactions so you can roll back. 12. This gets interesting if more than one client can interact with the leader and make requests. The system will need to make sure it handles them correctly and the order of transactions is still correct. Examples: Assume node1 starts with 1000 and node2 with 2000 "money". I will omit the currency here. No transactions happened. Now client1 wants to get a credit of 500 and contacts the leader. The leader will forward this to the nodes. In this case both nodes will respond with yes, since both have more than 750 and this client does not have credit with them yet. The leader splits up the credit 50/50 so 250 node1, 250 node2 node1 will save that client1:250, same for node2, the leader will store 500 for that client. They will also decrease their money. node1: 750, node2: 1750, client2 wants to get a credit of 700. Same process but now node1 would decline since they do not have enough money anymore. So they client will not get credit client1 wants another credit. Would also be declined. client3 wants 301 credit. This would be accepted by both, so the leader would split up the amount into 150/151 (if you want to you can also split it up so that node2 will give more since it has more money available I leave this up to you). So now node1 client1:250, client2:150 and node2 client1: 250, client2:151. client1 wants to pay back some of their credit 200. The leader will ask each node how much this client owes. So the nodes would each return 250 (remember it might be different for each node depending on the split up). The leader can now decide how to pay back. You could have the nodes also return the money they have and then do the payback depending on how much money the bank has, e.g. node1 would get more in that case, or you can do it evenly. Up to you. So now the node1 and node2 would update their records for client1. It gets interesting if you have more than 2 nodes. Since now 3 nodes might say yes and another one says no. Still the majority, so now the credit is split up between only the nodes that said yes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions