Skip to content

Commit

Permalink
feat: cherry-pick: Generated Files 101 & 102 Sequentially Ordered by …
Browse files Browse the repository at this point in the history
…Node ID (#17289)
  • Loading branch information
iwsimon authored Jan 9, 2025
1 parent 5f9fdff commit b208f7c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.hedera.hapi.node.base.TransactionFeeSchedule;
import com.hedera.hapi.node.file.FileCreateTransactionBody;
import com.hedera.hapi.node.file.FileUpdateTransactionBody;
import com.hedera.hapi.node.state.addressbook.Node;
import com.hedera.hapi.node.state.common.EntityNumber;
import com.hedera.hapi.node.state.file.File;
import com.hedera.hapi.node.state.primitives.ProtoBytes;
Expand All @@ -64,6 +65,7 @@
import com.swirlds.state.lifecycle.Schema;
import com.swirlds.state.lifecycle.StateDefinition;
import com.swirlds.state.lifecycle.info.NetworkInfo;
import com.swirlds.state.lifecycle.info.NodeInfo;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -202,36 +204,31 @@ public void createGenesisAddressBookAndNodeDetails(@NonNull final SystemContext
}

public Bytes genesisAddressBook(@NonNull final NetworkInfo networkInfo) {
final var nodeAddresses = new ArrayList<NodeAddress>();
for (final var nodeInfo : networkInfo.addressBook()) {
nodeAddresses.add(NodeAddress.newBuilder()
.nodeId(nodeInfo.nodeId())
.rsaPubKey(nodeInfo.hexEncodedPublicKey())
.nodeAccountId(nodeInfo.accountId()) // don't use memo as it is deprecated.
.serviceEndpoint(
// we really don't have grpc proxy name and port for now. Temporary values are set.
// After Dynamic Address Book Phase 2 release, we will have the correct values.Then update
// here.
V053AddressBookSchema.endpointFor("1.0.0.0", 1))
.build());
}
final var nodeAddresses = networkInfo.addressBook().stream()
.sorted(Comparator.comparingLong(NodeInfo::nodeId))
.map(nodeInfo -> NodeAddress.newBuilder()
.nodeId(nodeInfo.nodeId())
.nodeAccountId(nodeInfo.accountId())
.rsaPubKey(nodeInfo.hexEncodedPublicKey())
.serviceEndpoint(V053AddressBookSchema.endpointFor("1.0.0.0", 1))
.build())
.toList();
return NodeAddressBook.PROTOBUF.toBytes(
NodeAddressBook.newBuilder().nodeAddress(nodeAddresses).build());
}

public Bytes genesisNodeDetails(@NonNull final NetworkInfo networkInfo) {
final var nodeDetails = new ArrayList<NodeAddress>();
for (final var nodeInfo : networkInfo.addressBook()) {
nodeDetails.add(NodeAddress.newBuilder()
.stake(nodeInfo.weight())
.nodeAccountId(nodeInfo.accountId())
.nodeId(nodeInfo.nodeId())
.rsaPubKey(nodeInfo.hexEncodedPublicKey())
// we really don't have grpc proxy name and port for now.Temporary values are set.
// After Dynamic Address Book Phase 2 release, we will have the correct values. Then update here.
.serviceEndpoint(V053AddressBookSchema.endpointFor("1.0.0.0", 1))
.build());
}
final var nodeDetails = networkInfo.addressBook().stream()
.sorted(Comparator.comparingLong(NodeInfo::nodeId))
.map(nodeInfo -> NodeAddress.newBuilder()
.stake(nodeInfo.weight())
.nodeAccountId(nodeInfo.accountId())
.nodeId(nodeInfo.nodeId())
.rsaPubKey(nodeInfo.hexEncodedPublicKey())
.serviceEndpoint(V053AddressBookSchema.endpointFor("1.0.0.0", 1))
.build())
.toList();

return NodeAddressBook.PROTOBUF.toBytes(
NodeAddressBook.newBuilder().nodeAddress(nodeDetails).build());
}
Expand Down Expand Up @@ -271,6 +268,7 @@ private Bytes nodeStoreNodeDetails(@NonNull final ReadableNodeStore nodeStore) {
.mapToLong(EntityNumber::number)
.mapToObj(nodeStore::get)
.filter(node -> node != null && !node.deleted())
.sorted(Comparator.comparingLong(Node::nodeId))
.forEach(node -> nodeDetails.add(NodeAddress.newBuilder()
.nodeId(node.nodeId())
.nodeAccountId(node.accountId())
Expand All @@ -296,6 +294,7 @@ private Bytes nodeStoreAddressBook(@NonNull final ReadableNodeStore nodeStore) {
.mapToLong(EntityNumber::number)
.mapToObj(nodeStore::get)
.filter(node -> node != null && !node.deleted())
.sorted(Comparator.comparingLong(Node::nodeId))
.forEach(node -> nodeAddresses.add(NodeAddress.newBuilder()
.nodeId(node.nodeId())
.nodeCertHash(getHexStringBytesFromBytes(node.grpcCertificateHash()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 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.
Expand Down Expand Up @@ -75,6 +75,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -478,7 +479,9 @@ private static VisibleItemsValidator nodeDetailsExportValidator(
try {
final var updatedAddressBook = NodeAddressBook.PROTOBUF.parse(
Bytes.wrap(synthOp.getContents().toByteArray()));
var prevNodeId = -1L;
for (final var address : updatedAddressBook.nodeAddress()) {
assertTrue(address.nodeId() > prevNodeId, "Node IDs must be in ascending order");
final var expectedCert = gossipCertificates.get().get(address.nodeId());
final var expectedPubKey = expectedCert.getPublicKey().getEncoded();
final var actualPubKey = unhex(address.rsaPubKey());
Expand All @@ -495,6 +498,7 @@ private static VisibleItemsValidator nodeDetailsExportValidator(

final var expectedServiceEndpoint = endpointsFor((int) address.nodeId());
assertEquals(expectedServiceEndpoint, address.serviceEndpoint());
prevNodeId = address.nodeId();
}
} catch (ParseException e) {
Assertions.fail("Update contents was not protobuf " + e.getMessage());
Expand Down Expand Up @@ -523,12 +527,14 @@ private static VisibleItemsValidator addressBookExportValidator(
assertNotNull(updateItem, "No update for " + fileNumProperty + " found in post-upgrade txn");
final var synthOp = updateItem.body().getFileUpdate();
final var addressBookId =
new FileID(0, 0, Long.parseLong(spec.startupProperties().get("files.addressBook")));
new FileID(0, 0, Long.parseLong(spec.startupProperties().get(fileNumProperty)));
assertEquals(addressBookId, toPbj(synthOp.getFileID()));
try {
final var updatedAddressBook = NodeAddressBook.PROTOBUF.parse(
Bytes.wrap(synthOp.getContents().toByteArray()));
var prevNodeId = -1L;
for (final var address : updatedAddressBook.nodeAddress()) {
assertTrue(address.nodeId() > prevNodeId, "Node IDs must be in ascending order");
final var actualCertHash = address.nodeCertHash().toByteArray();
assertArrayEquals(
getHexStringBytesFromBytes(grpcCertHashes[(int) address.nodeId()]),
Expand All @@ -537,6 +543,7 @@ private static VisibleItemsValidator addressBookExportValidator(

final var expectedServiceEndpoint = endpointsFor((int) address.nodeId());
assertEquals(expectedServiceEndpoint, address.serviceEndpoint());
prevNodeId = address.nodeId();
}
} catch (ParseException e) {
Assertions.fail("Update contents was not protobuf " + e.getMessage());
Expand Down

0 comments on commit b208f7c

Please sign in to comment.