-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelfishMiner.java
62 lines (49 loc) · 1.6 KB
/
SelfishMiner.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
60
61
62
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 SelfishMiner extends BaseMiner implements Miner{
private Block currentHead, myHead;
public SelfishMiner(String id, int hashRate, int connectivity) {
super(id, hashRate, connectivity);
}
@Override
public Block currentlyMiningAt() {
return myHead;
}
@Override
public Block currentHead() {
return currentHead;
}
@Override
public void blockMined(Block block, boolean isMinerMe) {
if(isMinerMe) {
if(myHead == null){
this.myHead = block;
}
else if (block != null && block.getHeight() > myHead.getHeight()) {
this.myHead = block;
//this.currentHead = block;
}
}
else{
if(myHead == null) {
//currentHead = block;
this.myHead = block;
}
else if( block != null && (myHead.getHeight() - block.getHeight() <=1 ) && myHead.getHeight() >= block.getHeight() ){
this.currentHead = myHead;
}
else if(block != null && myHead.getHeight() < block.getHeight()) {
this.currentHead = block;
this.myHead = block;
}
}
}
@Override
public void initialize(Block genesis, NetworkStatistics networkStatistics) {
this.myHead = genesis;
}
@Override
public void networkUpdate(NetworkStatistics statistics) {
}
}