Search in sources :

Example 6 with TransactionViewModel

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

the class TransactionValidator method validate.

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

Example 7 with TransactionViewModel

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

the class TransactionValidator method checkSolidity.

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

Example 8 with TransactionViewModel

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

the class TipsManagerTest method updateRatings2TestWorks.

@Test
public void updateRatings2TestWorks() throws Exception {
    TransactionViewModel transaction, transaction1, transaction2, transaction3, transaction4;
    transaction = new TransactionViewModel(getRandomTransactionTrits(), getRandomTransactionHash());
    transaction1 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction.getHash(), transaction.getHash()), getRandomTransactionHash());
    transaction2 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction1.getHash(), transaction1.getHash()), getRandomTransactionHash());
    transaction3 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction2.getHash()), getRandomTransactionHash());
    transaction4 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction3.getHash(), transaction3.getHash()), getRandomTransactionHash());
    transaction.store(tangle);
    transaction1.store(tangle);
    transaction2.store(tangle);
    transaction3.store(tangle);
    transaction4.store(tangle);
    Map<Hash, Long> ratings = new HashMap<>();
    tipsManager.recursiveUpdateRatings(transaction.getHash(), ratings, new HashSet<>());
    Assert.assertTrue(ratings.get(transaction.getHash()).equals(5L));
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) Hash(com.iota.iri.model.Hash) TransactionViewModelTest.getRandomTransactionHash(com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionHash) Test(org.junit.Test)

Example 9 with TransactionViewModel

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

the class TipsManagerTest method updateRatingsTestWorks.

@Test
public void updateRatingsTestWorks() throws Exception {
    TransactionViewModel transaction, transaction1, transaction2, transaction3, transaction4;
    transaction = new TransactionViewModel(getRandomTransactionTrits(), getRandomTransactionHash());
    transaction1 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction.getHash(), transaction.getHash()), getRandomTransactionHash());
    transaction2 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction1.getHash(), transaction1.getHash()), getRandomTransactionHash());
    transaction3 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction1.getHash()), getRandomTransactionHash());
    transaction4 = new TransactionViewModel(getRandomTransactionWithTrunkAndBranch(transaction2.getHash(), transaction3.getHash()), getRandomTransactionHash());
    transaction.store(tangle);
    transaction1.store(tangle);
    transaction2.store(tangle);
    transaction3.store(tangle);
    transaction4.store(tangle);
    Map<Hash, Set<Hash>> ratings = new HashMap<>();
    tipsManager.updateHashRatings(transaction.getHash(), ratings, new HashSet<>());
    Assert.assertEquals(ratings.get(transaction.getHash()).size(), 5);
    Assert.assertEquals(ratings.get(transaction1.getHash()).size(), 4);
    Assert.assertEquals(ratings.get(transaction2.getHash()).size(), 3);
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) Hash(com.iota.iri.model.Hash) TransactionViewModelTest.getRandomTransactionHash(com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionHash) Test(org.junit.Test)

Example 10 with TransactionViewModel

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

the class ZmqPublishProvider method update.

@Override
public boolean update(Persistable model, Indexable index, String item) throws Exception {
    if (model instanceof Transaction) {
        Transaction transaction = ((Transaction) model);
        if (item.contains("sender")) {
            TransactionViewModel transactionViewModel = new TransactionViewModel(transaction, (Hash) index);
            StringBuffer sb = new StringBuffer(600);
            try {
                sb.append("tx ");
                sb.append(transactionViewModel.getHash());
                sb.append(" ");
                sb.append(transactionViewModel.getAddressHash());
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.value()));
                sb.append(" ");
                sb.append(transactionViewModel.getObsoleteTagValue().toString().substring(0, 27));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getTimestamp()));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getCurrentIndex()));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.lastIndex()));
                sb.append(" ");
                sb.append(transactionViewModel.getBundleHash());
                sb.append(" ");
                sb.append(transactionViewModel.getTrunkTransactionHash());
                sb.append(" ");
                sb.append(transactionViewModel.getBranchTransactionHash());
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getArrivalTime()));
                messageQ.publish(sb.toString());
            } catch (Exception e) {
                log.error(sb.toString());
                log.error("Error publishing to zmq.", e);
            }
            return true;
        }
    }
    return false;
}
Also used : Transaction(com.iota.iri.model.Transaction) TransactionViewModel(com.iota.iri.controllers.TransactionViewModel)

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