Search in sources :

Example 1 with Validity

use of com.iota.iri.Milestone.Validity in project iri by iotaledger.

the class Milestone method init.

public void init(final SpongeFactory.Mode mode, final LedgerValidator ledgerValidator, final boolean revalidate) throws Exception {
    this.ledgerValidator = ledgerValidator;
    AtomicBoolean ledgerValidatorInitialized = new AtomicBoolean(false);
    (new Thread(() -> {
        while (!ledgerValidatorInitialized.get()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        while (!shuttingDown) {
            long scanTime = System.currentTimeMillis();
            try {
                final int previousLatestMilestoneIndex = latestMilestoneIndex;
                Set<Hash> hashes = AddressViewModel.load(tangle, coordinator).getHashes();
                {
                    // Update Milestone
                    {
                        // find new milestones
                        for (Hash hash : hashes) {
                            if (analyzedMilestoneCandidates.add(hash)) {
                                TransactionViewModel t = TransactionViewModel.fromHash(tangle, hash);
                                if (t.getCurrentIndex() == 0) {
                                    final Validity valid = validateMilestone(mode, t, getIndex(t));
                                    switch(valid) {
                                        case VALID:
                                            MilestoneViewModel milestoneViewModel = MilestoneViewModel.latest(tangle);
                                            if (milestoneViewModel != null && milestoneViewModel.index() > latestMilestoneIndex) {
                                                latestMilestone = milestoneViewModel.getHash();
                                                latestMilestoneIndex = milestoneViewModel.index();
                                            }
                                            break;
                                        case INCOMPLETE:
                                            analyzedMilestoneCandidates.remove(t.getHash());
                                            break;
                                        case INVALID:
                                            // Do nothing
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (previousLatestMilestoneIndex != latestMilestoneIndex) {
                    messageQ.publish("lmi %d %d", previousLatestMilestoneIndex, latestMilestoneIndex);
                    log.info("Latest milestone has changed from #" + previousLatestMilestoneIndex + " to #" + latestMilestoneIndex);
                }
                Thread.sleep(Math.max(1, RESCAN_INTERVAL - (System.currentTimeMillis() - scanTime)));
            } catch (final Exception e) {
                log.error("Error during Latest Milestone updating", e);
            }
        }
    }, "Latest Milestone Tracker")).start();
    (new Thread(() -> {
        try {
            ledgerValidator.init();
            ledgerValidatorInitialized.set(true);
        } catch (Exception e) {
            log.error("Error initializing snapshots. Skipping.", e);
        }
        while (!shuttingDown) {
            long scanTime = System.currentTimeMillis();
            try {
                final int previousSolidSubtangleLatestMilestoneIndex = latestSolidSubtangleMilestoneIndex;
                if (latestSolidSubtangleMilestoneIndex < latestMilestoneIndex) {
                    updateLatestSolidSubtangleMilestone();
                }
                if (previousSolidSubtangleLatestMilestoneIndex != latestSolidSubtangleMilestoneIndex) {
                    messageQ.publish("lmsi %d %d", previousSolidSubtangleLatestMilestoneIndex, latestSolidSubtangleMilestoneIndex);
                    messageQ.publish("lmhs %s", latestSolidSubtangleMilestone);
                    log.info("Latest SOLID SUBTANGLE milestone has changed from #" + previousSolidSubtangleLatestMilestoneIndex + " to #" + latestSolidSubtangleMilestoneIndex);
                }
                Thread.sleep(Math.max(1, RESCAN_INTERVAL - (System.currentTimeMillis() - scanTime)));
            } catch (final Exception e) {
                log.error("Error during Solid Milestone updating", e);
            }
        }
    }, "Solid Milestone Tracker")).start();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Validity(com.iota.iri.Milestone.Validity) Hash(com.iota.iri.model.Hash)

Aggregations

Validity (com.iota.iri.Milestone.Validity)1 Hash (com.iota.iri.model.Hash)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1