Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
java-version: '21'
cache: 'maven'

- name: Build with Maven
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Support the project with XDAG donations:

### **1. Requirements**

- **JDK**: v17 or later
- **JDK**: v21 or later
- **Maven**: v3.9.9 or later
- **CMake**: v3.5 or later
- **GCC/Compiler**: GCC v4.8 or later (v7+ recommended for best performance)
Expand Down Expand Up @@ -115,7 +115,7 @@ To include `xdagj-native-randomx` in your project, add the following dependency
<dependency>
<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
</dependency>
```

Expand Down
18 changes: 15 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>

<name>xdagj-native-randomx</name>
<description>A Java RandomX Library For XDAGJ</description>
Expand All @@ -17,8 +17,8 @@
<!-- Build properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

<!-- Dependency versions -->
<commons-lang3.version>3.17.0</commons-lang3.version>
Expand Down Expand Up @@ -81,6 +81,14 @@
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<!-- Add other annotation processors if needed -->
</annotationProcessorPaths>
</configuration>
</plugin>

Expand All @@ -91,6 +99,7 @@
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<printSummary>true</printSummary>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>

Expand Down Expand Up @@ -151,6 +160,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/xdag/crypto/randomx/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
*/
public class Example {

/**
* Default constructor for the Example class.
* This constructor is intentionally empty as this class primarily serves as a demonstration
* with static methods or through its main method.
*/
public Example() {
// Default constructor
}

/**
* Main method demonstrating the RandomX hashing process.
* Shows initialization of RandomX components and hash calculation.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/xdag/crypto/randomx/RandomXFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public enum RandomXFlag {
* Each bit in the input value corresponds to a specific flag.
*
* @param flags The combined integer value of multiple flags
* @return A set of RandomXFlag enums corresponding to the enabled bits
* @return A set of RandomXFlag enums corresponding to the enabled bits.
*/
public static Set<RandomXFlag> fromValue(int flags) {
EnumSet<RandomXFlag> result = EnumSet.noneOf(RandomXFlag.class);
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/io/xdag/crypto/randomx/RandomXLibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
final class RandomXLibraryLoader {

private static boolean isLoaded = false;
private static String loadedLibraryPath = null; // Store the path of the loaded library for logging
private static Path loadedLibraryPath = null; // Store the path of the loaded library

// Private constructor to prevent instantiation
private RandomXLibraryLoader() {}
Expand All @@ -53,16 +53,19 @@ private RandomXLibraryLoader() {}
*
* @throws UnsatisfiedLinkError if the library cannot be loaded for any reason.
* @throws Exception for other unexpected errors during loading.
* @return Path to the loaded native library file, or null if already loaded or failed before critical point.
*/
public static synchronized void load() throws Exception {
public static synchronized Path load() throws Exception {
if (isLoaded) {
log.info("Native library already loaded from: {}", loadedLibraryPath);
return;
return loadedLibraryPath; // Return cached path
}

Path tempLibFilePath = null;
try {
File tempFile = extractAndLoadNativeLibrary();
loadedLibraryPath = tempFile.getAbsolutePath();
File tempFile = extractAndLoadNativeLibrary(); // This method now returns File
tempLibFilePath = tempFile.toPath();
loadedLibraryPath = tempLibFilePath; // Cache the path

String tempLibDir = tempFile.getParent();
if (tempLibDir != null) {
Expand All @@ -79,10 +82,12 @@ public static synchronized void load() throws Exception {

isLoaded = true;
log.info("RandomX native library loaded successfully via RandomXLibraryLoader from: {}", loadedLibraryPath);
return loadedLibraryPath;

} catch (UnsatisfiedLinkError ule) { // Catch specifically from System.load()
log.error("Failed to load native library (UnsatisfiedLinkError from System.load()): {}: {}",
(loadedLibraryPath != null ? loadedLibraryPath : "<path not determined>"), ule.getMessage(), ule);
(loadedLibraryPath != null ? loadedLibraryPath : (tempLibFilePath != null ? tempLibFilePath : "<path not determined>")),
ule.getMessage(), ule);
logLibraryPaths(); // Log paths for diagnostics
throw ule; // Re-throw to be handled by RandomXNative's static block
} catch (Exception e) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/xdag/crypto/randomx/RandomXNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
* This class registers the native methods after the library is successfully loaded.
*/
public class RandomXNative {
/**
* Private constructor to prevent instantiation.
* This class provides static JNA mappings and should not be instantiated.
*/
private RandomXNative() {
throw new UnsupportedOperationException("This is a JNA mapping class and cannot be instantiated");
}

static {
try {
// Step 1: Initialize the library using the loader.
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/xdag/crypto/randomx/RandomXTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@
@ToString
@Slf4j
public class RandomXTemplate implements AutoCloseable {
/**
* Private constructor to be used by the Lombok generated builder.
* Direct instantiation is discouraged; use the builder pattern.
*/
private RandomXTemplate(boolean miningMode, Set<RandomXFlag> flags, RandomXCache cache, RandomXDataset dataset, RandomXVM vm, byte[] currentKey) {
this.miningMode = miningMode;
this.flags = flags;
this.cache = cache;
this.dataset = dataset;
this.vm = vm;
this.currentKey = currentKey;
}

/** Flag indicating if the template is in mining mode */
@Getter
private final boolean miningMode;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/xdag/crypto/randomx/RandomXUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
*/
public final class RandomXUtils {

/**
* Private constructor to prevent instantiation of this utility class.
*/
private RandomXUtils() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

/**
* The size of a RandomX hash in bytes (usually 32 bytes).
*/
Expand Down
Loading