Skip to content

Commit 03bd4af

Browse files
authored
fix(jsonrpc): correct TransactionResult.nonce per JSON-RPC spec (tronprotocol#6709)
Per ethereum/execution-apis, TransactionInfo.nonce is `uint` (QUANTITY) and must match `^0x(0|[1-9a-f][0-9a-f]*)$`. java-tron emitted the field as `0x0000000000000000` via `ByteArray.toJsonHex(new byte[8])`, which violates the pattern. Both `TransactionResult` constructors now emit `"0x0"`. Block.nonce is intentionally left at `0x0000000000000000` because the Block schema defines it as `bytes8`, so that value is already compliant and shortening it would break conformance. Closes tronprotocol#6547
1 parent 16190df commit 03bd4af

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public TransactionResult(BlockCapsule blockCapsule, int index, Protocol.Transact
9898
TransactionCapsule capsule = new TransactionCapsule(tx);
9999
byte[] txId = capsule.getTransactionId().getBytes();
100100
hash = ByteArray.toJsonHex(txId);
101-
nonce = ByteArray.toJsonHex(new byte[8]); // no value
101+
nonce = "0x0"; // no value, QUANTITY type per Ethereum JSON-RPC spec
102102
blockHash = ByteArray.toJsonHex(blockCapsule.getBlockId().getBytes());
103103
blockNumber = ByteArray.toJsonHex(blockCapsule.getNum());
104104
transactionIndex = ByteArray.toJsonHex(index);
@@ -133,7 +133,7 @@ public TransactionResult(Transaction tx, Wallet wallet) {
133133
TransactionCapsule capsule = new TransactionCapsule(tx);
134134
byte[] txId = capsule.getTransactionId().getBytes();
135135
hash = ByteArray.toJsonHex(txId);
136-
nonce = ByteArray.toJsonHex(new byte[8]); // no value
136+
nonce = "0x0"; // no value, QUANTITY type per Ethereum JSON-RPC spec
137137
blockHash = "0x";
138138
blockNumber = "0x";
139139
transactionIndex = "0x";

framework/src/test/java/org/tron/core/services/jsonrpc/TransactionResultTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ public class TransactionResultTest extends BaseTest {
2323
private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
2424
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
2525

26+
// QUANTITY pattern from ethereum/execution-apis base-types schema (uint).
27+
private static final String QUANTITY_PATTERN = "^0x(0|[1-9a-f][0-9a-f]*)$";
28+
2629
static {
2730
Args.setParam(new String[] {"-d", dbPath()}, TestConstants.TEST_CONF);
2831
}
2932

33+
private static void assertQuantity(String value) {
34+
Assert.assertNotNull(value);
35+
Assert.assertTrue("not a valid QUANTITY: " + value, value.matches(QUANTITY_PATTERN));
36+
}
37+
3038
@Test
3139
public void testBuildTransactionResultWithBlock() {
3240
SmartContractOuterClass.TriggerSmartContract.Builder builder2 =
@@ -49,6 +57,8 @@ public void testBuildTransactionResultWithBlock() {
4957
transactionResult.getHash());
5058
Assert.assertEquals(transactionResult.getGasPrice(), "0x1");
5159
Assert.assertEquals(transactionResult.getGas(), "0x64");
60+
Assert.assertEquals("0x0", transactionResult.getNonce());
61+
assertQuantity(transactionResult.getNonce());
5262
}
5363

5464
@Test
@@ -65,7 +75,8 @@ public void testBuildTransactionResult() {
6575
Assert.assertEquals("0x5691531881bc44adbc722060d85fdf29265823db8e884b0d104fcfbba253cf11",
6676
transactionResult.getHash());
6777
Assert.assertEquals(transactionResult.getGasPrice(), "0x");
68-
Assert.assertEquals(transactionResult.getNonce(), "0x0000000000000000");
78+
Assert.assertEquals("0x0", transactionResult.getNonce());
79+
assertQuantity(transactionResult.getNonce());
6980
}
7081

7182
}

0 commit comments

Comments
 (0)