-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuerrillaMiner.java
65 lines (50 loc) · 1.78 KB
/
GuerrillaMiner.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
63
64
65
package edu.nyu.crypto.csci3033.miners;
import edu.nyu.crypto.csci3033.blockchain.Block;
import edu.nyu.crypto.csci3033.blockchain.NetworkStatistics;
public class GuerrillaMiner extends BaseMiner implements Miner{
private Block currentHead;
private Block currentlyMiningAt;
private double expectedBlockRewardSum = 0;
private double blockProbability;
private int counter = 0;
public GuerrillaMiner(String id, int hashRate, int connectivity) {
super(id, hashRate, connectivity);
}
@Override
public Block currentlyMiningAt() {
return currentHead;
}
@Override
public Block currentHead() {
return currentHead;
}
@Override
public void blockMined(Block block, boolean isMinerMe) {
this.expectedBlockRewardSum += block.getBlockValue();
this.counter ++;
double expectedBlockReward = this.expectedBlockRewardSum/this.counter; //update expected block reward everytime you hear about a block
if(isMinerMe) {
if (block.getHeight() > currentHead.getHeight()) {
this.currentHead = block;
}
}
else{
if (currentHead == null) {
currentHead = block;
} else if (block != null && block.getHeight() > currentHead.getHeight()) {
this.currentHead = block;
}
}
}
@Override
public void initialize(Block genesis, NetworkStatistics networkStatistics) {
this.currentHead = genesis;
this.currentlyMiningAt = genesis;
//this.expectedBlockRewardSum = 0;
networkUpdate(networkStatistics);
}
@Override
public void networkUpdate(NetworkStatistics statistics) {
this.blockProbability = (double)this.getHashRate()/statistics.getTotalHashRate();
}
}