Skip to content

Commit

Permalink
Merge pull request #60 from starcoinorg/fix_json_rpc_parse
Browse files Browse the repository at this point in the history
fix chain.get_block_txn_infos getObjectArray error.
  • Loading branch information
welbon authored Jul 1, 2024
2 parents 9455366 + fca0351 commit e5d393d
Show file tree
Hide file tree
Showing 38 changed files with 65 additions and 173 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Home page: [Home page](https://starcoinorg.github.io/starcoin-java/)
<dependency>
<groupId>org.starcoin</groupId>
<artifactId>sdk</artifactId>
<version>1.1.28</version>
<version>1.2.9</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion docs/index-files/index-7.html
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ <h2 class="title" id="I:G">G</h2>
<dd>
<div class="block">用于获取 STC 的市场容量</div>
</dd>
<dt><span class="member-name-link"><a href="../org/starcoin/api/TokenContractRPCClient.html#getSTCTreasurBalance()">getSTCTreasurBalance()</a></span> - 类中的方法 org.starcoin.api.<a href="../org/starcoin/api/TokenContractRPCClient.html" title="org.starcoin.api中的类">TokenContractRPCClient</a></dt>
<dt><span class="member-name-link"><a href="../org/starcoin/api/TokenContractRPCClient.html#getSTCTreasuryBalance()">getSTCTreasuryBalance()</a></span> - 类中的方法 org.starcoin.api.<a href="../org/starcoin/api/TokenContractRPCClient.html" title="org.starcoin.api中的类">TokenContractRPCClient</a></dt>
<dd>
<div class="block">用于获取 STC 在国库中的数量</div>
</dd>
Expand Down
2 changes: 1 addition & 1 deletion docs/member-search-index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/org/starcoin/api/TokenContractRPCClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ <h2>方法概要</h2>
<div class="block">用于获取 STC 的市场容量</div>
</div>
<div class="col-first even-row-color method-summary-table-tab2 method-summary-table-tab4 method-summary-table"><code><a href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/math/BigInteger.html" title="java.math中的类或接口" class="external-link">BigInteger</a></code></div>
<div class="col-second even-row-color method-summary-table-tab2 method-summary-table-tab4 method-summary-table"><code><span class="member-name-link"><a href="#getSTCTreasurBalance()">getSTCTreasurBalance</a></span>()</code></div>
<div class="col-second even-row-color method-summary-table-tab2 method-summary-table-tab4 method-summary-table"><code><span class="member-name-link"><a href="#getSTCTreasuryBalance()">getSTCTreasuryBalance</a></span>()</code></div>
<div class="col-last even-row-color method-summary-table-tab2 method-summary-table-tab4 method-summary-table">
<div class="block">用于获取 STC 在国库中的数量</div>
</div>
Expand Down Expand Up @@ -288,9 +288,9 @@ <h3>getSTCMarketCap</h3>
</section>
</li>
<li>
<section class="detail" id="getSTCTreasurBalance()">
<h3>getSTCTreasurBalance</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/math/BigInteger.html" title="java.math中的类或接口" class="external-link">BigInteger</a></span>&nbsp;<span class="element-name">getSTCTreasurBalance</span>()
<section class="detail" id="getSTCTreasuryBalance()">
<h3>getSTCTreasuryBalance</h3>
<div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/math/BigInteger.html" title="java.math中的类或接口" class="external-link">BigInteger</a></span>&nbsp;<span class="element-name">getSTCTreasuryBalance</span>()
throws <span class="exceptions"><a href="../jsonrpc/client/JSONRPC2SessionException.html" title="org.starcoin.jsonrpc.client中的类">JSONRPC2SessionException</a></span></div>
<div class="block">用于获取 STC 在国库中的数量</div>
<dl class="notes">
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.starcoin</groupId>
<artifactId>sdk</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>

<name>Starcoin Java SDK</name>
<description>Starcoin Java SDK</description>
Expand Down Expand Up @@ -56,6 +56,11 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>jsonrpc2-client</artifactId>
<version>1.16.5</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/starcoin/api/BlockRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package org.starcoin.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.starcoin.bean.Block;
import org.starcoin.bean.BlockGhostdagData;
import org.starcoin.bean.BlockHeader;
import org.starcoin.bean.BlockInfo;
import org.starcoin.jsonrpc.client.JSONRPC2Session;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;

import java.net.URL;
import java.util.ArrayList;
Expand All @@ -48,7 +47,7 @@ public BlockRPCClient(URL baseUrl) {
* 获取当前主链的block header
*/
@SuppressWarnings("unchecked")
public BlockHeader getChainHeader() throws JSONRPC2SessionException, JsonProcessingException {
public BlockHeader getChainHeader() throws JSONRPC2SessionException {
JsonRPCClient<BlockHeader> client = new JsonRPCClient<>();
return client.getSubObject(session, "chain.info", Collections.EMPTY_LIST, 0, "head", BlockHeader.class);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/starcoin/api/ContractRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.starcoin.api;

import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.starcoin.bean.ContractCall;
import org.starcoin.jsonrpc.client.JSONRPC2Session;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;

import java.net.URL;
import java.util.ArrayList;
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/starcoin/api/JsonRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package org.starcoin.api;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.starcoin.jsonrpc.JSONRPC2Request;
import org.starcoin.jsonrpc.JSONRPC2Response;
import org.starcoin.jsonrpc.client.JSONRPC2Session;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected T getObject(JSONRPC2Session session, String method, List<Object> param
if (response.indicatesSuccess()) {
Object result = response.getResult();
if (result != null) {
return new ObjectMapper().convertValue(result, clazz);
return JSON.parseObject(result.toString(), clazz);
} else {
logger.warn("get object result is null, method:" + method);
}
Expand Down Expand Up @@ -90,7 +91,7 @@ protected T getObjectParseJackson(JSONRPC2Session session, String method, List<O
Object result = response.getResult();
if (result != null) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(result, clazz);
return objectMapper.readValue(result.toString(), clazz);
} else {
logger.warn("get object parse jackson result is null, method:" + method);
}
Expand Down Expand Up @@ -118,14 +119,14 @@ protected T getObjectParseJackson(JSONRPC2Session session, String method, List<O
* @throws JSONRPC2SessionException
*/
@SuppressWarnings("rawtypes")
protected T getSubObject(JSONRPC2Session session, String method, List<Object> params, int requestId, String subKey, Class<T> clazz) throws JSONRPC2SessionException, JsonProcessingException {
protected T getSubObject(JSONRPC2Session session, String method, List<Object> params, int requestId, String subKey, Class<T> clazz) throws JSONRPC2SessionException {
JSONRPC2Request request = new JSONRPC2Request(method, params, requestId);
JSONRPC2Response response = session.send(request);
if (response.indicatesSuccess()) {
Object result = response.getResult();
if (result != null) {
Map map = new ObjectMapper().convertValue(result, Map.class);
return map.get(subKey) == null ? null : new ObjectMapper().convertValue(map.get(subKey), clazz);
JSONObject jb = JSON.parseObject(result.toString());
return jb.getObject(subKey, clazz);
} else {
logger.warn("get sub object result is null, method:" + method);
}
Expand Down Expand Up @@ -157,10 +158,7 @@ protected List<T> getObjectArray(JSONRPC2Session session, String method, List<Ob
if (response.indicatesSuccess()) {
Object result = response.getResult();
if (result != null) {
ObjectMapper objectMapper = new ObjectMapper();
List<Object> list = objectMapper.convertValue(result, new TypeReference<List<Object>>() {
});
return list.stream().map(item -> objectMapper.convertValue(item, clazz)).collect(Collectors.toList());
return JSON.parseArray(result.toString(), clazz);
} else {
logger.warn("get object result is null, method:" + method);
}
Expand Down
38 changes: 23 additions & 15 deletions src/main/java/org/starcoin/api/StateRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package org.starcoin.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.starcoin.bean.*;
import org.starcoin.jsonrpc.client.JSONRPC2Session;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;
import org.starcoin.bean.ListResource;
import org.starcoin.bean.ListResourceOption;
import org.starcoin.bean.Resource;
import org.starcoin.bean.TokenInfo;

import java.io.IOException;
import java.math.BigInteger;
Expand Down Expand Up @@ -81,7 +85,7 @@ public ListResource getState(String address, boolean isDecode, String state_root
return null;
}

public TokenInfo getTokenInfo(String address, String tokenCode) throws JSONRPC2SessionException, JsonProcessingException {
public TokenInfo getTokenInfo(String address, String tokenCode) throws JSONRPC2SessionException {
JsonRPCClient<TokenInfo> client = new JsonRPCClient<>();
List<Object> param = new ArrayList<>();
param.add(address);
Expand Down Expand Up @@ -148,7 +152,7 @@ private String tokenInfoParameter(String code) {
return "0x1::Token::TokenInfo<" + code + ">";
}

private ListResource getListResource(String address,List<String> resourceTypes,String stateRoot,Integer startIndex,Integer maxSize) throws JSONRPC2SessionException{
private ListResource getListResource(String address, List<String> resourceTypes, String stateRoot, Integer startIndex, Integer maxSize) throws JSONRPC2SessionException {
JsonRPCClient<ListResource> client = new JsonRPCClient<>();
List<Object> parameter = new ArrayList<>();
parameter.add(address);
Expand All @@ -164,46 +168,50 @@ private ListResource getListResource(String address,List<String> resourceTypes,S

/**
* 通过 address 查询当前账户下的所有资源集合
*
* @param address 地址
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByAddress(String address) throws JSONRPC2SessionException{
return this.getListResource(address,null,null,null,null);
public ListResource getAllResourcesByAddress(String address) throws JSONRPC2SessionException {
return this.getListResource(address, null, null, null, null);
}

/**
* 通过 address 和 resourceTypes 查询当前账户下的所有资源集合
* @param address 地址
*
* @param address 地址
* @param resourceTypes 资源类型集合
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByResourceTypes(String address,List<String> resourceTypes) throws JSONRPC2SessionException{
return this.getListResource(address,resourceTypes,null,null,null);
public ListResource getAllResourcesByResourceTypes(String address, List<String> resourceTypes) throws JSONRPC2SessionException {
return this.getListResource(address, resourceTypes, null, null, null);
}

/**
* 通过 address 和 TypeTags 查询当前账户下的所有资源集合
*
* @param address
* @param typeTags
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByTypeTags(String address,List<String> typeTags) throws JSONRPC2SessionException{
return this.getListResource(address,typeTags,null,null,null);
public ListResource getAllResourcesByTypeTags(String address, List<String> typeTags) throws JSONRPC2SessionException {
return this.getListResource(address, typeTags, null, null, null);
}

/**
* 通过 address 分页查询当前账户下的所有资源集合
*
* @param address
* @param startIndex
* @param maxSize
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getResourceListByPage(String address,int startIndex,int maxSize) throws JSONRPC2SessionException{
return this.getListResource(address,null,null,startIndex,maxSize);
public ListResource getResourceListByPage(String address, int startIndex, int maxSize) throws JSONRPC2SessionException {
return this.getListResource(address, null, null, startIndex, maxSize);
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/starcoin/api/TokenContractRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.starcoin.api;

import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import org.starcoin.bean.ContractCall;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;

import java.math.BigInteger;
import java.net.URL;
Expand Down Expand Up @@ -106,7 +106,7 @@ public BigInteger getSTCMarketCap() throws JSONRPC2SessionException {
/**
* 用于获取 STC 在国库中的数量
*/
public BigInteger getSTCTreasurBalance() throws JSONRPC2SessionException {
public BigInteger getSTCTreasuryBalance() throws JSONRPC2SessionException {
StateRPCClient stateRPCClient = new StateRPCClient(session.getURL());
BigInteger amount = stateRPCClient.getAddressAmountValue(AssociactionAddress, STCTypeTag);
return this.getTreasuryBalance(STCTypeTag).add(amount);
Expand All @@ -116,6 +116,6 @@ public BigInteger getSTCTreasurBalance() throws JSONRPC2SessionException {
* 获取 STC 的实际供应量
*/
public BigInteger getSTCCurrentSupply() throws JSONRPC2SessionException {
return this.getSTCMarketCap().subtract(this.getSTCTreasurBalance());
return this.getSTCMarketCap().subtract(this.getSTCTreasuryBalance());
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/starcoin/api/TransactionRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.starcoin.api;

import com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.starcoin.bean.*;
import org.starcoin.jsonrpc.client.JSONRPC2Session;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;

import java.net.URL;
import java.util.*;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/starcoin/bean/AccountProof.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.starcoin.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class AccountProof extends BaseProof {

@JSONField(name = "leaf")
@JsonProperty("leaf")
private List<String> leaf;

public List<String> getLeaf() {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/starcoin/bean/AccumulatorNode.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package org.starcoin.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class AccumulatorNode {

@JSONField(name = "accumulator_root")
@JsonProperty("accumulator_root")
private String accumulatorRoot;

@JSONField(name = "frozen_subtree_roots")
@JsonProperty("frozen_subtree_roots")
private List<String> frozenSubtreeRoots;

@JSONField(name = "num_leaves")
@JsonProperty("num_leaves")
private String numLeaves;

@JSONField(name = "num_nodes")
@JsonProperty("num_nodes")
private String numNodes;

public String getAccumulatorRoot() {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/starcoin/bean/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

public class Authenticator {
@JSONField(name = "Ed25519")
@JsonProperty("Ed25519")
Ed25519 ed25519;

public MultiEd25519 getMultiEd25519() {
Expand All @@ -32,7 +31,6 @@ public void setMultiEd25519(MultiEd25519 multiEd25519) {
}

@JSONField(name = "MultiEd25519")
@JsonProperty("MultiEd25519")
MultiEd25519 multiEd25519;

public Ed25519 getEd25519() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/starcoin/bean/BaseProof.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
public class BaseProof {

@JSONField(name = "siblings")
@JsonProperty("siblings")
public List<String> siblings;

public List<String> getSiblings() {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/starcoin/bean/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@
@JSONType(naming = PropertyNamingStrategy.SnakeCase)
public class Block extends Base {
@JSONField(name = "metadata")
@JsonProperty("metadata")
BlockMetadata blockMetadata;

@JSONField(name = "uncles")
@JsonProperty("uncles")
List<BlockHeader> uncles;

@JSONField(name = "header")
@JsonProperty("header")
private BlockHeader header;

@JSONField(name = "raw")
@JsonProperty("raw")
private String raw;

private BlockBody body;
Expand Down
Loading

0 comments on commit e5d393d

Please sign in to comment.