-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelfishMiner.java
74 lines (61 loc) · 2.62 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
63
64
65
66
67
68
69
70
71
72
73
74
package edu.nyu.crypto.csci3033.miners;
import edu.nyu.crypto.csci3033.blockchain.Block;
import edu.nyu.crypto.csci3033.blockchain.NetworkStatistics;
public class SelfishMiner extends BaseMiner implements Miner{
private Block currentHead = null;
private Block currentlyMiningAt = null;
private int privateBranchLength = 0; //keeps track of the private branch length
private Block privateChain = null; //keeps track of the private bloks
public SelfishMiner(String id, int hashRate, int connectivity) {
super(id, hashRate, connectivity);
}
@Override
public Block currentlyMiningAt() {
return currentlyMiningAt;
}
@Override
public Block currentHead() {
return currentHead;
}
@Override
public void blockMined(Block block, boolean isMinerMe) {
int chainDistance = this.privateChain.getHeight() - this.currentHead.getHeight(); //check how far ahead is your chain
if(isMinerMe) { //if i mined it
this.privateChain = block; //add it to my private chain
privateBranchLength ++; //increase its length
if (chainDistance == 0 && privateBranchLength == 2) { //if chain distance is 0 but the branch length is 2
this.currentHead = this.privateChain; //make your private chain public
this.privateBranchLength = 0; //reset privateBranchLength
}
this.currentlyMiningAt = this.privateChain; //mine at your private chain (could be announced)
}else{
this.currentHead = block; //add it as you header
if (chainDistance == 0) { //attack loses (give up)
this.privateChain = this.currentHead;
this.privateBranchLength = 0;
} else if (chainDistance == 1) { //same length so try our luck
this.currentHead = this.privateChain;
} else if (chainDistance == 2) { //we win so make everything public to orphan the block
this.currentHead = this.privateChain;
this.privateBranchLength = 0;
} else { //publish the first unpublished block in the private chain
Block blockToPublish = this.privateChain;
for (int i = 0; i < chainDistance - 1; i++) {
blockToPublish = blockToPublish.getPreviousBlock();
}
this.currentHead = blockToPublish;
}
this.currentlyMiningAt = this.privateChain; //mine at your private chain
}
}
@Override
public void initialize(Block genesis, NetworkStatistics networkStatistics) {
this.currentHead = genesis;
//this.currentlyMiningAt = genesis;
this.privateChain = genesis;
this.privateBranchLength = 0;
}
@Override
public void networkUpdate(NetworkStatistics statistics) {
}
}