Skip to content

Commit

Permalink
<feat>(console): add sendRawTransaction feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Oct 15, 2024
1 parent dd3c678 commit 0e7c940
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/main/java/console/command/category/ContractOpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public static void setIsWasm(boolean wasm) {
DEPLOY.setMinParamLength(wasm ? 2 : 1);
}

public static final CommandInfo SEND_RAW_TRANSACTION =
new CommandInfo(
"sendRawTransaction",
"Send a raw signed transaction to chain",
() -> HelpInfo.deployHelp(isWasm),
(consoleInitializer, params, pwd) ->
consoleInitializer.getConsoleContractFace().sendRawTransaction(params),
1,
1);

public static final CommandInfo DEPLOY =
new CommandInfo(
"deploy",
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ public static void getTotalTransactionCountHelp() {
System.out.println("Usage: \ngetTotalTransactionCount");
}

public static void sendRawTransactionHelp() {
System.out.println("Send a raw signed transaction to chain.");
System.out.println("Usage: \nsendRawTransaction [rawTransactionHex]");
System.out.println("* rawTransactionHex -- Raw signed transaction in hex, and it is encoded by Tars.");

}

public static void deployHelp(boolean isWasm) {
if (!isWasm) {
System.out.println(
Expand Down Expand Up @@ -413,7 +420,7 @@ public static void systemConfigHelper() {
+ SystemConfigService.COMPATIBILITY_VERSION
+ " >= "
+ EnumNodeVersion.convertToVersion(feature.enableVersion())
.toVersionString()
.toVersionString()
+ ", value can only set 1 or greater equal than 1.");
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/console/contract/ConsoleContractFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import console.ConsoleInitializer;

public interface ConsoleContractFace {

void sendRawTransaction(String[] params) throws Exception;

void deploy(String[] params, String pwd) throws Exception;

void call(String[] params, String pwd) throws Exception;
Expand Down
67 changes: 48 additions & 19 deletions src/main/java/console/contract/ConsoleContractImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import console.contract.utils.ContractCompiler;
import console.exception.CompileSolidityException;
import console.exception.ConsoleMessageException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -35,6 +36,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.fisco.bcos.codegen.v3.exceptions.CodeGenException;
Expand All @@ -43,6 +45,7 @@
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;
import org.fisco.bcos.sdk.v3.client.protocol.response.BcosTransactionReceipt;
import org.fisco.bcos.sdk.v3.codec.ContractCodecException;
import org.fisco.bcos.sdk.v3.codec.EventEncoder;
import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2;
Expand Down Expand Up @@ -124,6 +127,32 @@ public void setExtension(byte[] extension) {
this.extension = extension;
}

@Override
public void sendRawTransaction(String[] params) throws Exception {
String rawTx = params[0];
BcosTransactionReceipt bcosTransactionReceipt = client.sendTransaction(rawTx, false);
TransactionReceipt transactionReceipt = bcosTransactionReceipt.getTransactionReceipt();
System.out.println(
"transaction hash: " + transactionReceipt.getTransactionHash());
ConsoleUtils.singleLine();
System.out.println("transaction status: " + transactionReceipt.getStatus());

if (transactionReceipt.getStatus() == 0) {
System.out.println("description: " + "transaction executed successfully");
}
ConsoleUtils.singleLine();
System.out.println("Receipt message: " + transactionReceipt.getMessage());
System.out.println("Return message: " + transactionReceipt.getOutput());
ConsoleUtils.singleLine();
if (transactionReceipt.getLogEntries() != null && !transactionReceipt.getLogEntries().isEmpty()) {
System.out.println("Event logs");
for (TransactionReceipt.Logs logEntry : transactionReceipt.getLogEntries()) {
System.out.println(logEntry);
}
}

}

@Override
public void deploy(String[] params, String pwd) throws Exception {
List<String> paramsList = new ArrayList<>(Arrays.asList(params));
Expand Down Expand Up @@ -285,7 +314,7 @@ public TransactionResponse deploySolidity(
boolean sm =
(client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE)
|| (client.getCryptoSuite().getCryptoTypeConfig()
== CryptoType.HSM_TYPE);
== CryptoType.HSM_TYPE);
AbiAndBin abiAndBin =
ContractCompiler.compileContract(
contractNameOrPath,
Expand Down Expand Up @@ -334,11 +363,11 @@ public TransactionResponse deploySolidity(
client.getGroup(), abiAndBin, contractName, contractAddress);
return response;
} catch (ClientException
| CompileContractException
| IOException
| ContractException
| JniException
| ContractCodecException e) {
| CompileContractException
| IOException
| ContractException
| JniException
| ContractCodecException e) {
throw new ConsoleMessageException("deploy contract failed for " + e.getMessage(), e);
}
}
Expand Down Expand Up @@ -409,10 +438,10 @@ public TransactionResponse deployWasm(
client.getGroup(), abiAndBin, contractName, contractAddress);
return response;
} catch (ClientException
| IOException
| JniException
| ContractException
| ContractCodecException e) {
| IOException
| JniException
| ContractException
| ContractCodecException e) {
throw new ConsoleMessageException("deploy contract failed due to:" + e.getMessage(), e);
}
}
Expand All @@ -432,9 +461,9 @@ private synchronized void writeLog(String contractName, String contractAddress)
return;
}
try (BufferedReader reader =
new BufferedReader(new FileReader(Common.CONTRACT_LOG_FILE_NAME));
PrintWriter pw =
new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true)); ) {
new BufferedReader(new FileReader(Common.CONTRACT_LOG_FILE_NAME));
PrintWriter pw =
new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true));) {
String line;
List<String> textList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
Expand Down Expand Up @@ -481,7 +510,7 @@ private synchronized void writeLog(String contractName, String contractAddress)
+ " "
+ contractAddress;
try (PrintWriter pw =
new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true))) {
new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true))) {
if (!logFile.exists() && !logFile.createNewFile()) {
System.out.println("Failed to create file " + Common.CONTRACT_LOG_FILE_NAME);
}
Expand Down Expand Up @@ -788,7 +817,7 @@ private void sendTransaction(
List<String> callParams,
ABIDefinition abiDefinition)
throws ContractCodecException, TransactionBaseException, ContractException,
JniException {
JniException {
if (logger.isTraceEnabled()) {
logger.trace(
"sendTransactionAndGetResponse request, params: {}, contractAddress: {}, contractName: {}, functionName: {}, paramSize:{}, abiDefinition: {}",
Expand Down Expand Up @@ -838,7 +867,7 @@ private void sendCall(
String functionName,
List<String> callParams)
throws TransactionBaseException, ContractCodecException, JniException,
ContractException {
ContractException {
if (logger.isDebugEnabled()) {
logger.debug(
"sendCall request, params: {}, contractAddress: {}, contractName: {}, functionName:{}, paramSize: {}",
Expand Down Expand Up @@ -995,7 +1024,7 @@ public void listDeployContractAddress(ConsoleInitializer consoleInitializer, Str
String contractAddress =
isWasm
? new String(
Base64.getUrlDecoder().decode(contractAddressFile.getName()))
Base64.getUrlDecoder().decode(contractAddressFile.getName()))
: contractAddressFile.getName();
System.out.printf(
"%s %s%n",
Expand Down Expand Up @@ -1057,8 +1086,8 @@ public void transfer(ConsoleInitializer consoleInitializer, String[] params) thr
System.out.println(
"description: transfer transaction failed."
+ (revertMessage.getValue1()
? " Reason: " + revertMessage.getValue2()
: ""));
? " Reason: " + revertMessage.getValue2()
: ""));
}
}

Expand Down

0 comments on commit 0e7c940

Please sign in to comment.