-
Notifications
You must be signed in to change notification settings - Fork 28
/
SnowBlossomNode.java
281 lines (232 loc) · 7.7 KB
/
SnowBlossomNode.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package snowblossom.node;
import com.google.common.collect.ImmutableList;
import duckutil.Config;
import duckutil.ConfigFile;
import duckutil.ConfigMem;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.netty.handler.ssl.SslContext;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import snowblossom.client.WalletUtil;
import snowblossom.lib.*;
import snowblossom.lib.db.DB;
import snowblossom.lib.db.lobstack.LobstackDB;
import snowblossom.lib.db.rocksdb.JRocksDB;
import snowblossom.lib.tls.CertGen;
import snowblossom.lib.trie.HashedTrie;
import snowblossom.lib.trie.TrieDBMap;
import snowblossom.proto.WalletDatabase;
import snowblossom.lib.SystemUtil;
public class SnowBlossomNode
{
private static final Logger logger = Logger.getLogger("snowblossom.node");
public static void main(String args[]) throws Exception
{
Globals.addCryptoProvider();
if (args.length != 1)
{
logger.log(Level.SEVERE, "Incorrect syntax. Syntax: SnowBlossomNode <config_file>");
System.exit(-1);
}
ConfigFile config = new ConfigFile(args[0]);
LogSetup.setup(config);
//LogSetup.listLoggers();
new SnowBlossomNode(config);
while(true)
{
Thread.sleep(2500);
LogSetup.fixLevels();
//LogSetup.listLoggers();
}
}
private Config config;
private SnowUserService user_service;
private SnowPeerService peer_service;
private DB db;
private NetworkParams params;
private BlockIngestor ingestor;
private BlockForge forge;
private MemPool mem_pool;
private Peerage peerage;
private BlockHeightCache block_height_cache;
private ImmutableList<Integer> service_ports;
private ImmutableList<Integer> tls_service_ports;
private AddressSpecHash node_tls_address;
private ImmutableList<StatusInterface> status_list;
private volatile boolean terminate;
public SnowBlossomNode(Config config)
throws Exception
{
this(config, ImmutableList.of(new StatusLogger()));
}
public SnowBlossomNode(Config config, List<StatusInterface> status_list)
throws Exception
{
this.config = config;
this.status_list = ImmutableList.copyOf(status_list);
setStatus("Initializing SnowBlossomNode");
config.require("db_type");
logger.info(String.format("Starting SnowBlossomNode version %s", Globals.VERSION));
setupParams();
loadDB();
loadWidgets();
startServices();
startWidgets();
setStatus("SnowBlossomNode started");
}
public void stop()
{
terminate=true;
}
private void setupParams()
{
params = NetworkParams.loadFromConfig(config);
}
public void setStatus(String status)
{
for(StatusInterface si : status_list)
{
si.setStatus(status);
}
}
private void loadWidgets()
throws Exception
{
ingestor = new BlockIngestor(this);
forge = new BlockForge(this);
mem_pool = new MemPool(db.getUtxoHashedTrie(), ingestor);
peerage = new Peerage(this);
mem_pool.setPeerage(peerage);
block_height_cache = new BlockHeightCache(this);
}
private void startWidgets()
{
logger.fine("Widget start");
peerage.start();
new TimeWatcher().start();
}
private void startServices()
throws Exception
{
user_service = new SnowUserService(this);
peer_service = new SnowPeerService(this);
LinkedList<Integer> ports = new LinkedList<>();
LinkedList<Integer> tls_ports = new LinkedList<>();
if (config.isSet("service_port"))
{
for(String port_str : config.getList("service_port"))
{
int port = Integer.parseInt(port_str);
ports.add(port);
Server s = ServerBuilder
.forPort(port)
.addService(user_service)
.addService(peer_service)
.build();
s.start();
}
}
if (config.isSet("tls_service_port"))
{
config.require("tls_key_path");
TreeMap<String, String> wallet_config_map = new TreeMap<>();
wallet_config_map.put("wallet_path", config.get("tls_key_path"));
wallet_config_map.put("key_count", "1");
wallet_config_map.put("key_mode", WalletUtil.MODE_STANDARD);
ConfigMem config_wallet = new ConfigMem(wallet_config_map);
File wallet_path = new File(config_wallet.get("wallet_path"));
WalletDatabase wallet_db = WalletUtil.loadWallet(wallet_path, true, params);
if (wallet_db == null)
{
logger.log(Level.WARNING, String.format("Directory %s does not contain tls keys, creating new keys", wallet_path.getPath()));
wallet_db = WalletUtil.makeNewDatabase(config_wallet, params);
WalletUtil.saveWallet(wallet_db, wallet_path);
}
node_tls_address = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
logger.info("My TLS address: " + AddressUtil.getAddressString(Globals.NODE_ADDRESS_STRING, node_tls_address));
SslContext ssl_ctx = CertGen.getServerSSLContext(wallet_db);
for(String port_str : config.getList("tls_service_port"))
{
int port = Integer.parseInt(port_str);
tls_ports.add(port);
Server s = NettyServerBuilder
.forPort(port)
.addService(user_service)
.addService(peer_service)
.sslContext(ssl_ctx)
.build();
s.start();
}
}
service_ports = ImmutableList.copyOf(ports);
tls_service_ports = ImmutableList.copyOf(tls_ports);
logger.info("Ports: " + service_ports + " " + tls_service_ports);
user_service.start();
}
private void loadDB()
throws Exception
{
String db_type = config.get("db_type");
if(db_type.equals("rocksdb"))
{
if (!SystemUtil.isJvm64Bit())
{
logger.log(Level.SEVERE,"Java Virtual Machine is 32-bit. rocksdb does not work with 32-bit jvm.");
logger.log(Level.SEVERE,"Upgrade to 64-bit JVM or set db_type=lobstack");
throw new RuntimeException("Needs 64-bit JVM for rocksdb");
}
db = new DB(config, new JRocksDB(config));
}
else if (db_type.equals("lobstack"))
{
db = new DB(config, new LobstackDB(config));
}
else
{
logger.log(Level.SEVERE, String.format("Unknown db_type: %s", db_type));
throw new RuntimeException("Unable to load DB");
}
db.open();
}
public boolean areWeSynced()
{
// Regtest network doesn't have this check for single instance networks
if (params.getNetworkName().equals("spoon")) return true;
int height = 0;
int seen_height = 0;
if (peerage.getConnectedPeerCount() == 0)
{
return false;
}
if (getBlockIngestor().getHead() != null)
{
height = getBlockIngestor().getHead().getHeader().getBlockHeight();
}
if (peerage.getHighestSeenHeader() != null)
{
seen_height = getPeerage().getHighestSeenHeader().getBlockHeight();
}
int diff = seen_height - height;
if (diff < 10) return true; //whatever
return false;
}
public Config getConfig(){return config;}
public DB getDB(){return db;}
public NetworkParams getParams(){return params;}
public BlockIngestor getBlockIngestor(){ return ingestor; }
public BlockForge getBlockForge() {return forge;}
public HashedTrie getUtxoHashedTrie(){return db.getUtxoHashedTrie();}
public MemPool getMemPool(){return mem_pool;}
public Peerage getPeerage(){return peerage;}
public SnowUserService getUserService() {return user_service;}
public BlockHeightCache getBlockHeightCache() {return block_height_cache; }
public ImmutableList<Integer> getServicePorts() {return service_ports;}
public ImmutableList<Integer> getTlsServicePorts() {return tls_service_ports;}
public AddressSpecHash getTlsAddress(){return node_tls_address;}
}