From ba326f081b97c37218fbb7c9d409d4fa46e715a5 Mon Sep 17 00:00:00 2001 From: grapebaba <281165273@qq.com> Date: Mon, 14 Nov 2022 17:19:23 +0800 Subject: [PATCH 1/2] feat: add getCommitteeInfo api Signed-off-by: grapebaba <281165273@qq.com> --- build.gradle | 2 + .../java/io/sui/SuiClientImplIntTests.java | 17 ++++ src/main/java/io/sui/SuiClient.java | 20 +++- src/main/java/io/sui/SuiClientImpl.java | 11 ++- .../java/io/sui/jsonrpc/GsonJsonHandler.java | 17 ++++ .../java/io/sui/models/CommitteeInfo.java | 97 +++++++++++++++++++ .../io/sui/models/CommitteeInfoResponse.java | 84 ++++++++++++++++ src/test/java/io/sui/SuiClientImplTests.java | 21 ++++ .../resources/mockdata/getCommitteeInfo.json | 25 +++++ 9 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/sui/models/CommitteeInfo.java create mode 100644 src/main/java/io/sui/models/CommitteeInfoResponse.java create mode 100644 src/test/resources/mockdata/getCommitteeInfo.json diff --git a/build.gradle b/build.gradle index a4b243b..a57e7b6 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,8 @@ dependencies { implementation 'com.google.code.gson:gson:2.10' // https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp implementation 'com.squareup.okhttp3:okhttp:4.10.0' + // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 + implementation 'org.apache.commons:commons-lang3:3.12.0' // https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver testImplementation 'com.squareup.okhttp3:mockwebserver:4.10.0' diff --git a/src/integrationTest/java/io/sui/SuiClientImplIntTests.java b/src/integrationTest/java/io/sui/SuiClientImplIntTests.java index a1cdabd..a54698a 100644 --- a/src/integrationTest/java/io/sui/SuiClientImplIntTests.java +++ b/src/integrationTest/java/io/sui/SuiClientImplIntTests.java @@ -23,6 +23,7 @@ import io.sui.jsonrpc.JsonRpc20Response.Error.ErrorCode; import io.sui.jsonrpc.JsonRpcClientProvider; import io.sui.jsonrpc.OkHttpJsonRpcClientProvider; +import io.sui.models.CommitteeInfoResponse; import io.sui.models.SuiApiException; import io.sui.models.events.EventQuery.TransactionEventQuery; import io.sui.models.events.PaginatedEvents; @@ -314,4 +315,20 @@ void getEvents() throws ExecutionException, InterruptedException { CompletableFuture res = client.getEvents(query, null, 1, false); System.out.println(res.get()); } + + /** + * Gets committee info. + * + * @throws ExecutionException the execution exception + * @throws InterruptedException the interrupted exception + */ + @Test + @DisplayName("Test getCommitteeInfo.") + void getCommitteeInfo() throws ExecutionException, InterruptedException { + CompletableFuture res = client.getCommitteeInfo(0L); + System.out.println(res.get()); + assertEquals(4, res.get().getCommittee_info().size()); + CompletableFuture res1 = client.getCommitteeInfo(null); + assertEquals(4, res1.get().getCommittee_info().size()); + } } diff --git a/src/main/java/io/sui/SuiClient.java b/src/main/java/io/sui/SuiClient.java index eae773d..bf17c97 100644 --- a/src/main/java/io/sui/SuiClient.java +++ b/src/main/java/io/sui/SuiClient.java @@ -17,6 +17,7 @@ package io.sui; +import io.sui.models.CommitteeInfoResponse; import io.sui.models.events.EventId; import io.sui.models.events.EventQuery; import io.sui.models.events.PaginatedEvents; @@ -88,8 +89,25 @@ public interface SuiClient { * @param end the end * @return the transactions in range */ - CompletableFuture> getTransactionsInRange(long start, long end); + CompletableFuture> getTransactionsInRange(Long start, Long end); + /** + * Gets events. + * + * @param query the query + * @param cursor the cursor + * @param limit the limit + * @param isDescOrder the is desc order + * @return the events + */ CompletableFuture getEvents( EventQuery query, EventId cursor, int limit, boolean isDescOrder); + + /** + * Gets committee info. + * + * @param epoch the epoch + * @return the committee info + */ + CompletableFuture getCommitteeInfo(Long epoch); } diff --git a/src/main/java/io/sui/SuiClientImpl.java b/src/main/java/io/sui/SuiClientImpl.java index e338668..f520a48 100644 --- a/src/main/java/io/sui/SuiClientImpl.java +++ b/src/main/java/io/sui/SuiClientImpl.java @@ -21,6 +21,7 @@ import com.google.common.reflect.TypeToken; import io.sui.jsonrpc.JsonRpc20Request; import io.sui.jsonrpc.JsonRpcClientProvider; +import io.sui.models.CommitteeInfoResponse; import io.sui.models.SuiApiException; import io.sui.models.events.EventId; import io.sui.models.events.EventQuery; @@ -98,7 +99,7 @@ public CompletableFuture getTransaction(String digest) { } @Override - public CompletableFuture> getTransactionsInRange(long start, long end) { + public CompletableFuture> getTransactionsInRange(Long start, Long end) { final JsonRpc20Request request = createJsonRpc20Request("sui_getTransactionsInRange", Lists.newArrayList(start, end)); return call("/sui_getTransactionsInRange", request, new TypeToken>() {}.getType()); @@ -113,6 +114,14 @@ public CompletableFuture getEvents( return call("/sui_getEvents", request, new TypeToken() {}.getType()); } + @Override + public CompletableFuture getCommitteeInfo(Long epoch) { + final JsonRpc20Request request = + createJsonRpc20Request("sui_getCommitteeInfo", Lists.newArrayList(epoch)); + return call( + "/sui_getCommitteeInfo", request, new TypeToken() {}.getType()); + } + private JsonRpc20Request createJsonRpc20Request(String method, List params) { final JsonRpc20Request request = new JsonRpc20Request(); request.setId(jsonRpcClientProvider.nextId()); diff --git a/src/main/java/io/sui/jsonrpc/GsonJsonHandler.java b/src/main/java/io/sui/jsonrpc/GsonJsonHandler.java index d92d670..c184a2d 100644 --- a/src/main/java/io/sui/jsonrpc/GsonJsonHandler.java +++ b/src/main/java/io/sui/jsonrpc/GsonJsonHandler.java @@ -30,6 +30,7 @@ import com.google.gson.JsonSerializer; import com.google.gson.ToNumberPolicy; import com.google.gson.reflect.TypeToken; +import io.sui.models.CommitteeInfo; import io.sui.models.events.EventKind; import io.sui.models.events.EventQuery; import io.sui.models.events.EventQuery.AllQuery; @@ -359,6 +360,21 @@ public JsonElement serialize(EventQuery src, Type typeOfSrc, JsonSerializationCo } } + /** The type Committee info deserializer. */ + public class CommitteeInfoDeserializer implements JsonDeserializer { + + @Override + public CommitteeInfo deserialize( + JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + List committeeInfoStr = json.getAsJsonArray().asList(); + CommitteeInfo committeeInfo = new CommitteeInfo(); + committeeInfo.setAuthorityName(committeeInfoStr.get(0).getAsString()); + committeeInfo.setStakeUnit(committeeInfoStr.get(1).getAsLong()); + return committeeInfo; + } + } + private final Gson gson; /** Instantiates a new Gson json handler. */ @@ -386,6 +402,7 @@ ParsedPublishResponse.class, new ParsedPublishResponseDeserializer()) AuthorityQuorumSignInfo.class, new AuthorityQuorumSignInfoDeserializer()) .registerTypeAdapter(MoveModule.class, new MoveModuleSerializer()) .registerTypeAdapter(EventQuery.class, new EventQuerySerializer()) + .registerTypeAdapter(CommitteeInfo.class, new CommitteeInfoDeserializer()) .create(); } diff --git a/src/main/java/io/sui/models/CommitteeInfo.java b/src/main/java/io/sui/models/CommitteeInfo.java new file mode 100644 index 0000000..091498b --- /dev/null +++ b/src/main/java/io/sui/models/CommitteeInfo.java @@ -0,0 +1,97 @@ +/* + * Copyright 2022 281165273grape@gmail.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package io.sui.models; + + +import java.util.Objects; + +/** + * The type Committee info. + * + * @author grapebaba + * @since 2022.11 + */ +public class CommitteeInfo { + + private String authorityName; + + private Long stakeUnit; + + /** + * Gets authority name. + * + * @return the authority name + */ + public String getAuthorityName() { + return authorityName; + } + + /** + * Sets authority name. + * + * @param authorityName the authority name + */ + public void setAuthorityName(String authorityName) { + this.authorityName = authorityName; + } + + /** + * Gets stake unit. + * + * @return the stake unit + */ + public Long getStakeUnit() { + return stakeUnit; + } + + /** + * Sets stake unit. + * + * @param stakeUnit the stake unit + */ + public void setStakeUnit(Long stakeUnit) { + this.stakeUnit = stakeUnit; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CommitteeInfo)) { + return false; + } + CommitteeInfo that = (CommitteeInfo) o; + return authorityName.equals(that.authorityName) && stakeUnit.equals(that.stakeUnit); + } + + @Override + public int hashCode() { + return Objects.hash(authorityName, stakeUnit); + } + + @Override + public String toString() { + return "CommitteeInfo{" + + "authorityName='" + + authorityName + + '\'' + + ", stakeUnit=" + + stakeUnit + + '}'; + } +} diff --git a/src/main/java/io/sui/models/CommitteeInfoResponse.java b/src/main/java/io/sui/models/CommitteeInfoResponse.java new file mode 100644 index 0000000..eb5b088 --- /dev/null +++ b/src/main/java/io/sui/models/CommitteeInfoResponse.java @@ -0,0 +1,84 @@ +/* + * Copyright 2022 281165273grape@gmail.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package io.sui.models; + + +import java.util.List; +import java.util.Objects; + +/** + * The type Committee info response. + * + * @author grapebaba + * @since 2022.11 + */ +public class CommitteeInfoResponse { + + @SuppressWarnings("checkstyle:MemberName") + private List committee_info; + + private Long epoch; + + public List getCommittee_info() { + return committee_info; + } + + @SuppressWarnings("checkstyle:ParameterName") + public void setCommittee_info(List committee_info) { + this.committee_info = committee_info; + } + + /** + * Gets epoch. + * + * @return the epoch + */ + public Long getEpoch() { + return epoch; + } + + /** + * Sets epoch. + * + * @param epoch the epoch + */ + public void setEpoch(Long epoch) { + this.epoch = epoch; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CommitteeInfoResponse)) { + return false; + } + CommitteeInfoResponse that = (CommitteeInfoResponse) o; + return committee_info.equals(that.committee_info) && epoch.equals(that.epoch); + } + + @Override + public int hashCode() { + return Objects.hash(committee_info, epoch); + } + + @Override + public String toString() { + return "CommitteeInfoResponse{" + "committee_info=" + committee_info + ", epoch=" + epoch + '}'; + } +} diff --git a/src/test/java/io/sui/SuiClientImplTests.java b/src/test/java/io/sui/SuiClientImplTests.java index 31e57be..f074db7 100644 --- a/src/test/java/io/sui/SuiClientImplTests.java +++ b/src/test/java/io/sui/SuiClientImplTests.java @@ -26,6 +26,7 @@ import io.sui.jsonrpc.JsonRpc20Response.Error.ErrorCode; import io.sui.jsonrpc.JsonRpcClientProvider; import io.sui.jsonrpc.OkHttpJsonRpcClientProvider; +import io.sui.models.CommitteeInfoResponse; import io.sui.models.SuiApiException; import io.sui.models.events.CoinBalanceChangeEvent; import io.sui.models.events.CoinBalanceChangeEvent.BalanceChangeType; @@ -167,6 +168,10 @@ public MockResponse dispatch(RecordedRequest request) { return getMockResponse("mockdata/getEvents.json"); } + if ("/sui_getCommitteeInfo".equals(request.getPath())) { + return getMockResponse("mockdata/getCommitteeInfo.json"); + } + return new MockResponse().setResponseCode(404); } }; @@ -447,4 +452,20 @@ void getEvents() throws ExecutionException, InterruptedException { CompletableFuture res = client.getEvents(query, null, 1, false); System.out.println(res.get()); } + + /** + * Gets committee info. + * + * @throws ExecutionException the execution exception + * @throws InterruptedException the interrupted exception + */ + @Test + @DisplayName("Test getCommitteeInfo.") + void getCommitteeInfo() throws ExecutionException, InterruptedException { + CompletableFuture res = client.getCommitteeInfo(1L); + System.out.println(res.get()); + assertEquals(4, res.get().getCommittee_info().size()); + CompletableFuture res1 = client.getCommitteeInfo(null); + assertEquals(4, res1.get().getCommittee_info().size()); + } } diff --git a/src/test/resources/mockdata/getCommitteeInfo.json b/src/test/resources/mockdata/getCommitteeInfo.json new file mode 100644 index 0000000..19f2119 --- /dev/null +++ b/src/test/resources/mockdata/getCommitteeInfo.json @@ -0,0 +1,25 @@ +{ + "jsonrpc": "2.0", + "result": { + "epoch": 0, + "committee_info": [ + [ + "jeDRw8vxm2a0n+aH0KOpZRUt+O96ydex7xuZ3UZU9Je1WhWtJu/j80cqeB6BouJOANhnrGCksJ0TmtvoQaPnsgX1uR1lWes984wLrSoSbqczUVSct8FmaM9UcHTQgMzV", + 100000000000000 + ], + [ + "jp2Cc9GOkbhF+kqgQ4ndXheLmVvVCfWmnDU1qqVimEZaHJaZvX23mbrIy0IBRrXqAn0YKDCas4WGTkjce9yWD4Ol7SvaYAK2Hd8JrF9+VlVhtx248qLxRWtN2BCT+AVn", + 100000000000000 + ], + [ + "rRATLWm9e1cG0cSGZNjOKaOcPG3Sqtw+JJnNYzFnmayzdnlejieFihS8ihTR0W1QEddxw0joB+2P8ZpRwKH7UNAWkeJXiEONTSq3VSQjKwjoH91/FPHoGIMmRf3WvC7Z", + 100000000000000 + ], + [ + "uGAzcF47LG7QB7H7XbHagmaywwgTLk7KkixYCKYagXLDk39uvLoULwJ//+3/l3nLGQcZVZ4iEjocmK8qzW6AnBcV6PbDPstSfrA2kv0UUCA71jBaKcHUqqime7G7PouY", + 100000000000000 + ] + ] + }, + "id": 1 +} \ No newline at end of file From fd9d7b628159c7d61d45f69bd4ff62cae907bafb Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 09:40:12 +0000 Subject: [PATCH 2/2] commit badge --- .github/badges/jacoco.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/badges/jacoco.svg b/.github/badges/jacoco.svg index 5f33b57..655bf64 100644 --- a/.github/badges/jacoco.svg +++ b/.github/badges/jacoco.svg @@ -1 +1 @@ -coverage63.7% \ No newline at end of file +coverage65.2% \ No newline at end of file