Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix chain.get_block_txn_infos getObjectArray error. #60

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;

Comment on lines +19 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Inconsistent Use of JSON Processing Libraries

The codebase contains mixed usage of com.alibaba.fastjson and com.fasterxml.jackson for JSON processing. To maintain consistency, please ensure that all JSON processing is handled by a single library. The following files still import com.fasterxml.jackson:

  • src/main/java/org/starcoin/utils/TransactionPayloadDeserializer.java
  • src/main/java/org/starcoin/utils/TypeTagDeserializer.java
  • src/main/java/org/starcoin/utils/TransactionPayloadSerializer.java
  • src/main/java/org/starcoin/utils/StructTagDeserializer.java
  • src/main/java/org/starcoin/utils/StructTagSerializer.java
  • src/main/java/org/starcoin/utils/ScriptFunctionSerializer.java
  • src/main/java/org/starcoin/utils/ScriptFunctionDeserializer.java
  • src/main/java/org/starcoin/utils/ParseUtil.java
  • src/main/java/org/starcoin/utils/ModuleIdSerializer.java
  • src/main/java/org/starcoin/utils/ModuleDeserializer.java
  • src/main/java/org/starcoin/utils/TypeTagSerializer.java
  • src/main/java/org/starcoin/jsonrpc/JSONRPC2Response.java
  • src/main/java/org/starcoin/jsonrpc/client/JSONRPC2Session.java
  • src/main/java/org/starcoin/api/StateRPCClient.java
  • src/main/java/org/starcoin/api/JsonRPCClient.java
  • src/main/java/org/starcoin/serde/format/jackson/VariantFormatDeserializer.java
  • src/main/java/org/starcoin/serde/format/jackson/utils/MappingUtils.java
  • src/main/java/org/starcoin/serde/format/jackson/FormatDeserializer.java
  • src/main/java/org/starcoin/serde/format/jackson/NamedDeserializer.java
  • src/main/java/org/starcoin/bean/CustomStringDeserializer.java
  • src/main/java/org/starcoin/bean/MultiEd25519.java
  • src/main/java/org/starcoin/bean/ListResource.java
  • src/main/java/org/starcoin/bean/Authenticator.java
  • src/main/java/org/starcoin/bean/Event.java
  • src/main/java/org/starcoin/bean/TransactionInfo.java
  • src/main/java/org/starcoin/bean/UserTransaction.java
  • src/main/java/org/starcoin/bean/TransactionInfoWithProof.java
  • src/main/java/org/starcoin/bean/Kind.java
  • src/main/java/org/starcoin/bean/Transaction.java
  • src/main/java/org/starcoin/bean/StateProof.java
  • src/main/java/org/starcoin/bean/RawTransaction.java
  • src/main/java/org/starcoin/bean/EventProof.java
  • src/main/java/org/starcoin/bean/ListResourceOption.java
  • src/main/java/org/starcoin/bean/TokenInfo.java
  • src/main/java/org/starcoin/bean/BlockInfo.java
  • src/main/java/org/starcoin/bean/EventFilter.java
  • src/main/java/org/starcoin/serde/format/jackson/ContainerFormatDeserializer.java
  • src/main/java/org/starcoin/bean/BlockHeader.java
  • src/main/java/org/starcoin/bean/BlockMetadata.java
  • src/main/java/org/starcoin/bean/Block.java
  • src/main/java/org/starcoin/bean/BlockGhostdagData.java
  • src/main/java/org/starcoin/bean/BaseProof.java
  • src/main/java/org/starcoin/bean/GetEventOption.java
  • src/main/java/org/starcoin/bean/EventNotificationResult.java
  • src/main/java/org/starcoin/serde/format/utils/SerdeGenJavaUtils.java
  • src/main/java/org/starcoin/bean/Resource.java
  • src/main/java/org/starcoin/serde/format/utils/ReferenceUtils.java
  • src/main/java/org/starcoin/bean/Ed25519.java

Please update these files to use com.alibaba.fastjson or ensure that com.fasterxml.jackson is consistently used across the entire codebase.

Analysis chain

Ensure Consistency in JSON Processing Libraries

The new imports for com.alibaba.fastjson should be consistently used across the codebase. Verify that all JSON processing is now handled by fastjson and not mixed with other libraries like Jackson.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify consistent use of `com.alibaba.fastjson` for JSON processing.

# Test: Search for the usage of `com.fasterxml.jackson`. Expect: No occurrences.
rg --type java 'import com.fasterxml.jackson'

Length of output: 15778

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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Missing JSON-RPC client dependency in build configuration files.

The import statement for JSONRPC2SessionException has been updated to use the com.thetransactioncompany package. However, the new dependency is not declared in the pom.xml or build.gradle files. Please ensure that the new dependency is added to the build configuration to avoid runtime issues.

  • pom.xml or build.gradle: Add the dependency com.thetransactioncompany.jsonrpc2.
Analysis chain

Verify the compatibility of the new JSON-RPC client dependency.

The import statement for JSONRPC2SessionException has been updated to use the com.thetransactioncompany package. Ensure that the new dependency is fully compatible with the existing codebase and that all necessary changes have been made.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the compatibility of the new JSON-RPC client dependency.

# Test: Search for the usage of JSONRPC2SessionException. Expect: Only occurances of the new package.
rg --type java 'JSONRPC2SessionException'

Length of output: 13047



Script:

#!/bin/bash
# Description: Verify the new JSON-RPC client dependency in build configuration files.

# Test: Search for the new JSON-RPC client dependency in build configuration files.
rg --type xml 'com.thetransactioncompany.jsonrpc2'
rg --type gradle 'com.thetransactioncompany.jsonrpc2'

Length of output: 253



Script:

#!/bin/bash
# Description: Verify the new JSON-RPC client dependency in build configuration files.

# Locate `pom.xml` and `build.gradle` files in the repository and search for the new JSON-RPC client dependency.
for file in $(fd -e xml -e gradle); do
    echo "Checking $file"
    rg 'com.thetransactioncompany.jsonrpc2' "$file"
done

Length of output: 150

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 {
nkysg marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 0 additions & 3 deletions src/main/java/org/starcoin/bean/BlockBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
package org.starcoin.bean;

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

import java.util.List;

public class BlockBody {
@JSONField(name = "Full")
@JsonProperty("Full")
List<UserTransaction> userTransactions;

@JSONField(name = "Hashes")
@JsonProperty("Hashes")
List<String> hashes;
public List<UserTransaction> getUserTransactions() {
return userTransactions;
Expand Down
Loading
Loading