|
| 1 | +package org.fisco.bcos.sdk.v3.client; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.concurrent.ArrayBlockingQueue; |
| 7 | +import java.util.concurrent.ThreadPoolExecutor; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import org.fisco.bcos.sdk.tars.Callback; |
| 11 | +import org.fisco.bcos.sdk.tars.Config; |
| 12 | +import org.fisco.bcos.sdk.tars.CryptoSuite; |
| 13 | +import org.fisco.bcos.sdk.tars.LogEntry; |
| 14 | +import org.fisco.bcos.sdk.tars.RPCClient; |
| 15 | +import org.fisco.bcos.sdk.tars.SWIGTYPE_p_bcos__bytesConstRef; |
| 16 | +import org.fisco.bcos.sdk.tars.SWIGTYPE_p_bcos__h256; |
| 17 | +import org.fisco.bcos.sdk.tars.SWIGTYPE_p_std__vectorT_unsigned_char_t; |
| 18 | +import org.fisco.bcos.sdk.tars.SendTransaction; |
| 19 | +import org.fisco.bcos.sdk.tars.StringVector; |
| 20 | +import org.fisco.bcos.sdk.tars.Transaction; |
| 21 | +import org.fisco.bcos.sdk.tars.TransactionFactoryImpl; |
| 22 | +import org.fisco.bcos.sdk.tars.TransactionReceipt; |
| 23 | +import org.fisco.bcos.sdk.tars.bcos; |
| 24 | +import org.fisco.bcos.sdk.v3.client.protocol.response.BcosTransactionReceipt; |
| 25 | +import org.fisco.bcos.sdk.v3.config.ConfigOption; |
| 26 | +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; |
| 27 | +import org.fisco.bcos.sdk.v3.utils.Hex; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +public class TarsClient extends ClientImpl implements Client { |
| 32 | + private static Logger logger = LoggerFactory.getLogger(TarsClient.class); |
| 33 | + private RPCClient tarsRPCClient; |
| 34 | + private TransactionFactoryImpl transactionFactory; |
| 35 | + private ThreadPoolExecutor asyncThreadPool; |
| 36 | + static final int queueSize = 10 * 10000; |
| 37 | + static final String libFileName = System.mapLibraryName("bcos_swig_java"); |
| 38 | + |
| 39 | + protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) { |
| 40 | + super(groupID, configOption, nativePointer); |
| 41 | + String connectionString = |
| 42 | + RPCClient.toConnectionString( |
| 43 | + new StringVector(configOption.getNetworkConfig().getTarsPeers())); |
| 44 | + |
| 45 | + logger.info("Tars connection: {}", connectionString); |
| 46 | + Config config = new Config(); |
| 47 | + config.setConnectionString(connectionString); |
| 48 | + config.setSendQueueSize(queueSize); |
| 49 | + config.setTimeoutMs(configOption.getNetworkConfig().getTimeout() * 1000); |
| 50 | + tarsRPCClient = new RPCClient(config); |
| 51 | + |
| 52 | + CryptoSuite cryptoSuite = |
| 53 | + bcos.newCryptoSuite(configOption.getCryptoMaterialConfig().getUseSmCrypto()); |
| 54 | + transactionFactory = new TransactionFactoryImpl(cryptoSuite); |
| 55 | + asyncThreadPool = |
| 56 | + new ThreadPoolExecutor( |
| 57 | + 1, |
| 58 | + configOption.getThreadPoolConfig().getThreadPoolSize(), |
| 59 | + 0, |
| 60 | + TimeUnit.SECONDS, |
| 61 | + new ArrayBlockingQueue<Runnable>(queueSize)); |
| 62 | + } |
| 63 | + |
| 64 | + public static void loadLibrary() { |
| 65 | + URL configUrl = TarsClient.class.getClassLoader().getResource(libFileName); |
| 66 | + System.load(configUrl.getPath()); |
| 67 | + } |
| 68 | + |
| 69 | + public static void loadLibrary(String libPath) { |
| 70 | + System.load(libPath); |
| 71 | + } |
| 72 | + |
| 73 | + public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) { |
| 74 | + logger.info( |
| 75 | + "build, groupID: {}, configOption: {}, nativePointer: {}", |
| 76 | + groupId, |
| 77 | + configOption, |
| 78 | + nativePointer); |
| 79 | + return new TarsClient(groupId, configOption, nativePointer); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public BcosTransactionReceipt sendTransaction( |
| 84 | + String node, String signedTransactionData, boolean withProof) { |
| 85 | + if (withProof) { |
| 86 | + return super.sendTransaction(node, signedTransactionData, withProof); |
| 87 | + } |
| 88 | + node = Objects.isNull(node) ? "" : node; |
| 89 | + |
| 90 | + Transaction transaction = toTransaction(signedTransactionData); |
| 91 | + TransactionReceipt receipt = new SendTransaction(tarsRPCClient).send(transaction).get(); |
| 92 | + BcosTransactionReceipt bcosReceipt = new BcosTransactionReceipt(); |
| 93 | + bcosReceipt.setResult(toJSONTransactionReceipt(receipt, transaction)); |
| 94 | + |
| 95 | + return bcosReceipt; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void sendTransactionAsync( |
| 100 | + String node, |
| 101 | + String signedTransactionData, |
| 102 | + boolean withProof, |
| 103 | + TransactionCallback callback) { |
| 104 | + if (withProof) { |
| 105 | + super.sendTransactionAsync(node, signedTransactionData, withProof, callback); |
| 106 | + return; |
| 107 | + } |
| 108 | + node = Objects.isNull(node) ? "" : node; |
| 109 | + Transaction transaction = toTransaction(signedTransactionData); |
| 110 | + SendTransaction sendTransaction = new SendTransaction(tarsRPCClient); |
| 111 | + |
| 112 | + sendTransaction.setCallback( |
| 113 | + new Callback() { |
| 114 | + public void onMessage() { |
| 115 | + asyncThreadPool.submit( |
| 116 | + () -> { |
| 117 | + TransactionReceipt receipt = sendTransaction.get(); |
| 118 | + callback.onResponse( |
| 119 | + toJSONTransactionReceipt(receipt, transaction)); |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + sendTransaction.send(transaction); |
| 124 | + } |
| 125 | + |
| 126 | + private Transaction toTransaction(String signedTransactionData) { |
| 127 | + byte[] transactionBytes = Hex.decode(signedTransactionData); |
| 128 | + |
| 129 | + SWIGTYPE_p_std__vectorT_unsigned_char_t vectorTransactionBytes = |
| 130 | + bcos.toBytes(transactionBytes); |
| 131 | + SWIGTYPE_p_bcos__bytesConstRef ref = bcos.toBytesConstRef(vectorTransactionBytes); |
| 132 | + Transaction transaction = transactionFactory.createTransaction(ref, false, false); |
| 133 | + return transaction; |
| 134 | + } |
| 135 | + |
| 136 | + private org.fisco.bcos.sdk.v3.model.TransactionReceipt toJSONTransactionReceipt( |
| 137 | + TransactionReceipt receipt, Transaction transaction) { |
| 138 | + org.fisco.bcos.sdk.v3.model.TransactionReceipt jsonReceipt = |
| 139 | + new org.fisco.bcos.sdk.v3.model.TransactionReceipt(); |
| 140 | + jsonReceipt.setTransactionHash("0x" + bcos.toHex(transaction.hash())); |
| 141 | + jsonReceipt.setVersion(receipt.version()); |
| 142 | + jsonReceipt.setReceiptHash("0x" + bcos.toHex(receipt.hash())); |
| 143 | + jsonReceipt.setBlockNumber(BigInteger.valueOf(receipt.blockNumber())); |
| 144 | + jsonReceipt.setFrom(bcos.toString(transaction.sender())); |
| 145 | + jsonReceipt.setTo(bcos.toString(transaction.to())); |
| 146 | + jsonReceipt.setGasUsed(bcos.toString(receipt.gasUsed())); |
| 147 | + jsonReceipt.setContractAddress(bcos.toString(receipt.contractAddress())); |
| 148 | + jsonReceipt.setChecksumContractAddress(jsonReceipt.getContractAddress()); // FIXME: how to? |
| 149 | + jsonReceipt.setLogEntries( |
| 150 | + bcos.logEntrySpanToVector(receipt.logEntries()).stream() |
| 151 | + .map( |
| 152 | + (LogEntry logEntry) -> { |
| 153 | + org.fisco.bcos.sdk.v3.model.TransactionReceipt.Logs |
| 154 | + rawLogEntry = |
| 155 | + new org.fisco.bcos.sdk.v3.model |
| 156 | + .TransactionReceipt.Logs(); |
| 157 | + rawLogEntry.setAddress(bcos.toString(logEntry.address())); |
| 158 | + rawLogEntry.setBlockNumber( |
| 159 | + String.valueOf(receipt.blockNumber())); |
| 160 | + rawLogEntry.setData("0x" + bcos.toHex(logEntry.data())); |
| 161 | + rawLogEntry.setTopics( |
| 162 | + bcos.h256SpanToVector(logEntry.topics()).stream() |
| 163 | + .map( |
| 164 | + (SWIGTYPE_p_bcos__h256 hash) -> { |
| 165 | + return "0x" + bcos.toHex(hash); |
| 166 | + }) |
| 167 | + .collect(Collectors.toList())); |
| 168 | + return rawLogEntry; |
| 169 | + }) |
| 170 | + .collect(Collectors.toList())); |
| 171 | + jsonReceipt.setStatus(receipt.status()); |
| 172 | + jsonReceipt.setInput("0x" + bcos.toHex(transaction.input())); |
| 173 | + jsonReceipt.setOutput("0x" + bcos.toHex(receipt.output())); |
| 174 | + jsonReceipt.setExtraData(bcos.toString(transaction.extraData())); |
| 175 | + |
| 176 | + return jsonReceipt; |
| 177 | + } |
| 178 | +} |
0 commit comments