Skip to content

Commit

Permalink
Merge pull request #159 from WeBankFinTech/feature/support-batch-tran…
Browse files Browse the repository at this point in the history
…saction

Feature/support batch transaction
  • Loading branch information
yanggang-JV committed Apr 10, 2020
2 parents 4afd79e + 4536181 commit d4dc61c
Show file tree
Hide file tree
Showing 10 changed files with 665 additions and 92 deletions.
29 changes: 17 additions & 12 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ public enum ErrorCode {
*/
ON_CHAIN_STRING_TOO_LONG(100504, "on-chain String length exceeds limit"),

/**
* offline evidence transaction saved failed.
*/
OFFLINE_EVIDENCE_SAVE_FAILED(100505, "offline evidence transaction saved failed."),

/**
* The challenge is invalid.
*/
Expand Down Expand Up @@ -498,12 +503,12 @@ public enum ErrorCode {
* pdf transfer error.
*/
TRANSPORTATION_PDF_VERIFY_ERROR(100809, "pdf verify error, please check the error log."),

/**
* the transmission type is invalid.
*/
TRANSPORTATION_TRANSMISSION_TYPE_INVALID(100810, "the trans type is invalid."),

/**
* the URI type is invalid.
*/
Expand All @@ -518,7 +523,7 @@ public enum ErrorCode {
* the trans mode is invalid.
*/
TRANSPORTATION_TRANSMODE_TYPE_INVALID(100813, "the trans mode is invalid."),

/**
* Authority issuer main error code.
*/
Expand Down Expand Up @@ -711,15 +716,15 @@ public enum ErrorCode {
100113,
"weIdAuth callback is not registered."
),

/**
* the channelId is null.
*/
WEID_AUTH_CHANNELID_IS_NULL(
100114,
"the channelId is null."
),

/**
* the channelId is invalid.
*/
Expand Down Expand Up @@ -796,27 +801,27 @@ public enum ErrorCode {
* the data is expire.
*/
SQL_DATA_EXPIRE(160015, "the data is expire."),

/**
* no premission.
*/
CNS_NO_PERMISSION(160016, "no premission for this cns."),

/**
* the cns does not exist.
*/
CNS_DOES_NOT_EXIST(160017, "the cns does not exist."),

/**
* the cns is used.
*/
CNS_IS_USED(160018, "the cns is used."),

/**
* the cns is not used.
*/
CNS_IS_NOT_USED(160019, "the cns is not used."),

/**
* the code is undefined.
*/
Expand All @@ -826,12 +831,12 @@ public enum ErrorCode {
* the data does not exist.
*/
SQL_DATA_DOES_NOT_EXIST(160021, "the data does not exist."),

/**
* this is unsupported.
*/
THIS_IS_UNSUPPORTED(160022, "this is unsupported."),

/**
* this is repeated call.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/webank/weid/constant/ParamKeyConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public final class ParamKeyConstant {
*/
public static final String AUTHORITY_ISSUER_NAME = "name";

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

/**
* CptService related param names.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.webank.weid.protocol.request;

import lombok.Getter;
import lombok.Setter;

/**
* @author tonychen 2020年4月4日
*/
@Getter
@Setter
public class TransactionArgs {


private String requestId;

private String method;

private String args;

private String extra;

private Long timeStamp;

/**
* 批次,目前是按天.
*/
private String batch;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.io.File;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
Expand All @@ -44,6 +42,7 @@
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.rpc.EvidenceService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.util.BatchTransactionUtils;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.DateUtils;
import com.webank.weid.util.WeIdUtils;
Expand Down Expand Up @@ -210,6 +209,33 @@ private ResponseData<String> hashToNewEvidence(String hashValue, String privateK
DataToolUtils.base64Encode(DataToolUtils.simpleSignatureSerialization(sigData)),
StandardCharsets.UTF_8);
Long timestamp = DateUtils.getNoMillisecondTimeStamp();

//如果
boolean flag = true;
if (flag) {
String rawData = new StringBuffer()
.append(hashValue)
.append(signature)
.append(extra)
.append(timestamp)
.append(WeIdUtils.getWeIdFromPrivateKey(privateKey)).toString();
String hash = DataToolUtils.sha3(rawData);
String requestId = new BigInteger(hash.substring(2), 16).toString();
String[] args = new String[5];
args[0] = hashValue;
args[1] = signature;
args[2] = extra;
args[3] = String.valueOf(timestamp);
args[4] = privateKey;
boolean isSuccess = BatchTransactionUtils
.writeTransaction(requestId, "createEvidence", args, StringUtils.EMPTY);
if (isSuccess) {
return new ResponseData<>(hashValue, ErrorCode.SUCCESS);
} else {
return new ResponseData<>(hashValue, ErrorCode.OFFLINE_EVIDENCE_SAVE_FAILED);
}
}

return evidenceServiceEngine.createEvidence(
hashValue,
signature,
Expand Down Expand Up @@ -384,6 +410,35 @@ public ResponseData<String> createEvidenceWithLogAndCustomKey(
DataToolUtils.base64Encode(DataToolUtils.simpleSignatureSerialization(sigData)),
StandardCharsets.UTF_8);
Long timestamp = DateUtils.getNoMillisecondTimeStamp();
//如果
boolean flag = true;
if (flag) {
String rawData = new StringBuffer()
.append(hashValue)
.append(signature)
.append(log)
.append(timestamp)
.append(customKey)
.append(WeIdUtils.getWeIdFromPrivateKey(privateKey)).toString();
String hash = DataToolUtils.sha3(rawData);
String requestId = new BigInteger(hash.substring(2), 16).toString();
String[] args = new String[6];
args[0] = hashValue;
args[1] = signature;
args[2] = log;
args[3] = String.valueOf(timestamp);
args[4] = customKey;
args[5] = privateKey;

boolean isSuccess = BatchTransactionUtils
.writeTransaction(requestId, "createEvidenceWithCustomKey", args,
StringUtils.EMPTY);
if (isSuccess) {
return new ResponseData<>(hashValue, ErrorCode.SUCCESS);
} else {
return new ResponseData<>(hashValue, ErrorCode.OFFLINE_EVIDENCE_SAVE_FAILED);
}
}
return evidenceServiceEngine.createEvidenceWithCustomKey(
hashValue,
signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.List;

import com.webank.weid.protocol.request.TransactionArgs;
import com.webank.weid.protocol.response.ResponseData;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public interface Persistence {

/**
* delete data by id.
*
*
* @param domain the domain of the data.
* @param id the key of the data.
* @return the data you stored.
Expand All @@ -70,14 +71,14 @@ public interface Persistence {

/**
* update data by id.
*
*
* @param domain the domain of the data.
* @param id the key you store with.
* @param data the data you want to update into.
* @return execute status of the "update" operation.
*/
public ResponseData<Integer> update(String domain, String id, String data);

/**
* save data to storage if not exist, others for update.
*
Expand All @@ -87,4 +88,12 @@ public interface Persistence {
* @return execute status of the "save" operation.
*/
public ResponseData<Integer> saveOrUpdate(String domain, String id, String data);

/**
* save transaction to storage.
*
* @param transactionArgs the transaction info.
* @return execute status of the "saveTransaction" operation.
*/
public ResponseData<Integer> saveTransaction(TransactionArgs transactionArgs);
}

0 comments on commit d4dc61c

Please sign in to comment.