-
Notifications
You must be signed in to change notification settings - Fork 13
/
GenerateSupr.java
120 lines (103 loc) · 3.02 KB
/
GenerateSupr.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package pocminer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import nxt.util.Convert;
import akka.actor.*;
import akka.routing.RoundRobinPool;
import pocminer.PlotGenerator;
import pocminer.util.MiningPlot;
public class GenerateSupr extends UntypedActor {
GenParams params = null;
ActorRef workers = null;
long currentNonce;
long recvresults;
byte[] outbuffer;
FileOutputStream out;
public GenerateSupr(GenParams params) {
super();
this.params = params;
}
@Override
public void onReceive(Object message) throws Exception {
if(message instanceof PlotGenerator.msgGenerateResult) {
recvresults++;
processPlot(((PlotGenerator.msgGenerateResult)message).plot,
((PlotGenerator.msgGenerateResult)message).nonce);
if(recvresults >= params.staggeramt) {
System.out.println("Writing from nonce " + currentNonce);
out.write(outbuffer);
currentNonce += params.staggeramt;
if(currentNonce < params.startnonce + params.plots) {
sendWork();
}
else {
out.close();
getContext().system().shutdown();
}
}
}
else {
unhandled(message);
}
}
@Override
public void preStart() {
init();
}
private void init() {
workers = getContext().actorOf(new RoundRobinPool(params.threads).props(Props.create(PlotGenerator.class)));
currentNonce = params.startnonce;
outbuffer = new byte[(int) (params.staggeramt * MiningPlot.PLOT_SIZE)];
String outname = Convert.toUnsignedLong(params.addr);
outname += "_";
outname += String.valueOf(params.startnonce);
outname += "_";
outname += String.valueOf(params.plots);
outname += "_";
outname += String.valueOf(params.staggeramt);
try {
File folder = new File("plots");
if(!folder.exists()) {
folder.mkdir();
}
out = new FileOutputStream(new File("plots/" + outname), false);
} catch (FileNotFoundException e) {
System.out.println("Failed to open file" + outname + "for writing");
e.printStackTrace();
getContext().system().shutdown();
}
sendWork();
}
private void processPlot(MiningPlot p, long nonce) {
long off = nonce - currentNonce;
for(int i = 0; i < MiningPlot.SCOOPS_PER_PLOT; i++) {
System.arraycopy(p.data,
i * MiningPlot.SCOOP_SIZE,
outbuffer,
(int) ((i * MiningPlot.SCOOP_SIZE * params.staggeramt) + (off * MiningPlot.SCOOP_SIZE)),
MiningPlot.SCOOP_SIZE);
}
}
private void sendWork() {
recvresults = 0;
System.out.println("Generating from nonce: " + currentNonce);
for(long i = 0; i < params.staggeramt; i++) {
workers.tell(new PlotGenerator.msgGenerate(params.addr, currentNonce + i), getSelf());
}
}
public static class GenParams {
public long addr;
public long startnonce;
public long plots;
public long staggeramt;
public int threads;
public GenParams(long addr, long startnonce, long plots, long staggeramt, int threads) {
this.addr = addr;
this.startnonce = startnonce;
this.plots = plots;
this.staggeramt = staggeramt;
this.threads = threads;
}
}
}