balance sheet save/load
This commit is contained in:
@@ -40,15 +40,48 @@ bool BalanceSheet_Lookup(uint8_t* address, balance_sheet_entry_t* out) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* TODO */
|
||||
bool BalanceSheet_SaveToFile(const char* outPath) {
|
||||
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;
|
||||
}
|
||||
|
||||
/* TODO */
|
||||
bool BalanceSheet_LoadFromFile(const char* inPath) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -176,6 +176,10 @@ int main(int argc, char* argv[]) {
|
||||
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 =
|
||||
(PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR) > 0
|
||||
? (PHASE1_TARGET_BLOCKS / EMISSION_ACCELERATION_FACTOR)
|
||||
@@ -448,6 +452,7 @@ int main(int argc, char* argv[]) {
|
||||
}*/
|
||||
|
||||
BalanceSheet_Print();
|
||||
BalanceSheet_SaveToFile(chainDataDir);
|
||||
|
||||
Chain_Destroy(chain);
|
||||
Block_ShutdownPowContext();
|
||||
|
||||
Reference in New Issue
Block a user