-
Notifications
You must be signed in to change notification settings - Fork 22
/
Node.d
522 lines (387 loc) · 15 KB
/
Node.d
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*******************************************************************************
Implementation of the Node's API.
Copyright:
Copyright (c) 2019 BOS Platform Foundation Korea
All rights reserved.
License:
MIT License. See LICENSE for details.
*******************************************************************************/
module agora.node.Node;
import agora.api.Validator;
import agora.consensus.data.Block;
import agora.common.Amount;
import agora.common.BanManager;
import agora.common.Config;
import agora.common.Hash;
import agora.common.Metadata;
import agora.common.crypto.Key;
import agora.common.Task;
import agora.common.Types;
import agora.common.TransactionPool;
import agora.consensus.data.Enrollment;
import agora.consensus.data.PreimageInfo;
import agora.consensus.data.Transaction;
import agora.consensus.data.UTXOSet;
import agora.consensus.EnrollmentManager;
import agora.consensus.protocol.Nominator;
import agora.network.NetworkManager;
import agora.node.BlockStorage;
import agora.node.Ledger;
import agora.utils.Log;
import agora.utils.PrettyPrinter;
import scpd.types.Stellar_SCP;
import scpd.types.Utils;
import vibe.data.json;
import vibe.web.rest : RestException;
import std.algorithm;
import std.exception;
import std.path : buildPath;
import std.range;
mixin AddLogger!();
/// Maximum number of blocks that will be sent in a call to getBlocksFrom()
private enum uint MaxBatchBlocksSent = 1000;
/*******************************************************************************
Implementation of the Node API
This class implement the business code of the node.
Communication with the other nodes is handled by the `Network` class.
*******************************************************************************/
public class Node : API
{
/// Metadata instance
protected Metadata metadata;
/// Config instance
protected const Config config;
/// Task manager
protected TaskManager taskman;
/// Network of connected nodes
protected NetworkManager network;
/// Reusable exception object
protected RestException exception;
/// Transaction pool
protected TransactionPool pool;
/// Set of unspent transaction outputs
protected UTXOSet utxo_set;
///
protected Ledger ledger;
/// Blockstorage
protected IBlockStorage storage;
/// Nominator instance
protected Nominator nominator;
/// Enrollment manager
protected EnrollmentManager enroll_man;
/// The SCP Quorum set
protected SCPQuorumSet scp_quorum;
/// Ctor
public this (const Config config)
{
this.metadata = this.getMetadata(config.node.data_dir);
this.config = config;
this.scp_quorum = verifyBuildSCPConfig(config.quorum);
this.taskman = this.getTaskManager();
this.network = this.getNetworkManager(config.node, config.banman,
config.network, config.dns_seeds, this.metadata, this.taskman);
this.storage = this.getBlockStorage(config.node.data_dir);
this.pool = this.getPool(config.node.data_dir);
this.utxo_set = this.getUtxoSet(config.node.data_dir);
this.ledger = new Ledger(this.pool, this.utxo_set, this.storage, config.node);
this.enroll_man = this.getEnrollmentManager(config.node.data_dir, config.node);
this.exception = new RestException(
400, Json("The query was incorrect"), string.init, int.init);
}
/***************************************************************************
Verify the quorum configuration, and create a normalized SCPQuorum.
Params:
config = the quorum configuration
Throws:
an Exception if the quorum configuration is invalid
***************************************************************************/
private static SCPQuorumSet verifyBuildSCPConfig (QuorumConfig config)
{
import scpd.scp.QuorumSetUtils;
import agora.network.NetworkClient;
auto scp_quorum = toSCPQuorumSet(config);
normalizeQSet(scp_quorum);
// todo: assertion fails do the misconfigured(?) threshold of 1 which
// is lower than vBlockingSize in QuorumSetSanityChecker::checkSanity
const ExtraChecks = false;
const(char)* reason;
if (!isQuorumSetSane(scp_quorum, ExtraChecks, &reason))
{
import std.conv;
string failure = reason.to!string;
log.fatal(failure);
throw new Exception(failure);
}
return scp_quorum;
}
/// The first task method, loading from disk, node discovery, etc
public void start ()
{
log.info("Doing network discovery..");
auto peers = this.network.discover();
this.network.retrieveLatestBlocks(this.ledger);
// nothing to do
if (!this.config.node.is_validator)
return;
import agora.common.Set;
import std.typecons;
void getNodes (QuorumConfig conf, ref bool[PublicKey] nodes)
{
foreach (node; conf.nodes)
nodes[node] = true;
foreach (sub_conf; conf.quorums)
getNodes(sub_conf, nodes);
}
// can't use Set(), requires serialization support
bool[PublicKey] quorum_keys;
getNodes(this.config.quorum, quorum_keys);
auto quorum_peers = peers.byKeyValue
.filter!(item => item.key in quorum_keys)
.map!(item => tuple(item.key, item.value))
.assocArray();
this.nominator = new Nominator(this.config.node.key_pair,
this.ledger, this.taskman, quorum_peers, this.scp_quorum);
}
/***************************************************************************
Called on node shutdown.
Note that this is called explicitly before any destructors,
to allow clean shutdown of e.g. databases, which may require
GC allocations during the shutdown phase.
***************************************************************************/
public void shutdown ()
{
log.info("Shutting down..");
this.network.dumpMetadata();
this.pool.shutdown();
this.pool = null;
this.utxo_set.shutdown();
this.utxo_set = null;
this.enroll_man.shutdown();
this.enroll_man = null;
}
/// GET /public_key
public override PublicKey getPublicKey () pure nothrow @safe @nogc
{
return this.config.node.key_pair.address;
}
/// GET: /network_info
public override NetworkInfo getNetworkInfo () pure nothrow @safe @nogc
{
return this.network.getNetworkInfo();
}
/***************************************************************************
Receive a transaction.
API:
PUT /transaction
Params:
tx = the received transaction
***************************************************************************/
public override void putTransaction (Transaction tx) @safe
{
log.trace("Received Transaction: {}", prettify(tx));
auto tx_hash = hashFull(tx);
if (this.ledger.hasTransactionHash(tx_hash))
return;
if (this.ledger.acceptTransaction(tx))
{
this.network.sendTransaction(tx);
this.ledger.tryNominateTXSet();
}
if (this.enroll_man.needRevealPreimage(this.ledger.getBlockHeight()))
{
PreimageInfo preimage;
if (this.enroll_man.getNextPreimage(preimage))
{
this.receivePreimage(preimage);
this.enroll_man.increaseNextRevealHeight();
}
}
}
/***************************************************************************
Receive an SCP envelope.
API:
GET /envelope
Params:
envelope = the SCP envelope
Returns:
true if the envelope was accepted
***************************************************************************/
public bool receiveEnvelope (SCPEnvelope envelope)
{
// we should not receive SCP messages unless we're a validator node
if (!this.config.node.is_validator)
return false;
return this.nominator.receiveEnvelope(envelope);
}
/// GET: /has_transaction_hash
public override bool hasTransactionHash (Hash tx) @safe
{
return this.ledger.hasTransactionHash(tx);
}
/// GET: /block_height
public ulong getBlockHeight ()
{
return this.ledger.getBlockHeight();
}
/// GET: /blocks_from
public const(Block)[] getBlocksFrom (ulong block_height, uint max_blocks)
@safe
{
return this.ledger.getBlocksFrom(block_height)
.take(min(max_blocks, MaxBatchBlocksSent)).array;
}
/***************************************************************************
Returns an instance of a NetworkManager
Unittests can override this method and return a custom NetworkManager.
Params:
node_config = the node config
banman_conf = the ban manager config
peers = the peers to connect to
dns_seeds = the DNS seeds to retrieve peers from
metadata = metadata containing known peers and other meta info
taskman = task manager
Returns:
an instance of a NetworkManager
***************************************************************************/
protected NetworkManager getNetworkManager (in NodeConfig node_config,
in BanManager.Config banman_conf, in string[] peers,
in string[] dns_seeds, Metadata metadata, TaskManager taskman)
{
return new NetworkManager(node_config, banman_conf, peers, dns_seeds,
metadata, taskman);
}
/***************************************************************************
Returns an instance of a TaskManager
Subclasses can override this method and return
a TaskManager backed by LocalRest.
Returns:
the task manager
***************************************************************************/
protected TaskManager getTaskManager ()
{
return new TaskManager();
}
/***************************************************************************
Returns an instance of a TransactionPool
Subclasses can override this method and return
a TransactionPool backed by an in-memory SQLite database.
Params:
data_dir = path to the data directory
Returns:
the transaction pool
***************************************************************************/
protected TransactionPool getPool (string data_dir)
{
return new TransactionPool(buildPath(
config.node.data_dir, "tx_pool.dat"));
}
/***************************************************************************
Returns an instance of a UTXOSet
Unittest code may override this method to provide a Utxo set
that doesn't do any I/O.
Params:
data_dir = path to the data directory
Returns:
the UTXOSet instance
***************************************************************************/
protected UTXOSet getUtxoSet (string data_dir)
{
return new UTXOSet(buildPath(config.node.data_dir, "utxo_set.dat"));
}
/***************************************************************************
Reads the metadata from the provided disk path.
Subclasses can override this method and return
a Metadata object which loads/dumps data in memory
rather than on disk, to avoid I/O (e.g. for unittesting)
Note: not exposed in the API.
Params:
data_dir = path to the data directory
Returns:
the metadata loaded from disk
***************************************************************************/
protected Metadata getMetadata (string data_dir) @system
{
return new DiskMetadata(data_dir);
}
/***************************************************************************
Returns an instance of a BlockStorage or MemoryStorage
Note: not exposed in the API.
Params:
data_dir = path to the blockdata directory
Returns:
Returns instance of `MemoryStorage` if data_dir is empty,
otherwise returns instance of `BlockStorage`
***************************************************************************/
protected IBlockStorage getBlockStorage (string data_dir) @system
{
version (unittest)
{
return new MemBlockStorage();
}
else
{
return new BlockStorage(data_dir);
}
}
/***************************************************************************
Returns an instance of a EnrollmentManager
Params:
data_dir = path to the data dirctory
node_config = the node config
Returns:
the enrollment manager
***************************************************************************/
protected EnrollmentManager getEnrollmentManager (string data_dir,
in NodeConfig node_config)
{
return new EnrollmentManager(buildPath(data_dir, "validator_set.dat"),
node_config.key_pair);
}
/***************************************************************************
Create an enrollment data for enrollment process
Params:
frozen_utxo_hash = the hash of a frozen UTXO used to identify
a validator or an enrollment data
Returns:
the Enrollment object created
***************************************************************************/
protected Enrollment createEnrollment (Hash frozen_utxo_hash) @safe
{
Enrollment enroll;
this.enroll_man.createEnrollment(frozen_utxo_hash, enroll);
return enroll;
}
/// GET: /merkle_path
public Hash[] getMerklePath (ulong block_height, Hash hash) @safe
{
return this.ledger.getMerklePath(block_height, hash);
}
/// PUT: /enroll_validator
public void enrollValidator (Enrollment enroll) @safe
{
log.trace("Received Enrollment: {}", prettify(enroll));
if (this.enroll_man.add(this.ledger.getBlockHeight(),
this.utxo_set.getUTXOFinder(),
enroll))
{
this.network.sendEnrollment(enroll);
}
}
/// GET: /has_enrollment
public bool hasEnrollment (Hash enroll_hash) @safe
{
return this.enroll_man.hasEnrollment(enroll_hash);
}
/// PUT: /receive_preimage
public void receivePreimage (PreimageInfo preimage) @safe
{
log.trace("Received Preimage: {}", prettify(preimage));
if (this.enroll_man.addPreimage(preimage))
this.network.sendPreimage(preimage);
}
/// GET: /has_preimage
public bool hasPreimage (Hash enroll_key, ulong height)
{
return this.enroll_man.hasPreimage(enroll_key, height);
}
}