Quicken blockchain validity check at start-up by only checking most recent 1440 blocks

This commit is contained in:
catbref 2020-08-12 14:19:14 +01:00
parent ab1de1aafa
commit 50e2bda020
3 changed files with 10 additions and 7 deletions

View File

@ -482,7 +482,7 @@ public class BlockChain {
} }
/** /**
* Some sort start-up/initialization/checking method. * Some sort of start-up/initialization/checking method.
* *
* @throws SQLException * @throws SQLException
*/ */
@ -492,7 +492,9 @@ public class BlockChain {
rebuildBlockchain(); rebuildBlockchain();
try (final Repository repository = RepositoryManager.getRepository()) { try (final Repository repository = RepositoryManager.getRepository()) {
BlockData detachedBlockData = repository.getBlockRepository().getDetachedBlockSignature(); int startHeight = Math.max(repository.getBlockRepository().getBlockchainHeight() - 1440, 1);
BlockData detachedBlockData = repository.getBlockRepository().getDetachedBlockSignature(startHeight);
if (detachedBlockData != null) { if (detachedBlockData != null) {
LOGGER.error(String.format("Block %d's reference does not match any block's signature", detachedBlockData.getHeight())); LOGGER.error(String.format("Block %d's reference does not match any block's signature", detachedBlockData.getHeight()));

View File

@ -152,11 +152,12 @@ public interface BlockRepository {
public int trimOldOnlineAccountsSignatures(long timestamp) throws DataException; public int trimOldOnlineAccountsSignatures(long timestamp) throws DataException;
/** /**
* Returns first (lowest height) block that doesn't link back to genesis block. * Returns first (lowest height) block that doesn't link back to specified block.
* *
* @param startHeight height of specified block
* @throws DataException * @throws DataException
*/ */
public BlockData getDetachedBlockSignature() throws DataException; public BlockData getDetachedBlockSignature(int startHeight) throws DataException;
/** /**
* Saves block into repository. * Saves block into repository.

View File

@ -471,14 +471,14 @@ public class HSQLDBBlockRepository implements BlockRepository {
} }
@Override @Override
public BlockData getDetachedBlockSignature() throws DataException { public BlockData getDetachedBlockSignature(int startHeight) throws DataException {
String sql = "SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks " String sql = "SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks "
+ "LEFT OUTER JOIN Blocks AS ParentBlocks " + "LEFT OUTER JOIN Blocks AS ParentBlocks "
+ "ON ParentBlocks.signature = Blocks.reference " + "ON ParentBlocks.signature = Blocks.reference "
+ "WHERE ParentBlocks.signature IS NULL AND Blocks.height > 1 " + "WHERE ParentBlocks.signature IS NULL AND Blocks.height > ? "
+ "ORDER BY Blocks.height ASC LIMIT 1"; + "ORDER BY Blocks.height ASC LIMIT 1";
try (ResultSet resultSet = this.repository.checkedExecute(sql)) { try (ResultSet resultSet = this.repository.checkedExecute(sql, startHeight)) {
return getBlockFromResultSet(resultSet); return getBlockFromResultSet(resultSet);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Error fetching block by signature from repository", e); throw new DataException("Error fetching block by signature from repository", e);