Skip to content

Commit

Permalink
feat: Initialize states API before Platform is constructed (#16209)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Moran <[email protected]>
Signed-off-by: Neeharika-Sompalli <[email protected]>
Signed-off-by: Michael Tinker <[email protected]>
Co-authored-by: Thomas Moran <[email protected]>
Co-authored-by: Michael Tinker <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2024
1 parent 04d2082 commit bb42578
Show file tree
Hide file tree
Showing 42 changed files with 480 additions and 724 deletions.
148 changes: 69 additions & 79 deletions hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import com.swirlds.common.crypto.CryptographyFactory;
import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.io.filesystem.FileSystemManager;
import com.swirlds.common.io.utility.FileUtils;
import com.swirlds.common.io.utility.RecycleBin;
import com.swirlds.common.merkle.crypto.MerkleCryptoFactory;
import com.swirlds.common.merkle.crypto.MerkleCryptographyFactory;
Expand All @@ -56,6 +55,7 @@
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource;
import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource;
import com.swirlds.platform.Browser;
import com.swirlds.platform.CommandLineArgs;
import com.swirlds.platform.ParameterProvider;
import com.swirlds.platform.builder.PlatformBuilder;
Expand All @@ -77,6 +77,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -134,7 +135,7 @@ public void run() {
}

/**
* Launches Services directly, without use of the "app browser" from {@link com.swirlds.platform.Browser}. The
* Launches Services directly, without use of the "app browser" from {@link Browser}. The
* approximate startup sequence is:
* <ol>
* <li>Scan the classpath for {@link RuntimeConstructable} classes,
Expand Down Expand Up @@ -169,7 +170,6 @@ public void run() {
public static void main(final String... args) throws Exception {
BootstrapUtils.setupConstructableRegistry();
final Hedera hedera = newHedera();

// Determine which node to run locally
// Load config.txt address book file and parse address book
final AddressBook diskAddressBook = loadAddressBook(DEFAULT_CONFIG_FILE_NAME);
Expand Down Expand Up @@ -207,6 +207,33 @@ public static void main(final String... args) throws Exception {
final var recycleBin =
RecycleBin.create(metrics, configuration, getStaticThreadManager(), time, fileSystemManager, selfId);

// Create initial state for the platform
final var isGenesis = new AtomicBoolean(false);
final var reservedState = getInitialState(
configuration,
recycleBin,
version,
() -> {
isGenesis.set(true);
final var genesisState = hedera.newMerkleStateRoot();
hedera.initializeStatesApi(
(MerkleStateRoot) genesisState, metrics, InitTrigger.GENESIS, diskAddressBook);
return genesisState;
},
SignedStateFileUtils::readState,
Hedera.APP_NAME,
Hedera.SWIRLD_NAME,
selfId,
diskAddressBook);
final var initialState = reservedState.state();
if (!isGenesis.get()) {
hedera.initializeStatesApi(
(MerkleStateRoot) initialState.get().getState().getSwirldState(),
metrics,
InitTrigger.RESTART,
null);
}

final var cryptography = CryptographyFactory.create();
CryptographyHolder.set(cryptography);
// the AddressBook is not changed after this point, so we calculate the hash now
Expand All @@ -225,17 +252,7 @@ public static void main(final String... args) throws Exception {
FileSystemManager.create(configuration),
recycleBin,
merkleCryptography);
// Create initial state for the platform
final var reservedState = getInitialState(
platformContext,
version,
hedera::newMerkleStateRoot,
SignedStateFileUtils::readState,
Hedera.APP_NAME,
Hedera.SWIRLD_NAME,
selfId,
diskAddressBook);
final var initialState = reservedState.state();

final var stateHash = reservedState.hash();

// Initialize the address book and set on platform builder
Expand Down Expand Up @@ -355,7 +372,7 @@ private static AddressBook loadAddressBook(@NonNull final String addressBookPath
requireNonNull(addressBookPath);
try {
final LegacyConfigProperties props =
LegacyConfigPropertiesLoader.loadConfigFile(FileUtils.getAbsolutePath(addressBookPath));
LegacyConfigPropertiesLoader.loadConfigFile(getAbsolutePath(addressBookPath));
props.appConfig().ifPresent(c -> ParameterProvider.getInstance().setParameters(c.params()));
return props.getAddressBook();
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.hedera.node.app.service.token.impl.schemas.V0490TokenSchema.STAKING_INFO_KEY;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.block.stream.output.StateChanges;
import com.hedera.hapi.node.state.common.EntityNumber;
import com.hedera.hapi.node.state.token.StakingNodeInfo;
import com.hedera.node.app.Hedera;
Expand All @@ -42,7 +41,6 @@
import com.swirlds.virtualmap.VirtualMapMigration;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.function.BiConsumer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -91,11 +89,6 @@ public MerkleStateLifecyclesImpl(
this.weightUpdateVisitor = requireNonNull(weightUpdateVisitor);
}

@Override
public List<StateChanges.Builder> initPlatformState(@NonNull final State state) {
return hedera.initPlatformState(state);
}

@Override
public void onPreHandle(@NonNull final Event event, @NonNull final State state) {
hedera.onPreHandle(event, state);
Expand All @@ -119,7 +112,7 @@ public void onStateInitialized(
@NonNull final Platform platform,
@NonNull final InitTrigger trigger,
@Nullable SoftwareVersion previousVersion) {
hedera.onStateInitialized(state, platform, trigger, previousVersion);
hedera.onStateInitialized(state, platform, trigger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ServicesSoftwareVersion(@NonNull final SemanticVersion semVer, final int

@Override
public int compareTo(@Nullable final SoftwareVersion other) {
if (other == null || other instanceof HederaSoftwareVersion) {
if (other == null) {
return 1;
} else if (other instanceof ServicesSoftwareVersion that) {
return SEMANTIC_VERSION_COMPARATOR.compare(this.stateSemVer, that.stateSemVer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void delegatesOnSealConsensusRound() {
void delegatesOnStateInitialized() {
subject.onStateInitialized(merkleStateRoot, platform, InitTrigger.GENESIS, null);

verify(hedera).onStateInitialized(merkleStateRoot, platform, InitTrigger.GENESIS, null);
verify(hedera).onStateInitialized(merkleStateRoot, platform, InitTrigger.GENESIS);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void forceFlush(ReadableKVState<?, ?> state) {
@ValueSource(booleans = {true, false})
void simpleReadAndWrite(boolean forceFlush) throws IOException, ConstructableRegistryException {
final var schemaV1 = createV1Schema();
final var originalTree = (MerkleStateRoot) createMerkleHederaState(schemaV1);
final var originalTree = createMerkleHederaState(schemaV1);

// When we serialize it to bytes and deserialize it back into a tree
MerkleStateRoot copy = originalTree.copy(); // make a copy to make VM flushable
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ void alwaysLaterThanNull() {
assertThat(subject.compareTo(null)).isGreaterThan(0);
}

@Test
void alwaysLaterThanHederaSoftwareVersion() {
final var prevVersion = new HederaSoftwareVersion(LATE, LATE, DEFAULT_CONFIG_VERSION);
final var subject = new ServicesSoftwareVersion(EARLY, DEFAULT_CONFIG_VERSION);
assertThat(subject.compareTo(prevVersion)).isGreaterThan(0);
}

@Test
void majorIsLaterThanMinor() {
final var prevVersion = new ServicesSoftwareVersion(MIDDLE, DEFAULT_CONFIG_VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ protected AbstractEmbeddedHedera(@NonNull final EmbeddedNode node) {

@Override
public void start() {
hedera.initPlatformState(state);
hedera.initializeStatesApi(state, fakePlatform().getContext().getMetrics(), GENESIS, addressBook);

final var writableStates = state.getWritableStates(PlatformStateService.NAME);
final WritableSingletonState<PlatformState> platformState = writableStates.getSingleton(PLATFORM_STATE_KEY);
final var currentState = requireNonNull(platformState.get());
Expand All @@ -144,7 +145,7 @@ public void start() {
((CommittableWritableStates) writableStates).commit();

hedera.setInitialStateHash(FAKE_START_OF_STATE_HASH);
hedera.onStateInitialized(state, fakePlatform(), GENESIS, null);
hedera.onStateInitialized(state, fakePlatform(), GENESIS);
hedera.init(fakePlatform(), defaultNodeId);
fakePlatform().start();
fakePlatform().notifyListeners(ACTIVE_NOTIFICATION);
Expand Down
Loading

0 comments on commit bb42578

Please sign in to comment.