Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
revalidation: obviated race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul D Handy committed Sep 12, 2017
1 parent 34b147b commit 5b26374
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.iota</groupId>
<artifactId>iri</artifactId>
<name>IRI</name>
<version>1.3.2.1</version>
<version>1.3.2.2</version>
<description>IOTA Reference Implementation</description>
<scm>
<connection>scm:git:git://github.com/iotaledger/iri.git</connection>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.iota</groupId>
<artifactId>iri</artifactId>
<version>1.3.2.1</version>
<version>1.3.2.2</version>

<name>IRI</name>
<description>IOTA Reference Implementation</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/iota/iri/IRI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/iota/iri/LedgerValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,15 @@ private MilestoneViewModel buildSnapshot(boolean revalidate) throws Exception {
}
}
}
/*
if (revalidate) {
MilestoneViewModel milestoneViewModel = MilestoneViewModel.first(tangle);
milestoneViewModel.delete(tangle);
while((milestoneViewModel = milestoneViewModel.next(tangle)) != null) {
milestoneViewModel.delete(tangle);
}
}
*/
}
return consistentMilestone;
}
Expand Down
46 changes: 28 additions & 18 deletions src/main/java/com/iota/iri/Milestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Hash> 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();
}
}
}
}
}

Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/iota/iri/storage/FileExportProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public boolean saveBatch(List<Pair<Indexable, Persistable>> models) throws Excep
return false;
}

@Override
public void clear(Class<?> column) throws Exception {

}

private static long lastFileNumber = 0L;
private static Object lock = new Object();

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/iota/iri/storage/PersistenceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ public interface PersistenceProvider {
Pair<Indexable, Persistable> first(Class<?> model, Class<?> indexModel) throws Exception;

boolean saveBatch(List<Pair<Indexable, Persistable>> models) throws Exception;

void clear(Class<?> column) throws Exception;
}
6 changes: 6 additions & 0 deletions src/main/java/com/iota/iri/storage/Tangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public Pair<Indexable, Persistable > 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;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/iota/iri/storage/ZmqPublishProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ public boolean saveBatch(List<Pair<Indexable, Persistable>> models) throws Excep
return false;
}

@Override
public void clear(Class<?> column) throws Exception {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ public boolean saveBatch(List<Pair<Indexable, Persistable>> 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<byte[]> itemsToDelete = new ArrayList<>();
RocksIterator iterator = db.newIterator(handle);
Expand Down

0 comments on commit 5b26374

Please sign in to comment.