Skip to content

Commit

Permalink
feat: add support for Soroban PRC's getVersionInfo API. (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Oct 13, 2024
1 parent b7b15ef commit e0220db
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ continued engagement as we shape the future of this project together.
- chore: Display the original definition in the XDR class documentation.
- chore: add some examples, you can find them in the `examples` directory.
- chore: bump dependencies.
- feat: add support for Soroban PRC's `getVersionInfo` API.

### Breaking changes
- refactor!: Refactored the handling of exceptions.
Expand Down
47 changes: 36 additions & 11 deletions src/main/java/org/stellar/sdk/SorobanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetVersionInfoResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SorobanRpcResponse;
Expand Down Expand Up @@ -144,8 +145,8 @@ public TransactionBuilderAccount getAccount(String address) {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getHealth" target="_blank">getHealth
* documentation</a>
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getHealth"
* target="_blank">getHealth documentation</a>
*/
public GetHealthResponse getHealth() {
return this.<Void, GetHealthResponse>sendRequest(
Expand Down Expand Up @@ -240,7 +241,8 @@ public Optional<GetLedgerEntriesResponse.LedgerEntryResult> getContractData(
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getLedgerEntries"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLedgerEntries"
* target="_blank">getLedgerEntries documentation</a>
*/
public GetLedgerEntriesResponse getLedgerEntries(Collection<LedgerKey> keys) {
Expand Down Expand Up @@ -280,7 +282,8 @@ public GetLedgerEntriesResponse getLedgerEntries(Collection<LedgerKey> keys) {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getTransaction"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransaction"
* target="_blank">getTransaction documentation</a>
*/
public GetTransactionResponse getTransaction(String hash) {
Expand Down Expand Up @@ -325,14 +328,33 @@ public GetTransactionsResponse getTransactions(GetTransactionsRequest getTransac
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getEvents" target="_blank">getEvents
* documentation</a>
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getEvents"
* target="_blank">getEvents documentation</a>
*/
public GetEventsResponse getEvents(GetEventsRequest getEventsRequest) {
return this.sendRequest(
"getEvents", getEventsRequest, new TypeToken<SorobanRpcResponse<GetEventsResponse>>() {});
}

/**
* Fetches version information about the RPC and Captive core.
*
* @return A {@link GetVersionInfoResponse} object containing the version information.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
* @throws SorobanRpcException If the Soroban-RPC instance returns an error response.
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getVersionInfo"
* target="_blank">getVersionInfo documentation</a>
*/
public GetVersionInfoResponse getVersionInfo() {
return this.<Void, GetVersionInfoResponse>sendRequest(
"getVersionInfo", null, new TypeToken<SorobanRpcResponse<GetVersionInfoResponse>>() {});
}

/**
* Fetches metadata about the network which Soroban-RPC is connected to.
*
Expand All @@ -343,8 +365,8 @@ public GetEventsResponse getEvents(GetEventsRequest getEventsRequest) {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getNetwork" target="_blank">getNetwork
* documentation</a>
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getNetwork"
* target="_blank">getNetwork documentation</a>
*/
public GetNetworkResponse getNetwork() {
return this.<Void, GetNetworkResponse>sendRequest(
Expand All @@ -361,7 +383,8 @@ public GetNetworkResponse getNetwork() {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/getLatestLedger"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLatestLedger"
* target="_blank">getLatestLedger documentation</a>
*/
public GetLatestLedgerResponse getLatestLedger() {
Expand All @@ -387,7 +410,8 @@ public GetLatestLedgerResponse getLatestLedger() {
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/simulateTransaction"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/simulateTransaction"
* target="_blank">simulateTransaction documentation</a>
*/
public SimulateTransactionResponse simulateTransaction(
Expand Down Expand Up @@ -500,7 +524,8 @@ public Transaction prepareTransaction(
* @throws RequestTimeoutException If the request timed out.
* @throws ConnectionErrorException When the request cannot be executed due to cancellation or
* connectivity problems, etc.
* @see <a href="https://soroban.stellar.org/api/methods/sendTransaction"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/sendTransaction"
* target="_blank">sendTransaction documentation</a>
*/
public SendTransactionResponse sendTransaction(Transaction transaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Request for JSON-RPC method getEvents.
*
* @see <a href="https://soroban.stellar.org/api/methods/getEvents#parameters"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getEvents"
* target="_blank">getEvents documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
/**
* Request for JSON-RPC method getLedgerEntries.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLedgerEntries#parameters"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLedgerEntries"
* target="_blank">getLedgerEntries documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Request for JSON-RPC method getTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/getTransaction#parameters"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransaction"
* target="_blank">getTransaction documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Request for JSON-RPC method sendTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/sendTransaction#parameters"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/sendTransaction"
* target="_blank">sendTransaction documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/**
* Request for JSON-RPC method simulateTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/simulateTransaction#parameters"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/simulateTransaction"
* target="_blank">simulateTransaction documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Response for JSON-RPC method getEvents.
*
* @see <a href="https://soroban.stellar.org/api/methods/getEvents#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getEvents"
* target="_blank">getEvents documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Response for JSON-RPC method getHealth.
*
* @see <a href="https://soroban.stellar.org/api/methods/getHealth#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getHealth"
* target="_blank">getHealth documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Response for JSON-RPC method getLatestLedger.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLatestLedger#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLatestLedger"
* target="_blank">getLatestLedger documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/**
* Response for JSON-RPC method getLedgerEntries.
*
* @see <a href="https://soroban.stellar.org/api/methods/getLedgerEntries#returns"
* @see <a
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLedgerEntries"
* target="_blank">getLedgerEntries documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Response for JSON-RPC method getNetwork.
*
* @see <a href="https://soroban.stellar.org/api/methods/getNetwork#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getNetwork"
* target="_blank">getNetwork documentation</a>
*/
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Response for JSON-RPC method getTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/getTransaction#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransaction"
* target="_blank">getTransaction documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.stellar.sdk.responses.sorobanrpc;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Response for JSON-RPC method getNetwork.
*
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/getVersionInfo"
* target="_blank">getNetwork documentation</a>
*/
@AllArgsConstructor
@Value
public class GetVersionInfoResponse {
String version;

String commitHash;

String buildTimestamp;

String captiveCoreVersion;

Integer protocolVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Response for JSON-RPC method sendTransaction.
*
* @see <a href="https://soroban.stellar.org/api/methods/sendTransaction#returns"
* @see <a href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/sendTransaction"
* target="_blank">sendTransaction documentation</a>
*/
@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* restore operation needed.
*
* <p>Please refer to the latest <a
* href="https://soroban.stellar.org/api/methods/simulateTransaction#returns"
* href="https://developers.stellar.org/docs/data/rpc/api-reference/methods/simulateTransaction"
* target="_blank">Soroban simulateTransaction documentation</a> for details on which members of the
* simulation response model are keyed to each type of response.
*/
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/stellar/sdk/SorobanServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.stellar.sdk.responses.sorobanrpc.GetLedgerEntriesResponse;
import org.stellar.sdk.responses.sorobanrpc.GetNetworkResponse;
import org.stellar.sdk.responses.sorobanrpc.GetTransactionsResponse;
import org.stellar.sdk.responses.sorobanrpc.GetVersionInfoResponse;
import org.stellar.sdk.responses.sorobanrpc.SendTransactionResponse;
import org.stellar.sdk.responses.sorobanrpc.SimulateTransactionResponse;
import org.stellar.sdk.xdr.ContractDataDurability;
Expand Down Expand Up @@ -698,6 +699,45 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest)
mockWebServer.close();
}

@Test
public void testGetVersionInfo() throws IOException, SorobanRpcException {
String filePath = "src/test/resources/soroban_server/get_version_info.json";
String json = new String(Files.readAllBytes(Paths.get(filePath)));
MockWebServer mockWebServer = new MockWebServer();
Dispatcher dispatcher =
new Dispatcher() {
@NotNull
@Override
public MockResponse dispatch(@NotNull RecordedRequest recordedRequest)
throws InterruptedException {
SorobanRpcRequest<Void> sorobanRpcRequest =
gson.fromJson(
recordedRequest.getBody().readUtf8(),
new TypeToken<SorobanRpcRequest<Void>>() {}.getType());
if ("POST".equals(recordedRequest.getMethod())
&& sorobanRpcRequest.getMethod().equals("getVersionInfo")) {
return new MockResponse().setResponseCode(200).setBody(json);
}
return new MockResponse().setResponseCode(404);
}
};
mockWebServer.setDispatcher(dispatcher);
mockWebServer.start();

HttpUrl baseUrl = mockWebServer.url("");
SorobanServer server = new SorobanServer(baseUrl.toString());
GetVersionInfoResponse resp = server.getVersionInfo();
assertEquals(resp.getVersion(), "21.1.0");
assertEquals(resp.getCommitHash(), "fcd2f0523f04279bae4502f3e3fa00ca627e6f6a");
assertEquals(resp.getBuildTimestamp(), "2024-05-10T11:18:38");
assertEquals(
resp.getCaptiveCoreVersion(),
"stellar-core 21.0.0.rc2 (c6f474133738ae5f6d11b07963ca841909210273)");
assertEquals(resp.getProtocolVersion().intValue(), 21);
server.close();
mockWebServer.close();
}

@Test
public void testGetLatestLedger() throws IOException, SorobanRpcException {
String filePath = "src/test/resources/soroban_server/get_latest_ledger.json";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.stellar.sdk.responses.sorobanrpc;

import static org.junit.Assert.assertEquals;

import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Test;
import org.stellar.sdk.responses.gson.GsonSingleton;

public class GetVersionInfoDeserializerTest {
@Test
public void testDeserialize() throws IOException {
String filePath = "src/test/resources/responses/sorobanrpc/get_version_info.json";
String json = new String(Files.readAllBytes(Paths.get(filePath)));
SorobanRpcResponse<GetVersionInfoResponse> getVersionInfoResponse =
GsonSingleton.getInstance()
.fromJson(
json, new TypeToken<SorobanRpcResponse<GetVersionInfoResponse>>() {}.getType());
assertEquals(getVersionInfoResponse.getResult().getVersion(), "21.1.0");
assertEquals(
getVersionInfoResponse.getResult().getCommitHash(),
"fcd2f0523f04279bae4502f3e3fa00ca627e6f6a");
assertEquals(getVersionInfoResponse.getResult().getBuildTimestamp(), "2024-05-10T11:18:38");
assertEquals(
getVersionInfoResponse.getResult().getCaptiveCoreVersion(),
"stellar-core 21.0.0.rc2 (c6f474133738ae5f6d11b07963ca841909210273)");
assertEquals(getVersionInfoResponse.getResult().getProtocolVersion().intValue(), 21);
}
}
11 changes: 11 additions & 0 deletions src/test/resources/responses/sorobanrpc/get_version_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsonrpc": "2.0",
"id": "198cb1a8-9104-4446-a269-88bf000c2721",
"result": {
"version": "21.1.0",
"commitHash": "fcd2f0523f04279bae4502f3e3fa00ca627e6f6a",
"buildTimestamp": "2024-05-10T11:18:38",
"captiveCoreVersion": "stellar-core 21.0.0.rc2 (c6f474133738ae5f6d11b07963ca841909210273)",
"protocolVersion": 21
}
}
11 changes: 11 additions & 0 deletions src/test/resources/soroban_server/get_version_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsonrpc": "2.0",
"id": "198cb1a8-9104-4446-a269-88bf000c2721",
"result": {
"version": "21.1.0",
"commitHash": "fcd2f0523f04279bae4502f3e3fa00ca627e6f6a",
"buildTimestamp": "2024-05-10T11:18:38",
"captiveCoreVersion": "stellar-core 21.0.0.rc2 (c6f474133738ae5f6d11b07963ca841909210273)",
"protocolVersion": 21
}
}

0 comments on commit e0220db

Please sign in to comment.