Skip to content

Commit

Permalink
refactor: 16436 Removed MerkleRoot interface. (#17130)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Malygin <[email protected]>
  • Loading branch information
imalygin authored Dec 20, 2024
1 parent aee0358 commit 0679def
Show file tree
Hide file tree
Showing 52 changed files with 250 additions and 308 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -133,7 +133,6 @@
import com.swirlds.platform.listeners.ReconnectCompleteListener;
import com.swirlds.platform.listeners.ReconnectCompleteNotification;
import com.swirlds.platform.listeners.StateWriteToDiskCompleteListener;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
Expand Down Expand Up @@ -505,7 +504,7 @@ public SoftwareVersion getSoftwareVersion() {
*/
@Override
@NonNull
public MerkleRoot newMerkleStateRoot() {
public PlatformMerkleStateRoot newMerkleStateRoot() {
return stateRootSupplier.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.node.app;

import static com.swirlds.common.io.utility.FileUtils.getAbsolutePath;
Expand Down Expand Up @@ -60,7 +75,6 @@
import com.swirlds.platform.crypto.CryptoStatic;
import com.swirlds.platform.roster.RosterHistory;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.address.AddressBookInitializer;
import com.swirlds.platform.state.service.ReadableRosterStore;
Expand Down Expand Up @@ -130,7 +144,7 @@ public void init(@NonNull final Platform platform, @NonNull final NodeId nodeId)
* {@inheritDoc}
*/
@Override
public @NonNull MerkleRoot newMerkleStateRoot() {
public @NonNull PlatformMerkleStateRoot newMerkleStateRoot() {
return hederaOrThrow().newMerkleStateRoot();
}

Expand Down Expand Up @@ -254,7 +268,7 @@ public static void main(final String... args) throws Exception {
version,
() -> {
isGenesis.set(true);
final var genesisState = (PlatformMerkleStateRoot) hedera.newMerkleStateRoot();
final var genesisState = hedera.newMerkleStateRoot();
final var genesisNetwork = DiskStartupNetworks.fromLegacyAddressBook(diskAddressBook);
hedera.initializeStatesApi(
genesisState,
Expand All @@ -271,17 +285,12 @@ public static void main(final String... args) throws Exception {
final var initialState = reservedState.state();
if (!isGenesis.get()) {
hedera.initializeStatesApi(
(PlatformMerkleStateRoot) initialState.get().getState().getSwirldState(),
metrics,
InitTrigger.RESTART,
null,
platformConfig,
diskAddressBook);
initialState.get().getState(), metrics, InitTrigger.RESTART, null, platformConfig, diskAddressBook);
}
hedera.setInitialStateHash(reservedState.hash());

// --- Now build the platform and start it ---
final var stateRoot = (PlatformMerkleStateRoot) initialState.get().getState();
final var stateRoot = initialState.get().getState();
final RosterHistory rosterHistory;
if (hedera.isRosterLifecycleEnabled()) {
final var rosterStore = new ReadableStoreFactory(stateRoot).getStore(ReadableRosterStore.class);
Expand Down Expand Up @@ -461,7 +470,7 @@ private static HashedReservedSignedState loadInitialState(
@NonNull final Configuration configuration,
@NonNull final RecycleBin recycleBin,
@NonNull final SoftwareVersion softwareVersion,
@NonNull final Supplier<MerkleRoot> stateRootSupplier,
@NonNull final Supplier<PlatformMerkleStateRoot> stateRootSupplier,
@NonNull final String mainClassName,
@NonNull final String swirldName,
@NonNull final NodeId selfId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -326,7 +326,7 @@ private PlatformMerkleStateRoot createMerkleHederaState(Schema schemaV1) {
final SignedState randomState =
new RandomSignedStateGenerator().setRound(1).build();

final var originalTree = (PlatformMerkleStateRoot) randomState.getState();
final var originalTree = randomState.getState();
final var originalRegistry =
new MerkleSchemaRegistry(registry, FIRST_SERVICE, DEFAULT_CONFIG, new SchemaApplications());
originalRegistry.register(schemaV1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright (C) 2024 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.services.bdd.junit.support.validators.block;

import static com.hedera.node.app.blocks.impl.BlockImplUtils.combine;
Expand Down Expand Up @@ -191,7 +206,7 @@ public StateChangesValidator(
final var addressBook = loadLegacyBookWithGeneratedCerts(pathToAddressBook);
final var metrics = new NoOpMetrics();
final var hedera = ServicesMain.newHedera(NodeId.of(0L), metrics);
this.state = (PlatformMerkleStateRoot) hedera.newMerkleStateRoot();
this.state = hedera.newMerkleStateRoot();
final var platformConfig = ServicesMain.buildPlatformConfig();
hedera.initializeStatesApi(
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.swirlds.platform.system.status.actions.ReconnectCompleteAction;
import com.swirlds.platform.wiring.PlatformWiring;
import com.swirlds.state.State;
import com.swirlds.state.merkle.MerkleStateRoot;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -118,7 +117,7 @@ public void loadReconnectState(@NonNull final SignedState signedState) {
}

// Before attempting to load the state, verify that the platform roster matches the state roster.
final State state = (MerkleStateRoot<?>) signedState.getState().getSwirldState();
final State state = signedState.getState();
final Roster stateRoster = RosterRetriever.retrieveActiveOrGenesisRoster(state);
if (!roster.equals(stateRoster)) {
throw new IllegalStateException("Current roster and state-based roster do not contain the same nodes "
Expand Down Expand Up @@ -170,9 +169,7 @@ public void loadReconnectState(@NonNull final SignedState signedState) {
.getNotifierWiring()
.getInputWire(AppNotifier::sendReconnectCompleteNotification)
.put(new ReconnectCompleteNotification(
signedState.getRound(),
signedState.getConsensusTimestamp(),
signedState.getState().getSwirldState()));
signedState.getRound(), signedState.getConsensusTimestamp(), signedState.getState()));

} catch (final RuntimeException e) {
logger.debug(RECONNECT.getMarker(), "`loadReconnectState` : FAILED, reason: {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.swirlds.common.context.PlatformContext;
import com.swirlds.common.merkle.crypto.MerkleCryptoFactory;
import com.swirlds.platform.config.StateConfig;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
Expand Down Expand Up @@ -70,18 +70,18 @@ public static void initializeState(
trigger = RESTART;
}

final MerkleRoot initialState = signedState.getState();
final PlatformMerkleStateRoot initialState = signedState.getState();

// Although the state from disk / genesis state is initially hashed, we are actually dealing with a copy
// of that state here. That copy should have caused the hash to be cleared.
if (initialState.getHash() != null) {
throw new IllegalStateException("Expected initial state to be unhashed");
}
if (initialState.getSwirldState().getHash() != null) {
if (initialState.getHash() != null) {
throw new IllegalStateException("Expected initial swirld state to be unhashed");
}

initialState.getSwirldState().init(platform, trigger, previousSoftwareVersion);
initialState.init(platform, trigger, previousSoftwareVersion);

abortAndThrowIfInterrupted(
() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -58,7 +58,7 @@
import com.swirlds.platform.pool.TransactionPoolNexus;
import com.swirlds.platform.publisher.DefaultPlatformPublisher;
import com.swirlds.platform.publisher.PlatformPublisher;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.SwirldStateManager;
import com.swirlds.platform.state.nexus.DefaultLatestCompleteStateNexus;
Expand Down Expand Up @@ -365,7 +365,7 @@ private BirthRoundMigrationShim buildBirthRoundMigrationShim(
return null;
}

final MerkleRoot state = initialState.getState();
final PlatformMerkleStateRoot state = initialState.getState();
final PlatformStateAccessor platformState = state.getReadablePlatformState();

return new DefaultBirthRoundMigrationShim(
Expand Down Expand Up @@ -515,6 +515,6 @@ public <T extends SwirldState> AutoCloseableWrapper<T> getLatestImmutableState(@
final ReservedSignedState wrapper = latestImmutableStateNexus.getState(reason);
return wrapper == null
? AutoCloseableWrapper.empty()
: new AutoCloseableWrapper<>((T) wrapper.get().getState().getSwirldState(), wrapper::close);
: new AutoCloseableWrapper<>((T) wrapper.get().getState(), wrapper::close);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Integer call() throws IOException, ExecutionException, InterruptedExcepti
});
{
System.out.printf("Resetting the RosterService state %n");
final State state = (State) reservedSignedState.get().getState().getSwirldState();
final State state = reservedSignedState.get().getState();
final WritableStates writableStates = state.getWritableStates(RosterStateId.NAME);
final WritableRosterStore writableRosterStore = new WritableRosterStore(writableStates);
writableRosterStore.resetRosters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.swirlds.platform.system.address.AddressBookUtils;
import com.swirlds.platform.system.address.AddressBookValidator;
import com.swirlds.platform.util.BootstrapUtils;
import com.swirlds.state.State;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -81,7 +80,7 @@ public Integer call() throws IOException, ExecutionException, InterruptedExcepti
try (final ReservedSignedState reservedSignedState = deserializedSignedState.reservedSignedState()) {
System.out.printf("Extracting the state address book for comparison %n");
stateAddressBook = RosterUtils.buildAddressBook(RosterRetriever.retrieveActiveOrGenesisRoster(
(State) reservedSignedState.get().getState().getSwirldState()));
reservedSignedState.get().getState()));
}

System.out.printf("Validating address book %n");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -38,7 +38,7 @@
import com.swirlds.platform.event.PlatformEvent;
import com.swirlds.platform.internal.ConsensusRound;
import com.swirlds.platform.metrics.RoundHandlingMetrics;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.SwirldStateManager;
import com.swirlds.platform.state.signed.ReservedSignedState;
Expand Down Expand Up @@ -282,7 +282,7 @@ private StateAndRound createSignedState(
swirldStateManager.sealConsensusRound(consensusRound);

handlerMetrics.setPhase(GETTING_STATE_TO_SIGN);
final MerkleRoot immutableStateCons = swirldStateManager.getStateForSigning();
final PlatformMerkleStateRoot immutableStateCons = swirldStateManager.getStateForSigning();

handlerMetrics.setPhase(CREATING_SIGNED_STATE);
final SignedState signedState = new SignedState(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -25,7 +25,7 @@
import com.swirlds.logging.legacy.payload.ReconnectStartPayload;
import com.swirlds.platform.config.StateConfig;
import com.swirlds.platform.network.Connection;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.state.signed.SignedStateValidator;
Expand All @@ -51,7 +51,7 @@ public class ReconnectHelper {
/** clears all data that is no longer needed since we fell behind */
private final Clearable clearAll;
/** supplier of the initial signed state against which to perform a delta based reconnect */
private final Supplier<MerkleRoot> workingStateSupplier;
private final Supplier<PlatformMerkleStateRoot> workingStateSupplier;
/** provides the latest signed state round for which we have a supermajority of signatures */
private final LongSupplier lastCompleteRoundSupplier;
/** throttles reconnect learner attempts */
Expand All @@ -66,7 +66,7 @@ public class ReconnectHelper {
public ReconnectHelper(
final Runnable pauseGossip,
final Clearable clearAll,
final Supplier<MerkleRoot> workingStateSupplier,
final Supplier<PlatformMerkleStateRoot> workingStateSupplier,
final LongSupplier lastCompleteRoundSupplier,
final ReconnectLearnerThrottle reconnectLearnerThrottle,
final Consumer<SignedState> loadSignedState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@
import com.swirlds.platform.crypto.CryptoStatic;
import com.swirlds.platform.metrics.ReconnectMetrics;
import com.swirlds.platform.network.Connection;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.signed.SigSet;
import com.swirlds.platform.state.signed.SignedState;
Expand All @@ -57,7 +57,7 @@ public class ReconnectLearner {

private final Connection connection;
private final Roster roster;
private final MerkleRoot currentState;
private final PlatformMerkleStateRoot currentState;
private final Duration reconnectSocketTimeout;
private final ReconnectMetrics statistics;
private final SignedStateValidationData stateValidationData;
Expand Down Expand Up @@ -89,7 +89,7 @@ public ReconnectLearner(
@NonNull final ThreadManager threadManager,
@NonNull final Connection connection,
@NonNull final Roster roster,
@NonNull final MerkleRoot currentState,
@NonNull final PlatformMerkleStateRoot currentState,
@NonNull final Duration reconnectSocketTimeout,
@NonNull final ReconnectMetrics statistics) {

Expand Down Expand Up @@ -204,7 +204,7 @@ private ReservedSignedState reconnect() throws InterruptedException {
platformContext.getMetrics());
synchronizer.synchronize();

final MerkleRoot state = (MerkleRoot) synchronizer.getRoot();
final PlatformMerkleStateRoot state = (PlatformMerkleStateRoot) synchronizer.getRoot();
final SignedState newSignedState = new SignedState(
platformContext.getConfiguration(),
CryptoStatic::verifySignature,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
* Copyright (C) 2024 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 @@ -21,7 +21,7 @@
import com.swirlds.common.threading.manager.ThreadManager;
import com.swirlds.platform.metrics.ReconnectMetrics;
import com.swirlds.platform.network.Connection;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformMerkleStateRoot;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Duration;
import java.util.Objects;
Expand Down Expand Up @@ -63,7 +63,7 @@ public ReconnectLearnerFactory(
* @param workingState the state to use to perform a delta based reconnect
* @return a new instance
*/
public ReconnectLearner create(final Connection conn, final MerkleRoot workingState) {
public ReconnectLearner create(final Connection conn, final PlatformMerkleStateRoot workingState) {
return new ReconnectLearner(
platformContext, threadManager, conn, roster, workingState, reconnectSocketTimeout, statistics);
}
Expand Down
Loading

0 comments on commit 0679def

Please sign in to comment.