Refactor removeAndBackupDaoConsensusFiles method

Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2024-06-28 19:14:56 +07:00
parent 8be5f8a962
commit 02366eb6da
No known key found for this signature in database
GPG key ID: 02AA2BAE387C8307
2 changed files with 34 additions and 44 deletions

View file

@ -46,7 +46,7 @@ public class BsqBlocksStorageService {
public final static String NAME = "BsqBlocks";
private final int genesisBlockHeight;
private final File storageDir;
private final File blocksDir;
private final BlocksPersistence blocksPersistence;
@Getter
private int chainHeightOfPersistedBlocks;
@ -54,10 +54,10 @@ public class BsqBlocksStorageService {
@Inject
public BsqBlocksStorageService(GenesisTxInfo genesisTxInfo,
PersistenceProtoResolver persistenceProtoResolver,
@Named(Config.STORAGE_DIR) File dbStorageDir) {
@Named(Config.STORAGE_DIR) File storageDir) {
genesisBlockHeight = genesisTxInfo.getGenesisBlockHeight();
storageDir = new File(dbStorageDir.getAbsolutePath() + File.separator + NAME);
blocksPersistence = new BlocksPersistence(storageDir, NAME, persistenceProtoResolver);
blocksDir = new File(storageDir.getAbsolutePath() + File.separator + NAME);
blocksPersistence = new BlocksPersistence(blocksDir, NAME, persistenceProtoResolver);
}
public void persistBlocks(List<Block> blocks) {
@ -108,7 +108,7 @@ public class BsqBlocksStorageService {
String dirName = BsqBlocksStorageService.NAME;
String resourceDir = dirName + postFix;
try {
if (storageDir.exists()) {
if (blocksDir.exists()) {
log.info("No resource directory was copied. {} exists already.", dirName);
return;
}
@ -118,11 +118,11 @@ public class BsqBlocksStorageService {
log.info("No files in directory. {}", resourceDir);
return;
}
if (!storageDir.exists()) {
storageDir.mkdir();
if (!blocksDir.exists()) {
blocksDir.mkdir();
}
for (String fileName : fileNames) {
File destinationFile = new File(storageDir, fileName);
File destinationFile = new File(blocksDir, fileName);
// File.separator doesn't appear to work on Windows. It has to be "/", not "\".
// See: https://github.com/bisq-network/bisq/pull/5909#pullrequestreview-827992563
FileUtil.resourceToFile(resourceDir + "/" + fileName, destinationFile);
@ -144,12 +144,9 @@ public class BsqBlocksStorageService {
blocksPersistence.removeBlocksDirectory();
}
// We recreate the directory so that we don't fill the blocks after restart from resources
// In copyFromResources we only check for the directory not the files inside.
public void removeBlocksInDirectory() {
blocksPersistence.removeBlocksDirectory();
if (!storageDir.exists()) {
storageDir.mkdir();
public void makeBlocksDirectory() {
if (!blocksDir.exists()) {
blocksDir.mkdir();
}
}
}

View file

@ -191,9 +191,11 @@ public class DaoStateStorageService extends StoreService<DaoStateStore> {
}
public void resyncDaoStateFromGenesis(Runnable resultHandler) {
String backupDirName = "out_of_sync_dao_data";
try {
removeAndBackupDaoConsensusFiles(backupDirName);
removeAndBackupDaoConsensusFiles(false);
// We recreate the directory so that we don't fill the blocks after restart from resources
// In copyFromResources we only check for the directory not the files inside.
bsqBlocksStorageService.makeBlocksDirectory();
} catch (Throwable t) {
log.error(t.toString());
}
@ -201,42 +203,33 @@ public class DaoStateStorageService extends StoreService<DaoStateStore> {
store.setDaoStateAsProto(DaoState.getBsqStateCloneExcludingBlocks(new DaoState()));
store.setDaoStateHashChain(new LinkedList<>());
persistenceManager.persistNow(resultHandler);
bsqBlocksStorageService.removeBlocksInDirectory();
}
public void removeAndBackupAllDaoData() throws IOException {
// We delete all DAO consensus data and remove the daoState, so it will rebuild from latest
// We delete all DAO consensus data and remove the daoState and blocks, so it will rebuild from latest
// resource files.
String backupDirName = "out_of_sync_dao_data";
removeAndBackupDaoConsensusFiles(backupDirName);
String newFileName = "DaoStateStore_" + System.currentTimeMillis();
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "DaoStateStore"), newFileName, backupDirName);
removeAndBackupDaoConsensusFiles(true);
}
private void removeAndBackupDaoConsensusFiles(boolean removeDaoStateStore) throws IOException {
// We delete all DAO related data. Some will be rebuilt from resources.
if (removeDaoStateStore) {
removeAndBackupFile("DaoStateStore");
}
removeAndBackupFile("BlindVoteStore");
removeAndBackupFile("ProposalStore");
// We also need to remove ballot list as it contains the proposals as well. It will be recreated at resync
removeAndBackupFile("BallotList");
removeAndBackupFile("UnconfirmedBsqChangeOutputList");
removeAndBackupFile("TempProposalStore");
removeAndBackupFile("BurningManAccountingStore_v3");
bsqBlocksStorageService.removeBlocksDirectory();
}
private void removeAndBackupDaoConsensusFiles(String backupDirName) throws IOException {
// We delete all DAO related data. Some will be rebuilt from resources.
long currentTime = System.currentTimeMillis();
String newFileName = "BlindVoteStore_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "BlindVoteStore"), newFileName, backupDirName);
newFileName = "ProposalStore_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "ProposalStore"), newFileName, backupDirName);
// We also need to remove ballot list as it contains the proposals as well. It will be recreated at resync
newFileName = "BallotList_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "BallotList"), newFileName, backupDirName);
newFileName = "UnconfirmedBsqChangeOutputList_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "UnconfirmedBsqChangeOutputList"), newFileName, backupDirName);
newFileName = "TempProposalStore_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "TempProposalStore"), newFileName, backupDirName);
newFileName = "BurningManAccountingStore_v3_" + currentTime;
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, "BurningManAccountingStore_v3"), newFileName, backupDirName);
private void removeAndBackupFile(String fileName) throws IOException {
String backupDirName = "out_of_sync_dao_data";
String newFileName = fileName + "_" + System.currentTimeMillis();
FileUtil.removeAndBackupFile(storageDir, new File(storageDir, fileName), newFileName, backupDirName);
}