Skip to content

Commit

Permalink
Merge pull request #70 from chaoxinhu/feature/txninfo
Browse files Browse the repository at this point in the history
Add transaction info in ResponseData
  • Loading branch information
junqizhang-dev committed Apr 24, 2019
2 parents 588616d + 5f027e1 commit a20704c
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 75 deletions.
50 changes: 44 additions & 6 deletions src/main/java/com/webank/weid/protocol/response/ResponseData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018) WeBank Co., Ltd.
* Copyright© (2018-2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
Expand Down Expand Up @@ -32,10 +32,27 @@
@Data
public class ResponseData<T> {

/**
* The generic type result object.
*/
private T result;

/**
* The error code.
*/
private Integer errorCode;

/**
* The error message.
*/
private String errorMessage;

/**
* The blockchain transaction info. Note that this transaction only becomes valid (not null nor
* blank) when a successful transaction is sent to chain with a block generated.
*/
private TransactionInfo transactionInfo = null;

/**
* Instantiates a new response data.
*/
Expand All @@ -44,23 +61,44 @@ public ResponseData() {
}

/**
* Instantiates a new response data.
* Instantiates a new response data. Transaction info is left null to avoid unnecessary boxing.
*
* @param result the result
* @param errorCode the return code
*/
public ResponseData(T result, ErrorCode errorCode) {
this.result = result;
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
if (errorCode != null) {
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
}
}

/**
* Instantiates a new response data with transaction info.
*
* @param result the result
* @param errorCode the return code
*/
public ResponseData(T result, ErrorCode errorCode, TransactionInfo transactionInfo) {
this.result = result;
if (errorCode != null) {
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
}
if (transactionInfo != null) {
this.transactionInfo = transactionInfo;
}
}

/**
* set a ErrorCode type errorCode.
*/
public void setErrorCode(ErrorCode errorCode) {
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
if (errorCode != null) {
this.errorCode = errorCode.getCode();
this.errorMessage = errorCode.getCodeDesc();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright© (2019) WeBank Co., Ltd.
*
* This file is part of weidentity-java-sdk.
*
* weidentity-java-sdk is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* weidentity-java-sdk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with weidentity-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.protocol.response;

import java.math.BigInteger;

import lombok.Data;
import org.bcos.web3j.protocol.core.methods.response.TransactionReceipt;

/**
* The basic transaction information. Caller can further use these information to track the detailed
* transaction instance from blockchain.
*
* @author chaoxinhu 2019.4
*/

@Data
public class TransactionInfo {

/**
* The block number.
*/
private BigInteger blockNumber;

/**
* The transaction hash value.
*/
private String transactionHash;

/**
* The transaction index.
*/
private BigInteger transactionIndex;

/**
* Constructor from a transactionReceipt. If the receipt is null, an
*
* @param receipt the transaction receipt
*/
public TransactionInfo(TransactionReceipt receipt) {
if (receipt != null) {
this.blockNumber = receipt.getBlockNumber();
this.transactionHash = receipt.getTransactionHash();
this.transactionIndex = receipt.getTransactionIndex();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs;
import com.webank.weid.protocol.request.RemoveAuthorityIssuerArgs;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.protocol.response.TransactionInfo;
import com.webank.weid.rpc.AuthorityIssuerService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.service.BaseService;
Expand Down Expand Up @@ -136,9 +137,10 @@ public ResponseData<Boolean> registerAuthorityIssuer(RegisterAuthorityIssuerArgs
WeIdConstant.TRANSACTION_RECEIPT_TIMEOUT,
TimeUnit.SECONDS
);
TransactionInfo info = new TransactionInfo(receipt);
Boolean result = resolveRegisterAuthorityIssuerEvents(receipt);
if (result) {
return new ResponseData<>(result, ErrorCode.SUCCESS);
return new ResponseData<>(result, ErrorCode.SUCCESS, info);
}
} catch (TimeoutException e) {
logger.error("register authority issuer failed due to system timeout. ", e);
Expand Down Expand Up @@ -169,9 +171,10 @@ public ResponseData<String> registerAuthorityIssuer(String transactionHex) {
}
TransactionReceipt transactionReceipt = TransactionUtils
.sendTransaction(getWeb3j(), transactionHex);
TransactionInfo info = new TransactionInfo(transactionReceipt);
Boolean result = resolveRegisterAuthorityIssuerEvents(transactionReceipt);
if (result) {
return new ResponseData<>(Boolean.TRUE.toString(), ErrorCode.SUCCESS);
return new ResponseData<>(Boolean.TRUE.toString(), ErrorCode.SUCCESS, info);
}
} catch (Exception e) {
logger.error("[registerAuthorityIssuer] register failed due to transaction error.", e);
Expand Down Expand Up @@ -223,20 +226,21 @@ public ResponseData<Boolean> removeAuthorityIssuer(RemoveAuthorityIssuerArgs arg
List<AuthorityIssuerRetLogEventResponse> eventList =
AuthorityIssuerController.getAuthorityIssuerRetLogEvents(receipt);

TransactionInfo info = new TransactionInfo(receipt);
AuthorityIssuerRetLogEventResponse event = eventList.get(0);
if (event != null) {
ErrorCode errorCode = verifyAuthorityIssuerRelatedEvent(
event,
WeIdConstant.REMOVE_AUTHORITY_ISSUER_OPCODE
);
if (ErrorCode.SUCCESS.getCode() != errorCode.getCode()) {
return new ResponseData<>(false, errorCode);
return new ResponseData<>(false, errorCode, info);
} else {
return new ResponseData<>(true, errorCode);
return new ResponseData<>(true, errorCode, info);
}
} else {
logger.error("remove authority issuer failed, transcation event decoding failure.");
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR);
return new ResponseData<>(false, ErrorCode.AUTHORITY_ISSUER_ERROR, info);
}
} catch (TimeoutException e) {
logger.error("remove authority issuer failed due to system timeout. ", e);
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/webank/weid/service/impl/CptServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.webank.weid.protocol.request.CptStringArgs;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.protocol.response.RsvSignature;
import com.webank.weid.protocol.response.TransactionInfo;
import com.webank.weid.rpc.CptService;
import com.webank.weid.service.BaseService;
import com.webank.weid.util.DataTypetUtils;
Expand Down Expand Up @@ -190,9 +191,11 @@ public ResponseData<String> registerCpt(String transactionHex) {
}
TransactionReceipt transactionReceipt = TransactionUtils
.sendTransaction(getWeb3j(), transactionHex);
TransactionInfo info = new TransactionInfo(transactionReceipt);
CptBaseInfo cptBaseInfo = this.resolveRegisterCptEvents(transactionReceipt).getResult();
if (cptBaseInfo != null) {
return new ResponseData<>(JsonUtil.objToJsonStr(cptBaseInfo), ErrorCode.SUCCESS);
return new ResponseData<>(JsonUtil.objToJsonStr(cptBaseInfo), ErrorCode.SUCCESS,
info);
}
} catch (Exception e) {
logger.error("[registerCpt] register failed due to unknown transaction error. ", e);
Expand Down Expand Up @@ -353,7 +356,8 @@ public ResponseData<CptBaseInfo> updateCpt(CptMapArgs args, Integer cptId) {
return this.getResultByResolveEvent(
event.get(0).retCode,
event.get(0).cptId,
event.get(0).cptVersion
event.get(0).cptVersion,
transactionReceipt
);
} catch (InterruptedException | ExecutionException e) {
logger.error(
Expand Down Expand Up @@ -415,34 +419,37 @@ private TransactionReceipt getTransactionReceipt(
private ResponseData<CptBaseInfo> getResultByResolveEvent(
Uint256 retCode,
Uint256 cptId,
Int256 cptVersion) {
Int256 cptVersion,
TransactionReceipt receipt) {

TransactionInfo info = new TransactionInfo(receipt);
// register
if (DataTypetUtils.uint256ToInt(retCode)
== ErrorCode.CPT_ID_AUTHORITY_ISSUER_EXCEED_MAX.getCode()) {
logger.error("[getResultByResolveEvent] cptId limited max value. cptId:{}", cptId);
return new ResponseData<>(null, ErrorCode.CPT_ID_AUTHORITY_ISSUER_EXCEED_MAX);
return new ResponseData<>(null, ErrorCode.CPT_ID_AUTHORITY_ISSUER_EXCEED_MAX, info);
}

// register and update
if (DataTypetUtils.uint256ToInt(retCode)
== ErrorCode.CPT_PUBLISHER_NOT_EXIST.getCode()) {
logger.error("[getResultByResolveEvent] publisher does not exist. cptId:{}", cptId);
return new ResponseData<>(null, ErrorCode.CPT_PUBLISHER_NOT_EXIST);
return new ResponseData<>(null, ErrorCode.CPT_PUBLISHER_NOT_EXIST, info);
}

// update
if (DataTypetUtils.uint256ToInt(retCode)
== ErrorCode.CPT_NOT_EXISTS.getCode()) {
logger.error("[getResultByResolveEvent] cpt id : {} does not exist.", cptId);
return new ResponseData<>(null, ErrorCode.CPT_NOT_EXISTS);
return new ResponseData<>(null, ErrorCode.CPT_NOT_EXISTS, info);
}

CptBaseInfo result = new CptBaseInfo();
result.setCptId(DataTypetUtils.uint256ToInt(cptId));
result.setCptVersion(DataTypetUtils.int256ToInt(cptVersion));

ResponseData<CptBaseInfo> responseData = new ResponseData<>(result, ErrorCode.SUCCESS);
ResponseData<CptBaseInfo> responseData = new ResponseData<>(result, ErrorCode.SUCCESS,
info);
return responseData;
}

Expand Down Expand Up @@ -551,7 +558,8 @@ private ResponseData<CptBaseInfo> resolveRegisterCptEvents(
return this.getResultByResolveEvent(
event.get(0).retCode,
event.get(0).cptId,
event.get(0).cptVersion
event.get(0).cptVersion,
transactionReceipt
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.protocol.response.TransactionInfo;
import com.webank.weid.rpc.EvidenceService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.service.BaseService;
Expand Down Expand Up @@ -106,18 +107,13 @@ public ResponseData<String> createEvidence(
.isCreateEvidenceArgsValid(credential, weIdPrivateKey);
if (ErrorCode.SUCCESS.getCode() != innerResponse.getCode()) {
logger.error("Create Evidence input format error!");
return new ResponseData<>(
StringUtils.EMPTY,
innerResponse
);
return new ResponseData<>(StringUtils.EMPTY, innerResponse);
}

innerResponse = CredentialUtils.isCredentialValid(credential);
if (ErrorCode.SUCCESS.getCode() != innerResponse.getCode()) {
logger.error("Create Evidence input format error: credential!");
return new ResponseData<>(
StringUtils.EMPTY,
innerResponse);
return new ResponseData<>(StringUtils.EMPTY, innerResponse);
}

try {
Expand Down Expand Up @@ -154,28 +150,26 @@ public ResponseData<String> createEvidence(
TransactionReceipt receipt = future.get(
WeIdConstant.TRANSACTION_RECEIPT_TIMEOUT,
TimeUnit.SECONDS);
TransactionInfo info = new TransactionInfo(receipt);
List<CreateEvidenceLogEventResponse> eventResponseList =
EvidenceFactory.getCreateEvidenceLogEvents(receipt);
CreateEvidenceLogEventResponse event = eventResponseList.get(0);

if (event != null) {
innerResponse = verifyCreateEvidenceEvent(event);
if (ErrorCode.SUCCESS.getCode() != innerResponse.getCode()) {
return new ResponseData<>(
StringUtils.EMPTY,
innerResponse
);
return new ResponseData<>(StringUtils.EMPTY, innerResponse, info);
}
return new ResponseData<>(event.addr.toString(), ErrorCode.SUCCESS);
return new ResponseData<>(event.addr.toString(), ErrorCode.SUCCESS, info);
} else {
logger
.error(
"create evidence failed due to transcation event decoding failure. ");
return new ResponseData<>(StringUtils.EMPTY,
ErrorCode.CREDENTIAL_EVIDENCE_BASE_ERROR);
ErrorCode.CREDENTIAL_EVIDENCE_BASE_ERROR, info);
}
} catch (TimeoutException e) {
logger.error("create evidencefailed due to system timeout. ", e);
logger.error("create evidence failed due to system timeout. ", e);
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.TRANSACTION_TIMEOUT);
} catch (InterruptedException | ExecutionException e) {
logger.error("create evidence failed due to transaction error. ", e);
Expand Down

0 comments on commit a20704c

Please sign in to comment.