-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMajorityMiner.java
59 lines (46 loc) · 1.52 KB
/
MajorityMiner.java
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
package in.ac.iitk.cse.cs252.miners;
import in.ac.iitk.cse.cs252.blockchain.Block;
import in.ac.iitk.cse.cs252.blockchain.NetworkStatistics;
public class MajorityMiner extends BaseMiner implements Miner{
private Block currentHead, myHead;
private int totalHashRate=1, myHashRate=1;
public MajorityMiner(String id, int hashRate, int connectivity) {
super(id, hashRate, connectivity);
this.myHashRate = hashRate;
}
@Override
public Block currentlyMiningAt() {
return myHead;
}
@Override
public Block currentHead() {
return currentHead;
}
@Override
public void blockMined(Block block, boolean isMinerMe) {
if(isMinerMe) {
if (block.getHeight() > currentHead.getHeight()) {
this.currentHead = block;
this.myHead = block;
}
}
else {
if (currentHead == null) {
this.currentHead = block;
this.myHead = block;
}
else if (block != null && ( ((1.0*myHashRate)/(totalHashRate+1) < 0.51) || (block.getHeight() - myHead.getHeight() > 6) )) {
this.currentHead = block;
this.myHead = block;
}
}
}
@Override
public void initialize(Block genesis, NetworkStatistics networkStatistics) {
this.myHead = genesis;
}
@Override
public void networkUpdate(NetworkStatistics statistics) {
this.totalHashRate = statistics.getTotalHashRate();
}
}