forked from Qortal/qortal
Replaced all NTP.getTime with System.currentTimeMillis. Clocks handled by O/S
This commit is contained in:
parent
4d265b8acb
commit
2f2d9a664d
@ -31,7 +31,6 @@ import org.qora.network.PeerAddress;
|
||||
import org.qora.repository.DataException;
|
||||
import org.qora.repository.Repository;
|
||||
import org.qora.repository.RepositoryManager;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
@Path("/peers")
|
||||
@Tag(name = "Peers")
|
||||
@ -146,7 +145,7 @@ public class PeersResource {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
PeerAddress peerAddress = PeerAddress.fromString(address);
|
||||
|
||||
PeerData peerData = new PeerData(peerAddress, NTP.getTime(), "API");
|
||||
PeerData peerData = new PeerData(peerAddress, System.currentTimeMillis(), "API");
|
||||
repository.getNetworkRepository().save(peerData);
|
||||
repository.saveChanges();
|
||||
|
||||
|
@ -35,7 +35,6 @@ import org.qora.transform.transaction.TransactionTransformer;
|
||||
import org.qora.transform.transaction.TransactionTransformer.Transformation;
|
||||
import org.qora.utils.BIP39;
|
||||
import org.qora.utils.Base58;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
import com.google.common.primitives.Bytes;
|
||||
@ -424,7 +423,7 @@ public class UtilsResource {
|
||||
}
|
||||
)
|
||||
public long getTimestamp() {
|
||||
return NTP.getTime();
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
@ -40,7 +40,6 @@ import org.qora.transform.TransformationException;
|
||||
import org.qora.transform.block.BlockTransformer;
|
||||
import org.qora.transform.transaction.TransactionTransformer;
|
||||
import org.qora.utils.Base58;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import com.google.common.primitives.Bytes;
|
||||
|
||||
@ -799,7 +798,7 @@ public class Block {
|
||||
return ValidationResult.TIMESTAMP_OLDER_THAN_PARENT;
|
||||
|
||||
// Check timestamp is not in the future (within configurable ~500ms margin)
|
||||
if (this.blockData.getTimestamp() - BlockChain.getInstance().getBlockTimestampMargin() > NTP.getTime())
|
||||
if (this.blockData.getTimestamp() - BlockChain.getInstance().getBlockTimestampMargin() > System.currentTimeMillis())
|
||||
return ValidationResult.TIMESTAMP_IN_FUTURE;
|
||||
|
||||
// Legacy gen1 test: check timestamp milliseconds is the same as parent timestamp milliseconds?
|
||||
|
@ -28,7 +28,6 @@ import org.qora.settings.Settings;
|
||||
import org.qora.transaction.ArbitraryTransaction;
|
||||
import org.qora.transaction.Transaction.TransactionType;
|
||||
import org.qora.transform.Transformer;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
|
||||
@ -132,7 +131,7 @@ public class AutoUpdate extends Thread {
|
||||
for (String repo : autoUpdateRepos)
|
||||
if (attemptUpdate(commitHash, downloadHash, repo)) {
|
||||
// Consider ourselves updated so don't re-re-re-download
|
||||
buildTimestamp = NTP.getTime();
|
||||
buildTimestamp = System.currentTimeMillis();
|
||||
attemptedUpdate = true;
|
||||
break;
|
||||
}
|
||||
|
@ -67,7 +67,6 @@ import org.qora.transaction.Transaction.TransactionType;
|
||||
import org.qora.transaction.Transaction.ValidationResult;
|
||||
import org.qora.ui.UiService;
|
||||
import org.qora.utils.Base58;
|
||||
import org.qora.utils.NTP;
|
||||
import org.qora.utils.Triple;
|
||||
|
||||
public class Controller extends Thread {
|
||||
@ -77,7 +76,7 @@ public class Controller extends Thread {
|
||||
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
|
||||
}
|
||||
|
||||
/** Controller start-up time (ms) taken using <tt>System.currentTimeMillis()</tt>, NOT <tt>NTP.getTime()</tt>. */
|
||||
/** Controller start-up time (ms) taken using <tt>System.currentTimeMillis()</tt>. */
|
||||
public static final long startTime = System.currentTimeMillis();
|
||||
public static final String VERSION_PREFIX = "qora-core-";
|
||||
|
||||
@ -295,7 +294,7 @@ public class Controller extends Thread {
|
||||
}
|
||||
|
||||
// Clean up arbitrary data request cache
|
||||
final long requestMinimumTimestamp = NTP.getTime() - ARBITRARY_REQUEST_TIMEOUT;
|
||||
final long requestMinimumTimestamp = System.currentTimeMillis() - ARBITRARY_REQUEST_TIMEOUT;
|
||||
arbitraryDataRequests.entrySet().removeIf(entry -> entry.getValue().getC() < requestMinimumTimestamp);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
@ -339,7 +338,7 @@ public class Controller extends Thread {
|
||||
|
||||
// Don't use this peer again for a while
|
||||
PeerData peerData = peer.getPeerData();
|
||||
peerData.setLastMisbehaved(NTP.getTime());
|
||||
peerData.setLastMisbehaved(System.currentTimeMillis());
|
||||
|
||||
// Only save to repository if outbound peer
|
||||
if (peer.isOutbound())
|
||||
@ -831,7 +830,7 @@ public class Controller extends Thread {
|
||||
|
||||
byte[] signature = getArbitraryDataMessage.getSignature();
|
||||
String signature58 = Base58.encode(signature);
|
||||
Long timestamp = NTP.getTime();
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
Triple<String, Peer, Long> newEntry = new Triple<>(signature58, peer, timestamp);
|
||||
|
||||
// If we've seen this request recently, then ignore
|
||||
@ -941,7 +940,7 @@ public class Controller extends Thread {
|
||||
|
||||
// Save our request into requests map
|
||||
String signature58 = Base58.encode(signature);
|
||||
Triple<String, Peer, Long> requestEntry = new Triple<>(signature58, null, NTP.getTime());
|
||||
Triple<String, Peer, Long> requestEntry = new Triple<>(signature58, null, System.currentTimeMillis());
|
||||
|
||||
// Assign random ID to this message
|
||||
int id;
|
||||
@ -982,7 +981,7 @@ public class Controller extends Thread {
|
||||
|
||||
public static final Predicate<Peer> hasPeerMisbehaved = peer -> {
|
||||
Long lastMisbehaved = peer.getPeerData().getLastMisbehaved();
|
||||
return lastMisbehaved != null && lastMisbehaved > NTP.getTime() - MISBEHAVIOUR_COOLOFF;
|
||||
return lastMisbehaved != null && lastMisbehaved > System.currentTimeMillis() - MISBEHAVIOUR_COOLOFF;
|
||||
};
|
||||
|
||||
/** Returns whether we think our node has up-to-date blockchain based on our info about other peers. */
|
||||
@ -1011,7 +1010,7 @@ public class Controller extends Thread {
|
||||
}
|
||||
|
||||
public static long getMinimumLatestBlockTimestamp() {
|
||||
return NTP.getTime() - BlockChain.getInstance().getMaxBlockTime() * 1000L * MAX_BLOCKCHAIN_TIP_AGE;
|
||||
return System.currentTimeMillis() - BlockChain.getInstance().getMaxBlockTime() * 1000L * MAX_BLOCKCHAIN_TIP_AGE;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -47,7 +47,6 @@ import org.qora.repository.DataException;
|
||||
import org.qora.repository.Repository;
|
||||
import org.qora.repository.RepositoryManager;
|
||||
import org.qora.settings.Settings;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
// For managing peers
|
||||
public class Network extends Thread {
|
||||
@ -94,7 +93,7 @@ public class Network extends Thread {
|
||||
private ExecutorService peerExecutor;
|
||||
private ExecutorService mergePeersExecutor;
|
||||
private ExecutorService broadcastExecutor;
|
||||
/** Timestamp (ms) for next general info broadcast to all connected peers. Based on <tt>System.currentTimeMillis()</tt> NOT <tt>NTP.getTime()</tt>. */
|
||||
/** Timestamp (ms) for next general info broadcast to all connected peers. Based on <tt>System.currentTimeMillis()</tt>. */
|
||||
private long nextBroadcast;
|
||||
private Lock mergePeersLock;
|
||||
|
||||
@ -237,7 +236,7 @@ public class Network extends Thread {
|
||||
for (String address : INITIAL_PEERS) {
|
||||
PeerAddress peerAddress = PeerAddress.fromString(address);
|
||||
|
||||
PeerData peerData = new PeerData(peerAddress, NTP.getTime(), "INIT");
|
||||
PeerData peerData = new PeerData(peerAddress, System.currentTimeMillis(), "INIT");
|
||||
repository.getNetworkRepository().save(peerData);
|
||||
}
|
||||
|
||||
@ -328,7 +327,7 @@ public class Network extends Thread {
|
||||
// "Old" peers:
|
||||
// we have attempted to connect within the last day
|
||||
// we last managed to connect over a week ago
|
||||
final long now = NTP.getTime();
|
||||
final long now = System.currentTimeMillis();
|
||||
Predicate<PeerData> isNotOldPeer = peerData -> {
|
||||
if (peerData.getLastAttempted() == null || peerData.getLastAttempted() < now - OLD_PEER_ATTEMPTED_PERIOD)
|
||||
return true;
|
||||
@ -371,7 +370,7 @@ public class Network extends Thread {
|
||||
List<PeerData> peers = repository.getNetworkRepository().getAllPeers();
|
||||
|
||||
// Don't consider peers with recent connection failures
|
||||
final long lastAttemptedThreshold = NTP.getTime() - CONNECT_FAILURE_BACKOFF;
|
||||
final long lastAttemptedThreshold = System.currentTimeMillis() - CONNECT_FAILURE_BACKOFF;
|
||||
peers.removeIf(peerData -> peerData.getLastAttempted() != null && peerData.getLastAttempted() > lastAttemptedThreshold);
|
||||
|
||||
// Don't consider peers that we know loop back to ourself
|
||||
@ -422,7 +421,7 @@ public class Network extends Thread {
|
||||
|
||||
// Update connection attempt info
|
||||
repository.discardChanges();
|
||||
peerData.setLastAttempted(NTP.getTime());
|
||||
peerData.setLastAttempted(System.currentTimeMillis());
|
||||
repository.getNetworkRepository().save(peerData);
|
||||
repository.saveChanges();
|
||||
}
|
||||
@ -626,7 +625,7 @@ public class Network extends Thread {
|
||||
LOGGER.debug(String.format("Handshake completed with peer %s", peer));
|
||||
|
||||
// Make a note that we've successfully completed handshake (and when)
|
||||
peer.getPeerData().setLastConnected(NTP.getTime());
|
||||
peer.getPeerData().setLastConnected(System.currentTimeMillis());
|
||||
|
||||
// Update connection info for outbound peers only
|
||||
if (peer.isOutbound())
|
||||
@ -669,7 +668,7 @@ public class Network extends Thread {
|
||||
List<PeerData> knownPeers = repository.getNetworkRepository().getAllPeers();
|
||||
|
||||
// Filter out peers that we've not connected to ever or within X milliseconds
|
||||
final long connectionThreshold = NTP.getTime() - RECENT_CONNECTION_THRESHOLD;
|
||||
final long connectionThreshold = System.currentTimeMillis() - RECENT_CONNECTION_THRESHOLD;
|
||||
Predicate<PeerData> notRecentlyConnected = peerData -> {
|
||||
final Long lastAttempted = peerData.getLastAttempted();
|
||||
final Long lastConnected = peerData.getLastConnected();
|
||||
@ -836,7 +835,7 @@ public class Network extends Thread {
|
||||
try {
|
||||
mergePeersLock.lockInterruptibly();
|
||||
|
||||
final long addedWhen = NTP.getTime();
|
||||
final long addedWhen = System.currentTimeMillis();
|
||||
|
||||
try {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
|
@ -32,7 +32,6 @@ import org.qora.network.message.Message.MessageType;
|
||||
import org.qora.settings.Settings;
|
||||
import org.qora.network.message.PingMessage;
|
||||
import org.qora.network.message.VersionMessage;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import com.google.common.net.HostAndPort;
|
||||
import com.google.common.net.InetAddresses;
|
||||
@ -294,7 +293,7 @@ public class Peer extends Thread {
|
||||
private void setup() throws IOException {
|
||||
this.socket.setSoTimeout(SOCKET_TIMEOUT);
|
||||
this.out = this.socket.getOutputStream();
|
||||
this.connectionTimestamp = NTP.getTime();
|
||||
this.connectionTimestamp = System.currentTimeMillis();
|
||||
this.replyQueues = Collections.synchronizedMap(new HashMap<Integer, BlockingQueue<Message>>());
|
||||
|
||||
this.unsolicitedQueue = new ArrayBlockingQueue<>(UNSOLICITED_MESSAGE_QUEUE_CAPACITY);
|
||||
|
@ -30,7 +30,6 @@ import org.qora.repository.Repository;
|
||||
import org.qora.settings.Settings;
|
||||
import org.qora.transform.TransformationException;
|
||||
import org.qora.transform.transaction.TransactionTransformer;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import static java.util.Arrays.stream;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
@ -518,7 +517,7 @@ public abstract class Transaction {
|
||||
return ValidationResult.TIMESTAMP_TOO_OLD;
|
||||
|
||||
// Transactions with a timestamp too far into future are too new
|
||||
long maxTimestamp = NTP.getTime() + Settings.getInstance().getMaxTransactionTimestampFuture();
|
||||
long maxTimestamp = System.currentTimeMillis() + Settings.getInstance().getMaxTransactionTimestampFuture();
|
||||
if (this.transactionData.getTimestamp() > maxTimestamp)
|
||||
return ValidationResult.TIMESTAMP_TOO_NEW;
|
||||
|
||||
@ -738,7 +737,7 @@ public abstract class Transaction {
|
||||
Transaction transaction = Transaction.fromData(repository, transactionData);
|
||||
|
||||
// Check transaction has not expired
|
||||
if (transaction.getDeadline() <= blockTimestamp || transaction.getDeadline() < NTP.getTime())
|
||||
if (transaction.getDeadline() <= blockTimestamp || transaction.getDeadline() < System.currentTimeMillis())
|
||||
return false;
|
||||
|
||||
// Is transaction is past max approval period?
|
||||
|
@ -1,60 +0,0 @@
|
||||
package org.qora.utils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
import org.apache.commons.net.ntp.NTPUDPClient;
|
||||
import org.apache.commons.net.ntp.TimeInfo;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public final class NTP {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(NTP.class);
|
||||
private static final long TIME_TILL_UPDATE = 1000 * 60 * 10;
|
||||
private static final String NTP_SERVER = "pool.ntp.org";
|
||||
|
||||
private static long lastUpdate = 0;
|
||||
private static long offset = 0;
|
||||
|
||||
/** Returns NTP-synced current time from unix epoch, in milliseconds. */
|
||||
public static long getTime() {
|
||||
// Every so often use NTP to find out offset between this system's time and internet time
|
||||
if (System.currentTimeMillis() > lastUpdate + TIME_TILL_UPDATE) {
|
||||
updateOffset();
|
||||
lastUpdate = System.currentTimeMillis();
|
||||
|
||||
// Log new value of offset
|
||||
LOGGER.info("Adjusting time with %offset% milliseconds.".replace("%offset%", String.valueOf(offset)));
|
||||
}
|
||||
|
||||
// Return time that is nearer internet time
|
||||
return System.currentTimeMillis() + offset;
|
||||
}
|
||||
|
||||
private static void updateOffset() {
|
||||
// Create NTP client
|
||||
NTPUDPClient client = new NTPUDPClient();
|
||||
|
||||
// Set communications timeout
|
||||
client.setDefaultTimeout(10000);
|
||||
try {
|
||||
// Open client (create socket, etc.)
|
||||
client.open();
|
||||
|
||||
// Get time info from NTP server
|
||||
InetAddress hostAddr = InetAddress.getByName(NTP_SERVER);
|
||||
TimeInfo info = client.getTime(hostAddr);
|
||||
info.computeDetails();
|
||||
|
||||
// Cache offset between this system's time and internet time
|
||||
if (info.getOffset() != null)
|
||||
offset = info.getOffset();
|
||||
} catch (Exception e) {
|
||||
// Error while communicating with NTP server - ignored
|
||||
}
|
||||
|
||||
// We're done with NTP client
|
||||
client.close();
|
||||
}
|
||||
|
||||
}
|
@ -10,7 +10,6 @@ import org.qora.repository.Repository;
|
||||
import org.qora.repository.RepositoryManager;
|
||||
import org.qora.test.common.Common;
|
||||
import org.qora.utils.Base58;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@ -48,7 +47,7 @@ public class SignatureTests extends Common {
|
||||
BigDecimal totalFees = BigDecimal.ZERO.setScale(8);
|
||||
byte[] transactionsSignature = null;
|
||||
int height = 0;
|
||||
long timestamp = NTP.getTime() - 5000;
|
||||
long timestamp = System.currentTimeMillis() - 5000;
|
||||
BigDecimal generatingBalance = BigDecimal.valueOf(10_000_000L).setScale(8);
|
||||
byte[] generatorPublicKey = generator.getPublicKey();
|
||||
byte[] generatorSignature = null;
|
||||
|
@ -7,7 +7,6 @@ import org.qora.block.BlockChain;
|
||||
import org.qora.data.transaction.BaseTransactionData;
|
||||
import org.qora.group.Group;
|
||||
import org.qora.repository.DataException;
|
||||
import org.qora.utils.NTP;
|
||||
|
||||
public abstract class TestTransaction {
|
||||
|
||||
@ -18,7 +17,7 @@ public abstract class TestTransaction {
|
||||
if (lastReference == null)
|
||||
lastReference = account.getLastReference();
|
||||
|
||||
return new BaseTransactionData(NTP.getTime(), Group.NO_GROUP, lastReference, account.getPublicKey(), BlockChain.getInstance().getUnitFee(), null);
|
||||
return new BaseTransactionData(System.currentTimeMillis(), Group.NO_GROUP, lastReference, account.getPublicKey(), BlockChain.getInstance().getUnitFee(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user