From a2d07c9cb2049e49dbec8d376db4ddc778f318ad Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:31:10 +0800 Subject: [PATCH] (ContractImpl): add v1 transaction service. (#809) --- src/main/java/console/ConsoleInitializer.java | 10 +- .../console/contract/ConsoleContractImpl.java | 103 ++++++++++++++---- 2 files changed, 89 insertions(+), 24 deletions(-) diff --git a/src/main/java/console/ConsoleInitializer.java b/src/main/java/console/ConsoleInitializer.java index 3a8920e3..c3a9c97e 100644 --- a/src/main/java/console/ConsoleInitializer.java +++ b/src/main/java/console/ConsoleInitializer.java @@ -69,9 +69,15 @@ public void init(String[] args) throws ConfigException { accountInfo = loadConfig(args); loadAccountInfo(accountInfo, groupID); + + boolean useV1TxService = false; + if (args.length > 3 && "-v1".equals(args[3])) { + // use v1 transaction service + useV1TxService = true; + } this.consoleClientFace = new ConsoleClientImpl(client); this.precompiledFace = new PrecompiledImpl(client); - this.consoleContractFace = new ConsoleContractImpl(client); + this.consoleContractFace = new ConsoleContractImpl(client, useV1TxService); this.collaborationFace = new CollaborationImpl(client); this.authFace = new AuthImpl(client); } @@ -140,7 +146,7 @@ private AccountInfo loadConfig(String[] args) throws ConfigException { return new AccountInfo("HSM", "", ""); } - if (args.length == 3) { + if (args.length >= 3) { return loadAccount(bcosSDK, args); } } catch (NumberFormatException e) { diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index efafe44c..74113d32 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -37,6 +37,7 @@ import org.apache.commons.io.FilenameUtils; import org.fisco.bcos.codegen.v3.exceptions.CodeGenException; import org.fisco.bcos.codegen.v3.utils.CodeGenUtils; +import org.fisco.bcos.sdk.jni.common.JniException; import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.exceptions.ClientException; import org.fisco.bcos.sdk.v3.client.protocol.response.Abi; @@ -56,8 +57,12 @@ import org.fisco.bcos.sdk.v3.model.TransactionReceipt; import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionProcessorInterface; import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory; -import org.fisco.bcos.sdk.v3.transaction.manager.Transactionv2.ProxySignTransactionManager; -import org.fisco.bcos.sdk.v3.transaction.manager.Transactionv2.TransferTransactionService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.AssembleTransactionService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.ProxySignTransactionManager; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.TransferTransactionService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.utils.TransactionRequestBuilder; import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; @@ -78,9 +83,13 @@ public class ConsoleContractImpl implements ConsoleContractFace { private final Client client; private final AssembleTransactionProcessorInterface assembleTransactionProcessor; + // new version tx v2 + private final AssembleTransactionService assembleTransactionService; private final TransferTransactionService transferTransactionService; private final BFSService bfsService; + private boolean useTransactionV1 = false; + public ConsoleContractImpl(Client client) { this.client = client; CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair(); @@ -91,6 +100,12 @@ public ConsoleContractImpl(Client client) { ProxySignTransactionManager proxySignTransactionManager = new ProxySignTransactionManager(client); transferTransactionService = new TransferTransactionService(proxySignTransactionManager); + assembleTransactionService = new AssembleTransactionService(client); + } + + public ConsoleContractImpl(Client client, boolean useTransactionV1) { + this(client); + this.useTransactionV1 = useTransactionV1; } @Override @@ -240,9 +255,17 @@ public TransactionResponse deploySolidity( if (sm) { bin = abiAndBin.getSmBin(); } - TransactionResponse response = - this.assembleTransactionProcessor.deployAndGetResponseWithStringParams( - abiAndBin.getAbi(), bin, tempInputParams, null); + TransactionResponse response; + if (useTransactionV1) { + DeployTransactionRequestWithStringParams request = + new TransactionRequestBuilder(abiAndBin.getAbi(), bin) + .buildDeployStringParamsRequest(tempInputParams); + response = assembleTransactionService.deployContractWithStringParams(request); + } else { + response = + this.assembleTransactionProcessor.deployAndGetResponseWithStringParams( + abiAndBin.getAbi(), bin, tempInputParams, null); + } if (response.getReturnCode() != PrecompiledRetCode.CODE_SUCCESS.getCode()) { System.out.println("deploy contract for " + contractName + " failed!"); System.out.println("return message: " + response.getReturnMessage()); @@ -270,6 +293,8 @@ public TransactionResponse deploySolidity( } catch (ClientException | CompileContractException | IOException + | ContractException + | JniException | ContractCodecException e) { throw new ConsoleMessageException("deploy contract failed for " + e.getMessage(), e); } @@ -294,9 +319,18 @@ public TransactionResponse deployWasm( String binStr = Hex.toHexString(bin); File abiFile = new File(abiPath); String abi = FileUtils.readFileToString(abiFile); - TransactionResponse response = - this.assembleTransactionProcessor.deployAndGetResponseWithStringParams( - abi, binStr, inputParams, path); + TransactionResponse response; + if (useTransactionV1) { + DeployTransactionRequestWithStringParams request = + new TransactionRequestBuilder(abi, binStr) + .setTo(path) + .buildDeployStringParamsRequest(inputParams); + response = assembleTransactionService.deployContractWithStringParams(request); + } else { + response = + this.assembleTransactionProcessor.deployAndGetResponseWithStringParams( + abi, binStr, inputParams, path); + } if (response.getReturnCode() != PrecompiledRetCode.CODE_SUCCESS.getCode()) { System.out.println("deploy contract for " + path + " failed!"); System.out.println("return message: " + response.getReturnMessage()); @@ -330,7 +364,11 @@ public TransactionResponse deployWasm( ContractCompiler.saveAbiAndBin( client.getGroup(), abiAndBin, contractName, contractAddress); return response; - } catch (ClientException | IOException | ContractCodecException e) { + } catch (ClientException + | IOException + | JniException + | ContractException + | ContractCodecException e) { throw new ConsoleMessageException("deploy contract failed due to:" + e.getMessage(), e); } } @@ -691,6 +729,10 @@ protected void callContract( + errorMessage + ", please refer to " + StatusCodeLink.txReceiptStatusLink); + } catch (JniException | ContractException e) { + System.out.println( + "call for " + contractName + " failed, contractAddress: " + contractAddress); + System.out.println(e.getMessage()); } } @@ -701,7 +743,8 @@ private void sendTransaction( String functionName, List callParams, ABIDefinition abiDefinition) - throws ContractCodecException, TransactionBaseException { + throws ContractCodecException, TransactionBaseException, ContractException, + JniException { if (logger.isTraceEnabled()) { logger.trace( "sendTransactionAndGetResponse request, params: {}, contractAddress: {}, contractName: {}, functionName: {}, paramSize:{}, abiDefinition: {}", @@ -712,9 +755,17 @@ private void sendTransaction( callParams.size(), abiDefinition); } - TransactionResponse response = - assembleTransactionProcessor.sendTransactionWithStringParamsAndGetResponse( - contractAddress, abiAndBin.getAbi(), functionName, callParams); + TransactionResponse response; + if (useTransactionV1) { + TransactionRequestWithStringParams request = + new TransactionRequestBuilder(abiAndBin.getAbi(), functionName, contractAddress) + .buildStringParamsRequest(callParams); + response = assembleTransactionService.sendTransactionWithStringParams(request); + } else { + response = + assembleTransactionProcessor.sendTransactionWithStringParamsAndGetResponse( + contractAddress, abiAndBin.getAbi(), functionName, callParams); + } System.out.println( "transaction hash: " + response.getTransactionReceipt().getTransactionHash()); ConsoleUtils.singleLine(); @@ -741,7 +792,8 @@ private void sendCall( String contractAddress, String functionName, List callParams) - throws TransactionBaseException, ContractCodecException { + throws TransactionBaseException, ContractCodecException, JniException, + ContractException { if (logger.isDebugEnabled()) { logger.debug( "sendCall request, params: {}, contractAddress: {}, contractName: {}, functionName:{}, paramSize: {}", @@ -752,14 +804,21 @@ private void sendCall( callParams.size()); } CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair(); - CallResponse response = - assembleTransactionProcessor.sendCallWithSignWithStringParams( - cryptoKeyPair.getAddress(), - contractAddress, - abiAndBin.getAbi(), - functionName, - callParams); - + CallResponse response; + if (useTransactionV1) { + TransactionRequestWithStringParams request = + new TransactionRequestBuilder(abiAndBin.getAbi(), functionName, contractAddress) + .buildStringParamsRequest(callParams); + response = assembleTransactionService.sendCall(request); + } else { + response = + assembleTransactionProcessor.sendCallWithSignWithStringParams( + cryptoKeyPair.getAddress(), + contractAddress, + abiAndBin.getAbi(), + functionName, + callParams); + } ConsoleUtils.singleLine(); System.out.println("Return code: " + response.getReturnCode()); if (response.getReturnCode() == PrecompiledRetCode.CODE_SUCCESS.getCode()) {