Skip to content

Commit

Permalink
modify the flag setting of randomx
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMLK committed Aug 30, 2023
1 parent 908b4ba commit a10a3e9
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/main/java/io/xdag/crypto/randomx/RandomXWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class RandomXWrapper {
final List<RandomXVM> vms = new ArrayList<>();

private boolean fastInit;
private boolean miningMode;

private Pointer memory;
private int keySize;
Expand All @@ -58,10 +59,10 @@ public final class RandomXWrapper {
* @param key The key to initialize randomX with. (generally a hash)
*/
public void init(byte[] key) {
if(flags.contains(RandomXFlag.FULL_MEM)) {
setCache(key);
if(miningMode) {
//Initialized cache is required to create dataset, first initialize it
setDataset(key);
} else {
setCache(key);
}
}

Expand All @@ -71,10 +72,19 @@ public void init(byte[] key) {
* @return RandomX_VM an Object representing the resulting VM
*/
public RandomXVM createVM() {
if (flags.contains(RandomXFlag.JIT)){
flagsValue = RandomXFlag.JIT.getValue();
if(flags.isEmpty()) {
flagsValue = RandomXJNA.INSTANCE.randomx_get_flags();
} else {
for (RandomXFlag flag : flags) {
flagsValue += flag.getValue();
}

}
PointerByReference pointer = RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset);
if(pointer == null) {
throw new RuntimeException("create randomx vm error.");
}
RandomXVM vm = new RandomXVM(RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset), this);
RandomXVM vm = new RandomXVM(pointer, this);
vms.add(vm);
return vm;
}
Expand All @@ -86,9 +96,7 @@ public RandomXVM createVM() {
private void setCache(byte[] key) {
if(this.memory != null && Arrays.equals(key, this.memory.getByteArray(0, keySize)))
return;
if (flags.contains(RandomXFlag.JIT)){
flagsValue = RandomXFlag.JIT.getValue();
}

PointerByReference newCache = RandomXJNA.INSTANCE.randomx_alloc_cache(flagsValue);

this.memory = new Memory(key.length);
Expand All @@ -113,19 +121,8 @@ private void setDataset(byte[] key) {
return;
}

//Initialized cache is required to create dataset, first initialize it
setCache(key);

PointerByReference newDataset;

//Allocate memory for dataset
if(flags.contains(RandomXFlag.LARGE_PAGES)) {
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.LARGE_PAGES.getValue());
} else if (flags.contains(RandomXFlag.JIT)){
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.JIT.getValue());
} else {
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(0);
}
PointerByReference newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flagsValue);

if(fastInit) {
/*
Expand Down Expand Up @@ -181,21 +178,20 @@ private void setDataset(byte[] key) {
* @param key The key to initialize randomX with. (generally a hash)
*/
public void changeKey(byte[] key) {
if(flags.contains(RandomXFlag.FULL_MEM)) {
setCache(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
}
}
if(miningMode) {
setDataset(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_dataset(vm.getPointer(), dataset);
}

}
} else {
setCache(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
}
}
}
}

Expand Down

0 comments on commit a10a3e9

Please sign in to comment.