2
2
package com .hedera .services .bdd .spec ;
3
3
4
4
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 ;
7
5
import static com .hedera .services .bdd .spec .transactions .contract .HapiParserUtil .asHeadlongAddress ;
8
6
import static com .hedera .services .bdd .suites .utils .sysfiles .BookEntryPojo .asOctets ;
9
7
import static java .lang .System .arraycopy ;
45
43
import org .hiero .base .utility .CommonUtils ;
46
44
47
45
public interface HapiPropertySource {
48
-
46
+ HapiPropertySource defaultSource = initializeDefaultSource ();
49
47
String ENTITY_STRING = "%d.%d.%d" ;
50
48
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
+ }
53
59
54
60
static byte [] explicitBytesOf (@ NonNull final Address address ) {
55
61
var asBytes = address .value ().toByteArray ();
@@ -151,7 +157,7 @@ default RealmID getRealm(String property) {
151
157
default long getRealm () {
152
158
return Optional .ofNullable (get ("hapi.spec.default.realm" ))
153
159
.map (Long ::parseLong )
154
- .orElse (REALM );
160
+ .orElse (getSpecDefaultRealm () );
155
161
}
156
162
157
163
@ Deprecated
@@ -162,19 +168,27 @@ default ShardID getShard(String property) {
162
168
default long getShard () {
163
169
return Optional .ofNullable (get ("hapi.spec.default.shard" ))
164
170
.map (Long ::parseLong )
165
- .orElse (( long ) SHARD );
171
+ .orElse (getSpecDefaultShard () );
166
172
}
167
173
168
174
static long getConfigShard () {
169
175
return Optional .ofNullable (System .getProperty ("hapi.spec.default.shard" ))
170
176
.map (Long ::parseLong )
171
- .orElse (( long ) SHARD );
177
+ .orElse (getSpecDefaultShard () );
172
178
}
173
179
174
180
static long getConfigRealm () {
175
181
return Optional .ofNullable (System .getProperty ("hapi.spec.default.realm" ))
176
182
.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" ));
178
192
}
179
193
180
194
default TimeUnit getTimeUnit (String property ) {
@@ -246,9 +260,7 @@ default HapiSpec.SpecStatus getSpecStatus(String property) {
246
260
static HapiPropertySource [] asSources (Object ... sources ) {
247
261
return Stream .of (sources )
248
262
.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 )
252
264
.toArray (HapiPropertySource []::new );
253
265
}
254
266
@@ -351,7 +363,7 @@ static String asAliasableAccountString(final AccountID account) {
351
363
return asAccountString (account );
352
364
} else {
353
365
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 );
355
367
}
356
368
}
357
369
@@ -554,7 +566,7 @@ static String asHexedSolidityAddress(final int shard, final long realm, final lo
554
566
static ContractID contractIdFromHexedMirrorAddress (final String hexedEvm ) {
555
567
byte [] unhex = CommonUtils .unhex (hexedEvm );
556
568
return ContractID .newBuilder ()
557
- .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (unhex , 0 , 4 )))
569
+ .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (requireNonNull ( unhex ) , 0 , 4 )))
558
570
.setRealmNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 4 , 12 )))
559
571
.setContractNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 12 , 20 )))
560
572
.build ();
@@ -563,7 +575,7 @@ static ContractID contractIdFromHexedMirrorAddress(final String hexedEvm) {
563
575
static AccountID accountIdFromHexedMirrorAddress (final String hexedEvm ) {
564
576
byte [] unhex = CommonUtils .unhex (hexedEvm );
565
577
return AccountID .newBuilder ()
566
- .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (unhex , 0 , 4 )))
578
+ .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (requireNonNull ( unhex ) , 0 , 4 )))
567
579
.setRealmNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 4 , 12 )))
568
580
.setAccountNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 12 , 20 )))
569
581
.build ();
@@ -572,7 +584,7 @@ static AccountID accountIdFromHexedMirrorAddress(final String hexedEvm) {
572
584
static String literalIdFromHexedMirrorAddress (final String hexedEvm ) {
573
585
byte [] unhex = CommonUtils .unhex (hexedEvm );
574
586
return HapiPropertySource .asContractString (ContractID .newBuilder ()
575
- .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (unhex , 0 , 4 )))
587
+ .setShardNum (Ints .fromByteArray (Arrays .copyOfRange (requireNonNull ( unhex ) , 0 , 4 )))
576
588
.setRealmNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 4 , 12 )))
577
589
.setContractNum (Longs .fromByteArray (Arrays .copyOfRange (unhex , 12 , 20 )))
578
590
.build ());
@@ -606,20 +618,19 @@ static long numberOfLongZero(@NonNull final byte[] explicit) {
606
618
explicit [19 ]);
607
619
}
608
620
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 ());
623
634
}
624
635
625
636
private static long longFrom (
@@ -640,8 +651,4 @@ private static long longFrom(
640
651
| (b7 & 0xFFL ) << 8
641
652
| (b8 & 0xFFL );
642
653
}
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
- }
647
654
}
0 commit comments