From 5b26374041da309053b35992a0cf945bb22905a4 Mon Sep 17 00:00:00 2001 From: Paul D Handy Date: Mon, 11 Sep 2017 21:52:17 -0600 Subject: [PATCH] revalidation: obviated race condition --- Dockerfile | 2 +- changelog.txt | 3 ++ dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- src/main/java/com/iota/iri/IRI.java | 2 +- .../java/com/iota/iri/LedgerValidator.java | 2 + src/main/java/com/iota/iri/Milestone.java | 46 +++++++++++-------- .../iota/iri/storage/FileExportProvider.java | 5 ++ .../iota/iri/storage/PersistenceProvider.java | 2 + .../java/com/iota/iri/storage/Tangle.java | 6 +++ .../iota/iri/storage/ZmqPublishProvider.java | 5 ++ .../rocksDB/RocksDBPersistenceProvider.java | 5 ++ 12 files changed, 60 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 52173d7287..f5ff317799 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ COPY . /iri #RUN git clone https://github.com/iotaledger/iri.git /iri/ RUN mvn clean package -RUN mv /iri/target/iri-1.3.2.1.jar /tmp +RUN mv /iri/target/iri-1.3.2.2.jar /tmp RUN rm -rf * RUN rm -rf /tmp/junit* RUN mv /tmp/iri*.jar iri.jar diff --git a/changelog.txt b/changelog.txt index c3294389a4..ca6cabdfe9 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,6 @@ +1.3.2.2 + - Reorganized milestone rescan to improve update time + 1.3.2.1 - Add milestone index parsing checks - Clear all milestone cache with --revalidate diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 3d2b5ab278..363bca2f63 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ com.iota iri IRI - 1.3.2.1 + 1.3.2.2 IOTA Reference Implementation scm:git:git://github.com/iotaledger/iri.git diff --git a/pom.xml b/pom.xml index 266e832958..10b8239e37 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.iota iri - 1.3.2.1 + 1.3.2.2 IRI IOTA Reference Implementation diff --git a/src/main/java/com/iota/iri/IRI.java b/src/main/java/com/iota/iri/IRI.java index 54627a9fe4..f7b803d0e9 100644 --- a/src/main/java/com/iota/iri/IRI.java +++ b/src/main/java/com/iota/iri/IRI.java @@ -40,7 +40,7 @@ public class IRI { public static final String MAINNET_NAME = "IRI"; public static final String TESTNET_NAME = "IRI Testnet"; - public static final String VERSION = "1.3.2.1"; + public static final String VERSION = "1.3.2.2"; public static Iota iota; public static API api; public static IXI ixi; diff --git a/src/main/java/com/iota/iri/LedgerValidator.java b/src/main/java/com/iota/iri/LedgerValidator.java index ea0678e9cd..5ceaa7167d 100644 --- a/src/main/java/com/iota/iri/LedgerValidator.java +++ b/src/main/java/com/iota/iri/LedgerValidator.java @@ -254,6 +254,7 @@ private MilestoneViewModel buildSnapshot(boolean revalidate) throws Exception { } } } + /* if (revalidate) { MilestoneViewModel milestoneViewModel = MilestoneViewModel.first(tangle); milestoneViewModel.delete(tangle); @@ -261,6 +262,7 @@ private MilestoneViewModel buildSnapshot(boolean revalidate) throws Exception { milestoneViewModel.delete(tangle); } } + */ } return consistentMilestone; } diff --git a/src/main/java/com/iota/iri/Milestone.java b/src/main/java/com/iota/iri/Milestone.java index b791742927..3183aae797 100644 --- a/src/main/java/com/iota/iri/Milestone.java +++ b/src/main/java/com/iota/iri/Milestone.java @@ -10,6 +10,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import javax.net.ssl.HttpsURLConnection; @@ -61,36 +62,44 @@ public Milestone(final Tangle tangle, private boolean shuttingDown; private static int RESCAN_INTERVAL = 5000; - public void init(final SpongeFactory.Mode mode, final LedgerValidator ledgerValidator, final boolean revalidate) { + public void init(final SpongeFactory.Mode mode, final LedgerValidator ledgerValidator, final boolean revalidate) throws Exception { this.ledgerValidator = ledgerValidator; + AtomicBoolean ledgerValidatorInitialized = new AtomicBoolean(false); + if (revalidate) { + tangle.clearColumn(com.iota.iri.model.Milestone.class); + tangle.clearColumn(com.iota.iri.model.StateDiff.class); + } (new Thread(() -> { + while(!ledgerValidatorInitialized.get()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } while (!shuttingDown) { long scanTime = System.currentTimeMillis(); try { final int previousLatestMilestoneIndex = latestMilestoneIndex; - + Set hashes = AddressViewModel.load(tangle, coordinator).getHashes(); { // Update Milestone { // find new milestones - AddressViewModel.load(tangle, coordinator).getHashes().stream() - .filter(analyzedMilestoneCandidates::add) - .map(hash -> TransactionViewModel.quietFromHash(tangle, hash)) - .filter(t -> t.getCurrentIndex() == 0) - .forEach(t -> { - try { - if(!validateMilestone(mode, t, getIndex(t))) { - analyzedMilestoneCandidates.remove(t.getHash()); + for(Hash hash: hashes) { + if(analyzedMilestoneCandidates.add(hash)) { + TransactionViewModel t = TransactionViewModel.fromHash(tangle, hash); + if (t.getCurrentIndex() == 0) { + if (validateMilestone(mode, t, getIndex(t))) { + MilestoneViewModel milestoneViewModel = MilestoneViewModel.latest(tangle); + if (milestoneViewModel != null && milestoneViewModel.index() > latestMilestoneIndex) { + latestMilestone = milestoneViewModel.getHash(); + latestMilestoneIndex = milestoneViewModel.index(); } - } catch (Exception e) { + } else { analyzedMilestoneCandidates.remove(t.getHash()); - log.error("Could not validate milestone: ", t.getHash()); } - }); - } - MilestoneViewModel milestoneViewModel = MilestoneViewModel.latest(tangle); - if (milestoneViewModel != null && milestoneViewModel.index() > latestMilestoneIndex) { - latestMilestone = milestoneViewModel.getHash(); - latestMilestoneIndex = milestoneViewModel.index(); + } + } + } } } @@ -113,6 +122,7 @@ public void init(final SpongeFactory.Mode mode, final LedgerValidator ledgerVali try { ledgerValidator.init(revalidate); + ledgerValidatorInitialized.set(true); } catch (Exception e) { log.error("Error initializing snapshots. Skipping.", e); } diff --git a/src/main/java/com/iota/iri/storage/FileExportProvider.java b/src/main/java/com/iota/iri/storage/FileExportProvider.java index 28c781d448..bef3fb3be4 100644 --- a/src/main/java/com/iota/iri/storage/FileExportProvider.java +++ b/src/main/java/com/iota/iri/storage/FileExportProvider.java @@ -148,6 +148,11 @@ public boolean saveBatch(List> models) throws Excep return false; } + @Override + public void clear(Class column) throws Exception { + + } + private static long lastFileNumber = 0L; private static Object lock = new Object(); diff --git a/src/main/java/com/iota/iri/storage/PersistenceProvider.java b/src/main/java/com/iota/iri/storage/PersistenceProvider.java index b8c38a102e..7c0ecfc83e 100644 --- a/src/main/java/com/iota/iri/storage/PersistenceProvider.java +++ b/src/main/java/com/iota/iri/storage/PersistenceProvider.java @@ -42,4 +42,6 @@ public interface PersistenceProvider { Pair first(Class model, Class indexModel) throws Exception; boolean saveBatch(List> models) throws Exception; + + void clear(Class column) throws Exception; } diff --git a/src/main/java/com/iota/iri/storage/Tangle.java b/src/main/java/com/iota/iri/storage/Tangle.java index f9a872074a..8987a6739c 100644 --- a/src/main/java/com/iota/iri/storage/Tangle.java +++ b/src/main/java/com/iota/iri/storage/Tangle.java @@ -181,6 +181,12 @@ public Pair getFirst(Class model, Class index) th return latest; } + public void clearColumn(Class column) throws Exception { + for(PersistenceProvider provider: persistenceProviders) { + provider.clear(column); + } + } + /* public boolean merge(Persistable model, Indexable index) throws Exception { boolean exists = false; diff --git a/src/main/java/com/iota/iri/storage/ZmqPublishProvider.java b/src/main/java/com/iota/iri/storage/ZmqPublishProvider.java index ab4b2df2a6..d25011162e 100644 --- a/src/main/java/com/iota/iri/storage/ZmqPublishProvider.java +++ b/src/main/java/com/iota/iri/storage/ZmqPublishProvider.java @@ -138,4 +138,9 @@ public boolean saveBatch(List> models) throws Excep return false; } + @Override + public void clear(Class column) throws Exception { + + } + } \ No newline at end of file diff --git a/src/main/java/com/iota/iri/storage/rocksDB/RocksDBPersistenceProvider.java b/src/main/java/com/iota/iri/storage/rocksDB/RocksDBPersistenceProvider.java index dd3d1ba2c1..73180681d3 100644 --- a/src/main/java/com/iota/iri/storage/rocksDB/RocksDBPersistenceProvider.java +++ b/src/main/java/com/iota/iri/storage/rocksDB/RocksDBPersistenceProvider.java @@ -351,6 +351,11 @@ public boolean saveBatch(List> models) throws Excep return true; } + @Override + public void clear(Class column) throws Exception { + flushHandle(classTreeMap.get().get(column)); + } + private void flushHandle(ColumnFamilyHandle handle) throws RocksDBException { List itemsToDelete = new ArrayList<>(); RocksIterator iterator = db.newIterator(handle);