This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPendulum.java
More file actions
335 lines (311 loc) · 16.7 KB
/
Copy pathPendulum.java
File metadata and controls
335 lines (311 loc) · 16.7 KB
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
package net.helix.pendulum;
import net.helix.pendulum.conf.PendulumConfig;
import net.helix.pendulum.conf.TipSelConfig;
import net.helix.pendulum.controllers.TipsViewModel;
import net.helix.pendulum.controllers.TransactionViewModel;
import net.helix.pendulum.network.Node;
import net.helix.pendulum.network.TransactionRequester;
import net.helix.pendulum.network.UDPReceiver;
import net.helix.pendulum.network.impl.TransactionRequesterWorkerImpl;
import net.helix.pendulum.network.replicator.Replicator;
import net.helix.pendulum.service.TipsSolidifier;
import net.helix.pendulum.service.ledger.impl.LedgerServiceImpl;
import net.helix.pendulum.service.milestone.impl.*;
import net.helix.pendulum.service.snapshot.SnapshotException;
import net.helix.pendulum.service.snapshot.impl.LocalSnapshotManagerImpl;
import net.helix.pendulum.service.snapshot.impl.SnapshotProviderImpl;
import net.helix.pendulum.service.snapshot.impl.SnapshotServiceImpl;
import net.helix.pendulum.service.spentaddresses.SpentAddressesException;
import net.helix.pendulum.service.spentaddresses.impl.SpentAddressesProviderImpl;
import net.helix.pendulum.service.spentaddresses.impl.SpentAddressesServiceImpl;
import net.helix.pendulum.service.stats.TransactionStatsPublisher;
import net.helix.pendulum.service.tipselection.EntryPointSelector;
import net.helix.pendulum.service.tipselection.RatingCalculator;
import net.helix.pendulum.service.tipselection.TailFinder;
import net.helix.pendulum.service.tipselection.TipSelector;
import net.helix.pendulum.service.tipselection.Walker;
import net.helix.pendulum.service.tipselection.impl.CumulativeWeightCalculator;
import net.helix.pendulum.service.tipselection.impl.EntryPointSelectorImpl;
import net.helix.pendulum.service.tipselection.impl.TailFinderImpl;
import net.helix.pendulum.service.tipselection.impl.TipSelectorImpl;
import net.helix.pendulum.service.tipselection.impl.WalkerAlpha;
import net.helix.pendulum.service.transactionpruning.TransactionPruningException;
import net.helix.pendulum.service.transactionpruning.async.AsyncTransactionPruner;
import net.helix.pendulum.service.validatomanager.impl.CandidateSolidifierImpl;
import net.helix.pendulum.service.validatomanager.impl.CandidateTrackerImpl;
import net.helix.pendulum.service.validatomanager.impl.ValidatorManagerServiceImpl;
import net.helix.pendulum.storage.Indexable;
import net.helix.pendulum.storage.Persistable;
import net.helix.pendulum.storage.PersistenceProvider;
import net.helix.pendulum.storage.Tangle;
import net.helix.pendulum.storage.rocksdb.RocksDBPersistenceProvider;
import net.helix.pendulum.utils.Pair;
import net.helix.pendulum.zmq.MessageQProviderImpl;
import org.apache.commons.lang3.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.List;
/**
*
* The main class of SBX. This will propagate transactions into and throughout the network.
* This data is stored as a {@link Tangle}, a form of a Directed acyclic graph.
* All incoming data will be stored in one or more implementations of {@link PersistenceProvider}.
*
* <p>
* During initialization, all the Providers can be set to rescan or revalidate their transactions.
* After initialization, an asynchronous process has started which will process inbound and outbound transactions.
* Each full node should be peered with 7-9 other full nodes (neighbors) to function optimally.
* </p>
* <p>
* If this node has no Neighbors defined, no data is transferred.
* However, if the node has Neighbors, but no Internet connection,
* synchronization will continue after Internet connection is established.
* Any transactions sent to this node in its local network will then be processed.
* This makes the node able to run partially offline if an already existing database exists on this node.
* </p>
* <p>
* Validation of a transaction is the process by which other devices choose the transaction.
* This is done via a {@link TipSelector} algorithm, after which the transaction performs
* the necessary proof-of-work in order to cast their vote of confirmation/approval upon those tips. <br/>
*
* As many other transactions repeat this process on top of each other,
* validation of the transaction in question slowly builds up enough verifications.
* Eventually this will reach a minimum acceptable verification threshold.
* This threshold is determined by the recipient of the transaction.
* When this minimum threshold is reached, the transaction is "confirmed".
* </p>
*
*/
public class Pendulum {
private static final Logger log = LoggerFactory.getLogger(Pendulum.class);
public final SpentAddressesProviderImpl spentAddressesProvider;
public final SpentAddressesServiceImpl spentAddressesService;
public final SnapshotProviderImpl snapshotProvider;
public final SnapshotServiceImpl snapshotService;
public final LocalSnapshotManagerImpl localSnapshotManager;
public final MilestoneServiceImpl milestoneService;
public final ValidatorManagerServiceImpl validatorManagerService;
public final MilestoneTrackerImpl latestMilestoneTracker;
public final CandidateTrackerImpl candidateTracker;
public final LatestSolidMilestoneTrackerImpl latestSolidMilestoneTracker;
public final SeenMilestonesRetrieverImpl seenMilestonesRetriever;
public final LedgerServiceImpl ledgerService = new LedgerServiceImpl();
public final AsyncTransactionPruner transactionPruner;
public final MilestoneSolidifierImpl milestoneSolidifier;
public final CandidateSolidifierImpl candidateSolidifier;
public final TransactionRequesterWorkerImpl transactionRequesterWorker;
public final VirtualTransactionServiceImpl virtualTransactionService;
public final Tangle tangle;
public final TransactionValidator transactionValidator;
public final TipsSolidifier tipsSolidifier;
public final TransactionRequester transactionRequester;
public final Node node;
public final UDPReceiver udpReceiver;
public final Replicator replicator;
public final PendulumConfig configuration;
public final TipsViewModel tipsViewModel;
public final TipSelector tipsSelector;
public final TransactionStatsPublisher transactionStatsPublisher;
public final BundleValidator bundleValidator;
/**
* Initializes the latest snapshot and then creates all services needed to run a node.
*
* @param configuration Information about how this node will be configured.
* @throws TransactionPruningException If the TransactionPruner could not restore its state.
* @throws SnapshotException If the Snapshot fails to initialize.
* This can happen if the snapshot signature is invalid or the file cannot be read.
*/
public Pendulum(PendulumConfig configuration) throws TransactionPruningException, SnapshotException, SpentAddressesException {
this.configuration = configuration;
// new refactored instances
spentAddressesProvider = new SpentAddressesProviderImpl();
spentAddressesService = new SpentAddressesServiceImpl();
snapshotProvider = new SnapshotProviderImpl();
snapshotService = new SnapshotServiceImpl();
localSnapshotManager = configuration.getLocalSnapshotsEnabled()
? new LocalSnapshotManagerImpl()
: null;
milestoneService = new MilestoneServiceImpl();
//validatorService = new ValidatorServiceImpl();
validatorManagerService = new ValidatorManagerServiceImpl();
latestMilestoneTracker = new MilestoneTrackerImpl();
candidateTracker = new CandidateTrackerImpl();
latestSolidMilestoneTracker = new LatestSolidMilestoneTrackerImpl();
seenMilestonesRetriever = new SeenMilestonesRetrieverImpl();
milestoneSolidifier = new MilestoneSolidifierImpl();
//validatorSolidifier = new ValidatorSolidifierImpl();
candidateSolidifier = new CandidateSolidifierImpl();
transactionPruner = configuration.getLocalSnapshotsEnabled() && configuration.getLocalSnapshotsPruningEnabled()
? new AsyncTransactionPruner()
: null;
transactionRequesterWorker = new TransactionRequesterWorkerImpl();
virtualTransactionService = new VirtualTransactionServiceImpl();
// legacy code
bundleValidator = new BundleValidator();
tangle = new Tangle();
tipsViewModel = new TipsViewModel();
transactionRequester = new TransactionRequester(tangle, snapshotProvider);
transactionValidator = new TransactionValidator(tangle, snapshotProvider, tipsViewModel, transactionRequester, configuration);
node = new Node(tangle, snapshotProvider, transactionValidator, transactionRequester, tipsViewModel,
latestMilestoneTracker, virtualTransactionService, configuration);
replicator = new Replicator(node, configuration);
udpReceiver = new UDPReceiver(node, configuration);
tipsSolidifier = new TipsSolidifier(tangle, transactionValidator, tipsViewModel, configuration);
tipsSelector = createTipSelector(configuration);
transactionStatsPublisher = new TransactionStatsPublisher(tangle, tipsViewModel, tipsSelector);
injectDependencies();
}
/**
* Adds all database providers, and starts initialization of our services.
* According to the {@link PendulumConfig}, data is optionally cleared, reprocessed and reverified.<br/>
* After this function, incoming and outbound transaction processing has started.
*
* @throws Exception If along the way a service fails to initialize.
* Most common cause is a file read or database error.
*/
public void init() throws Exception {
initializeTangle();
tangle.init();
if (configuration.isRescanDb()){
rescanDb();
}
if (configuration.isRevalidate()) {
tangle.clearColumn(net.helix.pendulum.model.persistables.Round.class);
tangle.clearColumn(net.helix.pendulum.model.StateDiff.class);
tangle.clearMetadata(net.helix.pendulum.model.persistables.Transaction.class);
}
transactionValidator.init(configuration.isTestnet(), configuration.getMwm());
tipsSolidifier.init();
transactionRequester.init(configuration.getpRemoveRequest());
udpReceiver.init();
replicator.init();
node.init();
latestMilestoneTracker.start();
latestSolidMilestoneTracker.start();
candidateTracker.start();
seenMilestonesRetriever.start();
milestoneSolidifier.start();
transactionRequesterWorker.start();
if (localSnapshotManager != null) {
localSnapshotManager.start(latestMilestoneTracker);
}
if (transactionPruner != null) {
transactionPruner.start();
}
if (configuration.isZmqEnabled()) {
tangle.addMessageQueueProvider(new MessageQProviderImpl(configuration));
transactionStatsPublisher.init();
}
if(Files.notExists(Paths.get(configuration.getResourcePath()))){
new File(configuration.getResourcePath()).mkdir();
}
}
//TODO: bundleValidator should be passed to: milestoneService, spentAddressService and ledgerService.
private void injectDependencies() throws SnapshotException, TransactionPruningException, SpentAddressesException {
//snapshot provider must be initialized first
//because we check whether spent addresses data exists
snapshotProvider.init(configuration);
spentAddressesProvider.init(configuration);
spentAddressesService.init(tangle, snapshotProvider, spentAddressesProvider);
snapshotService.init(tangle, snapshotProvider, spentAddressesService, spentAddressesProvider, configuration);
if (localSnapshotManager != null) {
localSnapshotManager.init(snapshotProvider, snapshotService, transactionPruner, configuration);
}
milestoneService.init(tangle, snapshotProvider, snapshotService, transactionValidator, configuration);
virtualTransactionService.init(tangle, snapshotProvider, transactionValidator);
validatorManagerService.init(tangle, snapshotProvider, snapshotService, configuration);
candidateTracker.init(tangle, snapshotProvider, validatorManagerService, candidateSolidifier, configuration);
latestMilestoneTracker.init(tangle, snapshotProvider, milestoneService, milestoneSolidifier, candidateTracker, configuration);
latestSolidMilestoneTracker.init(tangle, snapshotProvider, milestoneService, ledgerService,
latestMilestoneTracker);
seenMilestonesRetriever.init(tangle, snapshotProvider, transactionRequester);
milestoneSolidifier.init(snapshotProvider, transactionValidator);
//validatorSolidifier.init(snapshotProvider, transactionValidator);
candidateSolidifier.init(snapshotProvider, transactionValidator);
ledgerService.init(tangle, snapshotProvider, snapshotService, milestoneService, configuration);
if (transactionPruner != null) {
transactionPruner.init(tangle, snapshotProvider, spentAddressesService, tipsViewModel, configuration)
.restoreState();
}
transactionRequesterWorker.init(tangle, transactionRequester, tipsViewModel, node);
}
private void rescanDb() throws Exception {
//delete all transaction indexes
tangle.clearColumn(net.helix.pendulum.model.persistables.Address.class);
tangle.clearColumn(net.helix.pendulum.model.persistables.Bundle.class);
tangle.clearColumn(net.helix.pendulum.model.persistables.Approvee.class);
tangle.clearColumn(net.helix.pendulum.model.persistables.BundleNonce.class);
tangle.clearColumn(net.helix.pendulum.model.persistables.Tag.class);
tangle.clearColumn(net.helix.pendulum.model.persistables.Round.class);
tangle.clearColumn(net.helix.pendulum.model.StateDiff.class);
tangle.clearMetadata(net.helix.pendulum.model.persistables.Transaction.class);
//rescan all tx & refill the columns
TransactionViewModel tx = TransactionViewModel.first(tangle);
int counter = 0;
while (tx != null) {
if (++counter % 10000 == 0) {
log.info("Rescanned {} Transactions", counter);
}
List<Pair<Indexable, Persistable>> saveBatch = tx.getSaveBatch();
saveBatch.remove(5);
tangle.saveBatch(saveBatch);
tx = tx.next(tangle);
}
}
/**
* Gracefully shuts down by calling <tt>shutdown()</tt> on all used services.
* Exceptions during shutdown are not caught.
*/
public void shutdown() throws Exception {
transactionStatsPublisher.shutdown();
transactionRequesterWorker.shutdown();
milestoneSolidifier.shutdown();
seenMilestonesRetriever.shutdown();
latestSolidMilestoneTracker.shutdown();
latestMilestoneTracker.shutdown();
if (transactionPruner != null) {
transactionPruner.shutdown();
}
if (localSnapshotManager != null) {
localSnapshotManager.shutdown();
}
tipsSolidifier.shutdown();
node.shutdown();
udpReceiver.shutdown();
replicator.shutdown();
transactionValidator.shutdown();
tangle.shutdown();
// free the resources of the snapshot provider last because all other instances need it
snapshotProvider.shutdown();
}
private void initializeTangle() {
switch (configuration.getMainDb()) {
case "rocksdb": {
tangle.addPersistenceProvider(new RocksDBPersistenceProvider(
configuration.getDbPath(),
configuration.getDbLogPath(),
configuration.getDbCacheSize(),
Tangle.COLUMN_FAMILIES,
Tangle.METADATA_COLUMN_FAMILY)
);
break;
}
default: {
throw new NotImplementedException("No such database type.");
}
}
}
private TipSelector createTipSelector(TipSelConfig config) {
EntryPointSelector entryPointSelector = new EntryPointSelectorImpl(tangle, snapshotProvider,
latestMilestoneTracker);
RatingCalculator ratingCalculator = new CumulativeWeightCalculator(tangle, snapshotProvider);
TailFinder tailFinder = new TailFinderImpl(tangle);
Walker walker = new WalkerAlpha(tailFinder, tangle, new SecureRandom(), config);
return new TipSelectorImpl(tangle, snapshotProvider, ledgerService, entryPointSelector, ratingCalculator,
walker, config);
}
}