Skip to content

Commit

Permalink
<feat>(transaction,contract): add transaction manager nonce and block…
Browse files Browse the repository at this point in the history
…Limit fields. (#889)
  • Loading branch information
kyonRay authored Feb 27, 2024
1 parent b765754 commit 2ada6d8
Show file tree
Hide file tree
Showing 17 changed files with 584 additions and 396 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Function {
private List<TypeReference<Type>> outputParameters;
private int transactionAttribute = 0;
private BigInteger value;
private String nonce;
private BigInteger blockLimit;

public Function(
String name, List<Type> inputParameters, List<TypeReference<?>> outputParameters) {
Expand Down Expand Up @@ -42,6 +44,19 @@ public Function(
this.value = value;
}

public Function(
String name,
List<Type> inputParameters,
List<TypeReference<?>> outputParameters,
int transactionAttribute,
BigInteger value,
String nonce,
BigInteger blockLimit) {
this(name, inputParameters, outputParameters, transactionAttribute, value);
this.nonce = nonce;
this.blockLimit = blockLimit;
}

public Function() {
this.name = "";
this.inputParameters = Collections.<Type>emptyList();
Expand Down Expand Up @@ -75,4 +90,20 @@ public BigInteger getValue() {
public void setValue(BigInteger value) {
this.value = value;
}

public String getNonce() {
return nonce;
}

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

public BigInteger getBlockLimit() {
return blockLimit;
}

public void setBlockLimit(BigInteger blockLimit) {
this.blockLimit = blockLimit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface ContractGasProvider {
boolean isEIP1559Enabled();

EIP1559Struct getEIP1559Struct(String methodId);

EIP1559Struct getEIP1559Struct(byte[] methodId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public boolean isEIP1559Enabled() {
public EIP1559Struct getEIP1559Struct(String methodId) {
return new EIP1559Struct(maxFeePerGas, maxPriorityFeePerGas, gasLimit);
}

@Override
public EIP1559Struct getEIP1559Struct(byte[] methodId) {
return new EIP1559Struct(maxFeePerGas, maxPriorityFeePerGas, gasLimit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public boolean isEIP1559Enabled() {
public EIP1559Struct getEIP1559Struct(String methodId) {
return new EIP1559Struct(BigInteger.ZERO, BigInteger.ZERO, gasLimit);
}

@Override
public EIP1559Struct getEIP1559Struct(byte[] methodId) {
return new EIP1559Struct(BigInteger.ZERO, BigInteger.ZERO, gasLimit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.fisco.bcos.sdk.v3.transaction.codec.decode.ReceiptParser;
import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderInterface;
import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderService;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicDeployRequest;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicRequest;
import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest;
Expand Down Expand Up @@ -99,16 +100,10 @@ public TransactionResponse sendTransaction(BasicRequest request)
} else {
throw new ContractCodecException("Request type error, please check.");
}
AbiEncodedRequest abiEncodedRequest = new AbiEncodedRequest(request);
abiEncodedRequest.setEncodedData(encodeMethod);

TransactionReceipt receipt =
transactionManager.sendTransaction(
request.getTo(),
encodeMethod,
request.getValue(),
request.getGasPrice(),
request.getGasLimit(),
request.getAbi(),
false);
TransactionReceipt receipt = transactionManager.sendTransaction(abiEncodedRequest);
if (Objects.nonNull(receipt)
&& (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) {
receipt.setInput(Hex.toHexStringWithPrefix(encodeMethod));
Expand Down Expand Up @@ -155,15 +150,10 @@ public TransactionResponse deployContract(BasicDeployRequest request)
} else {
throw new ContractCodecException("DeployRequest type error, please check.");
}
TransactionReceipt receipt =
transactionManager.sendTransaction(
request.getTo(),
encodeConstructor,
request.getValue(),
request.getGasPrice(),
request.getGasLimit(),
request.getAbi(),
true);
AbiEncodedRequest abiEncodedRequest = new AbiEncodedRequest(request);
abiEncodedRequest.setEncodedData(encodeConstructor);
abiEncodedRequest.setCreate(true);
TransactionReceipt receipt = transactionManager.sendTransaction(abiEncodedRequest);
if (Objects.nonNull(receipt)
&& (Objects.isNull(receipt.getInput()) || receipt.getInput().isEmpty())) {
receipt.setInput(Hex.toHexStringWithPrefix(encodeConstructor));
Expand Down Expand Up @@ -210,15 +200,9 @@ public String asyncSendTransaction(BasicRequest request, TransactionCallback cal
} else {
throw new ContractCodecException("Request type error, please check.");
}
return transactionManager.asyncSendTransaction(
request.getTo(),
encodeMethod,
request.getValue(),
request.getGasPrice(),
request.getGasLimit(),
request.getAbi(),
false,
callback);
AbiEncodedRequest abiEncodedRequest = new AbiEncodedRequest(request);
abiEncodedRequest.setEncodedData(encodeMethod);
return transactionManager.asyncSendTransaction(abiEncodedRequest, callback);
}

/**
Expand Down Expand Up @@ -260,15 +244,10 @@ public String asyncDeployContract(BasicDeployRequest request, TransactionCallbac
} else {
throw new ContractCodecException("DeployRequest type error, please check.");
}
return transactionManager.asyncSendTransaction(
request.getTo(),
encodeConstructor,
request.getValue(),
request.getGasPrice(),
request.getGasLimit(),
request.getAbi(),
true,
callback);
AbiEncodedRequest abiEncodedRequest = new AbiEncodedRequest(request);
abiEncodedRequest.setEncodedData(encodeConstructor);
abiEncodedRequest.setCreate(true);
return transactionManager.asyncSendTransaction(abiEncodedRequest, callback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,124 @@ public void setNonceProvider(NonceAndBlockLimitProvider nonceProvider) {
this.nonceProvider = nonceProvider;
}

/**
* This method is used to send transaction.
*
* @param request An instance of AbiEncodedRequest which contains the necessary information to
* create a transaction: if it is a contract creation, request should setCreate(true), and
* the abi field should be set; if it is EIP1559 transaction, request should set
* EIP1559Struct.
* @return An instance of TxPair which contains the signed transaction and the transaction hash.
* @throws JniException If there is an error during the JNI operation.
*/
@Override
public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException {
String signedTransaction = createSignedTransaction(request).getSignedTx();
BcosTransactionReceipt bcosTransactionReceipt =
client.sendTransaction(signedTransaction, false);
return bcosTransactionReceipt.getTransactionReceipt();
}

/**
* This method is used to send transaction asynchronously.
*
* @param request An instance of AbiEncodedRequest which contains the necessary information to
* create a transaction: if it is a contract creation, request should setCreate(true), and
* the abi field should be set; if it is EIP1559 transaction, request should set
* EIP1559Struct.
* @param callback callback when transaction receipt is returned
* @return transaction data hash
* @throws JniException If there is an error during the JNI operation.
*/
@Override
public String asyncSendTransaction(AbiEncodedRequest request, TransactionCallback callback)
throws JniException {
TxPair txPair = createSignedTransaction(request);
client.sendTransactionAsync(txPair.getSignedTx(), false, callback);
return txPair.getTxHash();
}

/**
* This method is used to create a signed transaction.
*
* @param request An instance of AbiEncodedRequest which contains the necessary information to
* create a transaction: if it is a contract creation, request should setCreate(true), and
* the abi field should be set; if it is EIP1559 transaction, request should set
* EIP1559Struct.
* @return An instance of TxPair which contains the signed transaction and the transaction hash.
* @throws JniException If there is an error during the JNI operation.
*/
@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();
if (getGasProvider().isEIP1559Enabled() || request.isEIP1559Enabled()) {
EIP1559Struct eip1559Struct =
request.getEip1559Struct() == null
? getGasProvider().getEIP1559Struct(methodId)
: request.getEip1559Struct();
return TransactionBuilderV1JniObj.createSignedEIP1559TransactionWithFullFields(
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(eip1559Struct.getMaxFeePerGas()),
Numeric.toHexString(eip1559Struct.getMaxPriorityFeePerGas()),
eip1559Struct.getGasLimit().longValue(),
transactionAttribute,
client.getExtraData());
}

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
*
Expand Down Expand Up @@ -152,14 +270,6 @@ public TransactionReceipt sendTransaction(
return bcosTransactionReceipt.getTransactionReceipt();
}

@Override
public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException {
String signedTransaction = createSignedTransaction(request).getSignedTx();
BcosTransactionReceipt bcosTransactionReceipt =
client.sendTransaction(signedTransaction, false);
return bcosTransactionReceipt.getTransactionReceipt();
}

/**
* This method is used to create a signed transaction.
*
Expand Down Expand Up @@ -213,55 +323,6 @@ 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 @@ -391,14 +452,6 @@ 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
Loading

0 comments on commit 2ada6d8

Please sign in to comment.