use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.
the class BundleValidatorTest method isInconsistent.
@Test
public void isInconsistent() throws Exception {
String[] trytes = { "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999CFDEZBLZQYA9999999999999999999999999999999999999999999ZZWQHWD99C99999999C99999999CKWWDBWSCLMQULCTAAJGXDEMFJXPMGMAQIHDGHRBGEMUYNNCOK9YPHKEEFLFCZUSPMCJHAKLCIBQSGWAS999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ZZWQHWD99B99999999C99999999CKWWDBWSCLMQULCTAAJGXDEMFJXPMGMAQIHDGHRBGEMUYNNCOK9YPHKEEFLFCZUSPMCJHAKLCIBQSGWAS999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999VUWVAYOAJBZ9999999999999999999999999999999999999999999ZZWQHWD99A99999999C99999999CKWWDBWSCLMQULCTAAJGXDEMFJXPMGMAQIHDGHRBGEMUYNNCOK9YPHKEEFLFCZUSPMCJHAKLCIBQSGWAS999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999FPHIWXZWXNFVHFOUKSIAKQIEQCFGQRJLWXHOLAQV9ALVRMAXQSASTPWAAVAKHHLRLXFGGIUYQDVRUHHRQB99999999999999999999999999999999999999999999999999999ZZWQHWD99999999999C99999999CKWWDBWSCLMQULCTAAJGXDEMFJXPMGMAQIHDGHRBGEMUYNNCOK9YPHKEEFLFCZUSPMCJHAKLCIBQSGWAS999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" };
List<TransactionViewModel> transactions = Arrays.stream(trytes).map(Converter::allocatingTritsFromTrytes).map(t -> new TransactionViewModel(t, Hash.calculate(SpongeFactory.Mode.CURLP81, t))).map(t -> {
try {
t.store(tangle);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}).collect(Collectors.toList());
// Assert.assertFalse(BundleValidator.isInconsistent(transactions, false));
// Assert.assertFalse(BundleValidator.isInconsistent(transactions, true));
}
use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.
the class BundleValidatorTest method validateAll9s.
@Test
public void validateAll9s() throws Exception {
String[] trytes = { "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999XIJLRVTWOHCLAHVDWBJKESBMYK9LSEVZWWPXKMPTJJGIMRGLLBPR9WKORVCANWVQY9NKRGGYDMKF99999VKSMFDJWZJLVMYUOWAHEVGVK9CIMSYQ99PVAQFVZGVSTRIAQEOJLUDHOWSJKZVVEBTCLDMMEHVEIX9999ZXYZDVQSXGJEXRHKSVVDGIRYQFMJTBBJLPNVSOWQEFB9QXV9ALVUIUJRMKWEANEOK9LQ9HNMWFNJXGOWX" };
List<TransactionViewModel> transactions = Arrays.stream(trytes).map(Converter::allocatingTritsFromTrytes).map(t -> new TransactionViewModel(t, Hash.calculate(SpongeFactory.Mode.CURLP81, t))).map(t -> {
try {
t.store(tangle);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}).collect(Collectors.toList());
// Assert.assertTrue(BundleValidator.validate(tangle,transactions.get(0).getBundleHash()).get(0).size() == transactions.size());
}
use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.
the class BundleValidator method validate.
public static List<List<TransactionViewModel>> validate(Tangle tangle, Hash tailHash) throws Exception {
TransactionViewModel tail = TransactionViewModel.fromHash(tangle, tailHash);
List<List<TransactionViewModel>> transactions = new LinkedList<>();
if (tail.getCurrentIndex() != 0) {
return transactions;
}
final Map<Hash, TransactionViewModel> bundleTransactions = loadTransactionsFromTangle(tangle, tail);
for (TransactionViewModel transactionViewModel : bundleTransactions.values()) {
if (transactionViewModel.getCurrentIndex() == 0 && transactionViewModel.getValidity() >= 0) {
final List<TransactionViewModel> instanceTransactionViewModels = new LinkedList<>();
final long lastIndex = transactionViewModel.lastIndex();
long bundleValue = 0;
int i = 0;
final Sponge curlInstance = SpongeFactory.create(SpongeFactory.Mode.KERL);
final Sponge addressInstance = SpongeFactory.create(SpongeFactory.Mode.KERL);
final int[] addressTrits = new int[TransactionViewModel.ADDRESS_TRINARY_SIZE];
final int[] bundleHashTrits = new int[TransactionViewModel.BUNDLE_TRINARY_SIZE];
final int[] normalizedBundle = new int[Curl.HASH_LENGTH / ISS.TRYTE_WIDTH];
final int[] digestTrits = new int[Curl.HASH_LENGTH];
MAIN_LOOP: while (true) {
instanceTransactionViewModels.add(transactionViewModel);
if (transactionViewModel.getCurrentIndex() != i || transactionViewModel.lastIndex() != lastIndex || ((bundleValue = Math.addExact(bundleValue, transactionViewModel.value())) < -TransactionViewModel.SUPPLY || bundleValue > TransactionViewModel.SUPPLY)) {
instanceTransactionViewModels.get(0).setValidity(tangle, -1);
break;
}
if (transactionViewModel.value() != 0 && transactionViewModel.getAddressHash().trits()[Curl.HASH_LENGTH - 1] != 0) {
instanceTransactionViewModels.get(0).setValidity(tangle, -1);
break;
}
if (i++ == lastIndex) {
if (bundleValue == 0) {
if (instanceTransactionViewModels.get(0).getValidity() == 0) {
curlInstance.reset();
for (final TransactionViewModel transactionViewModel2 : instanceTransactionViewModels) {
curlInstance.absorb(transactionViewModel2.trits(), TransactionViewModel.ESSENCE_TRINARY_OFFSET, TransactionViewModel.ESSENCE_TRINARY_SIZE);
}
curlInstance.squeeze(bundleHashTrits, 0, bundleHashTrits.length);
if (Arrays.equals(instanceTransactionViewModels.get(0).getBundleHash().trits(), bundleHashTrits)) {
ISSInPlace.normalizedBundle(bundleHashTrits, normalizedBundle);
for (int j = 0; j < instanceTransactionViewModels.size(); ) {
transactionViewModel = instanceTransactionViewModels.get(j);
if (transactionViewModel.value() < 0) {
// let's recreate the address of the transactionViewModel.
addressInstance.reset();
int offset = 0, offsetNext = 0;
do {
offsetNext = (offset + ISS.NUMBER_OF_FRAGMENT_CHUNKS - 1) % (Curl.HASH_LENGTH / Converter.NUMBER_OF_TRITS_IN_A_TRYTE) + 1;
ISSInPlace.digest(SpongeFactory.Mode.KERL, normalizedBundle, offset % (Curl.HASH_LENGTH / Converter.NUMBER_OF_TRITS_IN_A_TRYTE), instanceTransactionViewModels.get(j).trits(), TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_OFFSET, digestTrits);
addressInstance.absorb(digestTrits, 0, Curl.HASH_LENGTH);
offset = offsetNext;
} while (++j < instanceTransactionViewModels.size() && instanceTransactionViewModels.get(j).getAddressHash().equals(transactionViewModel.getAddressHash()) && instanceTransactionViewModels.get(j).value() == 0);
addressInstance.squeeze(addressTrits, 0, addressTrits.length);
// if (!Arrays.equals(Converter.bytes(addressTrits, 0, TransactionViewModel.ADDRESS_TRINARY_SIZE), transactionViewModel.getAddress().getHash().bytes())) {
if (!Arrays.equals(transactionViewModel.getAddressHash().trits(), addressTrits)) {
instanceTransactionViewModels.get(0).setValidity(tangle, -1);
break MAIN_LOOP;
}
} else {
j++;
}
}
instanceTransactionViewModels.get(0).setValidity(tangle, 1);
transactions.add(instanceTransactionViewModels);
} else {
instanceTransactionViewModels.get(0).setValidity(tangle, -1);
}
} else {
transactions.add(instanceTransactionViewModels);
}
} else {
instanceTransactionViewModels.get(0).setValidity(tangle, -1);
}
break;
} else {
transactionViewModel = bundleTransactions.get(transactionViewModel.getTrunkTransactionHash());
if (transactionViewModel == null) {
break;
}
}
}
}
}
return transactions;
}
use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.
the class Node method weightQueueTxPair.
private static ConcurrentSkipListSet<Pair<TransactionViewModel, Neighbor>> weightQueueTxPair() {
return new ConcurrentSkipListSet<Pair<TransactionViewModel, Neighbor>>((transaction1, transaction2) -> {
TransactionViewModel tx1 = transaction1.getLeft();
TransactionViewModel tx2 = transaction2.getLeft();
if (tx1.weightMagnitude == tx2.weightMagnitude) {
for (int i = Hash.SIZE_IN_BYTES; i-- > 0; ) {
if (tx1.getHash().bytes()[i] != tx2.getHash().bytes()[i]) {
return tx2.getHash().bytes()[i] - tx1.getHash().bytes()[i];
}
}
return 0;
}
return tx2.weightMagnitude - tx1.weightMagnitude;
});
}
use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.
the class Node method preProcessReceivedData.
public void preProcessReceivedData(byte[] receivedData, SocketAddress senderAddress, String uriScheme) {
TransactionViewModel receivedTransactionViewModel = null;
Hash receivedTransactionHash = null;
boolean addressMatch = false;
boolean cached = false;
for (final Neighbor neighbor : getNeighbors()) {
addressMatch = neighbor.matches(senderAddress);
if (addressMatch) {
// Validate transaction
neighbor.incAllTransactions();
if (rnd.nextDouble() < P_DROP_TRANSACTION) {
// log.info("Randomly dropping transaction. Stand by... ");
break;
}
try {
// Transaction bytes
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(receivedData, 0, TransactionViewModel.SIZE);
ByteBuffer byteHash = ByteBuffer.wrap(digest.digest());
// check if cached
synchronized (recentSeenBytes) {
cached = (receivedTransactionHash = recentSeenBytes.get(byteHash)) != null;
}
if (!cached) {
// if not, then validate
receivedTransactionViewModel = new TransactionViewModel(receivedData, Hash.calculate(receivedData, TransactionViewModel.TRINARY_SIZE, SpongeFactory.create(SpongeFactory.Mode.CURLP81)));
receivedTransactionHash = receivedTransactionViewModel.getHash();
TransactionValidator.runValidation(receivedTransactionViewModel, transactionValidator.getMinWeightMagnitude());
synchronized (recentSeenBytes) {
recentSeenBytes.put(byteHash, receivedTransactionHash);
}
// if valid - add to receive queue (receivedTransactionViewModel, neighbor)
addReceivedDataToReceiveQueue(receivedTransactionViewModel, neighbor);
}
} catch (NoSuchAlgorithmException e) {
log.error("MessageDigest: " + e);
} catch (final TransactionValidator.StaleTimestampException e) {
log.debug(e.getMessage());
try {
transactionRequester.clearTransactionRequest(receivedTransactionHash);
} catch (Exception e1) {
log.error(e1.getMessage());
}
neighbor.incInvalidTransactions();
} catch (final RuntimeException e) {
log.error(e.getMessage());
log.error("Received an Invalid TransactionViewModel. Dropping it...");
neighbor.incInvalidTransactions();
break;
}
// Request bytes
// add request to reply queue (requestedHash, neighbor)
Hash requestedHash = new Hash(receivedData, TransactionViewModel.SIZE, TransactionRequester.REQUEST_HASH_SIZE);
if (requestedHash.equals(receivedTransactionHash)) {
// requesting a random tip
requestedHash = Hash.NULL_HASH;
}
addReceivedDataToReplyQueue(requestedHash, neighbor);
if (debug) {
long hitCount, missCount;
if (cached) {
hitCount = recentSeenBytesHitCount.incrementAndGet();
missCount = recentSeenBytesMissCount.get();
} else {
hitCount = recentSeenBytesHitCount.get();
missCount = recentSeenBytesMissCount.incrementAndGet();
}
if (((hitCount + missCount) % 50000L == 0)) {
log.info("RecentSeenBytes cache hit/miss ratio: " + hitCount + "/" + missCount);
messageQ.publish("hmr %d/%d", hitCount, missCount);
recentSeenBytesMissCount.set(0L);
recentSeenBytesHitCount.set(0L);
}
}
break;
}
}
if (!addressMatch && configuration.booling(Configuration.DefaultConfSettings.TESTNET)) {
int maxPeersAllowed = configuration.integer(Configuration.DefaultConfSettings.MAX_PEERS);
String uriString = uriScheme + ":/" + senderAddress.toString();
if (Neighbor.getNumPeers() < maxPeersAllowed) {
log.info("Adding non-tethered neighbor: " + uriString);
messageQ.publish("antn %s", uriString);
try {
final URI uri = new URI(uriString);
// 3rd parameter false (not tcp), 4th parameter true (configured tethering)
final Neighbor newneighbor = newNeighbor(uri, false);
if (!getNeighbors().contains(newneighbor)) {
getNeighbors().add(newneighbor);
Neighbor.incNumPeers();
}
} catch (URISyntaxException e) {
log.error("Invalid URI string: " + uriString);
}
} else {
if (rejectedAddresses.size() > 20) {
// Avoid ever growing list in case of an attack.
rejectedAddresses.clear();
} else if (rejectedAddresses.add(uriString)) {
messageQ.publish("rntn %s %s", uriString, String.valueOf(maxPeersAllowed));
log.info("Refused non-tethered neighbor: " + uriString + " (max-peers = " + String.valueOf(maxPeersAllowed) + ")");
}
}
}
}
Aggregations