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

chore: replace PlatformStateAccessor.getAddressBook() with calls to R… #17047

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,7 @@
// FUTURE: a lambda that tests if a ReadableTssStore
// constructed from the migration state returns a
// RosterKeys with the ledger id for the given roster
new RosterService(
roster -> true,
() -> new ReadablePlatformStateStore(
requireNonNull(initState).getReadableStates(PlatformStateService.NAME))),
new RosterService(roster -> true, () -> requireNonNull(initState)),

Check warning on line 461 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java#L461

Added line #L461 was not covered by tests
PLATFORM_STATE_SERVICE)
.forEach(servicesRegistry::register);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
import com.hedera.pbj.runtime.io.stream.WritableStreamingData;
import com.swirlds.common.platform.NodeId;
import com.swirlds.config.api.Configuration;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.state.service.ReadableRosterStore;
import com.swirlds.platform.state.service.ReadableRosterStoreImpl;
import com.swirlds.platform.system.address.AddressBook;
Expand Down Expand Up @@ -164,32 +163,31 @@ public Network migrationNetworkOrThrow() {
public static void writeNetworkInfo(@NonNull final State state, @NonNull final Path path) {
requireNonNull(state);
writeNetworkInfo(
state,
new ReadableTssStoreImpl(state.getReadableStates(TssBaseService.NAME)),
new ReadableNodeStoreImpl(state.getReadableStates(AddressBookService.NAME)),
new ReadableRosterStoreImpl(state.getReadableStates(RosterService.NAME)),
new ReadablePlatformStateStore(state.getReadableStates(PlatformStateService.NAME)),
path);
}

/**
* Writes a JSON representation of the {@link Network} information in the given state to a given path.
*
* @param platformStateStore the platform state store to read the network information from
* @param state the state to read the network information from
* @param path the path to write the JSON network information to.
*/
public static void writeNetworkInfo(
@NonNull final State state,
@NonNull final ReadableTssStore tssStore,
@NonNull final ReadableNodeStore nodeStore,
@NonNull final ReadableRosterStore rosterStore,
@NonNull final ReadablePlatformStateStore platformStateStore,
@NonNull final Path path) {
requireNonNull(state);
requireNonNull(tssStore);
requireNonNull(nodeStore);
requireNonNull(rosterStore);
requireNonNull(path);
requireNonNull(platformStateStore);
Optional.ofNullable(rosterStore.getActiveRoster())
.or(() -> Optional.ofNullable(buildRoster(platformStateStore.getAddressBook())))
Optional.ofNullable(RosterRetriever.retrieveActiveOrGenesisRoster(state))
.ifPresent(activeRoster -> {
final var network = Network.newBuilder();
final List<NodeMetadata> nodeMetadata = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.node.app.roster.schemas.V0540RosterSchema;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.platform.state.service.WritableRosterStore;
import com.swirlds.state.State;
import com.swirlds.state.lifecycle.SchemaRegistry;
import com.swirlds.state.lifecycle.Service;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -54,13 +54,11 @@ public class RosterService implements Service {
* we must initialize the active roster from the platform state's legacy address books.
*/
@Deprecated
private final Supplier<ReadablePlatformStateStore> platformStateStoreFactory;
private final Supplier<State> stateSupplier;

public RosterService(
@NonNull final Predicate<Roster> canAdopt,
@NonNull final Supplier<ReadablePlatformStateStore> platformStateStoreFactory) {
public RosterService(@NonNull final Predicate<Roster> canAdopt, @NonNull final Supplier<State> stateSupplier) {
this.canAdopt = requireNonNull(canAdopt);
this.platformStateStoreFactory = requireNonNull(platformStateStoreFactory);
this.stateSupplier = requireNonNull(stateSupplier);
}

@NonNull
Expand All @@ -77,6 +75,6 @@ public int migrationOrder() {
@Override
public void registerSchemas(@NonNull final SchemaRegistry registry) {
requireNonNull(registry);
registry.register(new V0540RosterSchema(canAdopt, WritableRosterStore::new, platformStateStoreFactory));
registry.register(new V0540RosterSchema(canAdopt, WritableRosterStore::new, stateSupplier));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package com.hedera.node.app.roster.schemas;

import static com.swirlds.platform.roster.RosterRetriever.buildRoster;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.SemanticVersion;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.node.state.roster.RosterState;
import com.hedera.node.app.version.ServicesSoftwareVersion;
import com.swirlds.platform.config.AddressBookConfig;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.platform.state.service.WritableRosterStore;
import com.swirlds.platform.state.service.schemas.V0540RosterBaseSchema;
import com.swirlds.state.State;
import com.swirlds.state.lifecycle.MigrationContext;
import com.swirlds.state.lifecycle.Schema;
import com.swirlds.state.lifecycle.StateDefinition;
Expand Down Expand Up @@ -74,16 +74,16 @@ public class V0540RosterSchema extends Schema implements RosterTransplantSchema
* we must initialize the active roster from the platform state's legacy address books.
*/
@Deprecated
private final Supplier<ReadablePlatformStateStore> platformStateStoreFactory;
private final Supplier<State> stateSupplier;

public V0540RosterSchema(
@NonNull final Predicate<Roster> canAdopt,
@NonNull final Function<WritableStates, WritableRosterStore> rosterStoreFactory,
@NonNull final Supplier<ReadablePlatformStateStore> platformStateStoreFactory) {
@NonNull final Supplier<State> stateSupplier) {
super(VERSION);
this.canAdopt = requireNonNull(canAdopt);
this.rosterStoreFactory = requireNonNull(rosterStoreFactory);
this.platformStateStoreFactory = requireNonNull(platformStateStoreFactory);
this.stateSupplier = requireNonNull(stateSupplier);
}

@Override
Expand Down Expand Up @@ -113,8 +113,7 @@ public void restart(@NonNull final MigrationContext ctx) {
} else if (rosterStore.getActiveRoster() == null) {
// (FUTURE) Once the roster lifecycle is active by default, remove this code building an initial
// roster history from the last address book and the first roster at the upgrade boundary
final var addressBook = platformStateStoreFactory.get().getAddressBook();
final var previousRoster = buildRoster(requireNonNull(addressBook));
final var previousRoster = RosterRetriever.retrieveActiveOrGenesisRoster(stateSupplier.get());
rosterStore.putActiveRoster(previousRoster, 0);
final var currentRoster = RosterUtils.rosterFrom(startupNetworks.migrationNetworkOrThrow());
rosterStore.putActiveRoster(currentRoster, activeRoundNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@
import com.hedera.pbj.runtime.io.stream.WritableStreamingData;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.platform.state.service.ReadableRosterStoreImpl;
import com.swirlds.platform.system.address.Address;
import com.swirlds.platform.system.address.AddressBook;
Expand Down Expand Up @@ -343,10 +341,7 @@ private State stateContainingInfoFrom(@NonNull final Network network) {
tssBaseService,
PLATFORM_STATE_SERVICE,
new EntityIdService(),
new RosterService(
roster -> true,
() -> new ReadablePlatformStateStore(
state.getReadableStates(PlatformStateService.NAME))),
new RosterService(roster -> true, () -> state),
new AddressBookServiceImpl())
.forEach(servicesRegistry::register);
final var migrator = new FakeServiceMigrator();
Expand Down Expand Up @@ -378,7 +373,7 @@ private void addRosterInfo(@NonNull final FakeState state, @NonNull final Networ
final var rosters = writableStates.<ProtoBytes, Roster>get(ROSTER_KEY);
rosters.put(new ProtoBytes(currentRosterHash), currentRoster);
final var rosterState = writableStates.<RosterState>getSingleton(ROSTER_STATES_KEY);
rosterState.put(new RosterState(Bytes.EMPTY, List.of(new RoundRosterPair(1L, currentRosterHash))));
rosterState.put(new RosterState(Bytes.EMPTY, List.of(new RoundRosterPair(0L, currentRosterHash))));
((CommittableWritableStates) writableStates).commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.node.app.roster.schemas.RosterTransplantSchema;
import com.hedera.node.app.roster.schemas.V0540RosterSchema;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.state.State;
import com.swirlds.state.lifecycle.Schema;
import com.swirlds.state.lifecycle.SchemaRegistry;
import java.util.function.Predicate;
Expand All @@ -42,13 +42,13 @@ class RosterServiceTest {
private Predicate<Roster> canAdopt;

@Mock
private Supplier<ReadablePlatformStateStore> platformStateStoreFactory;
private Supplier<State> stateSupplier;

private RosterService rosterService;

@BeforeEach
void setUp() {
rosterService = new RosterService(canAdopt, platformStateStoreFactory);
rosterService = new RosterService(canAdopt, stateSupplier);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,37 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.node.state.roster.RosterEntry;
import com.hedera.hapi.node.state.roster.RosterState;
import com.hedera.hapi.platform.state.PlatformState;
import com.hedera.node.internal.network.Network;
import com.hedera.node.internal.network.NodeMetadata;
import com.swirlds.common.RosterStateId;
import com.swirlds.platform.state.service.PbjConverter;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.platform.state.service.WritableRosterStore;
import com.swirlds.platform.state.service.schemas.V0540PlatformStateSchema;
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.state.State;
import com.swirlds.state.lifecycle.MigrationContext;
import com.swirlds.state.lifecycle.StartupNetworks;
import com.swirlds.state.lifecycle.StateDefinition;
import com.swirlds.state.spi.ReadableSingletonState;
import com.swirlds.state.spi.ReadableStates;
import com.swirlds.state.spi.WritableSingletonState;
import com.swirlds.state.spi.WritableStates;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -72,6 +81,9 @@ class V0540RosterSchemaTest {
@Mock
private MigrationContext ctx;

@Mock
private ReadableStates readableStates;

@Mock
private WritableStates writableStates;

Expand All @@ -88,19 +100,29 @@ class V0540RosterSchemaTest {
private Predicate<Roster> canAdopt;

@Mock
private Supplier<ReadablePlatformStateStore> platformStateStoreFactory;
private State state;

private State getState() {
return state;
}

@Mock
private ReadablePlatformStateStore platformStateStore;

@Mock
private ReadableSingletonState<PlatformState> platformStateSingleton;

@Mock
private PlatformState platformState;

@Mock
private WritableSingletonState<RosterState> rosterState;

private V0540RosterSchema subject;

@BeforeEach
void setUp() {
subject = new V0540RosterSchema(canAdopt, rosterStoreFactory, platformStateStoreFactory);
subject = new V0540RosterSchema(canAdopt, rosterStoreFactory, this::getState);
}

@Test
Expand Down Expand Up @@ -145,8 +167,24 @@ void usesAdaptedAddressBookAndMigrationRosterIfLifecycleEnabledIfApropos() {
given(ctx.startupNetworks()).willReturn(startupNetworks);
given(ctx.roundNumber()).willReturn(ROUND_NO);
given(startupNetworks.migrationNetworkOrThrow()).willReturn(NETWORK);
given(platformStateStoreFactory.get()).willReturn(platformStateStore);
given(platformStateStore.getAddressBook()).willReturn(ADDRESS_BOOK);

// Setup PlatformService states to return a given ADDRESS_BOOK,
// and the readable RosterService states to be empty:
doReturn(readableStates).when(state).getReadableStates(PlatformStateService.NAME);
doReturn(platformStateSingleton).when(readableStates).getSingleton(V0540PlatformStateSchema.PLATFORM_STATE_KEY);
doReturn(platformState).when(platformStateSingleton).get();
doReturn(PbjConverter.toPbjAddressBook(ADDRESS_BOOK))
.when(platformState)
.addressBook();
final ReadableStates rosterReadableStates = mock(ReadableStates.class);
doReturn(rosterReadableStates).when(state).getReadableStates(RosterStateId.NAME);
final ReadableSingletonState<RosterState> rosterStateSingleton = mock(ReadableSingletonState.class);
doReturn(rosterStateSingleton).when(rosterReadableStates).getSingleton(RosterStateId.ROSTER_STATES_KEY);
final RosterState rosterState = mock(RosterState.class);
doReturn(rosterState).when(rosterStateSingleton).get();
doReturn(List.of()).when(rosterState).roundRosterPairs();

// This is the rosterStore for when the code updates it and writes to it upon the migration:
given(rosterStoreFactory.apply(writableStates)).willReturn(rosterStore);

subject.restart(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import com.swirlds.platform.gossip.SyncGossip;
import com.swirlds.platform.pool.DefaultTransactionPool;
import com.swirlds.platform.pool.TransactionPool;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.hasher.DefaultStateHasher;
import com.swirlds.platform.state.hasher.StateHasher;
import com.swirlds.platform.state.hashlogger.DefaultHashLogger;
Expand Down Expand Up @@ -890,11 +891,7 @@

issDetector = new DefaultIssDetector(
blocks.platformContext(),
blocks.initialState()
.get()
.getState()
.getReadablePlatformState()
.getAddressBook(),
RosterUtils.buildAddressBook(blocks.rosterHistory().getCurrentRoster()),

Check warning on line 894 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java#L894

Added line #L894 was not covered by tests
blocks.appVersion().getPbjSemanticVersion(),
ignorePreconsensusSignatures,
roundToIgnore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.platform.config.DefaultConfiguration;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.snapshot.DeserializedSignedState;
import com.swirlds.platform.state.snapshot.SignedStateFileReader;
import com.swirlds.platform.system.address.AddressBook;
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 @@ -77,10 +79,9 @@

final AddressBook stateAddressBook;
try (final ReservedSignedState reservedSignedState = deserializedSignedState.reservedSignedState()) {
final PlatformStateAccessor platformState =
reservedSignedState.get().getState().getReadablePlatformState();
System.out.printf("Extracting the state address book for comparison %n");
stateAddressBook = platformState.getAddressBook();
stateAddressBook = RosterUtils.buildAddressBook(RosterRetriever.retrieveActiveOrGenesisRoster(
(State) reservedSignedState.get().getState().getSwirldState()));

Check warning on line 84 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/cli/ValidateAddressBookStateCommand.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/cli/ValidateAddressBookStateCommand.java#L83-L84

Added lines #L83 - L84 were not covered by tests
}

System.out.printf("Validating address book %n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ private RosterRetriever() {}
* If the active roster is missing from RosterState,
* then fall back to reading an AddressBook from the PlatformState
* and converting it to a Roster.
* <p>
* This method may return null in case the RosterService states are not populated,
* and the PlatformService state doesn't have an AddressBook,
* which generally represents a new network genesis case.
*
* @return an active Roster for the round of the state, or a Roster that represents the current AddressBook in PlatformState
*/
@NonNull
@Nullable
public static Roster retrieveActiveOrGenesisRoster(@NonNull final State state) {
final var roster = retrieveActive(state, getRound(state));
if (roster != null) {
Expand All @@ -74,7 +78,7 @@ public static Roster retrieveActiveOrGenesisRoster(@NonNull final State state) {
// We are currently in bootstrap for the genesis roster, which is set in the address book
final var readablePlatformStateStore =
new ReadablePlatformStateStore(state.getReadableStates(PlatformStateService.NAME));
return buildRoster(requireNonNull(readablePlatformStateStore.getAddressBook()));
return buildRoster(readablePlatformStateStore.getAddressBook());
}

/**
Expand Down
Loading