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

<feat>(transaction,contract): add transaction manager extension field, adapt v2 transaction, add contract wrapper. #899

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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"
bcosSdkJniVersion = "3.7.0-SNAPSHOT"
slf4jApiVerison = '1.7.36'
mockitoVersion = '4.8.0'
gsonVersion = '2.10.1'
Expand All @@ -35,7 +35,7 @@ ext {
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.java-sdk'
version = '3.6.0'
version = '3.7.0-SNAPSHOT'

apply plugin: 'maven-publish'
apply plugin: 'idea'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testTxHashCalculate() throws IOException {
// abi
byteArrayOutputStream.write(abi.getBytes());

if (transactionResponse.getVersion() == TransactionVersion.V1.getValue()) {
if (transactionResponse.getVersion() >= TransactionVersion.V1.getValue()) {
byteArrayOutputStream.write(transactionResponse.getValue().getBytes());
byteArrayOutputStream.write(transactionResponse.getGasPrice().getBytes());
byteArrayOutputStream.write(
Expand All @@ -125,6 +125,10 @@ public void testTxHashCalculate() throws IOException {
byteArrayOutputStream.write(transactionResponse.getMaxPriorityFeePerGas().getBytes());
}

if(transactionResponse.getVersion() >= TransactionVersion.V2.getValue()){
byteArrayOutputStream.write(transactionResponse.getExtension());
}

String hash = "";
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
hash = Keccak256.calculateHash(byteArrayOutputStream.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionData;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV1;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV2;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion;
import org.fisco.bcos.sdk.v3.client.exceptions.ClientException;
Expand Down Expand Up @@ -68,6 +69,9 @@ public class JsonTransactionResponse {
private String maxFeePerGas = "";
private String maxPriorityFeePerGas = "";

// Fields of v2 transaction
private byte[] extension = null;

public JsonTransactionResponse() {}

public String getAbi() {
Expand Down Expand Up @@ -232,6 +236,14 @@ public void setMaxPriorityFeePerGas(String maxPriorityFeePerGas) {
this.maxPriorityFeePerGas = maxPriorityFeePerGas;
}

public byte[] getExtension() {
return extension;
}

public void setExtension(byte[] extension) {
this.extension = extension;
}

/**
* This method is not correct, only can decode from method {@link #writeToHexString()}, which
* not enable to send transaction to blockchain.
Expand Down Expand Up @@ -304,6 +316,24 @@ public String calculateHash(CryptoSuite cryptoSuite) throws ClientException {
*/
public String calculateHash(int cryptoSuiteType) throws ClientException {
try {
// FIXME: core dump in here
// return TransactionStructBuilderJniObj.calcTransactionDataStructHash(
// cryptoSuiteType,
// new TransactionDataV2()
// .buildExtension(getExtension())
// .buildValue(getValue())
// .buildGasPrice(getGasPrice())
// .buildGasLimit(getGasLimit())
// .buildMaxFeePerGas(getMaxFeePerGas())
// .buildMaxPriorityFeePerGas(getMaxPriorityFeePerGas())
// .buildVersion(getVersion())
// .buildGroupId(getGroupID())
// .buildChainId(getChainID())
// .buildTo(getTo())
// .buildNonce(new String(Hex.decode(getNonce())))
// .buildInput(Hex.decode(getInput()))
// .buildAbi(getAbi())
// .buildBlockLimit(getBlockLimit()));
return TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields(
cryptoSuiteType,
(getVersion() == 0 ? TransactionVersion.V0 : TransactionVersion.V1),
Expand Down Expand Up @@ -371,19 +401,24 @@ public byte[] encodeTransactionData() throws IOException {
// abi
byteArrayOutputStream.write(getAbi().getBytes());

if (getVersion() == TransactionVersion.V1.getValue()) {
if (getVersion() >= TransactionVersion.V1.getValue()) {
byteArrayOutputStream.write(getValue().getBytes());
byteArrayOutputStream.write(getGasPrice().getBytes());
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(getGasLimit()), 8));
byteArrayOutputStream.write(getMaxFeePerGas().getBytes());
byteArrayOutputStream.write(getMaxPriorityFeePerGas().getBytes());
}

if (getVersion() >= TransactionVersion.V2.getValue()) {
byteArrayOutputStream.write(getExtension());
}

return byteArrayOutputStream.toByteArray();
}

public static JsonTransactionResponse decodeTransaction(String hexString) throws JniException {
Transaction transactionV1 =
TransactionStructBuilderJniObj.decodeTransactionStructV1(hexString);
TransactionStructBuilderJniObj.decodeTransactionStructV2(hexString);
TransactionData transactionData = transactionV1.getTransactionData();
JsonTransactionResponse jsonTransactionResponse = new JsonTransactionResponse();
jsonTransactionResponse.setVersion(transactionData.getVersion());
Expand All @@ -404,8 +439,7 @@ public static JsonTransactionResponse decodeTransaction(String hexString) throws
Hex.toHexStringWithPrefixNullable(transactionV1.getSignature(), ""));
jsonTransactionResponse.setImportTime(transactionV1.getImportTime());

if (transactionData instanceof TransactionDataV1
&& transactionData.getVersion() == TransactionVersion.V1.getValue()) {
if (transactionData.getVersion() >= TransactionVersion.V1.getValue()) {
TransactionDataV1 transactionDataV1 = (TransactionDataV1) transactionData;
jsonTransactionResponse.setValue(transactionDataV1.getValue());
jsonTransactionResponse.setGasPrice(transactionDataV1.getGasPrice());
Expand All @@ -414,6 +448,11 @@ public static JsonTransactionResponse decodeTransaction(String hexString) throws
jsonTransactionResponse.setMaxPriorityFeePerGas(
transactionDataV1.getMaxPriorityFeePerGas());
}

if (transactionData.getVersion() >= TransactionVersion.V2.getValue()) {
TransactionDataV2 transactionDataV2 = (TransactionDataV2) transactionData;
jsonTransactionResponse.setExtension(transactionDataV2.getExtension());
}
return jsonTransactionResponse;
}

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@
import org.fisco.bcos.sdk.v3.transaction.codec.decode.ReceiptParser;
import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessor;
import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.DefaultTransactionManager;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.TransactionManager;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.utils.TransactionRequestBuilder;
import org.fisco.bcos.sdk.v3.transaction.model.dto.CallRequest;
import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -560,6 +563,31 @@ protected String asyncExecuteTransaction(Function function, TransactionCallback
function.getValue());
}

protected String asyncExecuteTransaction(
ContractWrapper contractWrapper, TransactionCallback callback) {
try {
TransactionManager txManager = this.transactionManager;
if (txManager == null) {
txManager = new DefaultTransactionManager(client);
}
AbiEncodedRequest abiEncodedRequest =
new TransactionRequestBuilder()
.setNonce(contractWrapper.getNonce())
.setBlockLimit(contractWrapper.getBlockLimit())
.setExtension(contractWrapper.getExtension())
.setValue(
contractWrapper.getValue() != null
? contractWrapper.getValue().toBigIntegerExact()
: null)
.buildAbiEncodedRequest(
this.functionEncoder.encode(contractWrapper.getFunction()));
return txManager.asyncSendTransaction(abiEncodedRequest, callback);
} catch (JniException | ContractException e) {
logger.error("sendTransaction failed, error info: {}", e.getMessage(), e);
}
return null;
}

protected TransactionReceipt executeTransaction(Function function) {

if (transactionManager != null) {
Expand All @@ -586,6 +614,31 @@ protected TransactionReceipt executeTransaction(Function function) {
txAttribute);
}

protected TransactionReceipt executeTransaction(ContractWrapper contractWrapper) {
TransactionManager txManager = this.transactionManager;
if (txManager == null) {
txManager = new DefaultTransactionManager(client);
}
TransactionReceipt transactionReceipt = null;
try {
AbiEncodedRequest abiEncodedRequest =
new TransactionRequestBuilder()
.setNonce(contractWrapper.getNonce())
.setBlockLimit(contractWrapper.getBlockLimit())
.setExtension(contractWrapper.getExtension())
.setValue(
contractWrapper.getValue() != null
? contractWrapper.getValue().toBigIntegerExact()
: null)
.buildAbiEncodedRequest(
this.functionEncoder.encode(contractWrapper.getFunction()));
transactionReceipt = txManager.sendTransaction(abiEncodedRequest);
} catch (JniException | ContractException e) {
logger.error("sendTransaction failed, error info: {}", e.getMessage(), e);
}
return transactionReceipt;
}

protected TransactionReceipt executeDeployTransaction(byte[] data, String abi) {
if (transactionManager != null) {

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/contract/ContractWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.fisco.bcos.sdk.v3.contract;

import java.math.BigDecimal;
import java.math.BigInteger;
import org.fisco.bcos.sdk.v3.codec.datatypes.Function;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;

public class ContractWrapper {
private final Contract contract;
private Function function;
private String nonce;
private BigInteger blockLimit;
private BigDecimal value;
private byte[] extension;

public ContractWrapper(Contract contract) {
this.contract = contract;
}

public ContractWrapper(Contract contract, Function function) {
this.contract = contract;
this.function = function;
}

public Function getFunction() {
return function;
}

public ContractWrapper setFunction(Function function) {
this.function = function;
return this;
}

public String getNonce() {
return nonce;
}

public ContractWrapper setNonce(String nonce) {
this.nonce = nonce;
return this;
}

public BigInteger getBlockLimit() {
return blockLimit;
}

public ContractWrapper setBlockLimit(BigInteger blockLimit) {
this.blockLimit = blockLimit;
return this;
}

public BigDecimal getValue() {
return value;
}

public ContractWrapper setValue(BigDecimal value) {
this.value = value;
return this;
}

public byte[] getExtension() {
return extension;
}

public ContractWrapper setExtension(byte[] extension) {
this.extension = extension;
return this;
}

public TransactionReceipt send() {
return contract.executeTransaction(this);
}

public String asyncSend(TransactionCallback callback) {
return contract.asyncExecuteTransaction(this, callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public byte[] encodeTransactionReceipt() throws IOException {
byteArrayOutputStream.write(getContractAddress().getBytes());
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(getStatus()), 4));
byteArrayOutputStream.write(getOutput().getBytes());
if (getVersion() == TransactionVersion.V1.getValue()) {
if (getVersion() >= TransactionVersion.V1.getValue()) {
byteArrayOutputStream.write(getEffectiveGasPrice().getBytes());
}

Expand Down
Loading
Loading