-
Notifications
You must be signed in to change notification settings - Fork 0
/
mining-node.class.js
64 lines (58 loc) · 1.87 KB
/
mining-node.class.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
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
class MiningNode {
isMining = false;
currentBlock;
/**
*
* @param {*} id hier entweder 0, 1 oder 2
* @param {*} name Name der Miningnode für den Konstrukor
*/
constructor(id, name) {
this.id = id;
this.name = name;
this.blockData = { transactions: [{ from: 'BlockReward', to: this.name, amount: 5 }] };
renderCurrentTransactions(this.blockData.transactions);
broadcaster.subscribe((nodeID) => {
console.log('Nachricht empfangen:', nodeID);
if (nodeID !== this.id) {
this.killCurrentBlock();
}
});
newTransaction.subscribe((transaction) => {
this.blockData.transactions.push(transaction);
renderCurrentTransactions(this.blockData.transactions);
});
}
/**
* Toggle Funktion soll das Mining anschalten oder ausschalten in Abhängigkeit der
* Variable isMining
*/
toggle() {
this.isMining = !this.isMining;
if (this.isMining) {
this.mine();
} else {
this.killCurrentBlock();
}
}
/**
* Dient dafür, den aktuellen zu Minenden Block abzubrechen
*/
killCurrentBlock() {
if (this.currentBlock) {
this.currentBlock.kill = true;
}
this.blockData = { transactions: [{ from: 'BlockReward', to: this.name, amount: 5 }] };
}
/**
* Soll anzeigen, ob die Node aktuell arbeitet oder nichts macht
*/
async mine() {
renderCurrentTransactions(this.blockData.transactions);
this.currentBlock = new Block(Date.now(), this.blockData);
await blockchain.addBlock(this.currentBlock, this.id);
if (this.isMining) {
this.blockData = { transactions: [{ from: 'BlockReward', to: this.name, amount: 5 }] };
this.mine();
}
}
}