Search in sources :

Example 11 with Hash

use of com.iota.iri.model.Hash in project iri by iotaledger.

the class LedgerValidator method updateSnapshotMilestone.

/**
 * Descends through the tree of transactions, through trunk and branch, marking each as {mark} until it reaches
 * a transaction while the transaction confirmed marker is mutually exclusive to {mark}
 * // old @param hash start of the update tree
 * @param hash tail to traverse from
 * @param index milestone index
 * @throws Exception
 */
private void updateSnapshotMilestone(Hash hash, int index) throws Exception {
    Set<Hash> visitedHashes = new HashSet<>();
    final Queue<Hash> nonAnalyzedTransactions = new LinkedList<>(Collections.singleton(hash));
    Hash hashPointer;
    while ((hashPointer = nonAnalyzedTransactions.poll()) != null) {
        if (visitedHashes.add(hashPointer)) {
            final TransactionViewModel transactionViewModel2 = TransactionViewModel.fromHash(tangle, hashPointer);
            if (transactionViewModel2.snapshotIndex() == 0) {
                transactionViewModel2.setSnapshot(tangle, index);
                messageQ.publish("%s %s %d sn", transactionViewModel2.getAddressHash(), transactionViewModel2.getHash(), index);
                messageQ.publish("sn %d %s %s %s %s %s", index, transactionViewModel2.getHash(), transactionViewModel2.getAddressHash(), transactionViewModel2.getTrunkTransactionHash(), transactionViewModel2.getBranchTransactionHash(), transactionViewModel2.getBundleHash());
                nonAnalyzedTransactions.offer(transactionViewModel2.getTrunkTransactionHash());
                nonAnalyzedTransactions.offer(transactionViewModel2.getBranchTransactionHash());
            }
        }
    }
}
Also used : Hash(com.iota.iri.model.Hash)

Example 12 with Hash

use of com.iota.iri.model.Hash in project iri by iotaledger.

the class LedgerValidator method getLatestDiff.

/**
 * Returns a Map of Address and change in balance that can be used to build a new Snapshot state.
 * Under certain conditions, it will return null:
 *  - While descending through transactions, if a transaction is marked as {PREFILLED_SLOT}, then its hash has been
 *    referenced by some transaction, but the transaction data is not found in the database. It notifies
 *    TransactionRequester to increase the probability this transaction will be present the next time this is checked.
 *  - When a transaction marked as a tail transaction (if the current index is 0), but it is not the first transaction
 *    in any of the BundleValidator's transaction lists, then the bundle is marked as invalid, deleted, and re-requested.
 *  - When the bundle is not internally consistent (the sum of all transactions in the bundle must be zero)
 * As transactions are being traversed, it will come upon bundles, and will add the transaction value to {state}.
 * If {milestone} is true, it will search, through trunk and branch, all transactions, starting from {tip},
 * until it reaches a transaction that is marked as a "confirmed" transaction.
 * If {milestone} is false, it will search up until it reaches a confirmed transaction, or until it finds a hash that has been
 * marked as consistent since the previous milestone.
 * @param visitedNonMilestoneSubtangleHashes hashes that have been visited and considered as approved
 * @param tip                                the hash of a transaction to start the search from
 * @param latestSnapshotIndex                index of the latest snapshot to traverse to
 * @param milestone                          marker to indicate whether to stop only at confirmed transactions
 * @return {state}                           the addresses that have a balance changed since the last diff check
 * @throws Exception
 */
