BlockMinter now only acquires repository instance as needed to prevent long HSQLDB rollbacks

This commit is contained in:
catbref 2022-05-16 20:09:47 +01:00
parent 84d850ee0b
commit 0cf32f6c5e

View File

@ -65,9 +65,8 @@ public class BlockMinter extends Thread {
// Lite nodes do not mint // Lite nodes do not mint
return; return;
} }
try (final Repository repository = RepositoryManager.getRepository()) {
if (Settings.getInstance().getWipeUnconfirmedOnStart()) { if (Settings.getInstance().getWipeUnconfirmedOnStart()) {
try (final Repository repository = RepositoryManager.getRepository()) {
// Wipe existing unconfirmed transactions // Wipe existing unconfirmed transactions
List<TransactionData> unconfirmedTransactions = repository.getTransactionRepository().getUnconfirmedTransactions(); List<TransactionData> unconfirmedTransactions = repository.getTransactionRepository().getUnconfirmedTransactions();
@ -77,10 +76,12 @@ public class BlockMinter extends Thread {
} }
repository.saveChanges(); repository.saveChanges();
} catch (DataException e) {
LOGGER.warn("Repository issue trying to wipe unconfirmed transactions on start-up: {}", e.getMessage());
// Fall-through to normal behaviour in case we can recover
}
} }
// Going to need this a lot...
BlockRepository blockRepository = repository.getBlockRepository();
BlockData previousBlockData = null; BlockData previousBlockData = null;
// Vars to keep track of blocks that were skipped due to chain weight // Vars to keep track of blocks that were skipped due to chain weight
@ -94,13 +95,12 @@ public class BlockMinter extends Thread {
boolean isMintingPossible = false; boolean isMintingPossible = false;
boolean wasMintingPossible = isMintingPossible; boolean wasMintingPossible = isMintingPossible;
while (running) { while (running) {
repository.discardChanges(); // Free repository locks, if any
if (isMintingPossible != wasMintingPossible) if (isMintingPossible != wasMintingPossible)
Controller.getInstance().onMintingPossibleChange(isMintingPossible); Controller.getInstance().onMintingPossibleChange(isMintingPossible);
wasMintingPossible = isMintingPossible; wasMintingPossible = isMintingPossible;
try {
// Sleep for a while // Sleep for a while
Thread.sleep(1000); Thread.sleep(1000);
@ -118,6 +118,10 @@ public class BlockMinter extends Thread {
if (!OnlineAccountsManager.getInstance().hasOnlineAccounts()) if (!OnlineAccountsManager.getInstance().hasOnlineAccounts())
continue; continue;
try (final Repository repository = RepositoryManager.getRepository()) {
// Going to need this a lot...
BlockRepository blockRepository = repository.getBlockRepository();
List<MintingAccountData> mintingAccountsData = repository.getAccountRepository().getMintingAccounts(); List<MintingAccountData> mintingAccountsData = repository.getAccountRepository().getMintingAccounts();
// No minting accounts? // No minting accounts?
if (mintingAccountsData.isEmpty()) if (mintingAccountsData.isEmpty())
@ -194,6 +198,10 @@ public class BlockMinter extends Thread {
// so go ahead and mint a block if possible. // so go ahead and mint a block if possible.
isMintingPossible = true; isMintingPossible = true;
// Reattach newBlocks to new repository handle
for (Block newBlock : newBlocks)
newBlock.setRepository(repository);
// Check blockchain hasn't changed // Check blockchain hasn't changed
if (previousBlockData == null || !Arrays.equals(previousBlockData.getSignature(), lastBlockData.getSignature())) { if (previousBlockData == null || !Arrays.equals(previousBlockData.getSignature(), lastBlockData.getSignature())) {
previousBlockData = lastBlockData; previousBlockData = lastBlockData;
@ -224,7 +232,7 @@ public class BlockMinter extends Thread {
// The last iteration found a higher weight block in the network, so sleep for a while // The last iteration found a higher weight block in the network, so sleep for a while
// to allow is to sync the higher weight chain. We are sleeping here rather than when // to allow is to sync the higher weight chain. We are sleeping here rather than when
// detected as we don't want to hold the blockchain lock open. // detected as we don't want to hold the blockchain lock open.
LOGGER.debug("Sleeping for 10 seconds..."); LOGGER.info("Sleeping for 10 seconds...");
Thread.sleep(10 * 1000L); Thread.sleep(10 * 1000L);
} }
@ -333,16 +341,14 @@ public class BlockMinter extends Thread {
// If less than 30 seconds has passed since first detection the higher weight chain, // If less than 30 seconds has passed since first detection the higher weight chain,
// we should skip our block submission to give us the opportunity to sync to the better chain // we should skip our block submission to give us the opportunity to sync to the better chain
if (NTP.getTime() - timeOfLastLowWeightBlock < 30 * 1000L) { if (NTP.getTime() - timeOfLastLowWeightBlock < 30 * 1000L) {
LOGGER.debug("Higher weight chain found in peers, so not signing a block this round"); LOGGER.info("Higher weight chain found in peers, so not signing a block this round");
LOGGER.debug("Time since detected: {}ms", NTP.getTime() - timeOfLastLowWeightBlock); LOGGER.info("Time since detected: {}", NTP.getTime() - timeOfLastLowWeightBlock);
continue; continue;
} } else {
else {
// More than 30 seconds have passed, so we should submit our block candidate anyway. // More than 30 seconds have passed, so we should submit our block candidate anyway.
LOGGER.debug("More than 30 seconds passed, so proceeding to submit block candidate..."); LOGGER.info("More than 30 seconds passed, so proceeding to submit block candidate...");
} }
} } else {
else {
LOGGER.debug("No higher weight chain found in peers"); LOGGER.debug("No higher weight chain found in peers");
} }
} catch (DataException e) { } catch (DataException e) {
@ -356,7 +362,6 @@ public class BlockMinter extends Thread {
parentSignatureForLastLowWeightBlock = null; parentSignatureForLastLowWeightBlock = null;
timeOfLastLowWeightBlock = null; timeOfLastLowWeightBlock = null;
// Add unconfirmed transactions // Add unconfirmed transactions
addUnconfirmedTransactions(repository, newBlock); addUnconfirmedTransactions(repository, newBlock);
@ -421,14 +426,15 @@ public class BlockMinter extends Thread {
Network network = Network.getInstance(); Network network = Network.getInstance();
network.broadcast(broadcastPeer -> network.buildHeightMessage(broadcastPeer, newBlockData)); network.broadcast(broadcastPeer -> network.buildHeightMessage(broadcastPeer, newBlockData));
} }
}
} catch (DataException e) { } catch (DataException e) {
LOGGER.warn("Repository issue while running block minter", e); LOGGER.warn("Repository issue while running block minter", e);
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
// We've been interrupted - time to exit // We've been interrupted - time to exit
return; return;
} }
} }
}
/** /**
* Adds unconfirmed transactions to passed block. * Adds unconfirmed transactions to passed block.