Search in sources :

Example 21 with TransactionViewModel

use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.

the class Node method replyToRequest.

public void replyToRequest(Hash requestedHash, Neighbor neighbor) {
    TransactionViewModel transactionViewModel = null;
    Hash transactionPointer;
    // retrieve requested transaction
    if (requestedHash.equals(Hash.NULL_HASH)) {
        // Random Tip Request
        try {
            if (transactionRequester.numberOfTransactionsToRequest() > 0 && rnd.nextDouble() < P_REPLY_RANDOM_TIP) {
                neighbor.incRandomTransactionRequests();
                transactionPointer = getRandomTipPointer();
                transactionViewModel = TransactionViewModel.fromHash(tangle, transactionPointer);
            } else {
                // no tx to request, so no random tip will be sent as a reply.
                return;
            }
        } catch (Exception e) {
            log.error("Error getting random tip.", e);
        }
    } else {
        // find requested trytes
        try {
            // transactionViewModel = TransactionViewModel.find(Arrays.copyOf(requestedHash.bytes(), TransactionRequester.REQUEST_HASH_SIZE));
            transactionViewModel = TransactionViewModel.fromHash(tangle, new Hash(requestedHash.bytes(), 0, TransactionRequester.REQUEST_HASH_SIZE));
        // log.debug("Requested Hash: " + requestedHash + " \nFound: " + transactionViewModel.getHash());
        } catch (Exception e) {
            log.error("Error while searching for transaction.", e);
        }
    }
    if (transactionViewModel != null && transactionViewModel.getType() == TransactionViewModel.FILLED_SLOT) {
        // send trytes back to neighbor
        try {
            sendPacket(sendingPacket, transactionViewModel, neighbor);
        } catch (Exception e) {
            log.error("Error fetching transaction to request.", e);
        }
    } else {
        // trytes not found
        if (!requestedHash.equals(Hash.NULL_HASH) && rnd.nextDouble() < P_PROPAGATE_REQUEST) {
            // request is an actual transaction and missing in request queue add it.
            try {
                transactionRequester.requestTransaction(requestedHash, false);
            } catch (Exception e) {
                log.error("Error adding transaction to request.", e);
            }
        }
    }
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) Hash(com.iota.iri.model.Hash) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 22 with TransactionViewModel

use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.

the class TransactionValidator method validate.

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;
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel)

Example 23 with TransactionViewModel

use of com.iota.iri.controllers.TransactionViewModel in project iri by iotaledger.

the class BundleValidator method loadTransactionsFromTangle.

private static Map<Hash, TransactionViewModel> loadTransactionsFromTangle(Tangle tangle, TransactionViewModel tail) {
    final Map<Hash, TransactionViewModel> bundleTransactions = new HashMap<>();
    final Hash bundleHash = tail.getBundleHash();
    try {
        TransactionViewModel tx = tail;
        long i = 0, end = tx.lastIndex();
        do {
            bundleTransactions.put(tx.getHash(), tx);
            tx = tx.getTrunkTransaction(tangle);
        } while (i++ < end && tx.getCurrentIndex() != 0 && tx.getBundleHash().equals(bundleHash));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bundleTransactions;
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) Hash(com.iota.iri.model.Hash)

Aggregations

TransactionViewModel (com.iota.iri.controllers.TransactionViewModel)23 Hash (com.iota.iri.model.Hash)14 Test (org.junit.Test)11 TransactionViewModelTest.getRandomTransactionHash (com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionHash)6 SpongeFactory (com.iota.iri.hash.SpongeFactory)3 Tangle (com.iota.iri.storage.Tangle)3 RocksDBPersistenceProvider (com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider)3 Converter (com.iota.iri.utils.Converter)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AfterClass (org.junit.AfterClass)3 BeforeClass (org.junit.BeforeClass)3 TemporaryFolder (org.junit.rules.TemporaryFolder)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 TransactionValidator (com.iota.iri.TransactionValidator)1 Transaction (com.iota.iri.model.Transaction)1 ByteBuffer (java.nio.ByteBuffer)1 MessageDigest (java.security.MessageDigest)1