From 18710f79ee0f5979dc1b5d9414fba76d64141fa4 Mon Sep 17 00:00:00 2001 From: lbhan2 Date: Wed, 13 Dec 2023 09:29:49 +0800 Subject: [PATCH 1/2] (ClientWrapper): add block transactions details (BCOSDriver): assemble block transactions details --- build.gradle | 2 +- .../wecross/stub/fabric/FabricDriver.java | 76 ++++++++++++++++--- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 98005ac..ebbd227 100644 --- a/build.gradle +++ b/build.gradle @@ -72,7 +72,7 @@ dependencies { // WeCross - implementation ('com.webank:wecross-java-stub:1.3.2-SNAPSHOT') { + implementation ('com.webank:wecross-java-stub:1.4.0-SNAPSHOT') { exclude group: 'org.fisco-bcos', module: 'tcnative' } configurations.compile.exclude(group: 'ch.qos.logback') diff --git a/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java b/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java index 1971efd..f89e6e3 100644 --- a/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java +++ b/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java @@ -1,13 +1,25 @@ package com.webank.wecross.stub.fabric; -import static com.webank.wecross.utils.FabricUtils.bytesToLong; -import static com.webank.wecross.utils.FabricUtils.longToBytes; - import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; import com.webank.wecross.account.FabricAccount; import com.webank.wecross.account.FabricAccountFactory; import com.webank.wecross.common.FabricType; -import com.webank.wecross.stub.*; +import com.webank.wecross.stub.Account; +import com.webank.wecross.stub.Block; +import com.webank.wecross.stub.BlockManager; +import com.webank.wecross.stub.Connection; +import com.webank.wecross.stub.Driver; +import com.webank.wecross.stub.Path; +import com.webank.wecross.stub.Request; +import com.webank.wecross.stub.ResourceInfo; +import com.webank.wecross.stub.Response; +import com.webank.wecross.stub.StubConstant; +import com.webank.wecross.stub.Transaction; +import com.webank.wecross.stub.TransactionContext; +import com.webank.wecross.stub.TransactionException; +import com.webank.wecross.stub.TransactionRequest; +import com.webank.wecross.stub.TransactionResponse; import com.webank.wecross.stub.fabric.FabricCustomCommand.InstallChaincodeRequest; import com.webank.wecross.stub.fabric.FabricCustomCommand.InstallCommand; import com.webank.wecross.stub.fabric.FabricCustomCommand.InstantiateChaincodeRequest; @@ -15,15 +27,21 @@ import com.webank.wecross.stub.fabric.FabricCustomCommand.UpgradeChaincodeRequest; import com.webank.wecross.stub.fabric.FabricCustomCommand.UpgradeCommand; import com.webank.wecross.stub.fabric.proxy.ProxyChaincodeResource; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.hyperledger.fabric.protos.common.Common; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import static com.webank.wecross.utils.FabricUtils.bytesToLong; +import static com.webank.wecross.utils.FabricUtils.longToBytes; public class FabricDriver implements Driver { private Logger logger = LoggerFactory.getLogger(FabricDriver.class); @@ -396,7 +414,6 @@ public void asyncGetBlock( block.setRawBytes(response.getData()); FabricBlock fabricBlock = null; - List transactionsHashes = new ArrayList<>(); try { fabricBlock = FabricBlock.encode(response.getData()); if (blockVerifierString != null) { @@ -425,11 +442,48 @@ public void asyncGetBlock( } if (!onlyHeader) { - transactionsHashes = new ArrayList<>(fabricBlock.getValidTxs()); + Common.Block b = Common.Block.parseFrom(response.getData()); + List dataList = b.getData().getDataList(); + for (ByteString data : dataList) { + try { + Common.Envelope envelope = Common.Envelope.parseFrom(data); + String payload = envelope.toString(); + int from = payload.indexOf("-----BEGIN CERTIFICATE-----"); + int end = payload.indexOf("-----END CERTIFICATE-----"); + String identity = + payload.substring(from, end) + + "-----END CERTIFICATE-----\n"; + FabricTransaction fabricTransaction = + FabricTransaction.buildFromEnvelopeBytes( + envelope.toByteArray()); + if (StringUtils.isBlank(fabricTransaction.getTxID())) { + continue; + } + Transaction transaction = + parseFabricTransaction(fabricTransaction); + transaction + .getTransactionResponse() + .setErrorCode( + FabricType.TransactionResponseStatus + .SUCCESS); + transaction.setAccountIdentity(identity); + transaction + .getTransactionResponse() + .setHash(fabricTransaction.getTxID()); + transaction.setTxBytes(data.toByteArray()); + transaction + .getTransactionResponse() + .setBlockNumber(blockNumber); + block.getTransactionsWithDetail().add(transaction); + } catch (InvalidProtocolBufferException e) { + logger.warn( + "Invalid fabric block transactions,blockNumber: {},e: {}", + blockNumber, + e.getMessage()); + } + } } block.setBlockHeader(fabricBlock.dumpWeCrossHeader()); - block.setTransactionsHashes(transactionsHashes); - callback.onResponse(null, block); } catch (Exception e) { String errorMsg = From f4a215eb6813b8b045aa3125a428e8e41257c26b Mon Sep 17 00:00:00 2001 From: lbhan2 Date: Thu, 14 Dec 2023 17:56:11 +0800 Subject: [PATCH 2/2] (ClientWrapper): add block transactions details (BCOSDriver): assemble block transactions details --- .../wecross/stub/fabric/FabricDriver.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java b/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java index f89e6e3..aa5ae3b 100644 --- a/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java +++ b/src/main/java/com/webank/wecross/stub/fabric/FabricDriver.java @@ -1,5 +1,8 @@ package com.webank.wecross.stub.fabric; +import static com.webank.wecross.utils.FabricUtils.bytesToLong; +import static com.webank.wecross.utils.FabricUtils.longToBytes; + import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.webank.wecross.account.FabricAccount; @@ -27,21 +30,17 @@ import com.webank.wecross.stub.fabric.FabricCustomCommand.UpgradeChaincodeRequest; import com.webank.wecross.stub.fabric.FabricCustomCommand.UpgradeCommand; import com.webank.wecross.stub.fabric.proxy.ProxyChaincodeResource; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.hyperledger.fabric.protos.common.Common; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; - -import static com.webank.wecross.utils.FabricUtils.bytesToLong; -import static com.webank.wecross.utils.FabricUtils.longToBytes; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.hyperledger.fabric.protos.common.Common; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class FabricDriver implements Driver { private Logger logger = LoggerFactory.getLogger(FabricDriver.class);