Skip to content

Commit

Permalink
feat: add getCommitteeInfo api
Browse files Browse the repository at this point in the history
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Nov 14, 2022
1 parent a87058c commit 8d51772
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
17 changes: 17 additions & 0 deletions src/integrationTest/java/io/sui/SuiClientImplIntTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -314,4 +315,20 @@ void getEvents() throws ExecutionException, InterruptedException {
CompletableFuture<PaginatedEvents> 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<CommitteeInfoResponse> res = client.getCommitteeInfo(0L);
System.out.println(res.get());
assertEquals(4, res.get().getCommittee_info().size());
CompletableFuture<CommitteeInfoResponse> res1 = client.getCommitteeInfo(null);
assertEquals(4, res1.get().getCommittee_info().size());
}
}
20 changes: 19 additions & 1 deletion src/main/java/io/sui/SuiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,8 +89,25 @@ public interface SuiClient {
* @param end the end
* @return the transactions in range
*/
CompletableFuture<List<String>> getTransactionsInRange(long start, long end);
CompletableFuture<List<String>> 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<PaginatedEvents> getEvents(
EventQuery query, EventId cursor, int limit, boolean isDescOrder);

/**
* Gets committee info.
*
* @param epoch the epoch
* @return the committee info
*/
CompletableFuture<CommitteeInfoResponse> getCommitteeInfo(Long epoch);
}
11 changes: 10 additions & 1 deletion src/main/java/io/sui/SuiClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,7 +99,7 @@ public CompletableFuture<TransactionResponse> getTransaction(String digest) {
}

@Override
public CompletableFuture<List<String>> getTransactionsInRange(long start, long end) {
public CompletableFuture<List<String>> getTransactionsInRange(Long start, Long end) {
final JsonRpc20Request request =
createJsonRpc20Request("sui_getTransactionsInRange", Lists.newArrayList(start, end));
return call("/sui_getTransactionsInRange", request, new TypeToken<List<String>>() {}.getType());
Expand All @@ -113,6 +114,14 @@ public CompletableFuture<PaginatedEvents> getEvents(
return call("/sui_getEvents", request, new TypeToken<PaginatedEvents>() {}.getType());
}

@Override
public CompletableFuture<CommitteeInfoResponse> getCommitteeInfo(Long epoch) {
final JsonRpc20Request request =
createJsonRpc20Request("sui_getCommitteeInfo", Lists.newArrayList(epoch));
return call(
"/sui_getCommitteeInfo", request, new TypeToken<CommitteeInfoResponse>() {}.getType());
}

private JsonRpc20Request createJsonRpc20Request(String method, List<?> params) {
final JsonRpc20Request request = new JsonRpc20Request();
request.setId(jsonRpcClientProvider.nextId());
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/sui/jsonrpc/GsonJsonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -359,6 +360,21 @@ public JsonElement serialize(EventQuery src, Type typeOfSrc, JsonSerializationCo
}
}

/** The type Committee info deserializer. */
public class CommitteeInfoDeserializer implements JsonDeserializer<CommitteeInfo> {

@Override
public CommitteeInfo deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
List<JsonElement> 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. */
Expand Down Expand Up @@ -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();
}

Expand Down
97 changes: 97 additions & 0 deletions src/main/java/io/sui/models/CommitteeInfo.java
Original file line number Diff line number Diff line change
@@ -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
+ '}';
}
}
84 changes: 84 additions & 0 deletions src/main/java/io/sui/models/CommitteeInfoResponse.java
Original file line number Diff line number Diff line change
@@ -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<CommitteeInfo> committee_info;

private Long epoch;

public List<CommitteeInfo> getCommittee_info() {
return committee_info;
}

@SuppressWarnings("checkstyle:ParameterName")
public void setCommittee_info(List<CommitteeInfo> 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 + '}';
}
}
21 changes: 21 additions & 0 deletions src/test/java/io/sui/SuiClientImplTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -447,4 +452,20 @@ void getEvents() throws ExecutionException, InterruptedException {
CompletableFuture<PaginatedEvents> 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<CommitteeInfoResponse> res = client.getCommitteeInfo(1L);
System.out.println(res.get());
assertEquals(4, res.get().getCommittee_info().size());
CompletableFuture<CommitteeInfoResponse> res1 = client.getCommitteeInfo(null);
assertEquals(4, res1.get().getCommittee_info().size());
}
}
25 changes: 25 additions & 0 deletions src/test/resources/mockdata/getCommitteeInfo.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8d51772

Please sign in to comment.