Skip to content

Commit

Permalink
chore: use RosterRetriever in RosterUtils.buildRosterHistory
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
  • Loading branch information
anthony-swirldslabs committed Dec 5, 2024
1 parent 2914165 commit 267338c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static com.swirlds.platform.builder.internal.StaticPlatformBuilder.setupGlobalMetrics;
import static com.swirlds.platform.config.internal.PlatformConfigUtils.checkConfiguration;
import static com.swirlds.platform.crypto.CryptoStatic.initNodeSecurity;
import static com.swirlds.platform.roster.RosterRetriever.buildRoster;
import static com.swirlds.platform.state.signed.StartupStateUtils.getInitialState;
import static com.swirlds.platform.system.SystemExitCode.CONFIGURATION_ERROR;
import static com.swirlds.platform.system.SystemExitCode.NODE_ADDRESS_MISMATCH;
Expand Down Expand Up @@ -82,6 +81,7 @@
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.system.address.AddressBookUtils;
import com.swirlds.platform.util.BootstrapUtils;
import com.swirlds.state.State;
import com.swirlds.state.merkle.MerkleStateRoot;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.InstantSource;
Expand Down Expand Up @@ -294,11 +294,10 @@ public static void main(final String... args) throws Exception {
rosterHistory = RosterUtils.createRosterHistory(rosterStore);
} else {
rosterHistory =
RosterUtils.buildRosterHistory(initialState.get().getState().getReadablePlatformState());
RosterUtils.buildRosterHistory((State) initialState.get().getState());

Check warning on line 297 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/ServicesMain.java#L297

Added line #L297 was not covered by tests
}

// Follow the Inversion of Control pattern by injecting all needed dependencies into the PlatformBuilder.
final var roster = buildRoster(addressBook);
final var platformBuilder = PlatformBuilder.create(
Hedera.APP_NAME,
Hedera.SWIRLD_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.system.address.AddressBookUtils;
import com.swirlds.platform.util.BootstrapUtils;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.awt.GraphicsEnvironment;
import java.nio.file.Path;
Expand Down Expand Up @@ -302,7 +303,7 @@ private static void launchUnhandled(@NonNull final CommandLineArgs commandLineAr
initialState,
nodeId,
AddressBookUtils.formatConsensusEventStreamName(addressBook, nodeId),
RosterUtils.buildRosterHistory(initialState.get().getState().getReadablePlatformState()));
RosterUtils.buildRosterHistory((State) initialState.get().getState()));

Check warning on line 306 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Browser.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Browser.java#L306

Added line #L306 was not covered by tests
if (showUi && index == 0) {
builder.withPreconsensusEventCallback(guiEventStorage::handlePreconsensusEvent);
builder.withConsensusSnapshotOverrideCallback(guiEventStorage::handleSnapshotOverride);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import com.swirlds.common.crypto.Hash;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.crypto.CryptoStatic;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.service.ReadableRosterStore;
import com.swirlds.platform.system.address.Address;
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.util.PbjRecordHasher;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -218,28 +218,28 @@ public static int getNumberWithWeight(@NonNull final Roster roster) {
}

/**
* Build an instance of RosterHistory from the current/previous AddressBook found in the PlatformState.
* Build an instance of RosterHistory from the current/previous rosters as reported by the RosterRetriever.
*
* The RosterRetriever implementation fetches the rosters from the RosterState/RosterMap,
* and automatically falls back to fetching them from the PlatformState if the RosterState is empty.
*
* @deprecated To be removed once AddressBook to Roster refactoring is complete.
* @param readablePlatformState
* @param state a State object to fetch data from
* @return a RosterHistory
*/
@Deprecated(forRemoval = true)
@NonNull
public static RosterHistory buildRosterHistory(final PlatformStateAccessor readablePlatformState) {
if (readablePlatformState.getAddressBook() == null) {
throw new IllegalStateException("Address book is null");
}

public static RosterHistory buildRosterHistory(final State state) {
final List<RoundRosterPair> roundRosterPairList = new ArrayList<>();
final Map<Bytes, Roster> rosterMap = new HashMap<>();

final Roster currentRoster = RosterRetriever.buildRoster(readablePlatformState.getAddressBook());
final Roster currentRoster = RosterRetriever.retrieveActiveOrGenesisRoster(state);

Check warning on line 236 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java#L236

Added line #L236 was not covered by tests
final Bytes currentHash = RosterUtils.hash(currentRoster).getBytes();
roundRosterPairList.add(new RoundRosterPair(readablePlatformState.getRound(), currentHash));
roundRosterPairList.add(new RoundRosterPair(RosterRetriever.getRound(state), currentHash));

Check warning on line 238 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java#L238

Added line #L238 was not covered by tests
rosterMap.put(currentHash, currentRoster);

if (readablePlatformState.getPreviousAddressBook() != null) {
final Roster previousRoster = RosterRetriever.buildRoster(readablePlatformState.getPreviousAddressBook());
final Roster previousRoster = RosterRetriever.retrievePreviousRoster(state);

Check warning on line 241 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java#L241

Added line #L241 was not covered by tests
if (previousRoster != null) {
final Bytes previousHash = RosterUtils.hash(previousRoster).getBytes();
roundRosterPairList.add(new RoundRosterPair(0, previousHash));
rosterMap.put(previousHash, previousRoster);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.swirlds.platform.test.fixtures.turtle.gossip.SimulatedNetwork;
import com.swirlds.platform.util.RandomBuilder;
import com.swirlds.platform.wiring.PlatformSchedulersConfig_;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.function.Supplier;

Expand Down Expand Up @@ -121,7 +122,7 @@ public class TurtleNode {
nodeId,
AddressBookUtils.formatConsensusEventStreamName(addressBook, nodeId),
RosterUtils.buildRosterHistory(
initialState.get().getState().getReadablePlatformState()))
(State) initialState.get().getState()))
.withModel(model)
.withRandomBuilder(new RandomBuilder(randotron.nextLong()))
.withKeysAndCerts(privateKeys)
Expand Down

0 comments on commit 267338c

Please sign in to comment.