Don't allow core to start if transaction sequences haven't been rebuilt yet.

This commit is contained in:
CalDescent 2023-05-21 12:33:37 +01:00
parent a74fa15d60
commit c6456669e2
2 changed files with 26 additions and 3 deletions

View File

@ -440,6 +440,18 @@ public class Controller extends Thread {
}
}
try (Repository repository = RepositoryManager.getRepository()) {
if (RepositoryManager.needsTransactionSequenceRebuild(repository)) {
// Don't allow the node to start if transaction sequences haven't been built yet
// This is needed to handle a case when bootstrapping
Gui.getInstance().fatalError("Database upgrade needed", "Please start the core again to complete the process.");
return;
}
} catch (DataException e) {
LOGGER.error("Error checking transaction sequences in repository", e);
return;
}
// Import current trade bot states and minting accounts if they exist
Controller.importRepositoryData();

View File

@ -69,6 +69,19 @@ public abstract class RepositoryManager {
// Backup is best-effort so don't complain
}
}
public static boolean needsTransactionSequenceRebuild(Repository repository) throws DataException {
// Check if we have any unpopulated block_sequence values for the first 1000 blocks
List<byte[]> testSignatures = repository.getTransactionRepository().getSignaturesMatchingCustomCriteria(
null, Arrays.asList("block_height < 1000 AND block_sequence IS NULL"), new ArrayList<>());
if (testSignatures.isEmpty()) {
// block_sequence already populated for the first 1000 blocks, so assume complete.
return false;
}
return true;
}
public static boolean rebuildTransactionSequences(Repository repository) throws DataException {
if (Settings.getInstance().isLite()) {
// Lite nodes have no blockchain
@ -81,9 +94,7 @@ public abstract class RepositoryManager {
try {
// Check if we have any unpopulated block_sequence values for the first 1000 blocks
List<byte[]> testSignatures = repository.getTransactionRepository().getSignaturesMatchingCustomCriteria(
null, Arrays.asList("block_height < 1000 AND block_sequence IS NULL"), new ArrayList<>());
if (testSignatures.isEmpty()) {
if (!needsTransactionSequenceRebuild(repository)) {
// block_sequence already populated for the first 1000 blocks, so assume complete.
// We avoid checkpointing and prevent the node from starting up in the case of a rebuild failure, so
// we shouldn't ever be left in a partially rebuilt state.