Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6077 - if no state is loaded, always use config address book. #6079

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.swirlds.logging.LogMarker.STARTUP;

import com.swirlds.common.system.SoftwareVersion;
import com.swirlds.common.system.SwirldState;
import com.swirlds.common.system.address.AddressBook;
import com.swirlds.common.system.address.AddressBookValidator;
import com.swirlds.platform.config.AddressBookConfig;
Expand All @@ -37,7 +36,6 @@
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -69,9 +67,6 @@ public class AddressBookInitializer {
/** The current version of the application from config.txt. */
@NonNull
private final SoftwareVersion currentVersion;
/** The SwirldState to use at genesis. */
@NonNull
private final Supplier<SwirldState> genesisSupplier;
/** The SignedState loaded from disk. May be null. */
@Nullable
private final SignedState loadedSignedState;
Expand All @@ -93,24 +88,20 @@ public class AddressBookInitializer {
private final boolean useConfigAddressBook;

/**
* Constructs an AddressBookInitializer to initialize an address book from the swirld application state, config.txt,
* and the saved state from disk.
* Constructs an AddressBookInitializer to initialize an address book from config.txt,
* the saved state from disk, or the SwirldState on upgrade.
*
* @param currentVersion The current version of the application. Must not be null.
* @param signedState The signed state loaded from disk. May be null.
* @param genesisSupplier The swirld application state in genesis start. Must not be null.
* @param configAddressBook The address book derived from config.txt. Must not be null.
* @param addressBookConfig The configuration settings for AddressBooks.
* @param currentVersion The current version of the application. Must not be null.
* @param signedState The signed state loaded from disk. May be null.
* @param configAddressBook The address book derived from config.txt. Must not be null.
* @param addressBookConfig The configuration settings for AddressBooks.
*/
public AddressBookInitializer(
@NonNull final SoftwareVersion currentVersion,
@Nullable final SignedState signedState,
@NonNull final Supplier<SwirldState> genesisSupplier,
@NonNull final AddressBook configAddressBook,
@NonNull final AddressBookConfig addressBookConfig) {
this.currentVersion = Objects.requireNonNull(currentVersion, "The currentVersion must not be null.");
this.genesisSupplier =
Objects.requireNonNull(genesisSupplier, "The genesis swirldState supplier must not be null.");
this.configAddressBook = Objects.requireNonNull(configAddressBook, "The configAddressBook must not be null.");
Objects.requireNonNull(addressBookConfig, "The addressBookConfig must not be null.");
this.loadedSignedState = signedState;
Expand Down Expand Up @@ -162,11 +153,8 @@ private AddressBook initialize() {
logger.info(
STARTUP.getMarker(),
"The loaded signed state is null. The candidateAddressBook is set to "
+ "genesisSwirldState.updateStake(configAddressBook, null).");
final SwirldState genesisState = genesisSupplier.get();
candidateAddressBook =
genesisState.updateStake(configAddressBook.copy()).copy();
genesisState.release();
+ "the address book from config.txt.");
candidateAddressBook = configAddressBook;
} else {
final SoftwareVersion loadedSoftwareVersion = loadedSignedState
.getState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ private Collection<SwirldsPlatform> createLocalPlatforms(

// Initialize the address book from the configuration and platform saved state.
final AddressBookInitializer addressBookInitializer = new AddressBookInitializer(
appVersion, loadedSignedState, appMain::newState, addressBook.copy(), addressBookConfig);
appVersion, loadedSignedState, addressBook.copy(), addressBookConfig);
// set here, then given to the state in run(). A copy of it is given to hashgraph.
final AddressBook initialAddressBook = addressBookInitializer.getInitialAddressBook();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ void forceUseOfConfigAddressBookNullSignedState() throws IOException {
clearTestDirectory();
final AddressBook configAddressBook = getRandomAddressBook();
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(1),
getMockSignedState(0),
getMockSwirldStateSupplier(1),
configAddressBook,
getAddressBookConfig(true));
getMockSoftwareVersion(1), getMockSignedState(0), configAddressBook, getAddressBookConfig(true));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -89,11 +85,7 @@ void forceUseOfConfigAddressBookNonNullSignedState() throws IOException {
final AddressBook configAddressBook = getRandomAddressBook();
SignedState signedState = getMockSignedState(1);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(1),
signedState,
getMockSwirldStateSupplier(1),
configAddressBook,
getAddressBookConfig(true));
getMockSoftwareVersion(1), signedState, configAddressBook, getAddressBookConfig(true));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -104,20 +96,15 @@ void forceUseOfConfigAddressBookNonNullSignedState() throws IOException {
}

@Test
@DisplayName("No state loaded from disk. Genesis State Initializes Address Book.")
@DisplayName("No state loaded from disk. Config.txt Initializes Address Book.")
void noStateLoadedFromDisk() throws IOException {
clearTestDirectory();
final AddressBook configAddressBook = getRandomAddressBook();
final AddressBook expectedAddressBook = copyWithStakeChanges(configAddressBook, 10);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(1),
getMockSignedState(0),
getMockSwirldStateSupplier(10),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(1), getMockSignedState(0), configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
expectedAddressBook,
configAddressBook,
inititializedAddressBook,
"The initial address book must equal the expected address book.");
assertAddressBookFileContent(initializer, configAddressBook, null, inititializedAddressBook);
Expand All @@ -129,11 +116,7 @@ void noStateLoadedFromDiskGenesisStateSetZeroStake() throws IOException {
clearTestDirectory();
final AddressBook configAddressBook = getRandomAddressBook();
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(1),
getMockSignedState(0),
getMockSwirldStateSupplier(0),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(1), getMockSignedState(0), configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -148,11 +131,7 @@ void noStateLoadedFromDiskGenesisStateChangedAddressBook() throws IOException {
clearTestDirectory();
final AddressBook configAddressBook = getRandomAddressBook();
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(1),
getMockSignedState(0),
getMockSwirldStateSupplier(2),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(1), getMockSignedState(0), configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -168,13 +147,12 @@ void currentVersionLessThanStateVersion() throws IOException {
final SignedState signedState = getMockSignedState(2);
final AddressBook configAddressBook = getRandomAddressBook();
final SoftwareVersion configSoftwareVersion = getMockSoftwareVersion(1);
final Supplier<SwirldState> genesisSwirldState = getMockSwirldStateSupplier(1);
final AddressBookConfig addressBookConfig = getAddressBookConfig(false);

assertThrows(
IllegalStateException.class,
() -> new AddressBookInitializer(
configSoftwareVersion, signedState, genesisSwirldState, configAddressBook, addressBookConfig),
configSoftwareVersion, signedState, configAddressBook, addressBookConfig),
"IllegalStateException was not thrown.");
}

Expand All @@ -185,11 +163,7 @@ void currentVersionEqualsStateVersion() throws IOException {
final SignedState signedState = getMockSignedState(2);
final AddressBook configAddressBook = copyWithStakeChanges(signedState.getAddressBook(), 10);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(2),
signedState,
getMockSwirldStateSupplier(1),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(2), signedState, configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
signedState.getAddressBook(),
Expand All @@ -206,11 +180,7 @@ void versionUpgradeSwirldStateZeroStake() throws IOException {
final SignedState signedState = getMockSignedState(2, 0);
final AddressBook configAddressBook = copyWithStakeChanges(signedState.getAddressBook(), 10);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(3),
signedState,
getMockSwirldStateSupplier(0),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(3), signedState, configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -227,11 +197,7 @@ void versionUpgradeSwirldStateModifiedAddressBook() throws IOException {
final SignedState signedState = getMockSignedState(2, 2);
final AddressBook configAddressBook = copyWithStakeChanges(signedState.getAddressBook(), 3);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(3),
signedState,
getMockSwirldStateSupplier(2),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(3), signedState, configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertEquals(
configAddressBook,
Expand All @@ -248,11 +214,7 @@ void versionUpgradeSwirldStateStakeUpdateWorks() throws IOException {
final SignedState signedState = getMockSignedState(2);
final AddressBook configAddressBook = copyWithStakeChanges(signedState.getAddressBook(), 5);
final AddressBookInitializer initializer = new AddressBookInitializer(
getMockSoftwareVersion(3),
signedState,
getMockSwirldStateSupplier(10),
configAddressBook,
getAddressBookConfig(false));
getMockSoftwareVersion(3), signedState, configAddressBook, getAddressBookConfig(false));
final AddressBook inititializedAddressBook = initializer.getInitialAddressBook();
assertNotEquals(
configAddressBook,
Expand Down