Skip to content

Commit

Permalink
refactor: write current roster with state snapshot (#17328)
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Wertz <[email protected]>
  • Loading branch information
edward-swirldslabs authored Jan 10, 2025
1 parent 50747ad commit 500015a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2022-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@ public final class SignedStateFileUtils {
/**
* The name of the file that contains the human-readable address book in the saved state
*/
public static final String CURRENT_ADDRESS_BOOK_FILE_NAME = "currentAddressBook.txt";
public static final String CURRENT_ROSTER_FILE_NAME = "currentRoster.json";

/**
* The initial version of the signature set file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,23 +22,22 @@
import static com.swirlds.logging.legacy.LogMarker.STATE_TO_DISK;
import static com.swirlds.platform.config.internal.PlatformConfigUtils.writeSettingsUsed;
import static com.swirlds.platform.event.preconsensus.BestEffortPcesFileCopy.copyPcesFilesRetryOnFailure;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.CURRENT_ADDRESS_BOOK_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.CURRENT_ROSTER_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.HASH_INFO_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.INIT_SIG_SET_FILE_VERSION;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.SIGNATURE_SET_FILE_NAME;

import com.hedera.hapi.node.state.roster.Roster;
import com.swirlds.common.context.PlatformContext;
import com.swirlds.common.io.streams.MerkleDataOutputStream;
import com.swirlds.common.merkle.utility.MerkleTreeVisualizer;
import com.swirlds.common.platform.NodeId;
import com.swirlds.logging.legacy.payload.StateSavedToDiskPayload;
import com.swirlds.platform.config.StateConfig;
import com.swirlds.platform.recovery.emergencyfile.EmergencyRecoveryFile;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.signed.SigSet;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.system.address.AddressBook;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -157,9 +156,9 @@ public static void writeSignedStateFilesToDirectory(
writeHashInfoFile(platformContext, directory, signedState.getState());
writeMetadataFile(selfId, directory, signedState);
writeEmergencyRecoveryFile(directory, signedState);
if (!signedState.isGenesisState()) {
// Genesis states do not have address books.
writeStateAddressBookFile(directory, RosterUtils.buildAddressBook(signedState.getRoster()));
final Roster currentRoster = signedState.getRoster();
if (currentRoster != null) {
writeRosterFile(directory, currentRoster);
}
writeSettingsUsed(directory, platformContext.getConfiguration());

Expand All @@ -174,17 +173,17 @@ public static void writeSignedStateFilesToDirectory(
}

/**
* Write the state's address book in human-readable form.
* Write the state's roster in human-readable form.
*
* @param directory the directory to write to
* @param addressBook the address book to write
* @param directory the directory to write to
* @param roster the roster to write
*/
private static void writeStateAddressBookFile(@NonNull final Path directory, @NonNull final AddressBook addressBook)
private static void writeRosterFile(@NonNull final Path directory, @NonNull final Roster roster)
throws IOException {
final Path addressBookFile = directory.resolve(CURRENT_ADDRESS_BOOK_FILE_NAME);
final Path rosterFile = directory.resolve(CURRENT_ROSTER_FILE_NAME);

try (final BufferedWriter writer = new BufferedWriter(new FileWriter(addressBookFile.toFile()))) {
writer.write(addressBook.toConfigText());
try (final BufferedWriter writer = new BufferedWriter(new FileWriter(rosterFile.toFile()))) {
writer.write(Roster.JSON.toJSON(roster));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.swirlds.common.io.utility.FileUtils.throwIfFileExists;
import static com.swirlds.platform.state.snapshot.SignedStateFileReader.readStateFile;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.CURRENT_ADDRESS_BOOK_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.CURRENT_ROSTER_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.HASH_INFO_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileUtils.SIGNATURE_SET_FILE_NAME;
import static com.swirlds.platform.state.snapshot.SignedStateFileWriter.writeHashInfoFile;
Expand Down Expand Up @@ -173,7 +173,7 @@ void writeSavedStateToDiskTest() throws IOException {
final Path stateFile = directory.resolve(SIGNED_STATE_FILE_NAME);
final Path hashInfoFile = directory.resolve(HASH_INFO_FILE_NAME);
final Path settingsUsedFile = directory.resolve("settingsUsed.txt");
final Path addressBookFile = directory.resolve(CURRENT_ADDRESS_BOOK_FILE_NAME);
final Path addressBookFile = directory.resolve(CURRENT_ROSTER_FILE_NAME);

throwIfFileExists(stateFile, hashInfoFile, settingsUsedFile, directory);
final String configDir = testDirectory.resolve("data/saved").toString();
Expand Down

0 comments on commit 500015a

Please sign in to comment.