-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiner.js
28 lines (23 loc) · 884 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
class Miner {
constructor(name, blockchain, wallet) {
this.name = name;
this.wallet = wallet;
this.blockchain = blockchain;
}
mineBlock() {
const latestBlock = this.blockchain.getMaximumHeightBlock();
let candidateBlock = latestBlock.createChild(this.wallet.getAddress(), this.blockchain.difficulty);
const pendingTransactions = this.blockchain.getPendingTransactions();
for (const pendingTransaction in pendingTransactions) {
candidateBlock.addTransactionIfValid(pendingTransactions[pendingTransaction]);
}
const { mined, error } = this.blockchain.mineBlock(candidateBlock, this.wallet.getAddress());
if (mined) console.log(`Block #${candidateBlock._header.height} has been mined`);
else {
console.log(`Error mining block #${candidateBlock._header.height} REASON: ${error}`)
}
}
}
module.exports = {
Miner
};