Skip to content

Commit 2e08059

Browse files
authored
Merge pull request #1749 from cyrossignol/fix-snapshot-rebuild
Refactor tally initialization for snapshot rebuild
2 parents d39fd71 + 8ecb400 commit 2e08059

File tree

2 files changed

+206
-156
lines changed

2 files changed

+206
-156
lines changed

src/neuralnet/accrual/snapshot.h

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ class AccrualSnapshotRegistry
750750
{
751751
LogPrintf("Initializing accrual snapshot registry...");
752752

753+
if (!Close()) {
754+
return false;
755+
}
756+
753757
try {
754758
CAutoFile registry_file(
755759
fsbridge::fopen(RegistryPath(), "rb"),
@@ -994,7 +998,7 @@ class AccrualSnapshotRegistry
994998
}
995999
}; // DiskEntry
9961000

997-
FILE* m_file; //!< Handle of the registry file.
1001+
FILE* m_file = nullptr; //!< Handle of the registry file.
9981002
std::vector<Entry> m_entries; //!< Ordered heights of each snapshot.
9991003

10001004
//!
@@ -1185,14 +1189,29 @@ class AccrualSnapshotRepository
11851189
}
11861190

11871191
//!
1188-
//! \brief Check the integrity of each snapshot file on disk by comparing
1189-
//! the file hashes to those in the registry. Clean up any stray snapshot
1190-
//! files.
1192+
//! \brief Assert that the registry contains an entry for a snapshot at the
1193+
//! specified height and that the snapshot file hash matches.
11911194
//!
1192-
//! \throws SnapshotHashMismatchError If the hash of a disk snapshot does
1193-
//! not match the hash recorded in the registry.
1195+
//! \param height Height of the snapshot to check the hash for.
1196+
//!
1197+
//! \throws SnapshotHashMismatchError If the supplied hash does not match
1198+
//! the hash recorded in the registry.
1199+
//! \throws SnapshotMissingError If the registry contains no entry for the
1200+
//! supplied height.
1201+
//!
1202+
void AssertMatch(const uint64_t height) const
1203+
{
1204+
if (const auto* entry = m_registry.TryHeight(height)) {
1205+
entry->AssertHash(AccrualSnapshotReader::Hash(SnapshotPath(height)));
1206+
} else {
1207+
throw SnapshotMissingError(height);
1208+
}
1209+
}
1210+
1211+
//!
1212+
//! \brief Clean up extraneous accrual snapshot files.
11941213
//!
1195-
void AuditSnapshotIntegrity(const CBlockIndex* snapshot_baseline_pindex) const
1214+
void PruneSnapshotFiles() const
11961215
{
11971216
for (const auto& file : fs::directory_iterator(SnapshotDirectory())) {
11981217
const fs::path& file_path = file.path();
@@ -1202,11 +1221,7 @@ class AccrualSnapshotRepository
12021221
}
12031222

12041223
if (const uint64_t height = AccrualSnapshotFile::ParseHeight(file_path)) {
1205-
// If the snapshot height does not exist in the registry, fall-
1206-
// through to the end of the loop and remove the file:
1207-
//
1208-
if (const auto* entry = m_registry.TryHeight(height)) {
1209-
entry->AssertHash(AccrualSnapshotReader::Hash(file_path));
1224+
if (m_registry.TryHeight(height)) {
12101225
continue;
12111226
}
12121227
}
@@ -1218,37 +1233,6 @@ class AccrualSnapshotRepository
12181233

12191234
AccrualSnapshotFile::Remove(file_path);
12201235
}
1221-
1222-
// Iterate through the superblocks from the baseline_superblock
1223-
// forward. At this point, because the hashes of the
1224-
// registry entries have been checked against the files,
1225-
// and orphan files eliminated, the only thing left to check
1226-
// is whether the registry entries actually correspond to the
1227-
// superblocks on chain.
1228-
1229-
// First rewind to the first SB encountered lower than the snapshot
1230-
// baseline pindex, which is the baseline SB. Then seek forward
1231-
// through all of the SB's.
1232-
const CBlockIndex* pindex = snapshot_baseline_pindex;
1233-
1234-
for (; pindex; pindex = pindex->pprev) {
1235-
1236-
if (pindex->nIsSuperBlock == 1) {
1237-
break;
1238-
}
1239-
1240-
}
1241-
1242-
for (; pindex; pindex = pindex->pnext) {
1243-
1244-
if (pindex->nIsSuperBlock != 1) {
1245-
continue;
1246-
}
1247-
1248-
if (m_registry.TryHeight(pindex->nHeight) == nullptr) {
1249-
throw SnapshotMissingError(pindex->nHeight);
1250-
}
1251-
}
12521236
}
12531237

12541238
//!

0 commit comments

Comments
 (0)