Started work to compute a PoW nonce in the online accounts manager.

This isn't used for anything yet, but it's enough to measure the performance implications of continually computing a nonce.
This commit is contained in:
CalDescent 2021-10-09 17:32:43 +01:00
parent bf10c46d9f
commit 6da671ba86
3 changed files with 50 additions and 1 deletions

View File

@ -469,6 +469,10 @@ public class Controller extends Thread {
// LOGGER.info("Starting arbitrary-transaction data manager");
// ArbitraryDataManager.getInstance().start();
// Online accounts manager
LOGGER.info("Starting online accounts manager");
OnlineAccountsManager.getInstance().start();
// Auto-update service?
if (Settings.getInstance().isAutoUpdateEnabled()) {
LOGGER.info("Starting auto-update");
@ -1028,6 +1032,10 @@ public class Controller extends Thread {
// LOGGER.info("Shutting down arbitrary-transaction data manager");
// ArbitraryDataManager.getInstance().shutdown();
// Online accounts manager
LOGGER.info("Shutting down online accounts manager");
OnlineAccountsManager.getInstance().shutdown();
if (blockMinter != null) {
LOGGER.info("Shutting down block minter");
blockMinter.shutdown();

View File

@ -7,6 +7,7 @@ import org.qortal.account.Account;
import org.qortal.account.PrivateKeyAccount;
import org.qortal.account.PublicKeyAccount;
import org.qortal.block.BlockChain;
import org.qortal.crypto.MemoryPoW;
import org.qortal.data.account.MintingAccountData;
import org.qortal.data.account.RewardShareData;
import org.qortal.data.network.OnlineAccountData;
@ -24,12 +25,14 @@ import org.qortal.utils.NTP;
import java.util.*;
import java.util.stream.Collectors;
public class OnlineAccountsManager {
public class OnlineAccountsManager extends Thread {
private static final Logger LOGGER = LogManager.getLogger(OnlineAccountsManager.class);
private static OnlineAccountsManager instance;
private volatile boolean isStopping = false;
// To do with online accounts list
private static final long ONLINE_ACCOUNTS_TASKS_INTERVAL = 10 * 1000L; // ms
private static final long ONLINE_ACCOUNTS_BROADCAST_INTERVAL = 1 * 60 * 1000L; // ms
@ -38,6 +41,8 @@ public class OnlineAccountsManager {
/** How many (latest) blocks' worth of online accounts we cache */
private static final int MAX_BLOCKS_CACHED_ONLINE_ACCOUNTS = 2;
public static final int POW_BUFFER_SIZE = 8 * 1024 * 1024; // bytes
private long onlineAccountsTasksTimestamp = Controller.startTime + ONLINE_ACCOUNTS_TASKS_INTERVAL; // ms
/** Cache of current 'online accounts' */
@ -57,6 +62,36 @@ public class OnlineAccountsManager {
return instance;
}
@Override
public void run() {
Thread.currentThread().setName("Online Accounts Manager");
try {
while (!isStopping) {
Thread.sleep(2000);
int difficulty = 15;
long timestamp = System.currentTimeMillis();
LOGGER.info("Computing nonce at difficulty {} for timestamp {}...", difficulty, timestamp);
byte[] bytes = Longs.toByteArray(timestamp);
Integer nonce = MemoryPoW.compute2(bytes, POW_BUFFER_SIZE, difficulty);
LOGGER.info("Computed nonce: {}. Time taken: {} ms", nonce, (System.currentTimeMillis() - timestamp));
}
} catch (InterruptedException e) {
// Fall-through to exit thread...
}
}
public void shutdown() {
isStopping = true;
this.interrupt();
}
public void checkOnlineAccountsTasks(long now) {
// Perform tasks to do with managing online accounts list

View File

@ -1,5 +1,7 @@
package org.qortal.crypto;
import org.qortal.controller.Controller;
import java.nio.ByteBuffer;
public class MemoryPoW {
@ -33,6 +35,10 @@ public class MemoryPoW {
if (Thread.currentThread().isInterrupted())
return -1;
// Or, if the Controller is stopping, do the same
if (Controller.isStopping())
return -1;
seed *= seedMultiplier; // per nonce
state[0] = longHash[0] ^ seed;