From 8d3b6b3fde2f6fc81945d48f3106b686d6b6d794 Mon Sep 17 00:00:00 2001 From: hanlinbo123 <152951473+hanlinbo123@users.noreply.github.com> Date: Mon, 11 Dec 2023 18:25:19 +0800 Subject: [PATCH] (block): add block transaction details list (#597) * (block): add block transaction details list (transactionFetcher): support transaction details list and transaction hash list * (block): add block transaction details list (transactionFetcher): support transaction details list and transaction hash list --------- Co-authored-by: lbhan2 --- .../fetcher/TransactionFetcher.java | 128 +++++++++++++----- .../response/TransactionListResponse.java | 99 ++++++++++++++ .../java/com/webank/wecross/stub/Block.java | 11 ++ 3 files changed, 202 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/webank/wecross/restserver/fetcher/TransactionFetcher.java b/src/main/java/com/webank/wecross/restserver/fetcher/TransactionFetcher.java index 28fc410af..2a2d47e15 100644 --- a/src/main/java/com/webank/wecross/restserver/fetcher/TransactionFetcher.java +++ b/src/main/java/com/webank/wecross/restserver/fetcher/TransactionFetcher.java @@ -9,9 +9,11 @@ import com.webank.wecross.stub.Driver; import com.webank.wecross.stub.Path; import com.webank.wecross.stub.StubConstant; +import com.webank.wecross.stub.Transaction; import com.webank.wecross.zone.Chain; import com.webank.wecross.zone.ZoneManager; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -244,53 +246,107 @@ private void recursiveFetchTransactionList( response); } - if (block.transactionsHashes.isEmpty()) { + if (block.transactionsHashes.isEmpty() + && block.transactionsWithDetail.isEmpty()) { // blank block response.setNextBlockNumber(blockNumber - 1); response.setNextOffset(0); recursiveFetchTransactionList(chain, driver, size, response, mainCallback); return; } - - int offset = response.getNextOffset(); - if (offset >= block.transactionsHashes.size()) { - logger.warn( - "Wrong offset, total txHash: {}, offset: {}, response: {}", - block.transactionsHashes.size(), - offset, - response); - mainCallback.onResponse( - new WeCrossException( - WeCrossException.ErrorCode.GET_BLOCK_ERROR, "Wrong offset"), - null); - return; - } - int index; int count = size; - for (index = offset; - index < block.transactionsHashes.size() && count > 0; - index++) { - // hash is blank - if (!"".equals(block.transactionsHashes.get(index).trim())) { - TransactionListResponse.Transaction transaction = - new TransactionListResponse.Transaction(); - transaction.setBlockNumber(blockNumber); - transaction.setTxHash(block.transactionsHashes.get(index)); - response.addTransaction(transaction); - count--; + if (!block.transactionsWithDetail.isEmpty()) { + int offset = response.getNextOffset(); + if (offset >= block.transactionsWithDetail.size()) { + logger.warn( + "Wrong offset, total txHash: {}, offset: {}, response: {}", + block.transactionsWithDetail.size(), + offset, + response); + mainCallback.onResponse( + new WeCrossException( + WeCrossException.ErrorCode.GET_BLOCK_ERROR, + "Wrong offset"), + null); + return; + } + for (index = offset; + index < block.transactionsWithDetail.size() && count > 0; + index++) { + Transaction transaction = block.transactionsWithDetail.get(index); + if (Objects.nonNull(transaction) + && StringUtils.isNotBlank( + transaction.getTransactionResponse().getHash())) { + TransactionListResponse.TransactionWithDetail transactionDetail = + new TransactionListResponse.TransactionWithDetail(); + transactionDetail.setBlockNumber(blockNumber); + transactionDetail.setTxHash( + transaction.getTransactionResponse().getHash()); + if (transaction.isTransactionByProxy()) { + transactionDetail.setPath(transaction.getResource()); + transactionDetail.setAccountIdentity( + transaction.getAccountIdentity()); + transactionDetail.setMethod( + transaction.getTransactionRequest().getMethod()); + transactionDetail.setXaTransactionID( + (String) + transaction + .getTransactionRequest() + .getOptions() + .get(StubConstant.XA_TRANSACTION_ID)); + } + response.addTransactionWithDetail(transactionDetail); + count--; + } } - } - long nextBlockNumber = - index == block.transactionsHashes.size() - ? blockNumber - 1 - : blockNumber; - int nextOffset = index == block.transactionsHashes.size() ? 0 : index; - response.setNextBlockNumber(nextBlockNumber); - response.setNextOffset(nextOffset); + long nextBlockNumber = + index == block.transactionsWithDetail.size() + ? blockNumber - 1 + : blockNumber; + int nextOffset = index == block.transactionsWithDetail.size() ? 0 : index; + response.setNextBlockNumber(nextBlockNumber); + response.setNextOffset(nextOffset); + recursiveFetchTransactionList(chain, driver, count, response, mainCallback); + } else { + int offset = response.getNextOffset(); + if (offset >= block.transactionsHashes.size()) { + logger.warn( + "Wrong offset, total txHash: {}, offset: {}, response: {}", + block.transactionsHashes.size(), + offset, + response); + mainCallback.onResponse( + new WeCrossException( + WeCrossException.ErrorCode.GET_BLOCK_ERROR, + "Wrong offset"), + null); + return; + } + for (index = offset; + index < block.transactionsHashes.size() && count > 0; + index++) { + // hash is blank + if (!"".equals(block.transactionsHashes.get(index).trim())) { + TransactionListResponse.Transaction transaction = + new TransactionListResponse.Transaction(); + transaction.setBlockNumber(blockNumber); + transaction.setTxHash(block.transactionsHashes.get(index)); + response.addTransaction(transaction); + count--; + } + } - recursiveFetchTransactionList(chain, driver, count, response, mainCallback); + long nextBlockNumber = + index == block.transactionsHashes.size() + ? blockNumber - 1 + : blockNumber; + int nextOffset = index == block.transactionsHashes.size() ? 0 : index; + response.setNextBlockNumber(nextBlockNumber); + response.setNextOffset(nextOffset); + recursiveFetchTransactionList(chain, driver, count, response, mainCallback); + } }); } diff --git a/src/main/java/com/webank/wecross/restserver/response/TransactionListResponse.java b/src/main/java/com/webank/wecross/restserver/response/TransactionListResponse.java index c22de05b4..ede0150a0 100644 --- a/src/main/java/com/webank/wecross/restserver/response/TransactionListResponse.java +++ b/src/main/java/com/webank/wecross/restserver/response/TransactionListResponse.java @@ -10,8 +10,19 @@ public class TransactionListResponse { private int nextOffset; private List transactions = Collections.synchronizedList(new LinkedList<>()); + private List transactionWithDetails = + Collections.synchronizedList(new LinkedList<>()); + public TransactionListResponse() {} + public void addTransactionWithDetail(TransactionWithDetail transactionWithDetail) { + this.transactionWithDetails.add(transactionWithDetail); + } + + public void addTransactionWithDetails(List transactionWithDetails) { + this.transactionWithDetails.addAll(transactionWithDetails); + } + public void addTransaction(Transaction transaction) { this.transactions.add(transaction); } @@ -44,6 +55,14 @@ public void setTransactions(List transactions) { this.transactions = transactions; } + public List getTransactionWithDetails() { + return transactionWithDetails; + } + + public void setTransactionWithDetails(List transactionWithDetails) { + this.transactionWithDetails = transactionWithDetails; + } + @Override public String toString() { return "TransactionListResponse{" @@ -87,4 +106,84 @@ public String toString() { + '}'; } } + + public static class TransactionWithDetail { + private String txHash; + private long blockNumber; + private String accountIdentity; + private String path; + private String method; + private String xaTransactionID; + + public String getTxHash() { + return txHash; + } + + public void setTxHash(String txHash) { + this.txHash = txHash; + } + + public long getBlockNumber() { + return blockNumber; + } + + public void setBlockNumber(long blockNumber) { + this.blockNumber = blockNumber; + } + + public String getAccountIdentity() { + return accountIdentity; + } + + public void setAccountIdentity(String accountIdentity) { + this.accountIdentity = accountIdentity; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public String getXaTransactionID() { + return xaTransactionID; + } + + public void setXaTransactionID(String xaTransactionID) { + this.xaTransactionID = xaTransactionID; + } + + @Override + public String toString() { + return "TransactionWithDetail{" + + "txHash='" + + txHash + + '\'' + + ", blockNumber=" + + blockNumber + + ", accountIdentity='" + + accountIdentity + + '\'' + + ", path='" + + path + + '\'' + + ", method='" + + method + + '\'' + + ", xaTransactionID='" + + xaTransactionID + + '\'' + + '}'; + } + } } diff --git a/src/main/java/com/webank/wecross/stub/Block.java b/src/main/java/com/webank/wecross/stub/Block.java index c00ff8ace..d1a9f35d6 100644 --- a/src/main/java/com/webank/wecross/stub/Block.java +++ b/src/main/java/com/webank/wecross/stub/Block.java @@ -9,6 +9,7 @@ public class Block { @JsonIgnore public byte[] rawBytes; public BlockHeader blockHeader; public List transactionsHashes = new LinkedList<>(); + public List transactionsWithDetail = new LinkedList<>(); public BlockHeader getBlockHeader() { return blockHeader; @@ -34,6 +35,14 @@ public void setRawBytes(byte[] rawBytes) { this.rawBytes = rawBytes; } + public List getTransactionsWithDetail() { + return transactionsWithDetail; + } + + public void setTransactionsWithDetail(List transactionsWithDetail) { + this.transactionsWithDetail = transactionsWithDetail; + } + @Override public String toString() { return "Block{" @@ -43,6 +52,8 @@ public String toString() { + blockHeader + ", transactionsHashes=" + Arrays.toString(transactionsHashes.toArray()) + + ", transactionsWithDetail=" + + Arrays.toString(transactionsWithDetail.toArray()) + '}'; } }