Repository navigation
fix(jsonrpc): preserve memos on smart contract transactions #7120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Federico2014
wants to merge
4
commits into
tronprotocol:release_v4.8.3
from
Federico2014:fix/jt-957-contract-extra-data
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
10baa33
fix(jsonrpc): preserve memos on smart contract transactions
Federico2014 eddedd6
Merge remote-tracking branch 'upstream/release_v4.8.3' into fix/jt-95…
Federico2014 778793a
fix(jsonrpc): report invalid hex memos as invalid params
Federico2014 4962dc1
fix(api): encode visible transaction memos as UTF-8
Federico2014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
framework/src/test/java/org/tron/core/services/jsonrpc/BuildTransactionExtraDataTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 samevisible=truememo 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
txIDof 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.
There was a problem hiding this comment.
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.setTransactionExtraDatahelper now usesByteString.copyFromUtf8forvisible=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
3f3fbefore the fix and now verifye8aea2e58d95in the serialized transaction. All 10 targeted tests passed.