balance sheet save/load

This commit is contained in:
2026-04-03 11:43:02 +02:00
parent 6800ce2b60
commit b83f52a448
2 changed files with 40 additions and 2 deletions

View File

@@ -40,15 +40,48 @@ bool BalanceSheet_Lookup(uint8_t* address, balance_sheet_entry_t* out) {
return false; return false;
} }
/* TODO */
bool BalanceSheet_SaveToFile(const char* outPath) { bool BalanceSheet_SaveToFile(const char* outPath) {
if (!sheetMap) { return false; } if (!sheetMap) { return false; }
char outFile[512];
strcpy(outFile, outPath);
strcat(outFile, "/balance_sheet.data");
FILE* file = fopen(outFile, "wb");
if (!file) { return false; }
khiter_t k;
for (k = kh_begin(sheetMap); k != kh_end(sheetMap); ++k) {
if (kh_exist(sheetMap, k)) {
balance_sheet_entry_t entry = kh_val(sheetMap, k);
if (fwrite(&entry, sizeof(balance_sheet_entry_t), 1, file) != 1) {
fclose(file);
return false;
}
}
}
fclose(file);
return true; return true;
} }
/* TODO */
bool BalanceSheet_LoadFromFile(const char* inPath) { bool BalanceSheet_LoadFromFile(const char* inPath) {
if (!sheetMap) { return false; } if (!sheetMap) { return false; }
char inFile[512];
strcpy(inFile, inPath);
strcat(inFile, "/balance_sheet.data");
FILE* file = fopen(inFile, "rb");
if (!file) { return false; }
balance_sheet_entry_t entry;
while (fread(&entry, sizeof(balance_sheet_entry_t), 1, file) == 1) {
if (BalanceSheet_Insert(entry) < 0) {
fclose(file);
return false;
}
}
fclose(file);
return true; return true;
} }

View File

@@ -176,6 +176,10 @@ int main(int argc, char* argv[]) {
printf("No existing chain loaded from %s\n", chainDataDir); printf("No existing chain loaded from %s\n", chainDataDir);
} }
if (!BalanceSheet_LoadFromFile(chainDataDir)) {
printf("Failed to load the balance sheet or none existing\n");
}
const uint64_t effectivePhase1Blocks = const uint64_t effectivePhase1Blocks =
(PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR) > 0 (PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR) > 0
? (PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR) ? (PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR)
@@ -448,6 +452,7 @@ int main(int argc, char* argv[]) {
}*/ }*/
BalanceSheet_Print(); BalanceSheet_Print();
BalanceSheet_SaveToFile(chainDataDir);
Chain_Destroy(chain); Chain_Destroy(chain);
Block_ShutdownPowContext(); Block_ShutdownPowContext();