Skip to content

Commit

Permalink
Merge pull request #52 from yg3630536/feature/fix-weid-issue
Browse files Browse the repository at this point in the history
Solving the Query Problem of Multithread Synchronized Block Output and Optimize recursion to loop
  • Loading branch information
chenhaozx committed Mar 6, 2019
2 parents fb359ff + 337b938 commit 037753c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 64 deletions.
121 changes: 68 additions & 53 deletions src/main/java/com/webank/weid/service/impl/WeIdServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public class WeIdServiceImpl extends BaseService implements WeIdService {
* WeIdentity DID contract address.
*/
private static String weIdContractAddress;

/**
* Block number for stopping parsing.
*/
private static final int STOP_RESOLVE_BLOCK_NUMBER = 0;

/**
* The topic map.
Expand Down Expand Up @@ -307,63 +312,73 @@ private static void resolveTransaction(
String weId,
int blockNumber,
WeIdDocument result) {

if (blockNumber == 0) {
return;
}
EthBlock latestBlock = null;
try {
latestBlock =
getWeb3j().ethGetBlockByNumber(new DefaultBlockParameterNumber(blockNumber), true)
.send();
} catch (IOException e) {
logger.error(
"[resolveTransaction]:get block by number :{} failed. Exception message:{}",
blockNumber,
e);
}
if (latestBlock == null) {
logger.info(
"[resolveTransaction]:get block by number :{} . latestBlock is null",
blockNumber);
return;
}
List<Transaction> transList =
latestBlock
.getBlock()
.getTransactions()
.stream()
.map(transactionResult -> (Transaction) transactionResult.get())
.collect(Collectors.toList());

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

EthGetTransactionReceipt rec1 = getWeb3j().ethGetTransactionReceipt(transHash)
.send();
TransactionReceipt receipt = rec1.getTransactionReceipt().get();
List<Log> logs = rec1.getResult().getLogs();
for (Log log : logs) {
ResolveEventLogResult returnValue = resolveEventLog(weId, log, receipt, result);
if (returnValue.getResultStatus().equals(
ResolveEventLogStatus.STATUS_SUCCESS)) {
previousBlock = returnValue.getPreviousBlock();

int previousBlock = blockNumber;
while (previousBlock != STOP_RESOLVE_BLOCK_NUMBER) {
int currentBlockNumber = previousBlock;
EthBlock latestBlock = null;
try {
latestBlock =
getWeb3j()
.ethGetBlockByNumber(
new DefaultBlockParameterNumber(currentBlockNumber),
true
)
.send();
} catch (IOException e) {
logger.error(
"[resolveTransaction]:get block by number :{} failed. Exception message:{}",
currentBlockNumber,
e
);
}
if (latestBlock == null) {
logger.info(
"[resolveTransaction]:get block by number :{} . latestBlock is null",
currentBlockNumber
);
return;
}
List<Transaction> transList =
latestBlock
.getBlock()
.getTransactions()
.stream()
.map(transactionResult -> (Transaction) transactionResult.get())
.collect(Collectors.toList());

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

EthGetTransactionReceipt rec1 = getWeb3j().ethGetTransactionReceipt(transHash)
.send();
TransactionReceipt receipt = rec1.getTransactionReceipt().get();
List<Log> logs = rec1.getResult().getLogs();
for (Log log : logs) {
ResolveEventLogResult returnValue =
resolveEventLog(weId, log, receipt, result);
if (returnValue.getResultStatus().equals(
ResolveEventLogStatus.STATUS_SUCCESS)) {
if (returnValue.getPreviousBlock() == currentBlockNumber) {
continue;
}
previousBlock = returnValue.getPreviousBlock();
}
}
}
} catch (IOException | DataTypeCastException e) {
logger.error(
"[resolveTransaction]: get TransactionReceipt by weId :{} failed.",
weId,
e
);
throw new ResolveAttributeException(
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCode(),
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc());
}
} catch (IOException | DataTypeCastException e) {
logger.error(
"[resolveTransaction]: get TransactionReceipt by weId :{} failed.",
weId,
e);
throw new ResolveAttributeException(
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCode(),
ErrorCode.TRANSACTION_EXECUTE_ERROR.getCodeDesc());
}

resolveTransaction(weId, previousBlock, result);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/webank/weid/util/CredentialUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.request.CreateCredentialArgs;
import com.webank.weid.protocol.response.ResponseData;

/**
* The Class CredentialUtils.
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/webank/weid/util/DataTypetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
import org.bcos.web3j.abi.datatypes.generated.Int256;
import org.bcos.web3j.abi.datatypes.generated.Uint256;
import org.bcos.web3j.abi.datatypes.generated.Uint8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.exception.DataTypeCastException;

/**
* Data type conversion utilities between solidity data type and java data type.
Expand All @@ -46,8 +41,6 @@
*/
public final class DataTypetUtils {

private static final Logger logger = LoggerFactory.getLogger(DataTypetUtils.class);

/**
* Bytes array to bytes 32.
*
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/webank/weid/util/SignatureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.protocol.base.AuthenticationProperty;
import com.webank.weid.protocol.base.PublicKeyProperty;
import com.webank.weid.protocol.base.WeIdDocument;
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/webank/weid/full/TestBaseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -203,7 +202,6 @@ public static RegisterAuthorityIssuerArgs buildRegisterAuthorityIssuerArgs(

AuthorityIssuer authorityIssuer = new AuthorityIssuer();
authorityIssuer.setWeId(createWeId.getWeId());
authorityIssuer.setCreated(new Date().getTime());
authorityIssuer.setName(TestData.AUTHORITY_ISSUER_NAME);
authorityIssuer.setAccValue(TestData.AUTHORITY_ISSUER_ACCVALUE);

Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/webank/weid/util/TestJsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void testObjToJsonStr() {
Assert.assertNotNull(propertites);
}

@SuppressWarnings("unchecked")
@Test
public void testJsonStrToObj() {

Expand Down

0 comments on commit 037753c

Please sign in to comment.