-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tars sdk impl #818
Merged
Merged
Add tars sdk impl #818
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
116891a
Add TarsClient
965044e
Update bulid.gradle, add tars-sdk
33c68d0
Merge branch 'release-3.5.0' into feature-3.5.0
morebtcg da2b615
Update fisco-bcos-tars-sdk package name
b24e670
Using google code style
0a78d38
Merge origin
morebtcg 6d40fe2
Merge remote-tracking branch 'origin/feature-3.5.0' into feature-3.5.0
morebtcg 47424d6
Revert changes of xml
morebtcg a167cef
ExtraData use string type
morebtcg 611231c
Update tars client
morebtcg 0f2be63
Add tars config and impl
morebtcg 626e332
Update loadLibrary
morebtcg a7dd1c3
Use google java format
morebtcg e2a6a4e
Merge remote-tracking branch 'upstream/release-3.5.0' into release-3.5.0
morebtcg 850e7d6
Update library path find
morebtcg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package org.fisco.bcos.sdk.v3.client; | ||
|
||
import java.math.BigInteger; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import org.fisco.bcos.sdk.tars.Callback; | ||
import org.fisco.bcos.sdk.tars.Config; | ||
import org.fisco.bcos.sdk.tars.CryptoSuite; | ||
import org.fisco.bcos.sdk.tars.LogEntry; | ||
import org.fisco.bcos.sdk.tars.RPCClient; | ||
import org.fisco.bcos.sdk.tars.SWIGTYPE_p_bcos__bytesConstRef; | ||
import org.fisco.bcos.sdk.tars.SWIGTYPE_p_bcos__h256; | ||
import org.fisco.bcos.sdk.tars.SWIGTYPE_p_std__vectorT_unsigned_char_t; | ||
import org.fisco.bcos.sdk.tars.SendTransaction; | ||
import org.fisco.bcos.sdk.tars.StringVector; | ||
import org.fisco.bcos.sdk.tars.Transaction; | ||
import org.fisco.bcos.sdk.tars.TransactionFactoryImpl; | ||
import org.fisco.bcos.sdk.tars.TransactionReceipt; | ||
import org.fisco.bcos.sdk.tars.bcos; | ||
import org.fisco.bcos.sdk.v3.client.protocol.response.BcosTransactionReceipt; | ||
import org.fisco.bcos.sdk.v3.config.ConfigOption; | ||
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; | ||
import org.fisco.bcos.sdk.v3.utils.Hex; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TarsClient extends ClientImpl implements Client { | ||
private static Logger logger = LoggerFactory.getLogger(TarsClient.class); | ||
private RPCClient tarsRPCClient; | ||
private TransactionFactoryImpl transactionFactory; | ||
private ThreadPoolExecutor asyncThreadPool; | ||
static final int queueSize = 10 * 10000; | ||
static final String libFileName = System.mapLibraryName("bcos_swig_java"); | ||
|
||
protected TarsClient(String groupID, ConfigOption configOption, long nativePointer) { | ||
super(groupID, configOption, nativePointer); | ||
String connectionString = | ||
RPCClient.toConnectionString( | ||
new StringVector(configOption.getNetworkConfig().getTarsPeers())); | ||
|
||
logger.info("Tars connection: {}", connectionString); | ||
Config config = new Config(); | ||
config.setConnectionString(connectionString); | ||
config.setSendQueueSize(queueSize); | ||
config.setTimeoutMs(configOption.getNetworkConfig().getTimeout() * 1000); | ||
tarsRPCClient = new RPCClient(config); | ||
|
||
CryptoSuite cryptoSuite = | ||
bcos.newCryptoSuite(configOption.getCryptoMaterialConfig().getUseSmCrypto()); | ||
transactionFactory = new TransactionFactoryImpl(cryptoSuite); | ||
asyncThreadPool = | ||
new ThreadPoolExecutor( | ||
1, | ||
configOption.getThreadPoolConfig().getThreadPoolSize(), | ||
0, | ||
TimeUnit.SECONDS, | ||
new ArrayBlockingQueue<Runnable>(queueSize)); | ||
} | ||
|
||
public static void loadLibrary() { | ||
URL configUrl = TarsClient.class.getClassLoader().getResource(libFileName); | ||
System.load(configUrl.getPath()); | ||
} | ||
|
||
public static void loadLibrary(String libPath) { | ||
System.load(libPath); | ||
} | ||
|
||
public static TarsClient build(String groupId, ConfigOption configOption, long nativePointer) { | ||
logger.info( | ||
"build, groupID: {}, configOption: {}, nativePointer: {}", | ||
groupId, | ||
configOption, | ||
nativePointer); | ||
return new TarsClient(groupId, configOption, nativePointer); | ||
} | ||
|
||
@Override | ||
public BcosTransactionReceipt sendTransaction( | ||
String node, String signedTransactionData, boolean withProof) { | ||
if (withProof) { | ||
return super.sendTransaction(node, signedTransactionData, withProof); | ||
} | ||
node = Objects.isNull(node) ? "" : node; | ||
|
||
Transaction transaction = toTransaction(signedTransactionData); | ||
TransactionReceipt receipt = new SendTransaction(tarsRPCClient).send(transaction).get(); | ||
BcosTransactionReceipt bcosReceipt = new BcosTransactionReceipt(); | ||
bcosReceipt.setResult(toJSONTransactionReceipt(receipt, transaction)); | ||
|
||
return bcosReceipt; | ||
} | ||
|
||
@Override | ||
public void sendTransactionAsync( | ||
String node, | ||
String signedTransactionData, | ||
boolean withProof, | ||
TransactionCallback callback) { | ||
if (withProof) { | ||
super.sendTransactionAsync(node, signedTransactionData, withProof, callback); | ||
return; | ||
} | ||
node = Objects.isNull(node) ? "" : node; | ||
Transaction transaction = toTransaction(signedTransactionData); | ||
SendTransaction sendTransaction = new SendTransaction(tarsRPCClient); | ||
|
||
sendTransaction.setCallback( | ||
new Callback() { | ||
public void onMessage() { | ||
asyncThreadPool.submit( | ||
() -> { | ||
TransactionReceipt receipt = sendTransaction.get(); | ||
callback.onResponse( | ||
toJSONTransactionReceipt(receipt, transaction)); | ||
}); | ||
} | ||
}); | ||
sendTransaction.send(transaction); | ||
} | ||
|
||
private Transaction toTransaction(String signedTransactionData) { | ||
byte[] transactionBytes = Hex.decode(signedTransactionData); | ||
|
||
SWIGTYPE_p_std__vectorT_unsigned_char_t vectorTransactionBytes = | ||
bcos.toBytes(transactionBytes); | ||
SWIGTYPE_p_bcos__bytesConstRef ref = bcos.toBytesConstRef(vectorTransactionBytes); | ||
Transaction transaction = transactionFactory.createTransaction(ref, false, false); | ||
return transaction; | ||
} | ||
|
||
private org.fisco.bcos.sdk.v3.model.TransactionReceipt toJSONTransactionReceipt( | ||
TransactionReceipt receipt, Transaction transaction) { | ||
org.fisco.bcos.sdk.v3.model.TransactionReceipt jsonReceipt = | ||
new org.fisco.bcos.sdk.v3.model.TransactionReceipt(); | ||
jsonReceipt.setTransactionHash("0x" + bcos.toHex(transaction.hash())); | ||
jsonReceipt.setVersion(receipt.version()); | ||
jsonReceipt.setReceiptHash("0x" + bcos.toHex(receipt.hash())); | ||
jsonReceipt.setBlockNumber(BigInteger.valueOf(receipt.blockNumber())); | ||
jsonReceipt.setFrom(bcos.toString(transaction.sender())); | ||
jsonReceipt.setTo(bcos.toString(transaction.to())); | ||
jsonReceipt.setGasUsed(bcos.toString(receipt.gasUsed())); | ||
jsonReceipt.setContractAddress(bcos.toString(receipt.contractAddress())); | ||
jsonReceipt.setChecksumContractAddress(jsonReceipt.getContractAddress()); // FIXME: how to? | ||
jsonReceipt.setLogEntries( | ||
bcos.logEntrySpanToVector(receipt.logEntries()).stream() | ||
.map( | ||
(LogEntry logEntry) -> { | ||
org.fisco.bcos.sdk.v3.model.TransactionReceipt.Logs | ||
rawLogEntry = | ||
new org.fisco.bcos.sdk.v3.model | ||
.TransactionReceipt.Logs(); | ||
rawLogEntry.setAddress(bcos.toString(logEntry.address())); | ||
rawLogEntry.setBlockNumber( | ||
String.valueOf(receipt.blockNumber())); | ||
rawLogEntry.setData("0x" + bcos.toHex(logEntry.data())); | ||
rawLogEntry.setTopics( | ||
bcos.h256SpanToVector(logEntry.topics()).stream() | ||
.map( | ||
(SWIGTYPE_p_bcos__h256 hash) -> { | ||
return "0x" + bcos.toHex(hash); | ||
}) | ||
.collect(Collectors.toList())); | ||
return rawLogEntry; | ||
}) | ||
.collect(Collectors.toList())); | ||
jsonReceipt.setStatus(receipt.status()); | ||
jsonReceipt.setInput("0x" + bcos.toHex(transaction.input())); | ||
jsonReceipt.setOutput("0x" + bcos.toHex(receipt.output())); | ||
jsonReceipt.setExtraData(bcos.toString(transaction.extraData())); | ||
|
||
return jsonReceipt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missing Override annotation Note