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
/
TransactionValidator.java
274 lines (245 loc) · 11.3 KB
/
TransactionValidator.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
package com.iota.iri;
import com.iota.iri.controllers.TipsViewModel;
import com.iota.iri.hash.Curl;
import com.iota.iri.hash.Sponge;
import com.iota.iri.hash.SpongeFactory;
import com.iota.iri.network.TransactionRequester;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.model.Hash;
import com.iota.iri.zmq.MessageQ;
import com.iota.iri.storage.Tangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static com.iota.iri.controllers.TransactionViewModel.*;
/**
* Created by paul on 4/17/17.
*/
public class TransactionValidator {
private final Logger log = LoggerFactory.getLogger(TransactionValidator.class);
private final Tangle tangle;
private final TipsViewModel tipsViewModel;
private final TransactionRequester transactionRequester;
private final MessageQ messageQ;
private int MIN_WEIGHT_MAGNITUDE = 81;
private static long MIN_TIMESTAMP = 1517180400;
private static long MIN_TIMESTAMP_MS = MIN_TIMESTAMP * 1000;
private static long MAX_TIMESTAMP_FUTURE = 2 * 60 * 60;
private static long MAX_TIMESTAMP_FUTURE_MS = MAX_TIMESTAMP_FUTURE * 1000;
private Thread newSolidThread;
private final AtomicBoolean useFirst = new AtomicBoolean(true);
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
private final Object cascadeSync = new Object();
private final Set<Hash> newSolidTransactionsOne = new LinkedHashSet<>();
private final Set<Hash> newSolidTransactionsTwo = new LinkedHashSet<>();
public TransactionValidator(Tangle tangle, TipsViewModel tipsViewModel, TransactionRequester transactionRequester, MessageQ messageQ) {
this.tangle = tangle;
this.tipsViewModel = tipsViewModel;
this.transactionRequester = transactionRequester;
this.messageQ = messageQ;
}
public void init(boolean testnet,int MAINNET_MWM, int TESTNET_MWM) {
if(testnet) {
MIN_WEIGHT_MAGNITUDE = TESTNET_MWM;
} else {
MIN_WEIGHT_MAGNITUDE = MAINNET_MWM;
}
//lowest allowed MWM encoded in 46 bytes.
if (MIN_WEIGHT_MAGNITUDE<13){
MIN_WEIGHT_MAGNITUDE = 13;
}
newSolidThread = new Thread(spawnSolidTransactionsPropagation(), "Solid TX cascader");
newSolidThread.start();
}
public void shutdown() throws InterruptedException {
shuttingDown.set(true);
newSolidThread.join();
}
public int getMinWeightMagnitude() {
return MIN_WEIGHT_MAGNITUDE;
}
private static boolean hasInvalidTimestamp(TransactionViewModel transactionViewModel) {
if (transactionViewModel.getAttachmentTimestamp() == 0) {
return transactionViewModel.getTimestamp() < MIN_TIMESTAMP && !Objects.equals(transactionViewModel.getHash(), Hash.NULL_HASH)
|| transactionViewModel.getTimestamp() > (System.currentTimeMillis() / 1000) + MAX_TIMESTAMP_FUTURE;
}
return transactionViewModel.getAttachmentTimestamp() < MIN_TIMESTAMP_MS
|| transactionViewModel.getAttachmentTimestamp() > System.currentTimeMillis() + MAX_TIMESTAMP_FUTURE_MS;
}
public static void runValidation(TransactionViewModel transactionViewModel, final int minWeightMagnitude) {
transactionViewModel.setMetadata();
transactionViewModel.setAttachmentData();
if(hasInvalidTimestamp(transactionViewModel)) {
throw new StaleTimestampException("Invalid transaction timestamp.");
}
for (int i = VALUE_TRINARY_OFFSET + VALUE_USABLE_TRINARY_SIZE; i < VALUE_TRINARY_OFFSET + VALUE_TRINARY_SIZE; i++) {
if (transactionViewModel.trits()[i] != 0) {
throw new RuntimeException("Invalid transaction value");
}
}
int weightMagnitude = transactionViewModel.weightMagnitude;
if(weightMagnitude < minWeightMagnitude) {
throw new RuntimeException("Invalid transaction hash");
}
if (transactionViewModel.value() != 0 && transactionViewModel.getAddressHash().trits()[Curl.HASH_LENGTH - 1] != 0) {
throw new RuntimeException("Invalid transaction address");
}
}
public static TransactionViewModel validate(final int[] trits, int minWeightMagnitude) {
TransactionViewModel transactionViewModel = new TransactionViewModel(trits, Hash.calculate(trits, 0, trits.length, SpongeFactory.create(SpongeFactory.Mode.CURLP81)));
runValidation(transactionViewModel, minWeightMagnitude);
return transactionViewModel;
}
public static TransactionViewModel validate(final byte[] bytes, int minWeightMagnitude) {
return validate(bytes, minWeightMagnitude, SpongeFactory.create(SpongeFactory.Mode.CURLP81));
}
public static TransactionViewModel validate(final byte[] bytes, int minWeightMagnitude, Sponge curl) {
TransactionViewModel transactionViewModel = new TransactionViewModel(bytes, Hash.calculate(bytes, TransactionViewModel.TRINARY_SIZE, curl));
runValidation(transactionViewModel, minWeightMagnitude);
return transactionViewModel;
}
private final AtomicInteger nextSubSolidGroup = new AtomicInteger(1);
public boolean checkSolidity(Hash hash, boolean milestone) throws Exception {
if(TransactionViewModel.fromHash(tangle, hash).isSolid()) {
return true;
}
Set<Hash> analyzedHashes = new HashSet<>(Collections.singleton(Hash.NULL_HASH));
boolean solid = true;
final Queue<Hash> nonAnalyzedTransactions = new LinkedList<>(Collections.singleton(hash));
Hash hashPointer;
while ((hashPointer = nonAnalyzedTransactions.poll()) != null) {
if (analyzedHashes.add(hashPointer)) {
final TransactionViewModel transaction = TransactionViewModel.fromHash(tangle, hashPointer);
if(!transaction.isSolid()) {
if (transaction.getType() == TransactionViewModel.PREFILLED_SLOT && !hashPointer.equals(Hash.NULL_HASH)) {
transactionRequester.requestTransaction(hashPointer, milestone);
solid = false;
break;
} else {
if (solid) {
nonAnalyzedTransactions.offer(transaction.getTrunkTransactionHash());
nonAnalyzedTransactions.offer(transaction.getBranchTransactionHash());
}
}
}
}
}
if (solid) {
TransactionViewModel.updateSolidTransactions(tangle, analyzedHashes);
}
analyzedHashes.clear();
return solid;
}
public void addSolidTransaction(Hash hash) {
synchronized (cascadeSync) {
if (useFirst.get()) {
newSolidTransactionsOne.add(hash);
} else {
newSolidTransactionsTwo.add(hash);
}
}
}
private Runnable spawnSolidTransactionsPropagation() {
return () -> {
while(!shuttingDown.get()) {
Set<Hash> newSolidHashes = new HashSet<>();
useFirst.set(!useFirst.get());
synchronized (cascadeSync) {
if (useFirst.get()) {
newSolidHashes.addAll(newSolidTransactionsTwo);
} else {
newSolidHashes.addAll(newSolidTransactionsOne);
}
}
Iterator<Hash> cascadeIterator = newSolidHashes.iterator();
Set<Hash> hashesToCascade = new HashSet<>();
while(cascadeIterator.hasNext() && !shuttingDown.get()) {
try {
Hash hash = cascadeIterator.next();
TransactionViewModel transaction = TransactionViewModel.fromHash(tangle, hash);
Set<Hash> approvers = transaction.getApprovers(tangle).getHashes();
for(Hash h: approvers) {
TransactionViewModel tx = TransactionViewModel.fromHash(tangle, h);
if(quietQuickSetSolid(tx)) {
tx.update(tangle, "solid");
} else {
if (transaction.isSolid()) {
addSolidTransaction(hash);
}
}
}
} catch (Exception e) {
log.error("Some error", e);
// TODO: Do something, maybe, or do nothing.
}
}
synchronized (cascadeSync) {
if (useFirst.get()) {
newSolidTransactionsTwo.clear();
} else {
newSolidTransactionsOne.clear();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
public void updateStatus(TransactionViewModel transactionViewModel) throws Exception {
transactionRequester.clearTransactionRequest(transactionViewModel.getHash());
if(transactionViewModel.getApprovers(tangle).size() == 0) {
tipsViewModel.addTipHash(transactionViewModel.getHash());
}
tipsViewModel.removeTipHash(transactionViewModel.getTrunkTransactionHash());
tipsViewModel.removeTipHash(transactionViewModel.getBranchTransactionHash());
if(quickSetSolid(transactionViewModel)) {
addSolidTransaction(transactionViewModel.getHash());
}
}
public boolean quietQuickSetSolid(TransactionViewModel transactionViewModel) {
try {
return quickSetSolid(transactionViewModel);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private boolean quickSetSolid(final TransactionViewModel transactionViewModel) throws Exception {
if(!transactionViewModel.isSolid()) {
boolean solid = true;
if (!checkApproovee(transactionViewModel.getTrunkTransaction(tangle))) {
solid = false;
}
if (!checkApproovee(transactionViewModel.getBranchTransaction(tangle))) {
solid = false;
}
if(solid) {
transactionViewModel.updateSolid(true);
transactionViewModel.updateHeights(tangle);
return true;
}
}
//return isSolid();
return false;
}
private boolean checkApproovee(TransactionViewModel approovee) throws Exception {
if(approovee.getType() == PREFILLED_SLOT) {
transactionRequester.requestTransaction(approovee.getHash(), false);
return false;
}
if(approovee.getHash().equals(Hash.NULL_HASH)) {
return true;
}
return approovee.isSolid();
}
public static class StaleTimestampException extends RuntimeException {
public StaleTimestampException (String message) {
super(message);
}
}
}