Skip to content

Commit e8cef42

Browse files
authored
feat(TCK): AddressBookQuery implementation (#2610)
Signed-off-by: Mustafa Uzun <mustafa.uzun@limechain.tech>
1 parent ec7cd91 commit e8cef42

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

tck/src/main/java/com/hedera/hashgraph/tck/methods/sdk/NodeService.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
import com.hedera.hashgraph.tck.annotation.JSONRPC2Method;
77
import com.hedera.hashgraph.tck.annotation.JSONRPC2Service;
88
import com.hedera.hashgraph.tck.methods.AbstractJSONRPC2Service;
9+
import com.hedera.hashgraph.tck.methods.sdk.param.node.AddressBookQueryParams;
910
import com.hedera.hashgraph.tck.methods.sdk.param.node.NodeCreateParams;
1011
import com.hedera.hashgraph.tck.methods.sdk.param.node.NodeDeleteParams;
1112
import com.hedera.hashgraph.tck.methods.sdk.param.node.NodeUpdateParams;
1213
import com.hedera.hashgraph.tck.methods.sdk.param.node.ServiceEndpointParams;
14+
import com.hedera.hashgraph.tck.methods.sdk.response.AddressBookResponse;
1315
import com.hedera.hashgraph.tck.methods.sdk.response.NodeResponse;
1416
import com.hedera.hashgraph.tck.util.KeyUtils;
1517
import java.time.Duration;
1618
import java.util.ArrayList;
1719
import java.util.List;
20+
import java.util.stream.Collectors;
1821
import org.bouncycastle.util.encoders.Hex;
1922

2023
@JSONRPC2Service
@@ -26,6 +29,35 @@ public NodeService(SdkService sdkService) {
2629
this.sdkService = sdkService;
2730
}
2831

32+
@JSONRPC2Method("getAddressBook")
33+
public AddressBookResponse addressBookQuery(final AddressBookQueryParams params) {
34+
AddressBookQuery query = new AddressBookQuery().setFileId(FileId.fromString(params.getFileId()));
35+
Client client = sdkService.getClient(params.getSessionId());
36+
37+
NodeAddressBook addressBook = query.execute(client);
38+
AddressBookResponse response = new AddressBookResponse();
39+
40+
addressBook.getNodeAddresses().forEach(address -> {
41+
List<AddressBookResponse.Endpoint> mappedEndpoints = address.getAddresses().stream()
42+
.map(sdkEndpoint -> new AddressBookResponse.Endpoint(
43+
sdkEndpoint.getAddress() != null ? Hex.toHexString(sdkEndpoint.getAddress()) : null,
44+
sdkEndpoint.getPort(),
45+
sdkEndpoint.getDomainName() != null ? sdkEndpoint.getDomainName() : ""))
46+
.collect(Collectors.toList());
47+
48+
response.addNodeAddress(new AddressBookResponse.NodeAddress(
49+
address.getPublicKey() != null ? address.getPublicKey() : "",
50+
address.getAccountId() != null ? address.getAccountId().toString() : "",
51+
address.getNodeId(),
52+
address.getCertHash() != null ? address.getCertHash().toString() : "",
53+
mappedEndpoints,
54+
address.getDescription(),
55+
address.getStake()));
56+
});
57+
58+
return response;
59+
}
60+
2961
@JSONRPC2Method("createNode")
3062
public NodeResponse createNode(final NodeCreateParams params) throws Exception {
3163
NodeCreateTransaction tx = new NodeCreateTransaction().setGrpcDeadline(DEFAULT_GRPC_DEADLINE);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.param.node;
3+
4+
import com.hedera.hashgraph.tck.methods.JSONRPC2Param;
5+
import com.hedera.hashgraph.tck.util.JSONRPCParamParser;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
12+
@Getter
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
public class AddressBookQueryParams extends JSONRPC2Param {
16+
private String fileId;
17+
private Long limit;
18+
private String sessionId;
19+
20+
@Override
21+
public JSONRPC2Param parse(Map<String, Object> jrpcParams) throws Exception {
22+
Objects.requireNonNull(jrpcParams, "jrpcParams must not be null");
23+
24+
var parsedFileId = (String) jrpcParams.get("fileId");
25+
var parsedLimit = (Long) jrpcParams.get("limit");
26+
27+
return new AddressBookQueryParams(parsedFileId, parsedLimit, JSONRPCParamParser.parseSessionId(jrpcParams));
28+
}
29+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.hashgraph.tck.methods.sdk.response;
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Objects;
7+
import javax.annotation.Nullable;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Data;
10+
import lombok.Getter;
11+
12+
@Data
13+
public class AddressBookResponse {
14+
private final List<NodeAddress> nodeAddresses = new ArrayList<>();
15+
16+
@Getter
17+
@AllArgsConstructor
18+
public static class NodeAddress {
19+
/**
20+
* The RSA public key of the node.
21+
*/
22+
@Nullable
23+
String publicKey;
24+
/**
25+
* The account to be paid for queries and transactions sent to this node.
26+
*/
27+
@Nullable
28+
String accountId;
29+
/**
30+
* A non-sequential identifier for the node.
31+
*/
32+
long nodeId;
33+
/**
34+
* A hash of the X509 cert used for gRPC traffic to this node.
35+
*/
36+
@Nullable
37+
String certHash;
38+
/**
39+
* A node's service IP addresses and ports.
40+
*/
41+
List<Endpoint> serviceEndpoints;
42+
/**
43+
* A description of the node, with UTF-8 encoding up to 100 bytes.
44+
*/
45+
@Nullable
46+
String description;
47+
/**
48+
* The amount of tinybars staked to the node.
49+
*/
50+
long stake;
51+
}
52+
53+
@Getter
54+
@AllArgsConstructor
55+
public static class Endpoint {
56+
@Nullable
57+
String address;
58+
59+
int port;
60+
61+
String domainName;
62+
}
63+
64+
public void addNodeAddress(NodeAddress nodeAddress) {
65+
Objects.requireNonNull(nodeAddress, "nodeAddress must not be null");
66+
nodeAddresses.add(nodeAddress);
67+
}
68+
}

0 commit comments

Comments
 (0)