/
FieldScan.java
171 lines (142 loc) · 4.55 KB
/
FieldScan.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package snowblossom.miner;
import com.google.common.collect.ImmutableSortedMap;
import duckutil.Config;
import snowblossom.lib.Globals;
import snowblossom.lib.NetworkParams;
import snowblossom.lib.SnowFieldInfo;
import snowblossom.lib.Validation;
import snowblossom.proto.SnowPowProof;
import java.io.File;
import java.util.Map;
import java.util.SplittableRandom;
import java.util.TreeMap;
import java.util.logging.Logger;
public class FieldScan
{
private static final Logger logger = Logger.getLogger("snowblossom.miner");
private File path;
private NetworkParams params;
private ImmutableSortedMap<Integer, SnowMerkleProof> availible_fields;
private volatile AutoSnowFall auto_snow;
private Config config;
public FieldScan(File path, NetworkParams params, Config config)
{
this.path = path;
this.params = params;
this.config = config;
scan(true);
}
public void scan(boolean report)
{
TreeMap<Integer, SnowMerkleProof> fmap = new TreeMap<>();
for(Map.Entry<Integer, String> me : params.getFieldSeeds().entrySet())
{
int field_number = me.getKey();
SnowFieldInfo info = params.getSnowFieldInfo(field_number);
String name = me.getValue();
File field_folder = new File(path, name);
if (field_folder.exists() && field_folder.isDirectory())
{
try
{
double precacheGig = config.getDoubleWithDefault("memfield_precache_gb", 0);
int minDepthToDisk = config.getIntWithDefault("min_depth_to_disk", 0);
boolean memfield = config.getBoolean("memfield");
long precache = 0;
if (precacheGig > 0.01)
{
memfield = false;
precache = (long)(precacheGig * 1024.0 * 1024.0 * 1024.0);
}
logger.info("creating field: " + field_folder + " memfield=" + memfield + ", precache=" + precache + ", minDepthToDisk=" + minDepthToDisk);
SnowMerkleProof proof = new SnowMerkleProof(field_folder, name, memfield, precache, minDepthToDisk);
for(int i = 0; i<16; i++)
{
checkField(field_number, proof);
}
fmap.put(field_number, proof);
logger.info(String.format("Snow field %d %s (%s) is working", field_number,
name,
info.getName()));
}
catch(Throwable e)
{
logger.info(String.format("Unable to load %s, error: %s", name, e.toString()));
}
}
else
{
logger.info(String.format("Not loading %d %s (%s), directory not present: %s",
field_number,
name,
info.getName(),
field_folder));
}
}
availible_fields = ImmutableSortedMap.copyOf(fmap);
}
private void checkField(int field_number, SnowMerkleProof proof)
throws java.io.IOException
{
SplittableRandom rnd = new SplittableRandom();
long max = proof.getTotalWords();
long check = rnd.nextLong(max);
SnowPowProof p = proof.getProof(check);
SnowFieldInfo info = params.getSnowFieldInfo(field_number);
if (!Validation.checkProof(p, info.getMerkleRootHash(), info.getLength() ))
{
throw new RuntimeException("proof check failed");
}
}
public synchronized int selectField(int min_field)
{
if (auto_snow != null)
{
if(auto_snow.isDone())
{
auto_snow=null;
scan(true);
}
}
Integer i = availible_fields.ceilingKey(min_field);
if (i == null)
{
if (config.isSet("auto_snow") && config.getBoolean("auto_snow"))
{
if (auto_snow == null)
{
auto_snow = new AutoSnowFall(path, params, min_field);
auto_snow.start();
}
}
if (auto_snow != null)
{
throw new RuntimeException(String.format("Unable to select a field %d but snowfall is working on it", min_field));
}
else
{
throw new RuntimeException(String.format("Unable to select a field of at least %d. Availible: %s", min_field, availible_fields.keySet().toString()));
}
}
return i;
}
public SnowMerkleProof getFieldProof(int f)
{
return availible_fields.get(f);
}
public SnowMerkleProof getSingleUserFieldProof(int f)
throws Exception
{
if (!availible_fields.containsKey(f))
{
return null;
}
if (config.getBoolean("memfield") || config.getDoubleWithDefault("memfield_precache_gb", 0) > 0)
{
return getFieldProof(f);
}
String name = params.getFieldSeeds().get(f);
File field_folder = new File(path, name);
return new SnowMerkleProof(field_folder, name);
}
}