Skip to content

Commit 5aebd28

Browse files
committed
refactor: HapiPropertySource refactoring
Signed-off-by: ibankov <[email protected]>
1 parent e90a1c2 commit 5aebd28

File tree

3 files changed

+47
-65
lines changed

3 files changed

+47
-65
lines changed

hedera-node/test-clients/src/main/java/com/hedera/services/bdd/spec/HapiPropertySource.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
package com.hedera.services.bdd.spec;
33

44
import static com.hedera.node.app.hapi.utils.CommonPbjConverters.fromByteString;
5-
import static com.hedera.services.bdd.spec.HapiPropertySourceStaticInitializer.REALM;
6-
import static com.hedera.services.bdd.spec.HapiPropertySourceStaticInitializer.SHARD;
75
import static com.hedera.services.bdd.spec.transactions.contract.HapiParserUtil.asHeadlongAddress;
86
import static com.hedera.services.bdd.suites.utils.sysfiles.BookEntryPojo.asOctets;
97
import static java.lang.System.arraycopy;
@@ -45,11 +43,19 @@
4543
import org.hiero.base.utility.CommonUtils;
4644

4745
public interface HapiPropertySource {
48-
46+
HapiPropertySource defaultSource = initializeDefaultSource();
4947
String ENTITY_STRING = "%d.%d.%d";
5048

51-
String NODE_BLOCK_STREAM_DIR = String.format("block-%d.%d.3", SHARD, REALM);
52-
String NODE_RECORD_STREAM_DIR = String.format("record%d.%d.3", SHARD, REALM);
49+
String NODE_BLOCK_STREAM_DIR = String.format("block-%d.%d.3", getSpecDefaultShard(), getSpecDefaultRealm());
50+
String NODE_RECORD_STREAM_DIR = String.format("record%d.%d.3", getSpecDefaultShard(), getSpecDefaultRealm());
51+
52+
private static HapiPropertySource initializeDefaultSource() {
53+
final var source = new JutilPropertySource("spec-default.properties");
54+
// Validate the default shard/realm properties
55+
Objects.requireNonNull(source.get("default.shard"), "Missing default.shard in spec-default.properties");
56+
Objects.requireNonNull(source.get("default.realm"), "Missing default.realm in spec-default.properties");
57+
return source;
58+
}
5359

5460
static byte[] explicitBytesOf(@NonNull final Address address) {
5561
var asBytes = address.value().toByteArray();
@@ -151,7 +157,7 @@ default RealmID getRealm(String property) {
151157
default long getRealm() {
152158
return Optional.ofNullable(get("hapi.spec.default.realm"))
153159
.map(Long::parseLong)
154-
.orElse(REALM);
160+
.orElse(getSpecDefaultRealm());
155161
}
156162

157163
@Deprecated
@@ -162,19 +168,27 @@ default ShardID getShard(String property) {
162168
default long getShard() {
163169
return Optional.ofNullable(get("hapi.spec.default.shard"))
164170
.map(Long::parseLong)
165-
.orElse((long) SHARD);
171+
.orElse(getSpecDefaultShard());
166172
}
167173

168174
static long getConfigShard() {
169175
return Optional.ofNullable(System.getProperty("hapi.spec.default.shard"))
170176
.map(Long::parseLong)
171-
.orElse((long) SHARD);
177+
.orElse(getSpecDefaultShard());
172178
}
173179

174180
static long getConfigRealm() {
175181
return Optional.ofNullable(System.getProperty("hapi.spec.default.realm"))
176182
.map(Long::parseLong)
177-
.orElse(REALM);
183+
.orElse(getSpecDefaultRealm());
184+
}
185+
186+
private static long getSpecDefaultShard() {
187+
return Integer.parseInt(defaultSource.get("default.shard"));
188+
}
189+
190+
private static long getSpecDefaultRealm() {
191+
return Integer.parseInt(defaultSource.get("default.realm"));
178192
}
179193

180194
default TimeUnit getTimeUnit(String property) {
@@ -246,9 +260,7 @@ default HapiSpec.SpecStatus getSpecStatus(String property) {
246260
static HapiPropertySource[] asSources(Object... sources) {
247261
return Stream.of(sources)
248262
.filter(Objects::nonNull)
249-
.map(s -> (s instanceof HapiPropertySource)
250-
? s
251-
: ((s instanceof Map) ? new MapPropertySource((Map) s) : new JutilPropertySource((String) s)))
263+
.map(HapiPropertySource::toHapiPropertySource)
252264
.toArray(HapiPropertySource[]::new);
253265
}
254266

@@ -351,7 +363,7 @@ static String asAliasableAccountString(final AccountID account) {
351363
return asAccountString(account);
352364
} else {
353365
final var literalAlias = account.getAlias().toString();
354-
return String.format(ENTITY_STRING, account.getShardNum(), account.getRealmNum(), literalAlias);
366+
return asEntityString(account.getShardNum(), account.getRealmNum(), literalAlias);
355367
}
356368
}
357369

@@ -554,7 +566,7 @@ static String asHexedSolidityAddress(final int shard, final long realm, final lo
554566
static ContractID contractIdFromHexedMirrorAddress(final String hexedEvm) {
555567
byte[] unhex = CommonUtils.unhex(hexedEvm);
556568
return ContractID.newBuilder()
557-
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(unhex, 0, 4)))
569+
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(requireNonNull(unhex), 0, 4)))
558570
.setRealmNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 4, 12)))
559571
.setContractNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 12, 20)))
560572
.build();
@@ -563,7 +575,7 @@ static ContractID contractIdFromHexedMirrorAddress(final String hexedEvm) {
563575
static AccountID accountIdFromHexedMirrorAddress(final String hexedEvm) {
564576
byte[] unhex = CommonUtils.unhex(hexedEvm);
565577
return AccountID.newBuilder()
566-
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(unhex, 0, 4)))
578+
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(requireNonNull(unhex), 0, 4)))
567579
.setRealmNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 4, 12)))
568580
.setAccountNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 12, 20)))
569581
.build();
@@ -572,7 +584,7 @@ static AccountID accountIdFromHexedMirrorAddress(final String hexedEvm) {
572584
static String literalIdFromHexedMirrorAddress(final String hexedEvm) {
573585
byte[] unhex = CommonUtils.unhex(hexedEvm);
574586
return HapiPropertySource.asContractString(ContractID.newBuilder()
575-
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(unhex, 0, 4)))
587+
.setShardNum(Ints.fromByteArray(Arrays.copyOfRange(requireNonNull(unhex), 0, 4)))
576588
.setRealmNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 4, 12)))
577589
.setContractNum(Longs.fromByteArray(Arrays.copyOfRange(unhex, 12, 20)))
578590
.build());
@@ -606,20 +618,19 @@ static long numberOfLongZero(@NonNull final byte[] explicit) {
606618
explicit[19]);
607619
}
608620

