Skip to content

Commit

Permalink
Remove Stoppable interface and call Interruptable instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardladenthin committed Mar 17, 2024
1 parent c2e51ea commit 1b047dd
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;
import org.lmdbjava.LmdbException;

public abstract class AbstractPlaintextFile {
public abstract class AbstractPlaintextFile implements Interruptable {

@Nonnull
protected final File file;
@Nonnull
protected final ReadStatistic readStatistic;
@Nonnull
protected final Stoppable stoppable;
private final AtomicBoolean shouldRun = new AtomicBoolean(true);

public AbstractPlaintextFile(@Nonnull File file, @Nonnull ReadStatistic readStatistic, @Nonnull Stoppable stoppable) {
public AbstractPlaintextFile(@Nonnull File file, @Nonnull ReadStatistic readStatistic) {
this.file = file;
this.readStatistic = readStatistic;
this.stoppable = stoppable;
}

protected double calculateFileProgress(@Nonnull RandomAccessFile raf) throws IOException {
Expand All @@ -48,7 +48,7 @@ protected double calculateFileProgress(@Nonnull RandomAccessFile raf) throws IOE

public void readFile() throws IOException {
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
while(stoppable.shouldRun()) {
while(shouldRun.get()) {
String line = raf.readLine();
if (line == null) {
return;
Expand All @@ -68,4 +68,10 @@ public void readFile() throws IOException {
}
}
}

@Override
public void interrupt() {
shouldRun.set(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package net.ladenthin.bitcoinaddressfinder;

import java.math.BigInteger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import net.ladenthin.bitcoinaddressfinder.configuration.CProducer;
import org.apache.commons.codec.binary.Hex;
Expand All @@ -33,15 +32,15 @@ public abstract class AbstractProducer implements Producer {
private Logger logger = LoggerFactory.getLogger(this.getClass());

protected final AtomicBoolean running = new AtomicBoolean(false);
protected final Stoppable stoppable;
protected final Consumer consumer;
protected final KeyUtility keyUtility;
protected final SecretFactory secretFactory;
protected final ProducerCompletionCallback producerCompletionCallback;
protected final boolean runOnce;

protected final AtomicBoolean shouldRun = new AtomicBoolean(true);

public AbstractProducer(Stoppable stoppable, Consumer consumer, KeyUtility keyUtility, SecretFactory secretFactory, ProducerCompletionCallback producerCompletionCallback, boolean runOnce) {
this.stoppable = stoppable;
public AbstractProducer(Consumer consumer, KeyUtility keyUtility, SecretFactory secretFactory, ProducerCompletionCallback producerCompletionCallback, boolean runOnce) {
this.consumer = consumer;
this.keyUtility = keyUtility;
this.secretFactory = secretFactory;
Expand All @@ -52,7 +51,7 @@ public AbstractProducer(Stoppable stoppable, Consumer consumer, KeyUtility keyUt
@Override
public void run() {
running.set(true);
while (stoppable.shouldRun()) {
while (shouldRun.get()) {
produceKeys();
if (runOnce) {
break;
Expand Down Expand Up @@ -119,4 +118,8 @@ void setLogger(Logger logger) {
this.logger = logger;
}

@Override
public void interrupt() {
shouldRun.set(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class AddressFile extends AbstractPlaintextFile {
@Nonnull
private final Consumer<String> unsupportedConsumer;

public AddressFile(@Nonnull File file, ReadStatistic readStatistic, @Nonnull NetworkParameters networkParameters, @Nonnull Consumer<AddressToCoin> addressConsumer, @Nonnull Consumer<String> unsupportedConsumer, Stoppable stoppable) {
super(file, readStatistic, stoppable);
public AddressFile(@Nonnull File file, ReadStatistic readStatistic, @Nonnull NetworkParameters networkParameters, @Nonnull Consumer<AddressToCoin> addressConsumer, @Nonnull Consumer<String> unsupportedConsumer) {
super(file, readStatistic);
this.networkParameters = networkParameters;
this.addressConsumer = addressConsumer;
this.unsupportedConsumer = unsupportedConsumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import lombok.NonNull;
import net.ladenthin.bitcoinaddressfinder.configuration.CAddressFilesToLMDB;
import net.ladenthin.bitcoinaddressfinder.persistence.lmdb.LMDBPersistence;

public class AddressFilesToLMDB implements Runnable, Interruptable {

private final static long PROGRESS_LOG = 100_000;

private final Logger logger = LoggerFactory.getLogger(AddressFilesToLMDB.class);

Expand All @@ -42,14 +47,14 @@ public class AddressFilesToLMDB implements Runnable, Interruptable {
private final AtomicLong addressCounter = new AtomicLong();

private final ReadStatistic readStatistic = new ReadStatistic();

@NonNull
AtomicReference<AddressFile> currentAddressFile = new AtomicReference<>();

private final static long PROGRESS_LOG = 100_000;
protected final AtomicBoolean shouldRun = new AtomicBoolean(true);

private final Stoppable stoppable;

public AddressFilesToLMDB(CAddressFilesToLMDB addressFilesToLMDB, Stoppable stoppable) {
public AddressFilesToLMDB(CAddressFilesToLMDB addressFilesToLMDB) {
this.addressFilesToLMDB = addressFilesToLMDB;
this.stoppable = stoppable;
}

@Override
Expand All @@ -69,17 +74,21 @@ public void run() {

logger.info("Iterate address files ...");
for (File file : files) {
if (!shouldRun.get()) {
break;
}
AddressFile addressFile = new AddressFile(
file,
readStatistic,
networkParameters,
this::supported,
this::unsupported,
stoppable
this::unsupported
);

logger.info("process " + file.getAbsolutePath());
currentAddressFile.set(addressFile);
addressFile.readFile();
currentAddressFile.set(null);
logger.info("finished: " + file.getAbsolutePath());

logProgress();
Expand Down Expand Up @@ -119,5 +128,9 @@ private void logProgress() {

@Override
public void interrupt() {
AddressFile addressFile = currentAddressFile.get();
if (addressFile != null) {
addressFile.interrupt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -80,15 +81,15 @@ public class ConsumerJava implements Consumer, Interruptable {
private final List<Future<Void>> consumers = new ArrayList<>();
protected final LinkedBlockingQueue<PublicKeyBytes[]> keysQueue;
private final ByteBufferUtility byteBufferUtility = new ByteBufferUtility(true);
private final Stoppable stoppable;

protected final AtomicLong vanityHits = new AtomicLong();
private final Pattern vanityPattern;

private final AtomicBoolean shouldRun = new AtomicBoolean(true);

protected ConsumerJava(CConsumerJava consumerJava, Stoppable stoppable, KeyUtility keyUtility, PersistenceUtils persistenceUtils) {
protected ConsumerJava(CConsumerJava consumerJava, KeyUtility keyUtility, PersistenceUtils persistenceUtils) {
this.consumerJava = consumerJava;
this.keysQueue = new LinkedBlockingQueue<>(consumerJava.queueSize);
this.stoppable = stoppable;
this.keyUtility = keyUtility;
this.persistenceUtils = persistenceUtils;
if (consumerJava.enableVanity) {
Expand Down Expand Up @@ -153,7 +154,7 @@ private void consumeKeysRunner() {

ByteBuffer threadLocalReuseableByteBuffer = ByteBuffer.allocateDirect(PublicKeyBytes.HASH160_SIZE);

while (stoppable.shouldRun()) {
while (shouldRun.get()) {
if (keysQueue.size() >= consumerJava.queueSize) {
logger.warn("Attention, queue is full. Please increase queue size.");
}
Expand Down Expand Up @@ -343,6 +344,7 @@ public void interrupt() {
} catch (InterruptedException ex) {
// do nothing, it is no problem
}
shouldRun.set(false);
timer.cancel();
}
}
13 changes: 6 additions & 7 deletions src/main/java/net/ladenthin/bitcoinaddressfinder/Finder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class Finder implements Interruptable, ProducerCompletionCallback, Secret
protected Logger logger = LoggerFactory.getLogger(this.getClass());

private final CFinder finder;
private final Stoppable stoppable;
private final Shutdown shutdown;

private final List<ProducerOpenCL> openCLProducers = new ArrayList<>();
Expand All @@ -64,9 +63,8 @@ public class Finder implements Interruptable, ProducerCompletionCallback, Secret
@Nullable
private ConsumerJava consumerJava;

public Finder(CFinder finder, Stoppable stoppable, Shutdown shutdown) {
public Finder(CFinder finder, Shutdown shutdown) {
this.finder = finder;
this.stoppable = stoppable;
this.shutdown = shutdown;
try {
random = SecureRandom.getInstanceStrong();
Expand All @@ -77,7 +75,7 @@ public Finder(CFinder finder, Stoppable stoppable, Shutdown shutdown) {

public void startConsumer() {
if (finder.consumerJava != null) {
consumerJava = new ConsumerJava(finder.consumerJava, stoppable, keyUtility, persistenceUtils);
consumerJava = new ConsumerJava(finder.consumerJava, keyUtility, persistenceUtils);
consumerJava.initLMDB();
consumerJava.startConsumer();
consumerJava.startStatisticsTimer();
Expand All @@ -88,23 +86,23 @@ public void configureProducer() {
if (finder.producerJava != null) {
for (CProducerJava cProducerJava : finder.producerJava) {
cProducerJava.assertGridNumBitsCorrect();
ProducerJava producerJava = new ProducerJava(cProducerJava, stoppable, consumerJava, keyUtility, this, this);
ProducerJava producerJava = new ProducerJava(cProducerJava, consumerJava, keyUtility, this, this);
javaProducers.add(producerJava);
}
}

if (finder.producerJavaSecretsFiles != null) {
for (CProducerJavaSecretsFiles cProducerJavaSecretsFiles : finder.producerJavaSecretsFiles) {
cProducerJavaSecretsFiles.assertGridNumBitsCorrect();
ProducerJavaSecretsFiles producerJavaSecretsFiles = new ProducerJavaSecretsFiles(cProducerJavaSecretsFiles, stoppable, consumerJava, keyUtility, this, this);
ProducerJavaSecretsFiles producerJavaSecretsFiles = new ProducerJavaSecretsFiles(cProducerJavaSecretsFiles, consumerJava, keyUtility, this, this);
javaProducersSecretsFiles.add(producerJavaSecretsFiles);
}
}

if (finder.producerOpenCL != null) {
for (CProducerOpenCL cProducerOpenCL : finder.producerOpenCL) {
cProducerOpenCL.assertGridNumBitsCorrect();
ProducerOpenCL producerOpenCL = new ProducerOpenCL(cProducerOpenCL, stoppable, consumerJava, keyUtility, this, this);
ProducerOpenCL producerOpenCL = new ProducerOpenCL(cProducerOpenCL, consumerJava, keyUtility, this, this);
openCLProducers.add(producerOpenCL);
}
}
Expand All @@ -127,6 +125,7 @@ public void interrupt() {
logger.info("Shut down, please wait for remaining tasks.");

for (Producer producer : getAllProducers()) {
producer.interrupt();
producer.waitTillProducerNotRunning();
producer.releaseProducers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import net.ladenthin.bitcoinaddressfinder.configuration.CLMDBToAddressFile;

public class LMDBToAddressFile implements Runnable, Interruptable {

private final Logger logger = LoggerFactory.getLogger(LMDBToAddressFile.class);

private final CLMDBToAddressFile lmdbToAddressFile;

private final Stoppable stoppable;

private LMDBPersistence persistence;

private final AtomicBoolean shouldRun = new AtomicBoolean(true);

public LMDBToAddressFile(CLMDBToAddressFile lmdbToAddressFile, Stoppable stoppable) {
public LMDBToAddressFile(CLMDBToAddressFile lmdbToAddressFile) {
this.lmdbToAddressFile = lmdbToAddressFile;
this.stoppable = stoppable;
}

@Override
Expand All @@ -55,7 +55,7 @@ public void run() {
File addressesFile = new File(lmdbToAddressFile.addressesFile);
// delete before write all addresses
addressesFile.delete();
persistence.writeAllAmountsToAddressFile(addressesFile, lmdbToAddressFile.addressFileOutputFormat, stoppable);
persistence.writeAllAmountsToAddressFile(addressesFile, lmdbToAddressFile.addressFileOutputFormat, shouldRun);
logger.info("writeAllAmounts done");
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -66,5 +66,6 @@ public void run() {

@Override
public void interrupt() {
shouldRun.set(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.util.Random;

public interface Producer extends Runnable {
public interface Producer extends Runnable, Interruptable {

/**
* Initialize the producer to procue keys with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class ProducerJava extends AbstractProducer {

protected final CProducerJava producerJava;

public ProducerJava(CProducerJava producerJava, Stoppable stoppable, Consumer consumer, KeyUtility keyUtility, SecretFactory secretFactory, ProducerCompletionCallback producerCompletionCallback) {
super(stoppable, consumer, keyUtility, secretFactory, producerCompletionCallback, producerJava.runOnce);
public ProducerJava(CProducerJava producerJava, Consumer consumer, KeyUtility keyUtility, SecretFactory secretFactory, ProducerCompletionCallback producerCompletionCallback) {
super(consumer, keyUtility, secretFactory, producerCompletionCallback, producerJava.runOnce);
this.producerJava = producerJava;
}

Expand Down
Loading

0 comments on commit 1b047dd

Please sign in to comment.