3
3
import java .io .ObjectOutputStream ;
4
4
import java .net .ServerSocket ;
5
5
import java .net .Socket ;
6
- import java .util .ArrayList ;
7
6
import java .util .List ;
8
- import java .util .Scanner ;
9
7
import java .util .concurrent .ExecutorService ;
10
8
import java .util .concurrent .Executors ;
11
9
import java .util .stream .Collectors ;
@@ -59,7 +57,7 @@ public void stopServer() {
59
57
}
60
58
61
59
/**
62
- * Once a new peer is created and added to the network, it requests the latest block from
60
+ * Once a new peer is created and added to the network, it requests the latest block from
63
61
* all the peers and updates its blockchain if it is outdated.
64
62
*/
65
63
private void getLatestBlockFromPeers () {
@@ -119,71 +117,4 @@ public String toString() {
119
117
sb .append ('}' );
120
118
return sb .toString ();
121
119
}
122
-
123
- /**
124
- * The main starting point of the blockchain demo. At first, add some peers (option 1) and mine some data
125
- * by choosing a particular peer (option 2). You would soon see that the newly mined block is broadcast to
126
- * all the peers.
127
- *
128
- * @param args
129
- */
130
- public static void main (String [] args ) {
131
- try {
132
- int menuChoice ;
133
- int peerIndex ;
134
- String data ;
135
- Scanner s = new Scanner (System .in );
136
- Blockchain blockchain = new Blockchain (new ArrayList <>(), 3 );
137
-
138
- while (true ) {
139
-
140
- System .out .println ("\n ======= Welcome to Blockchain in Java =======" );
141
- System .out .println ("1. Add Peer" );
142
- System .out .println ("2. Mine data in peer" );
143
- System .out .println ("3. Remove peer" );
144
- System .out .println ("4. Show peers" );
145
- System .out .println ("5. Exit" );
146
-
147
- menuChoice = s .nextInt ();
148
-
149
- switch (menuChoice ) {
150
- case 1 :
151
- P2P .addPeer (blockchain );
152
- System .out .println ("New peer added!" );
153
- P2P .showPeersWithBlockchain ();
154
- break ;
155
- case 2 :
156
- System .out .println ("Choose peer: (a number for ex. 1, 2, etc.)" );
157
- P2P .showPeers ();
158
- peerIndex = s .nextInt ();
159
- Peer p = P2P .getPeer (peerIndex - 1 );
160
- System .out .println ("Enter data: (a string with no spaces)" );
161
- data = s .next ();
162
- p .mine (data );
163
- System .out .println ("Data mined!" );
164
- P2P .showPeersWithBlockchain ();
165
- break ;
166
- case 3 :
167
- System .out .println ("Choose peer: (a number for ex. 1, 2, etc.)" );
168
- P2P .showPeers ();
169
- peerIndex = s .nextInt ();
170
- P2P .removePeer (peerIndex - 1 );
171
- System .out .println ("Peer " + peerIndex + " removed!" );
172
- P2P .showPeersWithBlockchain ();
173
- break ;
174
- case 4 :
175
- P2P .showPeersWithBlockchain ();
176
- break ;
177
- case 5 :
178
- P2P .removeAllPeers ();
179
- System .out .println ("Bye, see you soon!" );
180
- System .exit (0 );
181
- default :
182
- System .out .println ("Wrong choice!" );
183
- }
184
- }
185
- } catch (Exception e ) {
186
- throw new RuntimeException (e );
187
- }
188
- }
189
120
}
0 commit comments