Skip to content
Closed
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 @@ -525,7 +525,7 @@ public static Transaction setTransactionExtraData(String data, Transaction trans
if (data.length() > 0) {
Transaction.raw.Builder raw = transaction.getRawData().toBuilder();
if (visible) {
raw.setData(ByteString.copyFrom(data.getBytes()));
raw.setData(ByteString.copyFromUtf8(data));
} else {
raw.setData(ByteString.copyFrom(ByteArray.fromHexString(data)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.hashToByteArray;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseBlockNumber;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseTxIndex;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.requireValidHex;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -85,6 +86,7 @@
import org.tron.core.services.NodeInfoService;
import org.tron.core.services.http.servlets.JsonFormat;
import org.tron.core.services.http.servlets.Util;
import org.tron.core.services.jsonrpc.JsonRpcApiUtil.HexMode;
import org.tron.core.services.jsonrpc.filters.BlockFilterAndResult;
import org.tron.core.services.jsonrpc.filters.LogBlockQuery;
import org.tron.core.services.jsonrpc.filters.LogFilter;
Expand Down Expand Up @@ -1261,6 +1263,7 @@ private TransactionJson buildCreateSmartContractTransaction(byte[] ownerAddress,

txBuilder.setRawData(rawBuilder);
tx = setTransactionPermissionId(args.getPermissionId(), txBuilder.build());
tx = setTransactionExtraData(args.getExtraData(), tx, args.isVisible());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MUST] Use explicit UTF-8 for contract memo text

The shared helper encodes visible memo text with String.getBytes(), which uses the node's default charset. In a JDK 17 check the same non-ASCII memo produced e8aea2e58d95 under UTF-8 and 3f3f under US-ASCII. The call path has the same issue. The difference changes the signed raw data and its txID.

Suggestion: Use explicit UTF-8 for visible memos in the two new contract paths and add a non-ASCII regression test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bladehan1 Agreed that visible memo text should use UTF-8. Since contract deployment, contract calls, regular transfers, and several HTTP endpoints all use Util.setTransactionExtraData, I think we should make this change once in the shared helper. A contract-only fix would let the same visible=true memo produce different bytes depending on which endpoint built the transaction.

On nodes with a non-UTF-8 default charset, the shared fix can change the memo bytes and txID of newly built transactions, and the derived address for contract deployments. Nodes already using UTF-8, hex memos (visible=false), and previously signed transactions are unaffected.

Does that scope look right to you? If so, I’ll update the shared helper and add non-ASCII regression coverage for deployment and calls.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bladehan1 Fixed in 4962dc1. The shared Util.setTransactionExtraData helper now uses ByteString.copyFromUtf8 for visible=true, so JSON-RPC and HTTP callers encode text memos consistently.

With US-ASCII as the JVM default charset, the deployment and call tests both reproduced 3f3f before the fix and now verify e8aea2e58d95 in the serialized transaction. All 10 targeted tests passed.


TransactionJson transactionJson = new TransactionJson();
transactionJson.setTransaction(
Expand Down Expand Up @@ -1314,6 +1317,7 @@ private TransactionJson buildTriggerSmartContractTransaction(byte[] ownerAddress
.triggerContract(build.build(), new TransactionCapsule(txBuilder.build()), trxExtBuilder,
retBuilder);
trx = setTransactionPermissionId(args.getPermissionId(), trx);
trx = setTransactionExtraData(args.getExtraData(), trx, args.isVisible());
trxExtBuilder.setTransaction(trx);
} catch (JsonRpcInvalidParamsException e) {
throw new JsonRpcInvalidParamsException(e.getMessage());
Expand Down Expand Up @@ -1429,6 +1433,10 @@ public TransactionJson buildTransaction(BuildArguments args)
throw new JsonRpcInvalidRequestException(JSON_ERROR);
}

if (!args.isVisible() && StringUtils.isNotEmpty(args.getExtraData())) {
requireValidHex("extraData", args.getExtraData(), HexMode.LENIENT);
Comment thread
Federico2014 marked this conversation as resolved.
Resolved
}

// check possible ContractType
ContractType contractType = args.getContractType(wallet);
switch (contractType.getNumber()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,47 @@ public void testFilterFieldNullSemanticsOverWire() throws Exception {
-32602, "invalid topic(s): null");
}

@Test
public void testBuildTrxTransferRejectsInvalidHexMemoOverWire() throws Exception {
JsonObject args = newTransferBuildArguments();
args.addProperty("extraData", "zz");
JSONObject response = handleJsonRpc(newJsonRpcServer(), "buildTransaction", jsonParams(args));
assertMappedError(response, -32602, "invalid hex string for \"extraData\"");
}

@Test
public void testBuildTrc10TransferRejectsInvalidHexMemoOverWire() throws Exception {
JsonObject args = newTransferBuildArguments();
args.remove("value");
args.addProperty("tokenId", 1000016);
args.addProperty("tokenValue", 20);
args.addProperty("visible", false);
args.addProperty("extraData", "zz");
JSONObject response = handleJsonRpc(newJsonRpcServer(), "buildTransaction", jsonParams(args));
assertMappedError(response, -32602, "invalid hex string for \"extraData\"");
}

@Test
public void testBuildDeploymentRejectsInvalidHexMemoOverWire() throws Exception {
JsonObject args = newCreateBuildArguments();
args.addProperty("extraData", "zz");
JSONObject response = handleJsonRpc(newJsonRpcServer(), "buildTransaction", jsonParams(args));
assertMappedError(response, -32602, "invalid hex string for \"extraData\"");
}

@Test
public void testBuildDeploymentPreservesVisibleTextMemoOverWire() throws Exception {
JsonObject args = newCreateBuildArguments();
args.addProperty("extraData", "zz");
args.addProperty("visible", true);
JSONObject response = handleJsonRpc(newJsonRpcServer(), "buildTransaction", jsonParams(args));
assertSuccessfulResponse(response);
JSONObject tx = response.getJSONObject("result").getJSONObject("transaction");
Transaction.raw raw = Transaction.raw.parseFrom(ByteArray.fromHexString(
tx.getString("raw_data_hex")));
Assert.assertEquals(ByteString.copyFromUtf8("zz"), raw.getData());
}

@Test
public void testOptionalDtoNullFieldsOverWire() throws Exception {
JsonRpcServer server = newJsonRpcServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,12 @@ public void testSetTransactionPermissionId() {
@Test
public void testSetTransactionExtraData() {
TransactionCapsule transactionCapsule = getTransactionCapsuleExample();
JSONObject jsonObject = JSONObject.parseObject("{\"extra_data\":\"test\"}");
String memo = new String(new char[] {0x8ba2, 0x5355});
JSONObject jsonObject = new JSONObject();
jsonObject.put("extra_data", memo);
Protocol.Transaction out = Util.setTransactionExtraData(jsonObject,
transactionCapsule.getInstance(), true);
Assert.assertNotNull(out);
Assert.assertEquals(ByteString.copyFromUtf8(memo), out.getRawData().getData());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.tron.core.services.jsonrpc;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Wallet;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.db.Manager;
import org.tron.core.db2.core.Chainbase.Cursor;
import org.tron.core.services.NodeInfoService;
import org.tron.core.services.jsonrpc.types.BuildArguments;
import org.tron.json.JSONObject;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
import org.tron.protos.contract.SmartContractOuterClass.TriggerSmartContract;

public class BuildTransactionExtraDataTest {

private final Wallet wallet = mock(Wallet.class);
private final BuildArguments args = new BuildArguments();
private TronJsonRpcImpl jsonRpc;

@Before
public void setUp() throws Exception {
when(wallet.getCursor()).thenReturn(Cursor.HEAD);
when(wallet.createTransactionCapsule(any(), any())).thenAnswer(invocation ->
new TransactionCapsule(invocation.getArgument(0, Message.class),
invocation.getArgument(1, ContractType.class)));
jsonRpc = new TronJsonRpcImpl(mock(NodeInfoService.class), wallet, mock(Manager.class));
args.setFrom("0x99357684bc659f5166046b56c95a0e99f1265cd1");
args.setExtraData("616263");
}

@After
public void tearDown() {
jsonRpc.close();
}

@Test
public void transferPreservesMemo() throws Exception {
args.setTo("0x548794500882809695a8a687866e76d4271a1abc");
args.setValue("0x1");
assertEquals(ContractType.TransferContract, buildWithMemo().getType());
}

@Test
public void deployPreservesMemoAndBytecode() throws Exception {
args.setData("0x6000");
CreateSmartContract deploy = buildWithMemo().getParameter().unpack(CreateSmartContract.class);
assertEquals("6000",
ByteArray.toHexString(deploy.getNewContract().getBytecode().toByteArray()));
}

@Test
public void triggerPreservesMemoAndCallData() throws Exception {
setUpContractCall();
TriggerSmartContract call = buildWithMemo().getParameter().unpack(TriggerSmartContract.class);
assertEquals("60fe47b1", ByteArray.toHexString(call.getData().toByteArray()));
}

@Test
public void deployEncodesVisibleMemoAsUtf8() throws Exception {
args.setData("0x6000");
assertVisibleMemoBytes();
}

@Test
public void triggerEncodesVisibleMemoAsUtf8() throws Exception {
setUpContractCall();
assertVisibleMemoBytes();
}

private void setUpContractCall() throws Exception {
args.setTo("0x548794500882809695a8a687866e76d4271a1abc");
args.setData("0x60fe47b1");
when(wallet.getContract(any())).thenReturn(SmartContract.getDefaultInstance());
when(wallet.triggerContract(any(), any(), any(), any())).thenAnswer(invocation ->
invocation.getArgument(1, TransactionCapsule.class).getInstance());
}

private void assertVisibleMemoBytes() throws Exception {
args.setVisible(true);
args.setExtraData(new String(new char[] {0x8ba2, 0x5355}));
JSONObject tx = jsonRpc.buildTransaction(args).getTransaction();
Transaction.raw raw = Transaction.raw.parseFrom(ByteArray.fromHexString(
tx.getString("raw_data_hex")));
assertEquals("e8aea2e58d95", ByteArray.toHexString(raw.getData().toByteArray()));
}

private Transaction.Contract buildWithMemo() throws Exception {
JSONObject tx = jsonRpc.buildTransaction(args).getTransaction();
assertEquals("616263", tx.getJSONObject("raw_data").getString("data"));
byte[] bytes = ByteArray.fromHexString(tx.getString("raw_data_hex"));
Transaction.raw raw = Transaction.raw.parseFrom(bytes);
assertEquals(ByteString.copyFromUtf8("abc"), raw.getData());
assertEquals(ByteArray.toHexString(Sha256Hash.hash(bytes)), tx.getString("txID"));
return raw.getContract(0);
}
}
Loading