Skip to content
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 #808

Closed
wants to merge 12 commits into from
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ext {
slf4jApiVerison = '1.7.36'
mockitoVersion = '4.8.0'
gsonVersion = '2.10.1'
tarsSDKVersion = '3.5.0-SNAPSHOT'
}

// check.dependsOn integrationTest
Expand Down Expand Up @@ -60,8 +61,8 @@ allprojects {
repositories {
mavenCentral()
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://oss.sonatype.org/service/local/staging/deploy/maven2" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven { url "https://oss.sonatype.org/service/local/staging/deploy/maven2" }
}

dependencies {
Expand Down Expand Up @@ -126,6 +127,7 @@ googleJavaFormat {
}

dependencies {
api("org.fisco-bcos.sdk-tars:fisco-bcos-tars-sdk:${tarsSDKVersion}")
api("org.fisco-bcos:bcos-sdk-jni:${bcosSdkJniVersion}") {
exclude group : "org.slf4j"
exclude group : "com.fasterxml.jackson.core"
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/client/TarsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.fisco.bcos.sdk.v3.client;

import java.math.BigInteger;
import java.util.Objects;
import java.util.stream.Collectors;

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.fisco.bcos.sdk.tars.FutureReceipt;
import org.fisco.bcos.sdk.tars.LogEntry;
import org.fisco.bcos.sdk.tars.TransactionReceipt;
import org.fisco.bcos.sdk.tars.bcos;
import org.fisco.bcos.sdk.tars.RPCClient;
import org.fisco.bcos.sdk.tars.SWIGTYPE_p_bcos__h256;
import org.fisco.bcos.sdk.tars.Transaction;
import org.fisco.bcos.sdk.tars.TransactionFactoryImpl;
import org.fisco.bcos.sdk.tars.CryptoSuite;

public class TarsClient extends ClientImpl implements Client {
private RPCClient tarsRPCClient;
private TransactionFactoryImpl transactionFactory;;

protected TarsClient(String groupID, ConfigOption configOption, long nativePointer, String connectionString) {
super(groupID, configOption, nativePointer);
tarsRPCClient = new RPCClient(connectionString);

CryptoSuite cryptoSuite = bcos.newCryptoSuite(configOption.getCryptoMaterialConfig().getUseSmCrypto());
transactionFactory = new TransactionFactoryImpl(cryptoSuite);
}

@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 = transactionFactory
.createTransaction(bcos.toBytesConstRef(Hex.decode(signedTransactionData)));
TransactionReceipt receipt = tarsRPCClient.sendTransaction(transaction).get();
BcosTransactionReceipt bcosReceipt = new BcosTransactionReceipt();
bcosReceipt.setResult(convert(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 = transactionFactory
.createTransaction(bcos.toBytesConstRef(Hex.decode(signedTransactionData)));
FutureReceipt future = tarsRPCClient.sendTransaction(transaction);
Fixed Show fixed Hide fixed
}

private org.fisco.bcos.sdk.v3.model.TransactionReceipt convert(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("0x" + bcos.toHex(transaction.extraData()));

return jsonReceipt;
}
}
Loading