Skip to content

Commit

Permalink
<feat>(block): add block transaction details list (#597)
Browse files Browse the repository at this point in the history
* <feat>(block): add block transaction details list
<feat>(transactionFetcher): support transaction details list and transaction hash list

* <feat>(block): add block transaction details list
<feat>(transactionFetcher): support transaction details list and transaction hash list

---------

Co-authored-by: lbhan2 <[email protected]>
  • Loading branch information
hanlinbo123 and lbhan2 authored Dec 11, 2023
1 parent e7ab30f commit 8d3b6b3
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ public class TransactionListResponse {
private int nextOffset;
private List<Transaction> transactions = Collections.synchronizedList(new LinkedList<>());

private List<TransactionWithDetail> transactionWithDetails =
Collections.synchronizedList(new LinkedList<>());

public TransactionListResponse() {}

public void addTransactionWithDetail(TransactionWithDetail transactionWithDetail) {
this.transactionWithDetails.add(transactionWithDetail);
}

public void addTransactionWithDetails(List<TransactionWithDetail> transactionWithDetails) {
this.transactionWithDetails.addAll(transactionWithDetails);
}

public void addTransaction(Transaction transaction) {
this.transactions.add(transaction);
}
Expand Down Expand Up @@ -44,6 +55,14 @@ public void setTransactions(List<Transaction> transactions) {
this.transactions = transactions;
}

public List<TransactionWithDetail> getTransactionWithDetails() {
return transactionWithDetails;
}

public void setTransactionWithDetails(List<TransactionWithDetail> transactionWithDetails) {
this.transactionWithDetails = transactionWithDetails;
}

@Override
public String toString() {
return "TransactionListResponse{"
Expand Down Expand Up @@ -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
+ '\''
+ '}';
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/webank/wecross/stub/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Block {
@JsonIgnore public byte[] rawBytes;
public BlockHeader blockHeader;
public List<String> transactionsHashes = new LinkedList<>();
public List<Transaction> transactionsWithDetail = new LinkedList<>();

public BlockHeader getBlockHeader() {
return blockHeader;
Expand All @@ -34,6 +35,14 @@ public void setRawBytes(byte[] rawBytes) {
this.rawBytes = rawBytes;
}

public List<Transaction> getTransactionsWithDetail() {
return transactionsWithDetail;
}

public void setTransactionsWithDetail(List<Transaction> transactionsWithDetail) {
this.transactionsWithDetail = transactionsWithDetail;
}

@Override
public String toString() {
return "Block{"
Expand All @@ -43,6 +52,8 @@ public String toString() {
+ blockHeader
+ ", transactionsHashes="
+ Arrays.toString(transactionsHashes.toArray())
+ ", transactionsWithDetail="
+ Arrays.toString(transactionsWithDetail.toArray())
+ '}';
}
}

0 comments on commit 8d3b6b3

Please sign in to comment.