This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
/
Iota.java
215 lines (201 loc) · 9.44 KB
/
Iota.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
package com.iota.iri;
import com.iota.iri.conf.Configuration;
import com.iota.iri.controllers.*;
import com.iota.iri.hash.SpongeFactory;
import com.iota.iri.network.TransactionRequester;
import com.iota.iri.model.Hash;
import com.iota.iri.network.Node;
import com.iota.iri.network.UDPReceiver;
import com.iota.iri.network.replicator.Replicator;
import com.iota.iri.zmq.MessageQ;
import com.iota.iri.service.TipsManager;
import com.iota.iri.storage.FileExportProvider;
import com.iota.iri.storage.Indexable;
import com.iota.iri.storage.Persistable;
import com.iota.iri.storage.Tangle;
import com.iota.iri.storage.ZmqPublishProvider;
import com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider;
import com.iota.iri.utils.Pair;
import org.apache.commons.lang3.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Created by paul on 5/19/17.
*/
public class Iota {
private static final Logger log = LoggerFactory.getLogger(Iota.class);
public static final String MAINNET_COORDINATOR_ADDRESS = "KPWCHICGJZXKE9GSUDXZYUAPLHAKAHYHDXNPHENTERYMMBQOPSQIDENXKLKCEYCPVTZQLEEJVYJZV9BWU";
public static final Hash MAINNET_COORDINATOR = new Hash(MAINNET_COORDINATOR_ADDRESS);
public static final String TESTNET_COORDINATOR_ADDRESS = "XNZBYAST9BETSDNOVQKKTBECYIPMF9IPOZRWUPFQGVH9HJW9NDSQVIPVBWU9YKECRYGDSJXYMZGHZDXCA";
public static final Hash TESTNET_COORDINATOR = new Hash(TESTNET_COORDINATOR_ADDRESS);
public final LedgerValidator ledgerValidator;
public final Milestone milestone;
public final Tangle tangle;
public final TransactionValidator transactionValidator;
public final TipsManager tipsManager;
public final TransactionRequester transactionRequester;
public final Node node;
public final UDPReceiver udpReceiver;
public final Replicator replicator;
public final Configuration configuration;
public final Hash coordinator;
public final TipsViewModel tipsViewModel;
public final MessageQ messageQ;
public final boolean testnet;
public final int maxPeers;
public final int udpPort;
public final int tcpPort;
public final int maxTipSearchDepth;
public Iota(Configuration configuration) {
this.configuration = configuration;
testnet = configuration.booling(Configuration.DefaultConfSettings.TESTNET);
maxPeers = configuration.integer(Configuration.DefaultConfSettings.MAX_PEERS);
udpPort = configuration.integer(Configuration.DefaultConfSettings.UDP_RECEIVER_PORT);
tcpPort = configuration.integer(Configuration.DefaultConfSettings.TCP_RECEIVER_PORT);
maxTipSearchDepth = configuration.integer(Configuration.DefaultConfSettings.MAX_DEPTH);
if(testnet) {
String coordinatorTrytes = configuration.string(Configuration.DefaultConfSettings.COORDINATOR);
if(coordinatorTrytes != null) {
coordinator = new Hash(coordinatorTrytes);
} else {
coordinator = TESTNET_COORDINATOR;
}
} else {
coordinator = MAINNET_COORDINATOR;
}
tangle = new Tangle();
messageQ = new MessageQ(configuration.integer(Configuration.DefaultConfSettings.ZMQ_PORT),
configuration.string(Configuration.DefaultConfSettings.ZMQ_IPC),
configuration.integer(Configuration.DefaultConfSettings.ZMQ_THREADS),
configuration.booling(Configuration.DefaultConfSettings.ZMQ_ENABLED)
);
tipsViewModel = new TipsViewModel();
transactionRequester = new TransactionRequester(tangle, messageQ);
transactionValidator = new TransactionValidator(tangle, tipsViewModel, transactionRequester, messageQ);
milestone = new Milestone(tangle, coordinator, new Snapshot(Snapshot.initialSnapshot), transactionValidator, testnet, messageQ);
node = new Node(configuration, tangle, transactionValidator, transactionRequester, tipsViewModel, milestone, messageQ);
replicator = new Replicator(node, tcpPort, maxPeers, testnet);
udpReceiver = new UDPReceiver(udpPort, node);
ledgerValidator = new LedgerValidator(tangle, milestone, transactionRequester, messageQ);
tipsManager = new TipsManager(tangle, ledgerValidator, transactionValidator, tipsViewModel, milestone, maxTipSearchDepth, messageQ);
}
public void init() throws Exception {
initializeTangle();
tangle.init();
if (configuration.booling(Configuration.DefaultConfSettings.RESCAN_DB)){
rescan_db();
}
boolean revalidate = configuration.booling(Configuration.DefaultConfSettings.REVALIDATE);
if (revalidate) {
tangle.clearColumn(com.iota.iri.model.Milestone.class);
tangle.clearColumn(com.iota.iri.model.StateDiff.class);
tangle.clearMetadata(com.iota.iri.model.Transaction.class);
}
milestone.init(SpongeFactory.Mode.CURLP27, ledgerValidator, revalidate);
transactionValidator.init(testnet, configuration.integer(Configuration.DefaultConfSettings.MAINNET_MWM), configuration.integer(Configuration.DefaultConfSettings.TESTNET_MWM));
tipsManager.init();
transactionRequester.init(configuration.doubling(Configuration.DefaultConfSettings.P_REMOVE_REQUEST.name()));
udpReceiver.init();
replicator.init();
node.init();
}
private void rescan_db() throws Exception {
int counter = 0;
//delete all Address , Bundle , Approvee & Tag
AddressViewModel add = AddressViewModel.first(tangle);
while (add != null) {
if (++counter % 10000 == 0) {
log.info("Clearing cache: {} Addresses", counter);
}
AddressViewModel NextAdd = add.next(tangle);
add.delete(tangle);
add = NextAdd;
}
counter = 0;
BundleViewModel bn = BundleViewModel.first(tangle);
while (bn != null) {
if (++counter % 10000 == 0) {
log.info("Clearing cache: {} Bundles", counter);
}
BundleViewModel NextBn = bn.next(tangle);
bn.delete(tangle);
bn = NextBn;
}
counter = 0;
ApproveeViewModel app = ApproveeViewModel.first(tangle);
while (app != null) {
if (++counter % 10000 == 0) {
log.info("Clearing cache: {} Approvees", counter);
}
ApproveeViewModel NextApp = app.next(tangle);
app.delete(tangle);
app = NextApp;
}
counter = 0;
TagViewModel tag = TagViewModel.first(tangle);
while (tag != null) {
if (++counter % 10000 == 0) {
log.info("Clearing cache: {} Tags", counter);
}
TagViewModel NextTag = tag.next(tangle);
tag.delete(tangle);
tag = NextTag;
}
//rescan all tx & refill the columns
TransactionViewModel tx = TransactionViewModel.first(tangle);
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);
}
}
public void shutdown() throws Exception {
milestone.shutDown();
tipsManager.shutdown();
node.shutdown();
udpReceiver.shutdown();
replicator.shutdown();
transactionValidator.shutdown();
tangle.shutdown();
}
private void initializeTangle() {
String dbPath = configuration.string(Configuration.DefaultConfSettings.DB_PATH);
if (testnet) {
if (dbPath.isEmpty() || dbPath.equals("mainnetdb")) {
// testnetusers must not use mainnetdb, overwrite it unless an explicit name is set.
configuration.put(Configuration.DefaultConfSettings.DB_PATH.name(), "testnetdb");
configuration.put(Configuration.DefaultConfSettings.DB_LOG_PATH.name(), "testnetdb.log");
}
} else {
if (dbPath.isEmpty() || dbPath.equals("testnetdb")) {
// mainnetusers must not use testnetdb, overwrite it unless an explicit name is set.
configuration.put(Configuration.DefaultConfSettings.DB_PATH.name(), "mainnetdb");
configuration.put(Configuration.DefaultConfSettings.DB_LOG_PATH.name(), "mainnetdb.log");
}
}
switch (configuration.string(Configuration.DefaultConfSettings.MAIN_DB)) {
case "rocksdb": {
tangle.addPersistenceProvider(new RocksDBPersistenceProvider(
configuration.string(Configuration.DefaultConfSettings.DB_PATH),
configuration.string(Configuration.DefaultConfSettings.DB_LOG_PATH),
configuration.integer(Configuration.DefaultConfSettings.DB_CACHE_SIZE)));
break;
}
default: {
throw new NotImplementedException("No such database type.");
}
}
if (configuration.booling(Configuration.DefaultConfSettings.EXPORT)) {
tangle.addPersistenceProvider(new FileExportProvider());
}
if (configuration.booling(Configuration.DefaultConfSettings.ZMQ_ENABLED)) {
tangle.addPersistenceProvider(new ZmqPublishProvider(messageQ));
}
}
}