Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
226 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// @flow | ||
|
||
import WebSocket from 'ws'; | ||
import Blockchain, { Block, Transaction } from '../blockchain'; | ||
import Miner from '../miner'; | ||
import P2pServer from './p2p_server'; | ||
import MESSAGE_TYPES from './message_types'; | ||
import type { MessageData } from './message_types'; | ||
|
||
class Router { | ||
blockchain: Blockchain; | ||
miner: Miner; | ||
sockets: Array<WebSocket>; | ||
p2pServer: P2pServer; | ||
|
||
constructor(blockchain: Blockchain, p2pEnabled: bool = true) { | ||
this.blockchain = blockchain; | ||
this.p2pServer = new P2pServer(this.blockchain, this.messageHandler.bind(this), p2pEnabled); | ||
} | ||
|
||
subscribe(miner: Miner) { | ||
this.miner = miner; | ||
} | ||
|
||
pushTransaction(tx: Transaction) { | ||
if (this.miner) { | ||
this.miner.pushTransaction(tx); | ||
} | ||
this.p2pServer.broadcastTransaction(tx); | ||
} | ||
|
||
mineDone() { | ||
this.p2pServer.broadcastMined(); | ||
} | ||
|
||
messageHandler(data: MessageData) { | ||
switch (data.type) { | ||
case MESSAGE_TYPES.BLOCK_CHAIN: | ||
this.blockchain.replaceChain(data.payload.map(o => Block.fromJSON(o))); | ||
break; | ||
case MESSAGE_TYPES.TRANSACTION: | ||
if (this.miner) { | ||
this.miner.pushTransaction(Transaction.fromJSON(data.payload)); | ||
} | ||
break; | ||
case MESSAGE_TYPES.MINED: | ||
this.blockchain.replaceChain(data.payload.map(o => Block.fromJSON(o))); | ||
if (this.miner) { | ||
this.miner.clearTransactions(); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const MESSAGE_TYPES = { | ||
BLOCK_CHAIN: 'BLOCK_CHAIN', | ||
TRANSACTION: 'TRANSACTION', | ||
MINED: 'MINED', | ||
}; | ||
|
||
type MessageData = { | ||
type: string, | ||
payload: any, | ||
}; | ||
|
||
export default MESSAGE_TYPES; | ||
export type { MessageData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// @flow | ||
|
||
import WebSocket from 'ws'; | ||
import Blockchain, { Transaction } from '../blockchain'; | ||
import MESSAGE_TYPES from './message_types'; | ||
import type { MessageData } from './message_types'; | ||
import { P2P_PORT, PEERS } from '../config'; | ||
|
||
class P2pServer { | ||
blockchain: Blockchain; | ||
messageHandler: Function; | ||
sockets: Array<WebSocket>; | ||
|
||
constructor(blockchain: Blockchain, messageHandler: Function, p2pEnabled: bool) { | ||
this.sockets = []; | ||
this.blockchain = blockchain; | ||
this.messageHandler = messageHandler; | ||
if (p2pEnabled) { | ||
this.listen(); | ||
} | ||
} | ||
|
||
listen() { | ||
try { | ||
const server = new WebSocket.Server({ port: P2P_PORT }); | ||
server.on('connection', socket => this.connect2Socket(socket)); | ||
this.connect2Peers(); | ||
console.log(`P2P Listening on ${P2P_PORT}`); | ||
} catch (e) { | ||
console.log('P2Pサーバの起動に失敗しました。'); | ||
} | ||
} | ||
|
||
connect2Peers() { | ||
PEERS.forEach((peer) => { | ||
const socket = new WebSocket(peer); | ||
socket.on('open', () => this.connect2Socket(socket)); | ||
}); | ||
} | ||
|
||
connect2Socket(socket: WebSocket) { | ||
this.sockets.push(socket); | ||
console.log('Socket connected'); | ||
socket.on('message', (message) => { | ||
console.log(`received from peer: ${message}`); | ||
const data: MessageData = JSON.parse(message); | ||
this.messageHandler(data); | ||
}); | ||
this.sendBlockchain(socket); | ||
} | ||
|
||
sendBlockchain(socket: WebSocket) { | ||
P2pServer.sendData(socket, { | ||
type: MESSAGE_TYPES.BLOCK_CHAIN, | ||
payload: this.blockchain.chain.map(b => b.toJSON()), | ||
}); | ||
} | ||
|
||
broadcastTransaction(transaction: Transaction) { | ||
this.sockets.forEach(socket => P2pServer.sendData(socket, { | ||
type: MESSAGE_TYPES.TRANSACTION, | ||
payload: transaction.toJSON(), | ||
})); | ||
} | ||
|
||
broadcastMined() { | ||
this.sockets.forEach(socket => P2pServer.sendData(socket, { | ||
type: MESSAGE_TYPES.MINED, | ||
payload: this.blockchain.chain.map(b => b.toJSON()), | ||
})); | ||
} | ||
|
||
static sendData(socket: WebSocket, data: MessageData) { | ||
socket.send(JSON.stringify(data)); | ||
} | ||
} | ||
|
||
export default P2pServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters