diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Utilities.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Utilities.java index f3e7529722d2..3926f4e6ddc3 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Utilities.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Utilities.java @@ -379,7 +379,6 @@ public static boolean hasAnyCauseSuppliedType( .filter(address -> !address.getNodeId().equals(selfId)) .map(address -> new PeerInfo( address.getNodeId(), - address.getSelfName(), Objects.requireNonNull(address.getHostnameExternal()), Objects.requireNonNull(address.getSigCert()))) .toList(); diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/PeerInfo.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/PeerInfo.java index 39873c2f5052..c3d69d66737f 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/PeerInfo.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/network/PeerInfo.java @@ -17,6 +17,7 @@ package com.swirlds.platform.network; import com.swirlds.common.platform.NodeId; +import com.swirlds.platform.roster.RosterUtils; import edu.umd.cs.findbugs.annotations.NonNull; import java.security.cert.Certificate; @@ -24,12 +25,16 @@ * A record representing a peer's network information. * * @param nodeId the ID of the peer - * @param nodeName the name of the peer * @param hostname the hostname (or IP address) of the peer * @param signingCertificate the certificate used to validate the peer's TLS certificate */ -public record PeerInfo( - @NonNull NodeId nodeId, - @NonNull String nodeName, - @NonNull String hostname, - @NonNull Certificate signingCertificate) {} +public record PeerInfo(@NonNull NodeId nodeId, @NonNull String hostname, @NonNull Certificate signingCertificate) { + /** + * Return a "node name" for the peer, e.g. "node1" for a peer with NodeId == 0. + * @return a "node name" + */ + @NonNull + public String nodeName() { + return RosterUtils.formatNodeName(nodeId.id()); + } +} diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java new file mode 100644 index 000000000000..837b9526151b --- /dev/null +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/roster/RosterUtils.java @@ -0,0 +1,39 @@ +/* + * 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.swirlds.platform.roster; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * A utility class to help use Rooster and RosterEntry instances. + */ +public final class RosterUtils { + private RosterUtils() {} + + /** + * Formats a "node name" for a given node id, e.g. "node1" for nodeId == 0. + * This name can be used for logging purposes, or to support code that + * uses strings to identify nodes. + * + * @param nodeId a node id + * @return a "node name" + */ + @NonNull + public static String formatNodeName(final long nodeId) { + return "node" + (nodeId + 1); + } +} diff --git a/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/network/NetworkPeerIdentifierTest.java b/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/network/NetworkPeerIdentifierTest.java index f3011e2f39fe..575ed2a0888c 100644 --- a/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/network/NetworkPeerIdentifierTest.java +++ b/platform-sdk/swirlds-platform-core/src/test/java/com/swirlds/platform/network/NetworkPeerIdentifierTest.java @@ -47,13 +47,16 @@ import java.util.HashSet; import java.util.List; import java.util.Objects; -import java.util.Random; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class NetworkPeerIdentifierTest { + private static final Pattern NODE_NAME_PATTERN = Pattern.compile("^node(\\d+)$"); + final PlatformContext platformContext = mock(PlatformContext.class); List peerInfoList = null; PublicStores publicStores = null; @@ -74,15 +77,17 @@ void setUp() throws URISyntaxException, KeyLoadingException, KeyStoreException { publicStores = PublicStores.fromAllPublic(publicKeys, names.stream().toList()); peerInfoList = new ArrayList<>(); - final Random random = new Random(); names.forEach(name -> { - final int id = random.nextInt(names.size()); + final Matcher nameMatcher = NODE_NAME_PATTERN.matcher(name); + if (!nameMatcher.matches()) { + throw new RuntimeException("Invalid node name " + name); + } + final int id = Integer.parseInt(nameMatcher.group(1)) - 1; final NodeId node = new NodeId(id); final PeerInfo peer; try { peer = new PeerInfo( node, - name, "127.0.0.1", Objects.requireNonNull(publicStores.getCertificate(KeyCertPurpose.SIGNING, name))); } catch (final KeyLoadingException e) {