Skip to content

Commit

Permalink
feat: hapi tests for DAB createNode (#14228)
Browse files Browse the repository at this point in the history
Signed-off-by: Lev Povolotsky <lev@swirldslabs.com>
Signed-off-by: Kim Rader <kim.rader@swirldslabs.com>
Signed-off-by: Michael Tinker <michael.tinker@swirldslabs.com>
Co-authored-by: Lev Povolotsky <lev@swirldslabs.com>
Co-authored-by: Michael Tinker <michael.tinker@swirldslabs.com>
  • Loading branch information
3 people authored Jul 16, 2024
1 parent 365a110 commit 075d35c
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record NodesConfig(
@ConfigProperty(defaultValue = "100") @NetworkProperty long maxNumber,
@ConfigProperty(defaultValue = "100") @NetworkProperty int nodeMaxDescriptionUtf8Bytes,
@ConfigProperty(defaultValue = "10") @NetworkProperty int maxGossipEndpoint,
@ConfigProperty(defaultValue = "10") @NetworkProperty int maxServiceEndpoint,
@ConfigProperty(defaultValue = "8") @NetworkProperty int maxServiceEndpoint,
@ConfigProperty(defaultValue = "true") @NetworkProperty boolean gossipFqdnRestricted,
@ConfigProperty(defaultValue = "253") @NetworkProperty int maxFqdnSize,
@ConfigProperty(defaultValue = "false") @NetworkProperty boolean updateAccountIdAllowed) {}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@
* @return the reasons the test cannot run concurrently with other tests
*/
ContextRequirement[] requirement() default {};

/**
* If set, the names of properties this test overrides and needs automatically
* restored to their original values after the test completes.
* @return the names of properties this test overrides
*/
String[] overrides() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.hedera.services.bdd.junit.GenesisHapiTest;
import com.hedera.services.bdd.junit.HapiTest;
import com.hedera.services.bdd.junit.LeakyEmbeddedHapiTest;
import com.hedera.services.bdd.junit.LeakyHapiTest;
import com.hedera.services.bdd.junit.hedera.HederaNetwork;
import com.hedera.services.bdd.junit.hedera.embedded.EmbeddedNetwork;
Expand Down Expand Up @@ -58,6 +59,9 @@ public void beforeEach(@NonNull final ExtensionContext extensionContext) {
if (isAnnotated(method, LeakyHapiTest.class)) {
HapiSpec.PROPERTIES_TO_PRESERVE.set(
List.of(method.getAnnotation(LeakyHapiTest.class).overrides()));
} else if (isAnnotated(method, LeakyEmbeddedHapiTest.class)) {
HapiSpec.PROPERTIES_TO_PRESERVE.set(List.of(
method.getAnnotation(LeakyEmbeddedHapiTest.class).overrides()));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import static java.util.Objects.requireNonNull;
import static java.util.stream.StreamSupport.stream;

import com.google.protobuf.ByteString;
import com.hedera.hapi.node.base.AccountID;
import com.hedera.services.bdd.junit.hedera.HederaNode;
import com.hedera.services.bdd.junit.hedera.NodeMetadata;
import com.hederahashgraph.api.proto.java.ServiceEndpoint;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.system.address.Address;
import com.swirlds.platform.system.address.AddressBook;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -136,4 +139,28 @@ public static NodeMetadata classicMetadataFor(
public static Stream<Long> nodeIdsFrom(AddressBook addressBook) {
return stream(addressBook.spliterator(), false).map(Address::getNodeId).map(NodeId::id);
}

/**
* Returns service end point base on the host and port. - used for hapi path for ServiceEndPoint
*
* @param host is an ip or domain name
* @param port
* @return ServiceEndpoint
*/
public static ServiceEndpoint endpointFor(@NonNull final String host, final int port) {
final Pattern IPV4_ADDRESS_PATTERN = Pattern.compile("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$");
final var builder = ServiceEndpoint.newBuilder().setPort(port);
if (IPV4_ADDRESS_PATTERN.matcher(host).matches()) {
final var octets = host.split("[.]");
builder.setIpAddressV4(ByteString.copyFrom((new byte[] {
(byte) Integer.parseInt(octets[0]),
(byte) Integer.parseInt(octets[1]),
(byte) Integer.parseInt(octets[2]),
(byte) Integer.parseInt(octets[3])
})));
} else {
builder.setDomainName(host);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ public HapiGetTokenInfo(String token) {
private boolean emptyMetadataKey = false;
private boolean emptyPauseKey = false;

private boolean invalidAdminKey = false;
private boolean invalidWipeKey = false;
private boolean invalidKycKey = false;
private boolean invalidSupplyKey = false;
private boolean invalidFreezeKey = false;
private boolean invalidFeeScheduleKey = false;
private boolean invalidMetadataKey = false;
private boolean invalidPauseKey = false;

@SuppressWarnings("java:S1068")
private Optional<Boolean> expectedDeletion = Optional.empty();

Expand Down Expand Up @@ -408,46 +399,6 @@ public HapiGetTokenInfo hasEmptyMetadataKey() {
return this;
}

public HapiGetTokenInfo hasInvalidAdminKey() {
invalidAdminKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidPauseKey() {
invalidPauseKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidFreezeKey() {
invalidFreezeKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidKycKey() {
invalidKycKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidSupplyKey() {
invalidSupplyKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidWipeKey() {
invalidWipeKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidFeeScheduleKey() {
invalidFeeScheduleKey = true;
return this;
}

public HapiGetTokenInfo hasInvalidMetadataKey() {
invalidMetadataKey = true;
return this;
}

@Override
@SuppressWarnings("java:S5960")
protected void assertExpectationsGiven(HapiSpec spec) {
Expand Down Expand Up @@ -529,8 +480,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyFreezeKey) {
assertForRemovedKey(actualInfo.getFreezeKey());
} else if (invalidFreezeKey) {
assertForAllZerosInvalidKey(actualInfo.getFreezeKey());
} else if (explicitFreezeKey != null) {
assertEquals(fromPbj(explicitFreezeKey), actualInfo.getFreezeKey(), "Wrong token freeze key!");
} else {
Expand All @@ -544,8 +493,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyAdminKey) {
assertForRemovedKey(actualInfo.getAdminKey());
} else if (invalidAdminKey) {
assertForAllZerosInvalidKey(actualInfo.getAdminKey());
} else if (explicitAdminKey != null) {
assertEquals(fromPbj(explicitAdminKey), actualInfo.getAdminKey(), "Wrong token admin key!");
} else {
Expand All @@ -559,8 +506,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyWipeKey) {
assertForRemovedKey(actualInfo.getWipeKey());
} else if (invalidWipeKey) {
assertForAllZerosInvalidKey(actualInfo.getWipeKey());
} else if (explicitWipeKey != null) {
assertEquals(fromPbj(explicitWipeKey), actualInfo.getWipeKey(), "Wrong token wipe key!");
} else {
Expand All @@ -574,8 +519,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyKycKey) {
assertForRemovedKey(actualInfo.getKycKey());
} else if (invalidKycKey) {
assertForAllZerosInvalidKey(actualInfo.getKycKey());
} else if (explicitKycKey != null) {
assertEquals(fromPbj(explicitKycKey), actualInfo.getKycKey(), "Wrong token KYC key!");
} else {
Expand All @@ -589,8 +532,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptySupplyKey) {
assertForRemovedKey(actualInfo.getSupplyKey());
} else if (invalidSupplyKey) {
assertForAllZerosInvalidKey(actualInfo.getSupplyKey());
} else if (explicitSupplyKey != null) {
assertEquals(fromPbj(explicitSupplyKey), actualInfo.getSupplyKey(), "Wrong token supply key!");
} else {
Expand All @@ -604,8 +545,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyFeeScheduleKey) {
assertForRemovedKey(actualInfo.getFeeScheduleKey());
} else if (invalidFeeScheduleKey) {
assertForAllZerosInvalidKey(actualInfo.getFeeScheduleKey());
} else if (explicitFeeScheduleKey != null) {
assertEquals(
fromPbj(explicitFeeScheduleKey), actualInfo.getFeeScheduleKey(), "Wrong token fee schedule key!");
Expand All @@ -620,8 +559,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyPauseKey) {
assertForRemovedKey(actualInfo.getPauseKey());
} else if (invalidPauseKey) {
assertForAllZerosInvalidKey(actualInfo.getPauseKey());
} else if (explicitPauseKey != null) {
assertEquals(fromPbj(explicitPauseKey), actualInfo.getPauseKey(), "Wrong token pause key!");
} else {
Expand All @@ -635,8 +572,6 @@ protected void assertExpectationsGiven(HapiSpec spec) {

if (emptyMetadataKey) {
assertForRemovedKey(actualInfo.getMetadataKey());
} else if (invalidMetadataKey) {
assertForAllZerosInvalidKey(actualInfo.getMetadataKey());
} else {
assertFor(
actualInfo.getMetadataKey(),
Expand All @@ -661,13 +596,6 @@ private <T, R> void assertFor(
}
}

private void assertForAllZerosInvalidKey(Key actual) {
assertEquals(
TxnUtils.ALL_ZEROS_INVALID_KEY,
actual,
"Does not equal to zero address `0x0000000000000000000000000000000000000000`");
}

private void assertForRemovedKey(Key actual) {
assertEquals(Key.getDefaultInstance(), actual, "Does not equal to a removed key");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public class TxnUtils {
Key.newBuilder().setThresholdKey(ThresholdKey.getDefaultInstance()).build();
public static Key EMPTY_KEY_LIST =
Key.newBuilder().setKeyList(KeyList.getDefaultInstance()).build();
public static Key ALL_ZEROS_INVALID_KEY = Key.newBuilder()
public static Key WRONG_LENGTH_EDDSA_KEY = Key.newBuilder()
.setEd25519(ByteString.fromHex("0000000000000000000000000000000000000000"))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.services.bdd.spec.transactions.node;

import static com.hedera.node.app.hapi.utils.CommonPbjConverters.fromPbj;
import static com.hedera.services.bdd.junit.hedera.utils.AddressBookUtils.endpointFor;
import static com.hedera.services.bdd.spec.transactions.TxnUtils.bannerWith;
import static com.hedera.services.bdd.spec.transactions.TxnUtils.netOf;
import static com.hederahashgraph.api.proto.java.ResponseCodeEnum.SUCCESS;
Expand All @@ -41,7 +42,7 @@
import com.hederahashgraph.api.proto.java.TransactionBody;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -57,8 +58,10 @@ public class HapiNodeCreate extends HapiTxnOp<HapiNodeCreate> {
private final String nodeName;
private Optional<AccountID> accountId = Optional.empty();
private Optional<String> description = Optional.empty();
private List<ServiceEndpoint> gossipEndpoints = Collections.emptyList();
private List<ServiceEndpoint> grpcEndpoints = Collections.emptyList();
private List<ServiceEndpoint> gossipEndpoints =
Arrays.asList(endpointFor("192.168.1.200", 123), endpointFor("192.168.1.201", 123));
private List<ServiceEndpoint> grpcEndpoints = Arrays.asList(
ServiceEndpoint.newBuilder().setDomainName("test.com").setPort(123).build());
private Optional<byte[]> gossipCaCertificate = Optional.empty();
private Optional<byte[]> grpcCertificateHash = Optional.empty();
private Optional<String> adminKeyName = Optional.empty();
Expand All @@ -67,9 +70,6 @@ public class HapiNodeCreate extends HapiTxnOp<HapiNodeCreate> {
@Nullable
private Key adminKey;

@Nullable
private String keyName;

public HapiNodeCreate(@NonNull final String nodeName) {
this.nodeName = nodeName;
}
Expand Down Expand Up @@ -124,13 +124,18 @@ public HapiNodeCreate grpcCertificateHash(final byte[] grpcCertificateHash) {
return this;
}

public HapiNodeCreate adminKeyName(final String s) {
adminKeyName = Optional.of(s);
public HapiNodeCreate adminKey(final String name) {
adminKeyName = Optional.of(name);
return this;
}

public HapiNodeCreate adminKey(final Key key) {
adminKey = key;
return this;
}

private void genKeysFor(final HapiSpec spec) {
adminKey = netOf(spec, adminKeyName, adminKeyShape);
adminKey = adminKey == null ? netOf(spec, adminKeyName, adminKeyShape) : adminKey;
}

@Override
Expand Down Expand Up @@ -166,12 +171,8 @@ protected Consumer<TransactionBody.Builder> opBodyDef(@NonNull final HapiSpec sp
accountId.ifPresent(builder::setAccountId);
description.ifPresent(builder::setDescription);
if (adminKey != null) builder.setAdminKey(adminKey);
if (!gossipEndpoints.isEmpty()) {
builder.clearGossipEndpoint().addAllGossipEndpoint(gossipEndpoints);
}
if (!grpcEndpoints.isEmpty()) {
builder.clearServiceEndpoint().addAllServiceEndpoint(grpcEndpoints);
}
builder.clearGossipEndpoint().addAllGossipEndpoint(gossipEndpoints);
builder.clearServiceEndpoint().addAllServiceEndpoint(grpcEndpoints);
gossipCaCertificate.ifPresent(s -> builder.setGossipCaCertificate(ByteString.copyFrom(s)));
grpcCertificateHash.ifPresent(s -> builder.setGrpcCertificateHash(ByteString.copyFrom(s)));
});
Expand Down
Loading

0 comments on commit 075d35c

Please sign in to comment.