Skip to content

Commit

Permalink
<fix&feat>(transaction,build): update jni version, add transaction ma…
Browse files Browse the repository at this point in the history
…nager builder interfaces. (FISCO-BCOS#882)
  • Loading branch information
kyonRay authored Feb 5, 2024
1 parent f874ddd commit 56aac84
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 56 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ext {
webankJavaCryptoVersion = "1.0.3"
junitVersion = '4.13.2'
commonsCollections4Version = "4.4"
bcosSdkJniVersion = "3.6.0-DEV-SNAPSHOT"
bcosSdkJniVersion = "3.6.0-SNAPSHOT"
slf4jApiVerison = '1.7.36'
mockitoVersion = '4.8.0'
gsonVersion = '2.10.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,9 @@ public TransactionReceipt sendTransaction(
return bcosTransactionReceipt.getTransactionReceipt();
}

// @Override
@Override
public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException {
String signedTransaction =
createSignedTransaction(
request.getTo(),
request.getEncodedData(),
request.getValue(),
request.getGasPrice(),
request.getGasLimit(),
request.getBlockLimit(),
request.getAbi(),
request.getAbi() != null);
String signedTransaction = createSignedTransaction(request).getSignedTx();
BcosTransactionReceipt bcosTransactionReceipt =
client.sendTransaction(signedTransaction, false);
return bcosTransactionReceipt.getTransactionReceipt();
Expand Down Expand Up @@ -222,6 +213,55 @@ public String createSignedTransaction(
return txPair.getSignedTx();
}

@Override
public TxPair createSignedTransaction(AbiEncodedRequest request) throws JniException {
if (!request.isTransactionEssentialSatisfy()) {
throw new JniException(
"Transaction essential fields are not satisfied: encodedData, to.");
}
int transactionAttribute;
if (client.isWASM()) {
transactionAttribute = TransactionAttribute.LIQUID_SCALE_CODEC;
if (request.isCreate()) {
transactionAttribute |= TransactionAttribute.LIQUID_CREATE;
}
} else {
transactionAttribute = TransactionAttribute.EVM_ABI_CODEC;
}
byte[] methodId = new byte[4];
if (!request.isCreate() && (request.getEncodedData().length >= 4)) {
System.arraycopy(request.getEncodedData(), 0, methodId, 0, 4);
}
String nonce =
request.getNonce() == null ? getNonceProvider().getNonce() : request.getNonce();
BigInteger blockLimit =
request.getBlockLimit() == null
? getNonceProvider().getBlockLimit(client)
: request.getBlockLimit();
BigInteger gasPrice =
request.getGasPrice() == null
? getGasProvider().getGasPrice(methodId)
: request.getGasPrice();
BigInteger gasLimit =
request.getGasLimit() == null
? getGasProvider().getGasLimit(methodId)
: request.getGasLimit();
return TransactionBuilderV1JniObj.createSignedTransactionWithFullFields(
client.getCryptoSuite().getCryptoKeyPair().getJniKeyPair(),
client.getGroup(),
client.getChainId(),
request.getTo(),
nonce,
request.getEncodedData(),
request.isCreate() ? request.getAbi() : "",
blockLimit.longValue(),
Numeric.toHexString(request.getValue()),
Numeric.toHexString(gasPrice),
gasLimit.longValue(),
transactionAttribute,
client.getExtraData());
}

/**
* Send tx with abi field asynchronously
*
Expand Down Expand Up @@ -351,6 +391,14 @@ public String asyncSendTransaction(
return txPair.getTxHash();
}

@Override
public String asyncSendTransaction(AbiEncodedRequest request, TransactionCallback callback)
throws JniException {
TxPair txPair = createSignedTransaction(request);
client.sendTransactionAsync(txPair.getSignedTx(), false, callback);
return txPair.getTxHash();
}

/**
* Send tx with EIP1559
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.fisco.bcos.sdk.jni.common.JniException;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion;
import org.fisco.bcos.sdk.jni.utilities.tx.TxPair;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.client.protocol.model.TransactionAttribute;
import org.fisco.bcos.sdk.v3.client.protocol.request.Transaction;
Expand All @@ -23,6 +24,7 @@
import org.fisco.bcos.sdk.v3.transaction.gasProvider.ContractGasProvider;
import org.fisco.bcos.sdk.v3.transaction.gasProvider.DefaultGasProvider;
import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest;
import org.fisco.bcos.sdk.v3.transaction.nonce.DefaultNonceAndBlockLimitProvider;
import org.fisco.bcos.sdk.v3.transaction.nonce.NonceAndBlockLimitProvider;
import org.fisco.bcos.sdk.v3.transaction.signer.AsyncTransactionSignercInterface;
Expand Down Expand Up @@ -173,6 +175,12 @@ public TransactionReceipt sendTransaction(
return client.sendTransaction(signedTransaction, false).getTransactionReceipt();
}

@Override
public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException {
TxPair txPair = createSignedTransaction(request);
return client.sendTransaction(txPair.getSignedTx(), false).getTransactionReceipt();
}

/**
* This method is used to create a signed transaction.
*
Expand Down Expand Up @@ -265,6 +273,94 @@ public String createSignedTransaction(
client.getExtraData());
}

@Override
public TxPair createSignedTransaction(AbiEncodedRequest request) throws JniException {
if (!request.isTransactionEssentialSatisfy()) {
throw new JniException("Transaction essential fields are not satisfied");
}
int transactionAttribute;
if (client.isWASM()) {
transactionAttribute = TransactionAttribute.LIQUID_SCALE_CODEC;
if (request.isCreate()) {
transactionAttribute |= TransactionAttribute.LIQUID_CREATE;
}
} else {
transactionAttribute = TransactionAttribute.EVM_ABI_CODEC;
}
byte[] methodId = new byte[4];
if (!request.isCreate() && (request.getEncodedData().length >= 4)) {
System.arraycopy(request.getEncodedData(), 0, methodId, 0, 4);
}
String nonce =
request.getNonce() == null ? getNonceProvider().getNonce() : request.getNonce();
BigInteger blockLimit =
request.getBlockLimit() == null
? getNonceProvider().getBlockLimit(client)
: request.getBlockLimit();
BigInteger gasPrice =
request.getGasPrice() == null
? getGasProvider().getGasPrice(methodId)
: request.getGasPrice();
BigInteger gasLimit =
request.getGasLimit() == null
? getGasProvider().getGasLimit(methodId)
: request.getGasLimit();

String dataHash =
TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields(
cryptoType,
TransactionVersion.V1,
client.getGroup(),
client.getChainId(),
request.getTo(),
nonce,
request.getEncodedData(),
request.isCreate() ? request.getAbi() : "",
blockLimit.longValue(),
Numeric.toHexString(request.getValue()),
Numeric.toHexString(gasPrice),
gasLimit.longValue(),
"",
"");
CompletableFuture<SignatureResult> signFuture = new CompletableFuture<>();
SignatureResult signatureResult;
try {
asyncTxSigner.signAsync(
Hex.decode(dataHash),
signature -> {
signFuture.complete(signature);
return 0;
});
signatureResult =
signFuture.get(
client.getConfigOption().getNetworkConfig().getTimeout(),
TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.error("Sign transaction failed, error message: {}", e.getMessage(), e);
throw new JniException("Sign transaction failed, error message: " + e.getMessage());
}
String signedTransactionWithSignature =
TransactionBuilderV1JniObj.createSignedTransactionWithSignature(
signatureResult.encode(),
dataHash,
TransactionVersion.V1,
client.getGroup(),
client.getChainId(),
request.getTo(),
nonce,
request.getEncodedData(),
request.isCreate() ? request.getAbi() : "",
blockLimit.longValue(),
Numeric.toHexString(request.getValue()),
Numeric.toHexString(gasPrice),
gasLimit.longValue(),
"",
"",
transactionAttribute,
client.getExtraData());
return new TxPair(dataHash, signedTransactionWithSignature);
}

/**
* Send tx with abi field asynchronously
*
Expand Down Expand Up @@ -474,6 +570,14 @@ public void onResponse(TransactionReceipt receipt) {
return dataHash;
}

@Override
public String asyncSendTransaction(AbiEncodedRequest request, TransactionCallback callback)
throws JniException {
TxPair txPair = createSignedTransaction(request);
client.sendTransactionAsync(txPair.getSignedTx(), false, callback);
return txPair.getTxHash();
}

/**
* Send tx with EIP1559
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.math.BigInteger;
import org.fisco.bcos.sdk.jni.common.JniException;
import org.fisco.bcos.sdk.jni.utilities.tx.TxPair;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.client.protocol.response.Call;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.model.callback.RespCallback;
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.v3.transaction.gasProvider.ContractGasProvider;
import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest;
import org.fisco.bcos.sdk.v3.transaction.nonce.NonceAndBlockLimitProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -116,8 +118,8 @@ public abstract TransactionReceipt sendTransaction(
boolean constructor)
throws JniException;

// public abstract TransactionReceipt sendTransaction(AbiEncodedRequest request) throws
// JniException;
public abstract TransactionReceipt sendTransaction(AbiEncodedRequest request)
throws JniException;

/**
* This method is used to create a signed transaction.
Expand Down Expand Up @@ -145,6 +147,8 @@ public abstract String createSignedTransaction(
boolean constructor)
throws JniException;

public abstract TxPair createSignedTransaction(AbiEncodedRequest request) throws JniException;

/**
* Simple send tx asynchronously
*
Expand Down Expand Up @@ -230,6 +234,9 @@ public abstract String asyncSendTransaction(
TransactionCallback callback)
throws JniException;

public abstract String asyncSendTransaction(
AbiEncodedRequest request, TransactionCallback callback) throws JniException;

/**
* Send tx with EIP1559
*
Expand Down
Loading

0 comments on commit 56aac84

Please sign in to comment.