From 0271ef69c9160ad10cc6c3606abb8c725412650c Mon Sep 17 00:00:00 2001 From: CalDescent Date: Wed, 23 Feb 2022 20:06:55 +0000 Subject: [PATCH] When submitting a new transaction, treat the chain as "synced" if the latest block is less than 30 minutes old. Increased from around 7.5 minutes. --- .../api/resource/TransactionsResource.java | 5 ++++- .../org/qortal/controller/Controller.java | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/qortal/api/resource/TransactionsResource.java b/src/main/java/org/qortal/api/resource/TransactionsResource.java index 9bc6d497..55ad7cde 100644 --- a/src/main/java/org/qortal/api/resource/TransactionsResource.java +++ b/src/main/java/org/qortal/api/resource/TransactionsResource.java @@ -638,7 +638,10 @@ public class TransactionsResource { ApiError.BLOCKCHAIN_NEEDS_SYNC, ApiError.INVALID_SIGNATURE, ApiError.INVALID_DATA, ApiError.TRANSFORMATION_ERROR, ApiError.REPOSITORY_ISSUE }) public String processTransaction(String rawBytes58) { - if (!Controller.getInstance().isUpToDate()) + // Only allow a transaction to be processed if our latest block is less than 30 minutes old + // If older than this, we should first wait until the blockchain is synced + final Long minLatestBlockTimestamp = NTP.getTime() - (30 * 60 * 1000L); + if (!Controller.getInstance().isUpToDate(minLatestBlockTimestamp)) throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.BLOCKCHAIN_NEEDS_SYNC); byte[] rawBytes = Base58.decode(rawBytes58); diff --git a/src/main/java/org/qortal/controller/Controller.java b/src/main/java/org/qortal/controller/Controller.java index fb1cbd47..542a2889 100644 --- a/src/main/java/org/qortal/controller/Controller.java +++ b/src/main/java/org/qortal/controller/Controller.java @@ -2048,10 +2048,13 @@ public class Controller extends Thread { return peers; } - /** Returns whether we think our node has up-to-date blockchain based on our info about other peers. */ - public boolean isUpToDate() { + /** + * Returns whether we think our node has up-to-date blockchain based on our info about other peers. + * @param minLatestBlockTimestamp - the minimum block timestamp to be considered recent + * @return boolean - whether our node's blockchain is up to date or not + */ + public boolean isUpToDate(Long minLatestBlockTimestamp) { // Do we even have a vaguely recent block? - final Long minLatestBlockTimestamp = getMinimumLatestBlockTimestamp(); if (minLatestBlockTimestamp == null) return false; @@ -2077,6 +2080,16 @@ public class Controller extends Thread { return !peers.isEmpty(); } + /** + * Returns whether we think our node has up-to-date blockchain based on our info about other peers. + * Uses the default minLatestBlockTimestamp value. + * @return boolean - whether our node's blockchain is up to date or not + */ + public boolean isUpToDate() { + final Long minLatestBlockTimestamp = getMinimumLatestBlockTimestamp(); + return this.isUpToDate(minLatestBlockTimestamp); + } + /** Returns minimum block timestamp for block to be considered 'recent', or null if NTP not synced. */ public static Long getMinimumLatestBlockTimestamp() { Long now = NTP.getTime();