609-
public static long realmOfLongZero(@NonNull final byte[] explicit) {
610-
return longFrom(
611-
explicit[4],
612-
explicit[5],
613-
explicit[6],
614-
explicit[7],
615-
explicit[8],
616-
explicit[9],
617-
explicit[10],
618-
explicit[11]);
619-
}
620-
621-
public static long shardOfLongZero(@NonNull final byte[] explicit) {
622-
return longFrom(explicit[0], explicit[1], explicit[2], explicit[3]);
621+
private static HapiPropertySource toHapiPropertySource(Object s) {
622+
if (s instanceof HapiPropertySource hps) {
623+
return hps;
624+
}
625+
if (s instanceof Map<?, ?> map) {
626+
@SuppressWarnings("unchecked")
627+
Map<String, String> typedMap = (Map<String, String>) map;
628+
return new MapPropertySource(typedMap);
629+
}
630+
if (s instanceof String str) {
631+
return new JutilPropertySource(str);
632+
}
633+
throw new IllegalArgumentException("Unsupported source type: " + s.getClass());
623634
}
624635

625636
private static long longFrom(
@@ -640,8 +651,4 @@ private static long longFrom(
640651
| (b7 & 0xFFL) << 8
641652
| (b8 & 0xFFL);
642653
}
643-
644-
private static long longFrom(final byte b1, final byte b2, final byte b3, final byte b4) {
645-
return (b1 & 0xFFL) << 24 | (b2 & 0xFFL) << 16 | (b3 & 0xFFL) << 8 | (b4 & 0xFFL);
646-
}
647654
}

hedera-node/test-clients/src/main/java/com/hedera/services/bdd/spec/HapiPropertySourceStaticInitializer.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

hedera-node/test-clients/src/main/java/com/hedera/services/bdd/spec/HapiSpecSetup.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static HapiPropertySource getDefaultPropertySource() {
6060
DEFAULT_PROPERTY_SOURCE =
6161
inPriorityOrder(asSources(Stream.of(Stream.of(sources), Stream.of(BASE_DEFAULT_PROPERTY_SOURCE))
6262
.flatMap(Function.identity())
63-
.toArray(n -> new Object[n])));
63+
.toArray(Object[]::new)));
6464
}
6565
return DEFAULT_PROPERTY_SOURCE;
6666
}
@@ -569,18 +569,11 @@ public TlsConfig tls() {
569569
}
570570

571571
public boolean getConfigTLS() {
572-
boolean useTls = false;
573-
switch (this.tls()) {
574-
case ON:
575-
useTls = Boolean.TRUE;
576-
break;
577-
case OFF:
578-
useTls = Boolean.FALSE;
579-
break;
580-
case ALTERNATE:
581-
useTls = r.nextBoolean();
582-
}
583-
return useTls;
572+
return switch (this.tls()) {
573+
case ON -> Boolean.TRUE;
574+
case OFF -> Boolean.FALSE;
575+
case ALTERNATE -> r.nextBoolean();
576+
};
584577
}
585578

586579
TxnProtoStructure txnProtoStructure() {

0 commit comments

Comments
 (0)