Skip to content

Commit 0a461c4

Browse files
author
Wenhua Zhang
committed
Merge branch 'master' into hotfix/print_unknownfield
2 parents 5f64660 + 72d951f commit 0a461c4

File tree

4 files changed

+148
-48
lines changed

4 files changed

+148
-48
lines changed

src/main/java/org/tron/walletcli/Client.java

Lines changed: 135 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,28 +2141,70 @@ private void deployContract(String[] parameter)
21412141
}
21422142
}
21432143

2144-
private void triggerContract(String[] parameters, boolean isConstant)
2145-
throws IOException, CipherException, CancelException, EncodingException {
2146-
String cmdMethodStr = isConstant ? "TriggerConstantContract" : "TriggerContract";
2147-
2148-
if (isConstant) {
2149-
if (parameters == null || (parameters.length != 4 && parameters.length != 5)) {
2150-
System.out.println(cmdMethodStr + " needs 4 or 5 parameters like: ");
2151-
System.out.println(cmdMethodStr + " [OwnerAddress] contractAddress method args isHex");
2144+
private void deployConstantContract(String[] parameters)
2145+
throws IOException, CipherException, CancelException {
2146+
2147+
if (parameters == null || (parameters.length != 5 && parameters.length != 8)) {
2148+
System.out.println("DeployConstantContract needs at least 4 parameters like: ");
2149+
System.out.println("DeployConstantContract ownerAddress(use # if you own)"
2150+
+ " byteCode constructor params isHex [value token_value token_id]");
2151+
return;
2152+
}
2153+
2154+
int idx = 0;
2155+
2156+
String ownerAddressStr = parameters[idx++];
2157+
byte[] ownerAddress = null;
2158+
if (!"#".equals(ownerAddressStr)) {
2159+
ownerAddress = WalletApi.decodeFromBase58Check(ownerAddressStr);
2160+
if (ownerAddress == null) {
2161+
System.out.println("Invalid Owner Address.");
21522162
return;
21532163
}
2154-
} else {
2155-
if (parameters == null || (parameters.length != 8 && parameters.length != 9)) {
2156-
System.out.println(cmdMethodStr + " needs 8 or 9 parameters like: ");
2157-
System.out.println(cmdMethodStr + " [OwnerAddress] contractAddress method args isHex"
2158-
+ " fee_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided)");
2159-
return;
2164+
}
2165+
2166+
String codeStr = parameters[idx++];
2167+
String constructorStr = parameters[idx++];
2168+
String argsStr = parameters[idx++];
2169+
boolean isHex = Boolean.parseBoolean(parameters[idx++]);
2170+
long callValue = 0;
2171+
long tokenValue = 0;
2172+
String tokenId = "";
2173+
if (parameters.length == 8) {
2174+
callValue = Long.parseLong(parameters[idx++]);
2175+
tokenValue = Long.parseLong(parameters[idx++]);
2176+
tokenId = parameters[idx];
2177+
}
2178+
2179+
if (!(constructorStr.equals("#") || argsStr.equals("#"))) {
2180+
if (isHex) {
2181+
codeStr += argsStr;
2182+
} else {
2183+
codeStr += Hex.toHexString(AbiUtil.encodeInput(constructorStr, argsStr));
21602184
}
21612185
}
21622186

2187+
if (tokenId.equalsIgnoreCase("#")) {
2188+
tokenId = "";
2189+
}
2190+
2191+
walletApiWrapper.callContract(
2192+
ownerAddress, null, callValue, Hex.decode(codeStr), 0, tokenValue, tokenId, true);
2193+
}
2194+
2195+
private void triggerContract(String[] parameters)
2196+
throws IOException, CipherException, CancelException {
2197+
2198+
if (parameters == null || (parameters.length != 8 && parameters.length != 9)) {
2199+
System.out.println("TriggerContract needs 8 or 9 parameters like: ");
2200+
System.out.println("TriggerContract [OwnerAddress] contractAddress method args isHex"
2201+
+ " fee_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided)");
2202+
return;
2203+
}
2204+
21632205
int index = 0;
21642206
byte[] ownerAddress = null;
2165-
if (parameters.length == 5 || parameters.length == 9) {
2207+
if (parameters.length == 9) {
21662208
ownerAddress = WalletApi.decodeFromBase58Check(parameters[index++]);
21672209
if (ownerAddress == null) {
21682210
System.out.println("Invalid OwnerAddress.");
@@ -2173,42 +2215,92 @@ private void triggerContract(String[] parameters, boolean isConstant)
21732215
String contractAddrStr = parameters[index++];
21742216
String methodStr = parameters[index++];
21752217
String argsStr = parameters[index++];
2176-
boolean isHex = Boolean.valueOf(parameters[index++]);
2177-
long feeLimit = 0;
2178-
long callValue = 0;
2179-
long tokenCallValue = 0;
2180-
String tokenId = "";
2218+
boolean isHex = Boolean.parseBoolean(parameters[index++]);
2219+
long feeLimit = Long.parseLong(parameters[index++]);
2220+
long callValue = Long.parseLong(parameters[index++]);
2221+
long tokenValue = Long.parseLong(parameters[index++]);
2222+
String tokenId = parameters[index];
21812223

2182-
if (!isConstant) {
2183-
feeLimit = Long.valueOf(parameters[index++]);
2184-
callValue = Long.valueOf(parameters[index++]);
2185-
tokenCallValue = Long.valueOf(parameters[index++]);
2186-
tokenId = parameters[index++];
2187-
}
21882224
if (argsStr.equalsIgnoreCase("#")) {
21892225
argsStr = "";
21902226
}
2227+
21912228
if (tokenId.equalsIgnoreCase("#")) {
21922229
tokenId = "";
21932230
}
2231+
21942232
byte[] input = new byte[0];
21952233
if (!methodStr.equalsIgnoreCase("#")) {
21962234
input = Hex.decode(AbiUtil.parseMethod(methodStr, argsStr, isHex));
21972235
}
21982236
byte[] contractAddress = WalletApi.decodeFromBase58Check(contractAddrStr);
21992237

2200-
boolean result = walletApiWrapper
2201-
.callContract(ownerAddress, contractAddress, callValue, input, feeLimit, tokenCallValue,
2202-
tokenId,
2203-
isConstant);
2204-
if (!isConstant) {
2205-
if (result) {
2206-
System.out.println("Broadcast the " + cmdMethodStr + " successful.\n"
2207-
+ "Please check the given transaction id to get the result on blockchain using getTransactionInfoById command");
2208-
} else {
2209-
System.out.println("Broadcast the " + cmdMethodStr + " failed");
2238+
boolean result = walletApiWrapper.callContract(
2239+
ownerAddress, contractAddress, callValue, input, feeLimit, tokenValue, tokenId, false);
2240+
if (result) {
2241+
System.out.println("Broadcast the TriggerContract successful.\n"
2242+
+ "Please check the given transaction id to get the result on blockchain using getTransactionInfoById command");
2243+
} else {
2244+
System.out.println("Broadcast the TriggerContract failed");
2245+
}
2246+
}
2247+
2248+
private void triggerConstantContract(String[] parameters)
2249+
throws IOException, CipherException, CancelException {
2250+
2251+
if (parameters == null || (parameters.length != 5 && parameters.length != 8)) {
2252+
System.out.println("TriggerConstantContract needs 5 or 8 parameters like: ");
2253+
System.out.println("TriggerConstantContract ownerAddress(use # if you own)"
2254+
+ " contractAddress method args isHex [value token_value token_id(e.g: TRXTOKEN, use # if don't provided)]");
2255+
return;
2256+
}
2257+
2258+
int idx = 0;
2259+
2260+
String ownerAddressStr = parameters[idx++];
2261+
byte[] ownerAddress = null;
2262+
if (!"#".equals(ownerAddressStr)) {
2263+
ownerAddress = WalletApi.decodeFromBase58Check(ownerAddressStr);
2264+
if (ownerAddress == null) {
2265+
System.out.println("Invalid Owner Address.");
2266+
return;
22102267
}
22112268
}
2269+
2270+
String contractAddressStr = parameters[idx++];
2271+
byte[] contractAddress = WalletApi.decodeFromBase58Check(contractAddressStr);
2272+
if (contractAddress == null) {
2273+
System.out.println("Invalid Contract Address.");
2274+
return;
2275+
}
2276+
2277+
String methodStr = parameters[idx++];
2278+
String argsStr = parameters[idx++];
2279+
boolean isHex = Boolean.parseBoolean(parameters[idx++]);
2280+
long callValue = 0;
2281+
long tokenValue = 0;
2282+
String tokenId = "";
2283+
if (parameters.length == 8) {
2284+
callValue = Long.parseLong(parameters[idx++]);
2285+
tokenValue = Long.parseLong(parameters[idx++]);
2286+
tokenId = parameters[idx];
2287+
}
2288+
2289+
if (argsStr.equalsIgnoreCase("#")) {
2290+
argsStr = "";
2291+
}
2292+
2293+
if (tokenId.equalsIgnoreCase("#")) {
2294+
tokenId = "";
2295+
}
2296+
2297+
byte[] input = new byte[0];
2298+
if (!methodStr.equalsIgnoreCase("#")) {
2299+
input = Hex.decode(AbiUtil.parseMethod(methodStr, argsStr, isHex));
2300+
}
2301+
2302+
walletApiWrapper.callContract(
2303+
ownerAddress, contractAddress, callValue, input, 0, tokenValue, tokenId, true);
22122304
}
22132305

22142306
private void getContract(String[] parameters) {
@@ -3692,6 +3784,7 @@ private void help() {
36923784

36933785
public static String[] getCmd(String cmdLine) {
36943786
if (cmdLine.indexOf("\"") < 0 || cmdLine.toLowerCase().startsWith("deploycontract")
3787+
|| cmdLine.toLowerCase().startsWith("deployconstantcontract")
36953788
|| cmdLine.toLowerCase().startsWith("triggercontract")
36963789
|| cmdLine.toLowerCase().startsWith("triggerconstantcontract")
36973790
|| cmdLine.toLowerCase().startsWith("updateaccountpermission")) {
@@ -4078,12 +4171,16 @@ private void run() {
40784171
deployContract(parameters);
40794172
break;
40804173
}
4174+
case "deployconstantcontract": {
4175+
deployConstantContract(parameters);
4176+
break;
4177+
}
40814178
case "triggercontract": {
4082-
triggerContract(parameters, false);
4179+
triggerContract(parameters);
40834180
break;
40844181
}
40854182
case "triggerconstantcontract": {
4086-
triggerContract(parameters, true);
4183+
triggerConstantContract(parameters);
40874184
break;
40884185
}
40894186
case "getcontract": {

src/main/java/org/tron/walletserver/WalletApi.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,9 @@ public static TriggerSmartContract triggerCallContract(
19851985
String tokenId) {
19861986
TriggerSmartContract.Builder builder = TriggerSmartContract.newBuilder();
19871987
builder.setOwnerAddress(ByteString.copyFrom(address));
1988-
builder.setContractAddress(ByteString.copyFrom(contractAddress));
1988+
if (contractAddress != null) {
1989+
builder.setContractAddress(ByteString.copyFrom(contractAddress));
1990+
}
19891991
builder.setData(ByteString.copyFrom(data));
19901992
builder.setCallValue(callValue);
19911993
if (tokenId != null && tokenId != "") {
@@ -2178,15 +2180,13 @@ public boolean triggerContract(
21782180
Transaction transaction = transactionExtention
21792181
.getTransaction();
21802182
// for constant
2181-
if (transaction.getRetCount() != 0
2182-
&& transactionExtention.getConstantResult(0) != null
2183-
&& transactionExtention.getResult() != null) {
2184-
byte[] result = transactionExtention.getConstantResult(0).toByteArray();
2185-
System.out.println("message:" + transaction.getRet(0).getRet());
2186-
System.out.println(
2187-
":" + ByteArray.toStr(transactionExtention.getResult().getMessage().toByteArray()));
2188-
System.out.println("Result:" + Hex.toHexString(result));
2189-
System.out.println("EnergyUsed:" + transactionExtention.getEnergyUsed());
2183+
if (transaction.getRetCount() != 0) {
2184+
TransactionExtention.Builder builder =
2185+
transactionExtention.toBuilder().clearTransaction().clearTxid();
2186+
if (transaction.getRet(0).getRet() == Result.code.FAILED) {
2187+
builder.setResult(builder.getResult().toBuilder().setResult(false));
2188+
}
2189+
System.out.println("Execution result = " + Utils.formatMessageString(builder.build()));
21902190
return true;
21912191
}
21922192

src/main/protos/api/api.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,8 @@ message TransactionExtention {
11931193
repeated bytes constant_result = 3;
11941194
Return result = 4;
11951195
int64 energy_used = 5;
1196+
repeated TransactionInfo.Log logs = 6;
1197+
repeated InternalTransaction internal_transactions = 7;
11961198
}
11971199

11981200
message BlockExtention {

src/main/protos/core/contract/smart_contract.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ message SmartContract {
5555
int64 origin_energy_limit = 8;
5656
bytes code_hash = 9;
5757
bytes trx_hash = 10;
58+
int32 version = 11;
5859
}
5960

6061
message CreateSmartContract {

0 commit comments

Comments
 (0)