Skip to content

Commit

Permalink
<feat>(ContractImpl): add v1 transaction service. (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay authored Dec 15, 2023
1 parent 5cc07a1 commit a2d07c9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
10 changes: 8 additions & 2 deletions src/main/java/console/ConsoleInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
103 changes: 81 additions & 22 deletions src/main/java/console/contract/ConsoleContractImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -701,7 +743,8 @@ private void sendTransaction(
String functionName,
List<String> callParams,
ABIDefinition abiDefinition)
throws ContractCodecException, TransactionBaseException {
throws ContractCodecException, TransactionBaseException, ContractException,
JniException {
if (logger.isTraceEnabled()) {
logger.trace(
"sendTransactionAndGetResponse request, params: {}, contractAddress: {}, contractName: {}, functionName: {}, paramSize:{}, abiDefinition: {}",
Expand All @@ -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();
Expand All @@ -741,7 +792,8 @@ private void sendCall(
String contractAddress,
String functionName,
List<String> callParams)
throws TransactionBaseException, ContractCodecException {
throws TransactionBaseException, ContractCodecException, JniException,
ContractException {
if (logger.isDebugEnabled()) {
logger.debug(
"sendCall request, params: {}, contractAddress: {}, contractName: {}, functionName:{}, paramSize: {}",
Expand All @@ -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()) {
Expand Down

0 comments on commit a2d07c9

Please sign in to comment.