Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix time slice synchronization and reduce memory usage in RandomX lig… #307

Merged
merged 1 commit into from
Apr 12, 2024
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
25 changes: 12 additions & 13 deletions src/main/java/io/xdag/consensus/XdagSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public class XdagSync {
private volatile boolean isRunning;

private long lastRequestTime;
private int count;

public XdagSync(Kernel kernel) {
this.kernel = kernel;
Expand All @@ -97,22 +96,19 @@ public void start() {
status = Status.SYNCING;
// TODO: paulochen 开始同步的时间点/快照时间点
// startSyncTime = 1588687929343L; // 1716ffdffff 171e52dffff
sendFuture = sendTask.scheduleAtFixedRate(this::syncLoop, 32, 2, TimeUnit.SECONDS);
sendFuture = sendTask.scheduleAtFixedRate(this::syncLoop, 32, 10, TimeUnit.SECONDS);
}
}

private void syncLoop() {
count++;
try {
if (syncWindow.size() < 32) {
if (syncWindow.isEmpty()) {
log.debug("start finding different time periods");
requestBlocks(0, 1L << 48);
}
if (getLastTime() >= lastRequestTime || count == 128) {
count = 0;
log.debug("start getting blocks");
getBlocks();
}

log.debug("start getting blocks");
getBlocks();
} catch (Throwable e) {
log.error("error when requestBlocks {}", e.getMessage());
}
Expand All @@ -138,7 +134,7 @@ private void getBlocks() {

// Segmented requests, each request for 32 time periods.
int size = syncWindow.size();
for (int i = 0; i < 32; i++) {
for (int i = 0; i < 128; i++) {
if (i >= size) {
break;
}
Expand All @@ -149,8 +145,11 @@ private void getBlocks() {
}

long time = syncWindow.get(i);
sendGetBlocks(xc, time, sf);
if (i == 30) lastRequestTime = time;
if (time >= lastRequestTime) {
sendGetBlocks(xc, time, sf);
lastRequestTime = time;
}

}

}
Expand Down Expand Up @@ -183,7 +182,7 @@ private void requestBlocks(long t, long dt) {
setSyncOld();
}

if ((syncWindow.isEmpty() || t > syncWindow.peekFirst()) && syncWindow.size() < 2048) {
if (t > getLastTime()) {
syncWindow.offerLast(t);
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/xdag/crypto/RandomX.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,22 @@ public void randomXPoolUpdateSeed(long memIndex) {
// 分配成功
RandomXJNA.INSTANCE.randomx_init_cache(rx_memory.rxCache, bytesToPointer(rx_memory.seed), new NativeSize(rx_memory.seed.length));

if (rx_memory.rxDataset == null) {
// 分配dataset
rx_memory.rxDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flags);
if (config.getRandomxSpec().getRandomxFlag()) {
if (rx_memory.rxDataset == null) {
//分配失败
log.debug("Failed alloc dataset");
return;
// 分配dataset
rx_memory.rxDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flags);
if (rx_memory.rxDataset == null) {
//分配失败
log.debug("Failed alloc dataset");
return;
}
}

randomXPoolInitDataset(rx_memory.rxCache, rx_memory.rxDataset);
} else {
rx_memory.rxDataset = null;
}

randomXPoolInitDataset(rx_memory.rxCache, rx_memory.rxDataset);

if (randomXUpdateVm(rx_memory, true) == null) {
// update failed
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/xdag/db/rocksdb/BlockStoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,14 @@ public int loadSum(long starttime, long endtime, MutableBytes sums) {
if ((level & 1) != 0) {
// Arrays.fill(sums, (byte)0);
sums.fill((byte) 0);
for (int i = 0; i < 256; i++) {
for (int i = 1; i <= 256; i++) {
// long totalsum = BytesUtils.bytesToLong(buf, i * 16, true);
long totalsum = buf.getLong(i * 16, ByteOrder.LITTLE_ENDIAN);
long totalsum = buf.getLong((i-1) * 16, ByteOrder.LITTLE_ENDIAN);
sum += totalsum;
// long totalsize = BytesUtils.bytesToLong(buf, i * 16 + 8, true);
long totalsize = buf.getLong(i * 16 + 8, ByteOrder.LITTLE_ENDIAN);
long totalsize = buf.getLong((i-1) * 16 + 8, ByteOrder.LITTLE_ENDIAN);
size += totalsize;
if (i % 16 == 0 && i != 0) {
if (i % 16 == 0) {
// System.arraycopy(BytesUtils.longToBytes(sum, true), 0, sums, i - 16, 8);
sums.set(i - 16, Bytes.wrap(BytesUtils.longToBytes(sum, true)));
// System.arraycopy(BytesUtils.longToBytes(size, true), 0, sums, i - 8, 8);
Expand Down
Loading