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 @@ -18,7 +18,6 @@

import static com.hedera.node.app.info.NodeInfoImpl.fromRosterEntry;
import static com.hedera.node.app.service.addressbook.impl.schemas.V053AddressBookSchema.NODES_KEY;
import static com.swirlds.platform.roster.RosterRetriever.buildRoster;
import static com.swirlds.platform.roster.RosterRetriever.retrieveActiveOrGenesisRoster;
import static java.util.Objects.requireNonNull;

Expand All @@ -29,10 +28,7 @@
import com.hedera.node.app.service.addressbook.AddressBookService;
import com.hedera.node.config.ConfigProvider;
import com.hedera.node.config.data.LedgerConfig;
import com.hedera.node.config.data.TssConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.platform.state.service.PlatformStateService;
import com.swirlds.platform.state.service.ReadablePlatformStateStore;
import com.swirlds.state.State;
import com.swirlds.state.lifecycle.info.NetworkInfo;
import com.swirlds.state.lifecycle.info.NodeInfo;
Expand Down Expand Up @@ -116,16 +112,11 @@ public boolean containsNode(final long nodeId) {
@Override
public void updateFrom(@NonNull final State state) {
final var config = configProvider.getConfiguration();
if (config.getConfigData(TssConfig.class).keyCandidateRoster()) {
activeRoster = retrieveActiveOrGenesisRoster(state);
} else {
// When the feature flag is disabled, the rosters in RosterService state are not up-to-date
// FUTURE: Once TSS Roster is implemented in the future, this will be removed and use roster state
// instead of the address book
final var readablePlatformStateStore =
new ReadablePlatformStateStore(state.getReadableStates(PlatformStateService.NAME));
activeRoster = buildRoster(requireNonNull(readablePlatformStateStore.getAddressBook()));
}

// RosterRetriever will fetch the roster from the RosterService state if it's populated.
// Otherwise, it falls back to reading the AddressBook from the PlatformStateService.
activeRoster = retrieveActiveOrGenesisRoster(state);
anthony-swirldslabs marked this conversation as resolved.
Show resolved Hide resolved

nodeInfos.clear();
nodeInfos.putAll(buildNodeInfoMap(state));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.hedera.hapi.node.base.ServiceEndpoint;
import com.hedera.hapi.node.state.addressbook.Node;
import com.hedera.hapi.node.state.common.EntityNumber;
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.node.state.roster.RosterEntry;
import com.hedera.hapi.platform.state.Address;
import com.hedera.hapi.platform.state.AddressBook;
import com.hedera.hapi.platform.state.NodeId;
import com.hedera.hapi.platform.state.PlatformState;
import com.hedera.node.app.service.addressbook.AddressBookService;
import com.hedera.node.config.ConfigProvider;
import com.hedera.node.config.VersionedConfigImpl;
import com.hedera.node.config.testfixtures.HederaTestConfigBuilder;
import com.swirlds.platform.state.service.PlatformStateService;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.platform.test.fixtures.roster.RosterServiceStateMock;
import com.swirlds.state.State;
import com.swirlds.state.spi.ReadableKVState;
import com.swirlds.state.spi.ReadableSingletonState;
import com.swirlds.state.spi.ReadableStates;
import java.security.cert.X509Certificate;
import java.util.List;
Expand All @@ -67,12 +64,6 @@ public class StateNetworkInfoTest {
@Mock
private ReadableStates readableStates;

@Mock
private ReadableSingletonState<PlatformState> platformReadableState;

@Mock
private PlatformState platformState;

private static final long SELF_ID = 1L;
private final Roster activeRoster = new Roster(List.of(
RosterEntry.newBuilder().nodeId(SELF_ID).weight(10).build(),
Expand All @@ -89,7 +80,6 @@ public void setUp() {
.thenReturn(new VersionedConfigImpl(HederaTestConfigBuilder.createConfig(), 1));
when(state.getReadableStates(AddressBookService.NAME)).thenReturn(readableStates);
when(readableStates.<EntityNumber, Node>get("NODES")).thenReturn(nodeState);
when(state.getReadableStates(PlatformStateService.NAME)).thenReturn(readableStates);
networkInfo = new StateNetworkInfo(state, activeRoster, SELF_ID, configProvider);
}

Expand Down Expand Up @@ -129,34 +119,27 @@ public void testContainsNode() {
@Test
public void testUpdateFrom() {
when(nodeState.get(any(EntityNumber.class))).thenReturn(mock(Node.class));
when(readableStates.<PlatformState>getSingleton("PLATFORM_STATE")).thenReturn(platformReadableState);
when(platformReadableState.get()).thenReturn(platformState);
when(platformState.addressBook())
.thenReturn(AddressBook.newBuilder()
.addresses(
Address.newBuilder()
.id(new NodeId(2L))
.weight(111L)
.signingCertificate(getCertBytes(CERTIFICATE_2))
// The agreementCertificate is unused, but required to prevent deserialization
// failure in
// States API.
.agreementCertificate(getCertBytes(CERTIFICATE_2))
.hostnameInternal("10.0.55.66")
.portInternal(222)
.build(),
Address.newBuilder()
.id(new NodeId(3L))
.weight(3L)
.signingCertificate(getCertBytes(CERTIFICATE_3))
// The agreementCertificate is unused, but required to prevent deserialization
// failure in
// States API.
.agreementCertificate(getCertBytes(CERTIFICATE_3))
.hostnameExternal("external3.com")
.portExternal(111)
.build())
.build());

final Roster roster = new Roster(List.of(
RosterEntry.newBuilder()
.nodeId(2L)
.weight(111L)
.gossipCaCertificate(getCertBytes(CERTIFICATE_2))
.gossipEndpoint(ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap(new byte[] {10, 0, 55, 66}))
.port(222)
.build())
.build(),
RosterEntry.newBuilder()
.nodeId(3L)
.weight(3L)
.gossipCaCertificate(getCertBytes(CERTIFICATE_3))
.gossipEndpoint(ServiceEndpoint.newBuilder()
.domainName("external3.com")
.port(111)
.build())
.build()));
RosterServiceStateMock.setup(state, roster);

networkInfo.updateFrom(state);
assertEquals(2, networkInfo.addressBook().size());
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import com.hedera.hapi.node.state.roster.RosterEntry;
import com.hedera.hapi.node.state.roster.RoundRosterPair;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.common.RosterStateId;
import com.swirlds.common.crypto.CryptographyException;
import com.swirlds.common.crypto.Hash;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.crypto.CryptoStatic;
import com.swirlds.platform.state.service.ReadableRosterStore;
import com.swirlds.platform.state.service.WritableRosterStore;
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 com.swirlds.state.spi.CommittableWritableStates;
import com.swirlds.state.spi.WritableStates;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -271,6 +275,20 @@ public static RosterHistory createRosterHistory(@NonNull final ReadableRosterSto
}
}

/**
* Sets the active Roster in a given State.
*
* @param state a state to set a Roster in
* @param roster a Roster to set as active
* @param round a round number since which the roster is considered active
*/
public static void setActiveRoster(@NonNull final State state, @NonNull final Roster roster, final long round) {
final WritableStates writableStates = state.getWritableStates(RosterStateId.NAME);
final WritableRosterStore writableRosterStore = new WritableRosterStore(writableStates);
writableRosterStore.putActiveRoster(roster, round);
((CommittableWritableStates) writableStates).commit();
}

/**
* Build an Address object out of a given RosterEntry object.
* @deprecated To be removed once AddressBook to Roster refactoring is complete.
Expand Down Expand Up @@ -323,13 +341,18 @@ public static Address buildAddress(@NonNull final RosterEntry entry) {

/**
* Build an AddressBook object out of a given Roster object.
* Returns null if the input roster is null.
* @deprecated To be removed once AddressBook to Roster refactoring is complete.
* @param roster a Roster
* @return an AddressBook
*/
@Deprecated(forRemoval = true)
@NonNull
@Nullable
public static AddressBook buildAddressBook(@NonNull final Roster roster) {
if (roster == null) {
edward-swirldslabs marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

AddressBook addressBook = new AddressBook();

for (final RosterEntry entry : roster.rosterEntries()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import com.swirlds.common.context.PlatformContext;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.config.AddressBookConfig;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.address.Address;
import com.swirlds.platform.system.address.AddressBook;
import com.swirlds.platform.system.address.AddressBookValidator;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.File;
Expand Down Expand Up @@ -130,8 +133,8 @@ public AddressBookInitializer(
platformContext.getConfiguration().getConfigData(AddressBookConfig.class);
this.initialState = Objects.requireNonNull(initialState, "The initialState must not be null.");

this.stateAddressBook =
initialState.getState().getReadablePlatformState().getAddressBook();
this.stateAddressBook = RosterUtils.buildAddressBook(RosterRetriever.retrieveActiveOrGenesisRoster(
(State) initialState.getState().getSwirldState()));
if (stateAddressBook == null && !initialState.isGenesisState()) {
throw new IllegalStateException("Only genesis states can have null address books.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ public void putActiveRoster(@NonNull final Roster roster, final long round) {
if (!roundRosterPairs.isEmpty()) {
final RoundRosterPair activeRosterPair = roundRosterPairs.getFirst();
if (round < 0 || round <= activeRosterPair.roundNumber()) {
throw new IllegalArgumentException(
"incoming round number must be greater than the round number of the current active roster.");
throw new IllegalArgumentException("incoming round number = " + round
+ " must be greater than the round number of the current active roster = "
+ activeRosterPair.roundNumber() + ".");
}
}
final Bytes activeRosterHash = RosterUtils.hash(roster).getBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
import static com.swirlds.platform.state.snapshot.SavedStateMetadataField.TOTAL_WEIGHT;
import static com.swirlds.platform.state.snapshot.SavedStateMetadataField.WALL_CLOCK_TIME;

import com.hedera.hapi.node.state.roster.Roster;
import com.swirlds.common.crypto.Hash;
import com.swirlds.common.formatting.TextTable;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.BufferedReader;
Expand Down Expand Up @@ -168,6 +172,8 @@ public static SavedStateMetadata create(
Objects.requireNonNull(now, "now must not be null");

final PlatformStateAccessor platformState = signedState.getState().getReadablePlatformState();
final Roster roster = RosterRetriever.retrieveActiveOrGenesisRoster(
(State) signedState.getState().getSwirldState());

final List<NodeId> signingNodes = signedState.getSigSet().getSigningNodes();
Collections.sort(signingNodes);
Expand All @@ -186,9 +192,7 @@ public static SavedStateMetadata create(
selfId,
signingNodes,
signedState.getSigningWeight(),
platformState.getAddressBook() == null
? 0
: platformState.getAddressBook().getTotalWeight());
roster == null ? 0 : RosterUtils.computeTotalWeight(roster));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import com.swirlds.common.formatting.TextTable;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.config.AddressBookConfig;
import com.swirlds.platform.roster.RosterRetriever;
import com.swirlds.platform.roster.RosterUtils;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.address.AddressBookInitializer;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.state.State;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.text.ParseException;
Expand Down Expand Up @@ -273,8 +276,8 @@
}

// At this point the initial state must have the current address book set. If not, something is wrong.
final AddressBook addressBook =
initialState.get().getState().getReadablePlatformState().getAddressBook();
final AddressBook addressBook = RosterUtils.buildAddressBook(RosterRetriever.retrieveActiveOrGenesisRoster(
(State) initialState.get().getState().getSwirldState()));

Check warning on line 280 in platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/address/AddressBookUtils.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/system/address/AddressBookUtils.java#L279-L280

Added lines #L279 - L280 were not covered by tests
if (addressBook == null) {
throw new IllegalStateException("The current address book of the initial state is null.");
}
Expand Down
Loading