Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.util.encoders.Hex;
import org.tron.trident.abi.TypeEncoder;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Type;
import org.tron.trident.core.exceptions.ContractCreateException;
import org.tron.trident.proto.Common.SmartContract.ABI.Entry;
Expand Down Expand Up @@ -51,10 +51,7 @@ public void encodeParameter(List<Type<?>> params) throws ContractCreateException
if (params.size() != paramTypes.size()) {
throw new ContractCreateException("Parameter amount doesn't match.");
}
StringBuilder builder = new StringBuilder();
for (Type<?> p : params) {
builder.append(TypeEncoder.encode(p));
}
this.bytecode = ByteString.copyFrom(Hex.decode(builder.toString()));
String encoded = FunctionEncoder.encodeConstructor(new ArrayList<Type>(params));
this.bytecode = ByteString.copyFrom(Hex.decode(encoded));
}
}
13 changes: 5 additions & 8 deletions core/src/main/java/org/tron/trident/core/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bouncycastle.util.encoders.Hex;
import org.tron.trident.abi.TypeEncoder;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Type;
import org.tron.trident.core.exceptions.ContractCreateException;
import org.tron.trident.core.transaction.BlockId;
import org.tron.trident.crypto.Hash;
import org.tron.trident.proto.Chain;
Expand Down Expand Up @@ -110,12 +110,9 @@ public static byte[] decodeFromBase58Check(String addressBase58) {
return address;
}

public static ByteString encodeParameter(List<Type<?>> params) throws ContractCreateException {
StringBuilder builder = new StringBuilder();
for (Type<?> p : params) {
builder.append(TypeEncoder.encode(p));
}
return ByteString.copyFrom(Hex.decode(builder.toString()));
public static ByteString encodeParameter(List<Type<?>> params) {
String encoded = FunctionEncoder.encodeConstructor(new ArrayList<Type>(params));
return ByteString.copyFrom(Hex.decode(encoded));
}

public static byte[] replaceLibraryAddress(String code, String libraryAddressPair,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.tron.trident.core;

import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.tron.trident.abi.FunctionReturnDecoder;
import org.tron.trident.abi.TypeReference;
import org.tron.trident.abi.datatypes.Function;
import org.tron.trident.abi.datatypes.Type;
import org.tron.trident.abi.datatypes.Utf8String;
import org.tron.trident.core.utils.ByteArray;
import org.tron.trident.proto.Chain.Transaction;
import org.tron.trident.proto.Response.TransactionExtention;
import org.tron.trident.proto.Response.TransactionInfo;
import org.tron.trident.proto.Response.TransactionInfo.code;
import org.tron.trident.utils.Base58Check;

/**
* deploying a contract whose constructor takes a dynamic-type parameter (string)
*
* <p>Contract source (solc 0.8.30):
* <pre>
* contract DynParam {
* string public name;
* constructor(string memory _name) { name = _name; }
* }
* </pre>
*/
@Disabled("add private key to enable this case")
class ConstructorDynamicParamDeployTest extends BaseTest {

private static final String CONSTRUCTOR_ARG = "Trident";

private static final String BYTECODE =
"608060405234801561001057600080fd5b5060405161072c38038061072c833981810160405281019061003291906101bd565b80600090816100419190610427565b50506104f9565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6100af82610066565b810181811067ffffffffffffffff821117156100ce576100cd610077565b5b80604052505050565b60006100e1610048565b90506100ed82826100a6565b919050565b600067ffffffffffffffff82111561010d5761010c610077565b5b61011682610066565b9050602081019050919050565b60005b83811015610141578082015181840152602081019050610126565b60008484015250505050565b600061016061015b846100f2565b6100d7565b90508281526020810184848401111561017c5761017b610061565b5b610187848285610123565b509392505050565b600082601f8301126101a4576101a361005c565b5b81516101b484826020860161014d565b91505092915050565b6000602082840312156101d3576101d2610052565b5b600082015167ffffffffffffffff8111156101f1576101f0610057565b5b6101fd8482850161018f565b91505092915050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061025857607f821691505b60208210810361026b5761026a610211565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026102d37fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610296565b6102dd8683610296565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061032461031f61031a846102f5565b6102ff565b6102f5565b9050919050565b6000819050919050565b61033e83610309565b61035261034a8261032b565b8484546102a3565b825550505050565b600090565b61036761035a565b610372818484610335565b505050565b5b818110156103965761038b60008261035f565b600181019050610378565b5050565b601f8211156103db576103ac81610271565b6103b584610286565b810160208510156103c4578190505b6103d86103d085610286565b830182610377565b50505b505050565b600082821c905092915050565b60006103fe600019846008026103e0565b1980831691505092915050565b600061041783836103ed565b9150826002028217905092915050565b61043082610206565b67ffffffffffffffff81111561044957610448610077565b5b6104538254610240565b61045e82828561039a565b600060209050601f831160018114610491576000841561047f578287015190505b610489858261040b565b8655506104f1565b601f19841661049f86610271565b60005b828110156104c7578489015182556001820191506020850194506020810190506104a2565b868310156104e457848901516104e0601f8916826103ed565b8355505b6001600288020188555050505b505050505050565b610224806105086000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c806306fdde0314610030575b600080fd5b61003861004e565b604051610045919061016c565b60405180910390f35b6000805461005b906101bd565b80601f0160208091040260200160405190810160405280929190818152602001828054610087906101bd565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600081519050919050565b600082825260208201905092915050565b60005b838110156101165780820151818401526020810190506100fb565b60008484015250505050565b6000601f19601f8301169050919050565b600061013e826100dc565b61014881856100e7565b93506101588185602086016100f8565b61016181610122565b840191505092915050565b600060208201905081810360008301526101868184610133565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101d557607f821691505b6020821081036101e8576101e761018e565b5b5091905056fea2646970667358221220717d5f255ee2df61d420c590d4031cc562654961fa9de4e1f006eca847734deb64736f6c634300081e0033";

@Test
void deployWithDynamicStringConstructorParam_succeedsAndStoresValue() throws Exception {
List<Type<?>> constructorParams =
Collections.singletonList(new Utf8String(CONSTRUCTOR_ARG));

TransactionExtention deployExt = client.deployContract("DynParam", null, BYTECODE,
constructorParams, 1000_000_000L, 100, 10_000_000L, 0L, null, 0L);
Transaction deployTx = client.signTransaction(deployExt.getTransaction());
String txId = client.broadcastTransaction(deployTx);

sleep(15_000L);

TransactionInfo info = client.getTransactionInfoById(txId);
assertEquals(code.SUCESS, info.getResult());

String contractAddress = Base58Check.bytesToBase58(info.getContractAddress().toByteArray());
Function nameFn = new Function("name",
Collections.emptyList(),
Collections.singletonList(new TypeReference<Utf8String>() {}));

TransactionExtention callExt = client.triggerConstantContract(testAddress, contractAddress, nameFn);
assertNotNull(callExt.getConstantResultList());
assertFalse(callExt.getConstantResultList().isEmpty());

List<Type> result = FunctionReturnDecoder.decode(
ByteArray.toHexString(callExt.getConstantResult(0).toByteArray()),
nameFn.getOutputParameters());
assertEquals(CONSTRUCTOR_ARG, ((Utf8String) result.get(0)).getValue());
}
}