public Map<Hash, Long> getLatestDiff(final Set<Hash> visitedNonMilestoneSubtangleHashes, Hash tip, int latestSnapshotIndex, boolean milestone) throws Exception {
    Map<Hash, Long> state = new HashMap<>();
    int numberOfAnalyzedTransactions = 0;
    Set<Hash> countedTx = new HashSet<>(Collections.singleton(Hash.NULL_HASH));
    visitedNonMilestoneSubtangleHashes.add(Hash.NULL_HASH);
    final Queue<Hash> nonAnalyzedTransactions = new LinkedList<>(Collections.singleton(tip));
    Hash transactionPointer;
    while ((transactionPointer = nonAnalyzedTransactions.poll()) != null) {
        if (visitedNonMilestoneSubtangleHashes.add(transactionPointer)) {
            final TransactionViewModel transactionViewModel = TransactionViewModel.fromHash(tangle, transactionPointer);
            if (transactionViewModel.snapshotIndex() == 0 || transactionViewModel.snapshotIndex() > latestSnapshotIndex) {
                numberOfAnalyzedTransactions++;
                if (transactionViewModel.getType() == TransactionViewModel.PREFILLED_SLOT) {
                    transactionRequester.requestTransaction(transactionViewModel.getHash(), milestone);
                    return null;
                } else {
                    if (transactionViewModel.getCurrentIndex() == 0) {
                        boolean validBundle = false;
                        final List<List<TransactionViewModel>> bundleTransactions = BundleValidator.validate(tangle, transactionViewModel.getHash());
                        /*
                            for(List<TransactionViewModel> transactions: bundleTransactions) {
                                if (transactions.size() > 0) {
                                    int index = transactions.get(0).snapshotIndex();
                                    if (index > 0 && index <= latestSnapshotIndex) {
                                        return null;
                                    }
                                }
                            }
                            */
                        for (final List<TransactionViewModel> bundleTransactionViewModels : bundleTransactions) {
                            if (BundleValidator.isInconsistent(bundleTransactionViewModels)) {
                                break;
                            }
                            if (bundleTransactionViewModels.get(0).getHash().equals(transactionViewModel.getHash())) {
                                validBundle = true;
                                for (final TransactionViewModel bundleTransactionViewModel : bundleTransactionViewModels) {
                                    if (bundleTransactionViewModel.value() != 0 && countedTx.add(bundleTransactionViewModel.getHash())) {
                                        final Hash address = bundleTransactionViewModel.getAddressHash();
                                        final Long value = state.get(address);
                                        state.put(address, value == null ? bundleTransactionViewModel.value() : Math.addExact(value, bundleTransactionViewModel.value()));
                                    }
                                }
                                break;
                            }
                        }
                        if (!validBundle) {
                            return null;
                        }
                    }
                    nonAnalyzedTransactions.offer(transactionViewModel.getTrunkTransactionHash());
                    nonAnalyzedTransactions.offer(transactionViewModel.getBranchTransactionHash());
                }
            }
        }
    }
    log.debug("Analyzed transactions = " + numberOfAnalyzedTransactions);
    if (tip == null) {
        numberOfConfirmedTransactions = numberOfAnalyzedTransactions;
    }
    log.debug("Confirmed transactions = " + numberOfConfirmedTransactions);
    return state;
}
Also used : Hash(com.iota.iri.model.Hash)

Example 13 with Hash

use of com.iota.iri.model.Hash in project iri by iotaledger.

the class LedgerValidator method checkConsistency.

public boolean checkConsistency(List<Hash> hashes) throws Exception {
    Set<Hash> visitedHashes = new HashSet<>();
    Map<Hash, Long> diff = new HashMap<>();
    for (Hash hash : hashes) {
        if (!updateDiff(visitedHashes, diff, hash)) {
            return false;
        }
    }
    return true;
}
Also used : Hash(com.iota.iri.model.Hash)

Example 14 with Hash

use of com.iota.iri.model.Hash in project iri by iotaledger.

the class TipsViewModelTest method nonsolidCapacityLimited.

@Test
public void nonsolidCapacityLimited() throws ExecutionException, InterruptedException {
    TipsViewModel tipsVM = new TipsViewModel();
    int capacity = TipsViewModel.MAX_TIPS;
    // fill tips list
    for (int i = 0; i < capacity * 2; i++) {
        Hash hash = TransactionViewModelTest.getRandomTransactionHash();
        tipsVM.addTipHash(hash);
    }
    // check that limit wasn't breached
    assertEquals(capacity, tipsVM.nonSolidSize());
}
Also used : Hash(com.iota.iri.model.Hash) Test(org.junit.Test)

Example 15 with Hash

use of com.iota.iri.model.Hash in project iri by iotaledger.

the class TipsViewModelTest method solidCapacityLimited.

@Test
public void solidCapacityLimited() throws ExecutionException, InterruptedException {
    TipsViewModel tipsVM = new TipsViewModel();
    int capacity = TipsViewModel.MAX_TIPS;
    // fill tips list
    for (int i = 0; i < capacity * 2; i++) {
        Hash hash = TransactionViewModelTest.getRandomTransactionHash();
        tipsVM.addTipHash(hash);
        tipsVM.setSolid(hash);
    }
    // check that limit wasn't breached
    assertEquals(capacity, tipsVM.size());
}
Also used : Hash(com.iota.iri.model.Hash) Test(org.junit.Test)

Aggregations

Hash (com.iota.iri.model.Hash)75 Test (org.junit.Test)16 TransactionViewModel (com.iota.iri.controllers.TransactionViewModel)12 RocksDBPersistenceProviderTest (com.iota.iri.storage.rocksDB.RocksDBPersistenceProviderTest)12 HttpString (io.undertow.util.HttpString)8 TransactionViewModelTest.getRandomTransactionHash (com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionHash)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Curl (com.iota.iri.hash.Curl)3 PearlDiver (com.iota.iri.hash.PearlDiver)3 TransactionRequester (com.iota.iri.network.TransactionRequester)3 Converter (com.iota.iri.utils.Converter)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 com.iota.iri (com.iota.iri)2 Configuration (com.iota.iri.conf.Configuration)2 DefaultConfSettings (com.iota.iri.conf.Configuration.DefaultConfSettings)2