Skip to content

Commit

Permalink
chore: Remove PeerInfo.nodeName (#15441)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <[email protected]>
  • Loading branch information
anthony-swirldslabs authored Sep 11, 2024
1 parent 1c641ab commit c518976
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@
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;

/**
* 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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerInfo> peerInfoList = null;
PublicStores publicStores = null;
Expand All @@ -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) {
Expand Down

0 comments on commit c518976

Please sign in to comment.