-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.js
32 lines (25 loc) · 917 Bytes
/
miner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const Wallet = require('./wallet');
const Transaction = require('./blockchain/transaction');
class Miner {
constructor(blockchain, transactionPool, wallet, p2pServer) {
this.blockchain = blockchain;
this.transactionPool = transactionPool;
this.wallet = wallet;
this.p2pServer = p2pServer;
}
mine(){
const validTransactions = this.transactionPool.validTransactions();
// include miner's reward
validTransactions.push(Transaction.rewardTransaction(this.wallet, Wallet.blockchainWallet()));
// create a block using valid transactions.
const block = this.blockchain.addBlock(validTransactions);
// syncChains()
this.p2pServer.syncChains();
// clear transactionPool
this.transactionPool.clear();
// and broadcast Transaction pool
this.p2pServer.broadcastClearTransactions();
return block;
}
}
module.exports = Miner;