Skip to content

Commit d9e9ceb

Browse files
Merge pull request #112 from OpenElements/microprofile-fix
standardized config
2 parents 65e9853 + 4d60bde commit d9e9ceb

File tree

7 files changed

+279
-188
lines changed

7 files changed

+279
-188
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.openelements.hedera.base.config;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import java.util.Objects;
5+
import org.jspecify.annotations.NonNull;
6+
7+
public record ConsensusNode(@NonNull String ip, @NonNull String port, @NonNull String account) {
8+
9+
public ConsensusNode {
10+
Objects.requireNonNull(ip, "ip must not be null");
11+
Objects.requireNonNull(port, "port must not be null");
12+
Objects.requireNonNull(account, "account must not be null");
13+
}
14+
15+
public String getAddress() {
16+
return ip + ":" + port;
17+
}
18+
19+
public AccountId getAccountId() {
20+
return AccountId.fromString(account);
21+
}
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.openelements.hedera.base.config;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.Client;
5+
import com.openelements.hedera.base.Account;
6+
import com.openelements.hedera.base.implementation.HederaNetwork;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Optional;
10+
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
import org.jspecify.annotations.NonNull;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
public interface HieroConfig {
17+
18+
final static Logger log = LoggerFactory.getLogger(HieroConfig.class);
19+
20+
@NonNull Account getOperatorAccount();
21+
22+
@NonNull Optional<String> getNetworkName();
23+
24+
@NonNull List<String> getMirrornodeAddresses();
25+
26+
@NonNull Set<ConsensusNode> getConsensusNodes();
27+
28+
@NonNull HederaNetwork getNetwork();
29+
30+
@NonNull
31+
default Client createClient() {
32+
final HederaNetwork hederaNetwork = getNetwork();
33+
if (hederaNetwork != HederaNetwork.CUSTOM) {
34+
try {
35+
log.debug("Hedera network '{}' will be used", hederaNetwork.getName());
36+
Client client = Client.forName(hederaNetwork.getName());
37+
client.setOperator(getOperatorAccount().accountId(), getOperatorAccount().privateKey());
38+
return client;
39+
} catch (Exception e) {
40+
throw new IllegalArgumentException("Can not create client for network " + hederaNetwork.getName(),
41+
e);
42+
}
43+
} else {
44+
try {
45+
final Map<String, AccountId> nodes = getConsensusNodes().stream()
46+
.collect(Collectors.toMap(n -> n.getAddress(), n -> n.getAccountId()));
47+
final Client client = Client.forNetwork(nodes);
48+
client.setMirrorNetwork(getMirrornodeAddresses());
49+
client.setOperator(getOperatorAccount().accountId(), getOperatorAccount().privateKey());
50+
return client;
51+
} catch (Exception e) {
52+
throw new IllegalArgumentException("Can not create client for custom network", e);
53+
}
54+
}
55+
}
56+
57+
}
Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package com.openelements.hedera.microprofile;
22

3-
import com.hedera.hashgraph.sdk.AccountId;
4-
import com.hedera.hashgraph.sdk.Client;
5-
import com.hedera.hashgraph.sdk.PrivateKey;
6-
import com.openelements.hedera.base.Account;
73
import com.openelements.hedera.base.AccountClient;
84
import com.openelements.hedera.base.ContractVerificationClient;
95
import com.openelements.hedera.base.FileClient;
106
import com.openelements.hedera.base.SmartContractClient;
7+
import com.openelements.hedera.base.config.HieroConfig;
118
import com.openelements.hedera.base.implementation.AccountClientImpl;
129
import com.openelements.hedera.base.implementation.FileClientImpl;
13-
import com.openelements.hedera.base.implementation.HederaNetwork;
1410
import com.openelements.hedera.base.implementation.ProtocolLayerClientImpl;
1511
import com.openelements.hedera.base.implementation.SmartContractClientImpl;
1612
import com.openelements.hedera.base.protocol.ProtocolLayerClient;
1713
import com.openelements.hedera.microprofile.implementation.ContractVerificationClientImpl;
14+
import com.openelements.hedera.microprofile.implementation.HieroConfigImpl;
1815
import jakarta.enterprise.context.ApplicationScoped;
1916
import jakarta.enterprise.inject.Produces;
2017
import jakarta.inject.Inject;
21-
import java.util.List;
22-
import java.util.Map;
23-
import java.util.Objects;
24-
import java.util.stream.Collectors;
2518
import org.eclipse.microprofile.config.inject.ConfigProperties;
2619
import org.jspecify.annotations.NonNull;
2720

@@ -35,78 +28,16 @@ public class ClientProvider {
3528
@ConfigProperties
3629
private HieroNetworkConfiguration networkConfiguration;
3730

38-
39-
private AccountId getAccountId() {
40-
if (configuration == null) {
41-
throw new IllegalStateException("configuration is null");
42-
}
43-
final String accountId = configuration.getAccountId();
44-
if (accountId == null) {
45-
throw new IllegalStateException("accountId value is null");
46-
}
47-
try {
48-
return AccountId.fromString(accountId);
49-
} catch (Exception e) {
50-
throw new IllegalArgumentException(
51-
"Can not parse 'hedera.newAccountId' property: '" + accountId + "'", e);
52-
}
53-
}
54-
55-
private PrivateKey getPrivateKey() {
56-
if (configuration == null) {
57-
throw new IllegalStateException("configuration is null");
58-
}
59-
final String privateKey = configuration.getPrivateKey();
60-
if (privateKey == null) {
61-
throw new IllegalStateException("privateKey value is null");
62-
}
63-
try {
64-
return PrivateKey.fromString(privateKey);
65-
} catch (Exception e) {
66-
throw new IllegalArgumentException(
67-
"Can not parse 'hedera.privateKey' property: '" + privateKey + "'", e);
68-
}
69-
}
70-
71-
private HederaNetwork getHederaNetwork() {
72-
if (networkConfiguration == null) {
73-
throw new IllegalStateException("network value is null");
74-
}
75-
return networkConfiguration.getName()
76-
.map(n -> HederaNetwork.findByName(n).orElse(HederaNetwork.CUSTOM))
77-
.orElse(HederaNetwork.CUSTOM);
78-
}
79-
80-
private Client createClient() {
81-
final AccountId accountId = getAccountId();
82-
final PrivateKey privateKey = getPrivateKey();
83-
final HederaNetwork hederaNetwork = getHederaNetwork();
84-
if (Objects.equals(HederaNetwork.CUSTOM, hederaNetwork)) {
85-
final Map<String, AccountId> nodes = networkConfiguration.getNodes()
86-
.stream().collect(Collectors.toMap(n -> n.getAddress(), n -> n.getAccountId()));
87-
Client client = Client.forNetwork(nodes);
88-
networkConfiguration.getMirrornode()
89-
.map(mirrorNode -> List.of(mirrorNode))
90-
.ifPresent(mirrorNodes -> {
91-
try {
92-
client.setMirrorNetwork(mirrorNodes);
93-
} catch (InterruptedException e) {
94-
throw new RuntimeException("Error setting mirror network", e);
95-
}
96-
});
97-
client.setOperator(accountId, privateKey);
98-
return client;
99-
} else {
100-
return Client.forName(hederaNetwork.getName())
101-
.setOperator(accountId, privateKey);
102-
}
31+
@Produces
32+
@ApplicationScoped
33+
HieroConfig createHieroConfig() {
34+
return new HieroConfigImpl(configuration, networkConfiguration);
10335
}
10436

10537
@Produces
10638
@ApplicationScoped
107-
ProtocolLayerClient createProtocolLayerClient() {
108-
final Account operator = Account.of(getAccountId(), getPrivateKey());
109-
return new ProtocolLayerClientImpl(createClient(), operator);
39+
ProtocolLayerClient createProtocolLayerClient(@NonNull final HieroConfig hieroConfig) {
40+
return new ProtocolLayerClientImpl(hieroConfig.createClient(), hieroConfig.getOperatorAccount());
11041
}
11142

11243
@Produces
@@ -130,8 +61,7 @@ AccountClient createAccountClient(@NonNull final ProtocolLayerClient protocolLay
13061

13162
@Produces
13263
@ApplicationScoped
133-
ContractVerificationClient createContractVerificationClient(@NonNull final ProtocolLayerClient protocolLayerClient,
134-
@NonNull final FileClient fileClient) {
135-
return new ContractVerificationClientImpl(getHederaNetwork());
64+
ContractVerificationClient createContractVerificationClient(@NonNull final HieroConfig hieroConfig) {
65+
return new ContractVerificationClientImpl(hieroConfig.getNetwork());
13666
}
13767
}

hedera-microprofile/src/main/java/com/openelements/hedera/microprofile/HieroNetworkConfiguration.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.openelements.hedera.microprofile;
22

3-
import com.hedera.hashgraph.sdk.AccountId;
3+
import com.openelements.hedera.base.config.ConsensusNode;
44
import jakarta.enterprise.context.Dependent;
55
import jakarta.inject.Inject;
66
import java.util.Optional;
@@ -14,17 +14,6 @@
1414
@Dependent
1515
public class HieroNetworkConfiguration {
1616

17-
public record Node(String ip, String port, String account) {
18-
19-
String getAddress() {
20-
return ip + ":" + port;
21-
}
22-
23-
AccountId getAccountId() {
24-
return AccountId.fromString(account);
25-
}
26-
}
27-
2817
private Optional<String> name;
2918

3019
@Inject
@@ -42,7 +31,7 @@ public Optional<String> getMirrornode() {
4231
return mirrornode;
4332
}
4433

45-
public Set<Node> getNodes() {
34+
public Set<ConsensusNode> getNodes() {
4635
return nodes.map(n -> n.split(","))
4736
.map(n -> Stream.of(n))
4837
.orElse(Stream.empty())
@@ -55,7 +44,7 @@ public Set<Node> getNodes() {
5544
final String ip = split[0];
5645
final String port = split[1];
5746
final String account = split[2];
58-
return new Node(ip, port, account);
47+
return new ConsensusNode(ip, port, account);
5948
}).collect(Collectors.toUnmodifiableSet());
6049
}
6150
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.openelements.hedera.microprofile.implementation;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.PrivateKey;
5+
import com.openelements.hedera.base.Account;
6+
import com.openelements.hedera.base.config.ConsensusNode;
7+
import com.openelements.hedera.base.config.HieroConfig;
8+
import com.openelements.hedera.base.implementation.HederaNetwork;
9+
import com.openelements.hedera.microprofile.HieroNetworkConfiguration;
10+
import com.openelements.hedera.microprofile.HieroOperatorConfiguration;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Objects;
14+
import java.util.Optional;
15+
import java.util.Set;
16+
import org.jspecify.annotations.NonNull;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
public class HieroConfigImpl implements HieroConfig {
21+
22+
private final static Logger log = LoggerFactory.getLogger(HieroConfigImpl.class);
23+
24+
private final Account operatorAccount;
25+
26+
private final String networkName;
27+
28+
private final List<String> mirrorNodeAddresses;
29+
30+
private final Set<ConsensusNode> consensusNodes;
31+
32+
private final HederaNetwork hederaNetwork;
33+
34+
public HieroConfigImpl(@NonNull final HieroOperatorConfiguration configuration,
35+
@NonNull final HieroNetworkConfiguration networkConfiguration) {
36+
Objects.requireNonNull(configuration, "configuration must not be null");
37+
Objects.requireNonNull(networkConfiguration, "networkConfiguration must not be null");
38+
39+
final AccountId operatorAccountId = AccountId.fromString(configuration.getAccountId());
40+
final PrivateKey operatorPrivateKey = PrivateKey.fromString(configuration.getPrivateKey());
41+
operatorAccount = Account.of(operatorAccountId, operatorPrivateKey);
42+
networkName = networkConfiguration.getName().orElse(null);
43+
mirrorNodeAddresses = networkConfiguration.getMirrornode().map(List::of).orElse(List.of());
44+
consensusNodes = Collections.unmodifiableSet(networkConfiguration.getNodes());
45+
hederaNetwork = HederaNetwork.findByName(networkName)
46+
.orElse(HederaNetwork.CUSTOM);
47+
}
48+
49+
@Override
50+
public @NonNull Account getOperatorAccount() {
51+
return operatorAccount;
52+
}
53+
54+
@Override
55+
public @NonNull Optional<String> getNetworkName() {
56+
return Optional.ofNullable(networkName);
57+
}
58+
59+
@Override
60+
public @NonNull List<String> getMirrornodeAddresses() {
61+
return mirrorNodeAddresses;
62+
}
63+
64+
@Override
65+
public @NonNull Set<ConsensusNode> getConsensusNodes() {
66+
return consensusNodes;
67+
}
68+
69+
@Override
70+
public @NonNull HederaNetwork getNetwork() {
71+
return hederaNetwork;
72+
}
73+
}

0 commit comments

Comments
 (0)