-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer.js
More file actions
90 lines (72 loc) · 2.45 KB
/
miner.js
File metadata and controls
90 lines (72 loc) · 2.45 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const { Transaction } = require('./blockchain');
class Miner {
constructor(address, blockchain, p2pNode) {
this.address = address;
this.blockchain = blockchain;
this.p2pNode = p2pNode;
this.isMining = false;
}
startMining(onBlockMined = null) {
if (this.isMining) {
console.log('[Miner] Mining already in progress');
return;
}
this.isMining = true;
console.log(`[Miner] Starting mining with address: ${this.address}`);
const miningInterval = setInterval(() => {
if (!this.isMining) {
clearInterval(miningInterval);
return;
}
if (this.blockchain.pendingTransactions.length > 0) {
console.log('[Miner] Mining new block...');
const block = this.blockchain.minePendingTransactions(this.address);
console.log(`[Miner] ✓ Block mined: ${block.calculateHash()}`);
console.log(`[Miner] Block index: ${block.index}`);
console.log(`[Miner] Miner reward: +10 V`);
if (this.p2pNode) {
this.p2pNode.broadcastNewBlock(block);
}
if (onBlockMined) {
onBlockMined(block);
}
}
}, 5000);
return miningInterval;
}
stopMining() {
this.isMining = false;
console.log('[Miner] Mining stopped');
}
castVote(blockIndex, voteValue = true) {
const votePercentage = this.blockchain.addVoteToLatestBlock(
this.address,
voteValue
);
console.log(`[Vote] ${this.address} voted ${voteValue ? 'YES' : 'NO'}`);
console.log(`[Vote] Current approval: ${votePercentage.toFixed(2)}%`);
if (this.p2pNode) {
this.p2pNode.broadcastVote(this.address, blockIndex, voteValue);
}
if (this.blockchain.isVoteApprovedForLatestBlock()) {
console.log('✓ Block approved by vote!');
return { approved: true, percentage: votePercentage };
}
return { approved: false, percentage: votePercentage };
}
getBlockchainStats() {
const balance = this.blockchain.getBalance(this.address);
const chainLength = this.blockchain.chain.length;
const pendingCount = this.blockchain.pendingTransactions.length;
return {
minerAddress: this.address,
balance,
chainLength,
pendingTransactions: pendingCount,
isChainValid: this.blockchain.isChainValid(),
latestBlockHash: this.blockchain.getLatestBlock().calculateHash(),
voteThreshold: `${this.blockchain.voteThreshold}%`
};
}
}
module.exports = { Miner };