@@ -9,21 +9,21 @@ const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
9
9
const MESSAGE_TYPES = {
10
10
chain : 'CHAIN' ,
11
11
transaction : 'TRANSACTION' ,
12
- clear_transactions : 'CLEAR_TRANSACTIONS' ,
12
+ clearTransactions : 'CLEAR_TRANSACTIONS' ,
13
13
} ;
14
14
15
15
export default class P2pServer {
16
- blockchain : Blockchain ;
17
- transactionPool : TransactionPool ;
18
- sockets : Websocket [ ] ;
16
+ public blockchain : Blockchain ;
17
+ public transactionPool : TransactionPool ;
18
+ public sockets : Websocket [ ] ;
19
19
20
- constructor ( blockchain : Blockchain , transactionPool : TransactionPool ) {
20
+ public constructor ( blockchain : Blockchain , transactionPool : TransactionPool ) {
21
21
this . blockchain = blockchain ;
22
22
this . transactionPool = transactionPool ;
23
23
this . sockets = [ ] ;
24
24
}
25
25
26
- listen ( ) : void {
26
+ public listen ( ) : void {
27
27
const server = new Websocket . Server ( { port : P2P_PORT } ) ;
28
28
server . on ( 'connection' , socket => this . connectSocket ( socket ) ) ;
29
29
@@ -32,15 +32,15 @@ export default class P2pServer {
32
32
console . log ( `Listening for peer-to-peer connections on: ${ P2P_PORT } ` ) ;
33
33
}
34
34
35
- connectToPeers ( ) : void {
35
+ public connectToPeers ( ) : void {
36
36
peers . forEach ( _ => {
37
37
const socket = new Websocket ( _ ) ;
38
38
39
39
socket . on ( 'open' , ( ) => this . connectSocket ( socket ) ) ;
40
40
} ) ;
41
41
}
42
42
43
- connectSocket ( socket : Websocket ) : void {
43
+ public connectSocket ( socket : Websocket ) : void {
44
44
this . sockets . push ( socket ) ;
45
45
console . log ( 'Socket connected' ) ;
46
46
@@ -49,7 +49,7 @@ export default class P2pServer {
49
49
this . sendChain ( socket ) ;
50
50
}
51
51
52
- messageHandler ( socket : Websocket ) : void {
52
+ public messageHandler ( socket : Websocket ) : void {
53
53
socket . on ( 'message' , message => {
54
54
const data = JSON . parse ( message as string ) ;
55
55
switch ( data . type ) {
@@ -59,14 +59,14 @@ export default class P2pServer {
59
59
case MESSAGE_TYPES . transaction :
60
60
this . transactionPool . updateOrAddTransaction ( data . transaction ) ;
61
61
break ;
62
- case MESSAGE_TYPES . clear_transactions :
62
+ case MESSAGE_TYPES . clearTransactions :
63
63
this . transactionPool . clear ( ) ;
64
64
break ;
65
65
}
66
66
} ) ;
67
67
}
68
68
69
- sendChain ( socket : Websocket ) : void {
69
+ public sendChain ( socket : Websocket ) : void {
70
70
socket . send (
71
71
JSON . stringify ( {
72
72
type : MESSAGE_TYPES . chain ,
@@ -75,7 +75,7 @@ export default class P2pServer {
75
75
) ;
76
76
}
77
77
78
- sendTransaction ( socket : Websocket , transaction : Transaction ) : void {
78
+ public sendTransaction ( socket : Websocket , transaction : Transaction ) : void {
79
79
socket . send (
80
80
JSON . stringify ( {
81
81
type : MESSAGE_TYPES . transaction ,
@@ -84,19 +84,19 @@ export default class P2pServer {
84
84
) ;
85
85
}
86
86
87
- syncChains ( ) : void {
87
+ public syncChains ( ) : void {
88
88
this . sockets . forEach ( socket => this . sendChain ( socket ) ) ;
89
89
}
90
90
91
- broadcastTransaction ( transaction : Transaction ) : void {
91
+ public broadcastTransaction ( transaction : Transaction ) : void {
92
92
this . sockets . forEach ( socket => this . sendTransaction ( socket , transaction ) ) ;
93
93
}
94
94
95
- broadcastClearTransactions ( ) : void {
95
+ public broadcastClearTransactions ( ) : void {
96
96
this . sockets . forEach ( socket =>
97
97
socket . send (
98
98
JSON . stringify ( {
99
- type : MESSAGE_TYPES . clear_transactions ,
99
+ type : MESSAGE_TYPES . clearTransactions ,
100
100
} ) ,
101
101
) ,
102
102
) ;
0 commit comments