Skip to content

Commit

Permalink
Merge pull request #38 from hupeng199/feature/fix-issue
Browse files Browse the repository at this point in the history
modify implemention of ErrorCode and fix code style issue
  • Loading branch information
chenhaozx committed Feb 21, 2019
2 parents dc60da0 + 359acb2 commit f23ed30
Show file tree
Hide file tree
Showing 30 changed files with 439 additions and 498 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,18 @@ public String getCodeDesc() {
protected void setCodeDesc(String codeDesc) {
this.codeDesc = codeDesc;
}

/**
* get ErrorType By errcode.
*
* @param errorCode the ErrorCode
*/
public static ErrorCode getTypeByErrorCode(int errorCode) {
for (ErrorCode type : ErrorCode.values()) {
if (type.getCode() == errorCode) {
return type;
}
}
return null;
}
}
5 changes: 0 additions & 5 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ public final class WeIdConstant {
*/
public static final Integer REMOVE_AUTHORITY_ISSUER_OPCODE = 1;

/**
* UTF-8.
*/
public static final String UTF_8 = "UTF-8";

/**
* 0L.
*/
Expand Down
37 changes: 23 additions & 14 deletions src/main/java/com/webank/weid/contract/deploy/DeployContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -116,7 +117,7 @@ private static boolean loadConfig() {
ChannelEthereumService channelEthereumService = new ChannelEthereumService();
channelEthereumService.setChannelService(service);
web3j = Web3j.build(channelEthereumService);
if (null == web3j) {
if (web3j == null) {
logger.error("[BaseService] web3j init failed. ");
return false;
}
Expand All @@ -126,7 +127,7 @@ private static boolean loadConfig() {
logger.info("begin init credentials");
credentials = GenCredential.create(toolConf.getPrivKey());

if (null == credentials) {
if (credentials == null) {
logger.error("[BaseService] credentials init failed. ");
return false;
}
Expand All @@ -140,7 +141,7 @@ private static boolean loadConfig() {
* @return the web3j instance
*/
protected static Web3j getWeb3j() {
if (null == web3j) {
if (web3j == null) {
loadConfig();
}
return web3j;
Expand All @@ -154,7 +155,7 @@ private static void deployContract() {
}

private static String deployWeIdContract() {
if (null == web3j) {
if (web3j == null) {
loadConfig();
}
Future<WeIdContract> f =
Expand All @@ -179,7 +180,7 @@ private static String deployWeIdContract() {

private static String deployCptContracts(
String authorityIssuerDataAddress, String weIdContractAddress) {
if (null == web3j) {
if (web3j == null) {
loadConfig();
}

Expand All @@ -204,7 +205,8 @@ private static String deployCptContracts(
WeIdConstant.GAS_LIMIT,
WeIdConstant.INILITIAL_VALUE,
new Address(cptDataAddress),
new Address(weIdContractAddress));
new Address(weIdContractAddress)
);
CptController cptController =
f2.get(DEFAULT_DEPLOY_CONTRACTS_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
String cptControllerAddress = cptController.getContractAddress();
Expand All @@ -217,7 +219,7 @@ private static String deployCptContracts(
}

private static String deployAuthorityIssuerContracts() {
if (null == web3j) {
if (web3j == null) {
loadConfig();
}

Expand Down Expand Up @@ -264,7 +266,8 @@ private static String deployAuthorityIssuerContracts() {
WeIdConstant.GAS_LIMIT,
WeIdConstant.INILITIAL_VALUE,
new Address(committeeMemberDataAddress),
new Address(roleControllerAddress));
new Address(roleControllerAddress)
);

} catch (Exception e) {
logger.error("CommitteeMemberData deployment error:", e);
Expand All @@ -280,7 +283,8 @@ private static String deployAuthorityIssuerContracts() {
WeIdConstant.GAS_PRICE,
WeIdConstant.GAS_LIMIT,
WeIdConstant.INILITIAL_VALUE,
new Address(roleControllerAddress));
new Address(roleControllerAddress)
);

} catch (Exception e) {
logger.error("CommitteeMemberController deployment error:", e);
Expand Down Expand Up @@ -310,8 +314,10 @@ private static String deployAuthorityIssuerContracts() {
// Step 6: Write [addrress] Into File
try {
AuthorityIssuerController authorityIssuerController =
f5.get(DEFAULT_DEPLOY_CONTRACTS_TIMEOUT_IN_SECONDS,
TimeUnit.SECONDS);
f5.get(
DEFAULT_DEPLOY_CONTRACTS_TIMEOUT_IN_SECONDS,
TimeUnit.SECONDS
);
String authorityIssuerControllerAddress =
authorityIssuerController.getContractAddress();
writeAddressToFile(authorityIssuerControllerAddress, "authorityIssuer.address");
Expand All @@ -323,7 +329,7 @@ private static String deployAuthorityIssuerContracts() {
}

private static String deployEvidenceContracts() {
if (null == web3j) {
if (web3j == null) {
loadConfig();
}

Expand Down Expand Up @@ -362,14 +368,17 @@ private static void writeAddressToFile(
logger.error("writeAddressToFile() delete file is fail.");
return;
}
ow = new OutputStreamWriter(new FileOutputStream(fileName, true), WeIdConstant.UTF_8);
ow = new OutputStreamWriter(
new FileOutputStream(fileName, true),
StandardCharsets.UTF_8
);
String content = new StringBuffer().append(contractAddress).toString();
ow.write(content);
ow.close();
} catch (IOException e) {
logger.error("writer file exception", e);
} finally {
if (null != ow) {
if (ow != null) {
try {
ow.close();
} catch (IOException e) {
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/webank/weid/protocol/response/ResponseData.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
public class ResponseData<T> {

private T result;
private Integer errorCode = ErrorCode.SUCCESS.getCode();
private String errorMessage = ErrorCode.SUCCESS.getCodeDesc();
private Integer errorCode;
private String errorMessage;

/**
* Instantiates a new response data.
*/
public ResponseData() {
this.setErrorCode(ErrorCode.SUCCESS);
}

/**
Expand All @@ -55,15 +56,11 @@ public ResponseData(T result, ErrorCode errorCode) {
}

/**
* Instantiates a new response data.
*
* @param result the result
* @param errorCode the return code
* @param errorMessage the return message
* set a ErrorCode type errorCode.
*/
public ResponseData(T result, Integer errorCode, String errorMessage) {
this.result = result;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
public void setErrorCode(ErrorCode errorCode) {
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
}

}
8 changes: 4 additions & 4 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static boolean initWeb3j() {
ChannelEthereumService channelEthereumService = new ChannelEthereumService();
channelEthereumService.setChannelService(service);
web3j = Web3j.build(channelEthereumService);
if (null == web3j) {
if (web3j == null) {
logger.error("[BaseService] web3j init failed. ");
return false;
}
Expand All @@ -88,7 +88,7 @@ private static boolean initCredentials() {
logger.info("begin init credentials");
credentials = GenCredential.create(toolConf.getPrivKey());

if (null == credentials) {
if (credentials == null) {
logger.error("[BaseService] credentials init failed. ");
return false;
}
Expand All @@ -101,7 +101,7 @@ private static boolean initCredentials() {
* @return the web3j
*/
protected static Web3j getWeb3j() {
if (null == web3j) {
if (web3j == null) {
if (!initWeb3j()) {
throw new InitWeb3jException();
}
Expand Down Expand Up @@ -184,7 +184,7 @@ protected static Contract getContractService(String contractAddress, Class<?> cl
Object contract = null;
try {
// load contract
if (null == credentials) {
if (credentials == null) {
initCredentials();
}
contract = loadContract(contractAddress, credentials, cls);
Expand Down

0 comments on commit f23ed30

Please sign in to comment.