Skip to content

Commit

Permalink
Merge pull request #267 from yanggang-JV/feature/adapt-contract-1.2.25
Browse files Browse the repository at this point in the history
* change the method to get weid
  • Loading branch information
junqizhang-dev committed Oct 27, 2020
2 parents ed41df2 + 3ddd7b1 commit 6f41f09
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 109 deletions.
50 changes: 10 additions & 40 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,22 @@ List oval = [

configurations {
localDeps
all*.exclude group: "io.netty", module: "netty-tcnative"
}

dependencies {

localDeps 'org.projectlombok:lombok:1.18.10'
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, redisson, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.26") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile fileTree(dir: 'lib', include: '*.jar')
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
}
if (gradleVer.startsWith("4")) {
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, redisson, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.24") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "org.fisco-bcos", module: "web3sdk"
exclude group: "org.fisco-bcos", module: "web3sdk-weevent"
exclude group: "io.netty", module: "netty-tcnative"
}
compile("org.fisco-bcos:web3sdk:2.4.4") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude module: "lombok"
exclude group: "io.netty", module: "netty-tcnative"
}
compile("org.fisco-bcos:web3sdk-weevent:1.2.6") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude module: "lombok"
exclude group: "io.netty", module: "netty-tcnative"
}
compile fileTree(dir: 'lib', include: '*.jar')
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
if (gradle.startParameter.isOffline()) {
compile fileTree(dir: 'dist/lib', include: '*.jar')
}
}
Expand All @@ -181,25 +170,6 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.10'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
compile logger, apache_commons, json, mysql_driver, redisson, zxing, rpc, pdfbox, protobuf, caffeine, oval
compile("com.webank:weid-contract-java:1.2.24") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "org.fisco-bcos", module: "web3sdk"
exclude group: "org.fisco-bcos", module: "web3sdk-weevent"
exclude group: "io.netty", module: "netty-tcnative"
}
compile("org.fisco-bcos:web3sdk:2.4.4") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude module: "lombok"
exclude group: "io.netty", module: "netty-tcnative"
}
compile("org.fisco-bcos:web3sdk-weevent:1.2.6") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude module: "lombok"
exclude group: "io.netty", module: "netty-tcnative"
}
compile fileTree(dir: 'lib', include: '*.jar')
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
compileOnly files('dist/lib/lombok-1.18.10.jar')
annotationProcessor files('dist/lib/lombok-1.18.10.jar')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ public class ResolveAttributeException extends RuntimeException {
* constructor.
*
* @param errorCode exception error code.
* @param errorMessage exception error messave.
* @param errorMessage exception error message.
*/
public ResolveAttributeException(Integer errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

/**
* constructor.
*
* @param errorCode exception error code.
* @param errorMessage exception error message.
* @param e the throwable.
*/
public ResolveAttributeException(Integer errorCode, String errorMessage, Throwable e) {
super(errorMessage, e);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

public Integer getErrorCode() {
return errorCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransactionReceipt;
import org.fisco.bcos.web3j.protocol.core.methods.response.SendTransaction;
import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
import org.fisco.bcos.web3j.protocol.exceptions.TransactionTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -41,6 +40,7 @@
import com.webank.weid.contract.v2.CptController.RegisterCptRetLogEventResponse;
import com.webank.weid.contract.v2.WeIdContract;
import com.webank.weid.contract.v2.WeIdContract.WeIdAttributeChangedEventResponse;
import com.webank.weid.exception.WeIdBaseException;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.protocol.response.TransactionInfo;
Expand Down Expand Up @@ -127,7 +127,7 @@ public static TransactionReceipt sendTransaction(String transactionHex)
}
}
} catch (Exception e) {
throw new TransactionTimeoutException("Transaction receipt was not generated after "
throw new WeIdBaseException("Transaction receipt was not generated after "
+ ((sumTime) / 1000
+ " seconds for transaction: " + ethSendTransaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.zip.DataFormatException;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.fisco.bcos.web3j.abi.EventEncoder;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.protocol.core.DefaultBlockParameterNumber;
import org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock;
import org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransactionReceipt;
import org.fisco.bcos.web3j.protocol.core.methods.response.BlockTransactionReceipts;
import org.fisco.bcos.web3j.protocol.core.methods.response.Log;
import org.fisco.bcos.web3j.protocol.core.methods.response.Transaction;
import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -54,6 +51,7 @@
import com.webank.weid.contract.v2.WeIdContract.WeIdHistoryEventEventResponse;
import com.webank.weid.exception.DataTypeCastException;
import com.webank.weid.exception.ResolveAttributeException;
import com.webank.weid.exception.WeIdBaseException;
import com.webank.weid.protocol.base.AuthenticationProperty;
import com.webank.weid.protocol.base.PublicKeyProperty;
import com.webank.weid.protocol.base.ServiceProperty;
Expand Down Expand Up @@ -152,38 +150,13 @@ private static void resolveEventHistory(
int previousBlock = blockNumber;
while (previousBlock != STOP_RESOLVE_BLOCK_NUMBER) {
int currentBlockNumber = previousBlock;
BcosBlock bcosBlock = null;
try {
bcosBlock = ((Web3j) getWeb3j()).getBlockByNumber(
new DefaultBlockParameterNumber(currentBlockNumber), true).send();
} catch (IOException e) {
logger.error("[resolveEventHistory] get block {} err: {}", currentBlockNumber, e);
}
if (bcosBlock == null) {
logger.info("[resolveEventHistory] get block {} err: is null", currentBlockNumber);
return;
}

List<Transaction> transList = bcosBlock
.getBlock()
.getTransactions()
.stream()
.map(transactionResult -> (Transaction) transactionResult.get())
.collect(Collectors.toList());

// Fill-in blockList
blockList.add(currentBlockNumber);

previousBlock = 0;
try {
for (Transaction transaction : transList) {
String transHash = transaction.getHash();

BcosTransactionReceipt rec1 = ((Web3j) getWeb3j())
.getTransactionReceipt(transHash)
.send();
TransactionReceipt receipt = rec1.getTransactionReceipt().get();
List<Log> logs = rec1.getResult().getLogs();
List<TransactionReceipt> receipts = getTransactionReceipts(currentBlockNumber);
for (TransactionReceipt receipt : receipts) {
List<Log> logs = receipt.getLogs();
for (Log log : logs) {
ResolveEventLogResult returnValue =
resolveSingleEventLog(weId, log, receipt, currentBlockNumber,
Expand All @@ -197,12 +170,13 @@ private static void resolveEventHistory(
}
}
}
} catch (IOException | DataTypeCastException e) {
} catch (IOException | DataTypeCastException | DataFormatException e) {
logger.error(
"[resolveEventHistory]: get TransactionReceipt by weId :{} failed.", weId, e);
throw new ResolveAttributeException(
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCode(),
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc());
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc(),
e);
}
}
}
Expand Down Expand Up @@ -565,12 +539,12 @@ public ResponseData<Boolean> createWeId(
+ "modifying weid is not allowed. we address is {}",
weAddress
);
return new ResponseData(false, ErrorCode.WEID_PRIVATEKEY_DOES_NOT_MATCH, info);
return new ResponseData<>(false, ErrorCode.WEID_PRIVATEKEY_DOES_NOT_MATCH, info);
}
return new ResponseData(true, ErrorCode.SUCCESS, info);
return new ResponseData<>(true, ErrorCode.SUCCESS, info);
} catch (Exception e) {
logger.error("[createWeId] create weid has error, Error Message:{}", e);
return new ResponseData(false, ErrorCode.WEID_PRIVATEKEY_DOES_NOT_MATCH);
return new ResponseData<>(false, ErrorCode.WEID_PRIVATEKEY_DOES_NOT_MATCH);
}
}

Expand Down Expand Up @@ -624,36 +598,29 @@ public ResponseData<Boolean> setAttribute(
}
}

private List<WeIdPojo> getWeIdListByBlockNumber(Integer blockNumber) {
// 根据块高获取当前块里面的所有weId
List<WeIdPojo> result = new ArrayList<WeIdPojo>();
BcosBlock bcosBlock = null;
private static List<TransactionReceipt> getTransactionReceipts(Integer blockNumber)
throws IOException, DataFormatException {
BlockTransactionReceipts blockTransactionReceipts = null;
try {
// 根据块高获取交易块
bcosBlock = ((Web3j) getWeb3j()).getBlockByNumber(
new DefaultBlockParameterNumber(blockNumber), true).send();
} catch (IOException e) {
logger.error("[getWeIdListByBlockNumber] get block {} err: {}", blockNumber, e);
blockTransactionReceipts = ((Web3j)getWeb3j())
.getBlockTransactionReceipts(BigInteger.valueOf(blockNumber)).send();
} catch (Exception e) {
logger.error("[getTransactionReceipts] get block {} err: {}", blockNumber, e);
}
if (bcosBlock == null) {
logger.info("[getWeIdListByBlockNumber] get block {} err: is null", blockNumber);
return result;
if (blockTransactionReceipts == null) {
logger.info("[getTransactionReceipts] get block {} err: is null", blockNumber);
throw new WeIdBaseException("the transactionReceipts is null.");
}
// 获取块中所有交易
List<Transaction> transList = bcosBlock
.getBlock()
.getTransactions()
.stream()
.map(transactionResult -> (Transaction) transactionResult.get())
.collect(Collectors.toList());
return blockTransactionReceipts.getBlockTransactionReceipts().getTransactionReceipts();
}

private List<WeIdPojo> getWeIdListByBlockNumber(Integer blockNumber) {
// 根据块高获取当前块里面的所有weId
List<WeIdPojo> result = new ArrayList<WeIdPojo>();
try {
List<TransactionReceipt> receipts = getTransactionReceipts(blockNumber);
int index = 0;
for (Transaction transaction : transList) {
String transHash = transaction.getHash();
BcosTransactionReceipt rec1 = ((Web3j) getWeb3j())
.getTransactionReceipt(transHash)
.send();
TransactionReceipt receipt = rec1.getTransactionReceipt().get();
for (TransactionReceipt receipt : receipts) {
List<WeIdHistoryEventEventResponse> eventlog =
weIdContract.getWeIdHistoryEventEvents(receipt);
if (CollectionUtils.isEmpty(eventlog)) {
Expand Down Expand Up @@ -684,7 +651,8 @@ private List<WeIdPojo> getWeIdListByBlockNumber(Integer blockNumber) {
);
throw new ResolveAttributeException(
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCode(),
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc());
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc(),
e);
}
return result;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/webank/weid/util/WeIdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.commons.lang3.math.NumberUtils;
import org.bcos.web3j.abi.datatypes.Address;
import org.bcos.web3j.crypto.ECKeyPair;
import org.bcos.web3j.crypto.Keys;
import org.bcos.web3j.crypto.WalletUtils;
import org.fisco.bcos.web3j.crypto.Keys;
import org.fisco.bcos.web3j.utils.Numeric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,7 +36,6 @@
import com.webank.weid.exception.WeIdBaseException;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.service.BaseService;
import com.webank.weid.service.impl.AbstractService;

/**
* The WeIdentity DID Utils.
Expand Down

0 comments on commit 6f41f09

Please sign in to comment.