Skip to content

Commit

Permalink
fix: Fixed 101, 102 protobuf files generation at Genesis (#13746)
Browse files Browse the repository at this point in the history
Signed-off-by: Iris Simon <[email protected]>
  • Loading branch information
iwsimon authored Jun 11, 2024
1 parent 564e403 commit d1f97ac
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,17 @@ private void createGenesisAddressBookAndNodeDetails(
final var nodeAddresses = new ArrayList<NodeAddress>();
for (final var nodeInfo : networkInfo.addressBook()) {
nodeAddresses.add(NodeAddress.newBuilder()
.ipAddress(Bytes.wrap(nodeInfo.externalHostName()))
.rsaPubKey(nodeInfo.hexEncodedPublicKey())
.nodeId(nodeInfo.nodeId())
.stake(nodeInfo.stake())
.memo(Bytes.wrap(nodeInfo.memo()))
.serviceEndpoint(ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap(nodeInfo.externalHostName()))
.port(nodeInfo.externalPort())
.build())
.nodeAccountId(nodeInfo.accountId())
.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.
ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap("1.0.0.0"))
.port(1)
.build())
.build());
}

Expand Down Expand Up @@ -282,7 +283,26 @@ private void createGenesisAddressBookAndNodeDetails(
.expirationSecond(bootstrapConfig.systemEntityExpiry())
.build());

// Create the node details
// Create the node details for file 102, their fields are different from 101, addressBook
final var nodeDetail = new ArrayList<NodeAddress>();
for (final var nodeInfo : networkInfo.addressBook()) {
nodeDetail.add(NodeAddress.newBuilder()
.stake(nodeInfo.stake())
.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(ServiceEndpoint.newBuilder()
.ipAddressV4(Bytes.wrap("1.0.0.0"))
.port(1)
.build())
.build());
}

final var nodeDetails =
NodeAddressBook.newBuilder().nodeAddress(nodeAddresses).build();
final var nodeDetailsProto = NodeAddressBook.PROTOBUF.toBytes(nodeDetails);
final var nodeInfoFileNum = filesConfig.nodeDetails();
final var nodeInfoFileId = FileID.newBuilder()
.shardNum(hederaConfig.shard())
Expand All @@ -294,7 +314,7 @@ private void createGenesisAddressBookAndNodeDetails(
files.put(
nodeInfoFileId,
File.newBuilder()
.contents(nodeAddressBookProto)
.contents(nodeDetailsProto)
.fileId(nodeInfoFileId)
.keys(masterKey)
.expirationSecond(bootstrapConfig.systemEntityExpiry())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.services.bdd.suites.file;

import static com.hedera.services.bdd.spec.HapiSpec.defaultHapiSpec;
import static com.hedera.services.bdd.spec.PropertySource.asAccount;
import static com.hedera.services.bdd.spec.assertions.AccountInfoAsserts.changeFromSnapshot;
import static com.hedera.services.bdd.spec.queries.QueryVerbs.getAccountBalance;
import static com.hedera.services.bdd.spec.queries.QueryVerbs.getFileContents;
Expand All @@ -41,7 +42,6 @@
import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.services.bdd.junit.HapiTest;
import com.hedera.services.bdd.junit.OrderedInIsolation;
import com.hedera.services.bdd.spec.HapiPropertySource;
import com.hedera.services.bdd.spec.HapiSpecOperation;
import com.hedera.services.bdd.spec.queries.file.HapiGetFileContents;
import com.hedera.services.bdd.spec.utilops.CustomSpecAssert;
Expand Down Expand Up @@ -108,6 +108,12 @@ final Stream<DynamicTest> account2CanUpdateAddressBook() {
GENESIS, ADDRESS_BOOK, true, contents -> extendedBioAddressBook(contents, TARGET_MEMO, REPLACE_MEMO));
}

@HapiTest
final Stream<DynamicTest> account2CanUpdateAddressBookAccount() {
return specialAccountCanUpdateSpecialFile(
GENESIS, ADDRESS_BOOK, true, contents -> extendedBioAddressBook2(contents, TARGET_MEMO, REPLACE_MEMO));
}

@HapiTest
final Stream<DynamicTest> account50CanUpdateAddressBook() {
return specialAccountCanUpdateSpecialFile(SYSTEM_ADMIN, ADDRESS_BOOK, TARGET_MEMO, REPLACE_MEMO);
Expand Down Expand Up @@ -305,8 +311,7 @@ private byte[] extendedBioAddressBook(byte[] contents, String targetMemo, String
node.setNodeId(nodeId++);
r.nextBytes(randCertHash);
node.setNodeCertHash(ByteString.copyFrom(randCertHash));
node.setNodeAccountId(
HapiPropertySource.asAccount(new String(node.getMemo().toByteArray())));
node.setNodeAccountId(node.getNodeAccountId());
if (new String(node.getMemo().toByteArray()).equals(targetMemo)) {
node.setMemo(ByteString.copyFrom(replaceMemo.getBytes()));
}
Expand All @@ -325,6 +330,36 @@ private byte[] extendedBioAddressBook(byte[] contents, String targetMemo, String
}
}

private byte[] extendedBioAddressBook2(byte[] contents, String targetAccount, String replaceAccount) {
var r = new SplittableRandom();
try {
var book = NodeAddressBook.parseFrom(contents);
var builder = book.toBuilder();
byte[] randCertHash = new byte[32];
long nodeId = 0;
for (NodeAddress.Builder node : builder.getNodeAddressBuilderList()) {
node.setNodeId(nodeId++);
r.nextBytes(randCertHash);
node.setNodeCertHash(ByteString.copyFrom(randCertHash));
node.setNodeAccountId(node.getNodeAccountId());
if (node.getNodeAccountId().equals(asAccount(targetAccount))) {
node.setNodeAccountId(asAccount(replaceAccount));
}
}
var newBook = builder.build();
var bookJson = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(AddressBookPojo.addressBookFrom(newBook));
log.info("New address book w/ extended bio: {}", bookJson);
return builder.build().toByteArray();
} catch (InvalidProtocolBufferException e) {
log.error("Basic address book could not be parsed", e);
throw new AssertionError("Unparseable address book!");
} catch (JsonProcessingException e) {
log.error("Extended address book could not be serialized", e);
throw new AssertionError("Unserializable address book!");
}
}

private byte[] extendedBioNodeDetails(byte[] contents, String targetMemo, String replaceMemo) {
var r = new SplittableRandom();
try {
Expand Down

0 comments on commit d1f97ac

Please sign in to comment.