Skip to content

Commit ed66c1e

Browse files
committed
feat(vm): reject whitespace ABI types and recognize receive in test helpers
- AbiValidator now rejects types with leading/trailing whitespace upfront with a precise error, so passing validation implies the persisted type bytes equal the checked bytes. - PublicMethod / TvmTestUtils helpers map "receive" in getEntryType and exempt receive from the no-inputs guard, so test ABIs with a receive entry reach the validator's Receive branch instead of falling to UNRECOGNIZED.
1 parent 1c4c06d commit ed66c1e

6 files changed

Lines changed: 79 additions & 3 deletions

File tree

actuator/src/main/java/org/tron/core/utils/AbiValidator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ private static String checkType(String raw) {
118118
if (raw == null || raw.isEmpty()) {
119119
return "type must not be empty";
120120
}
121-
String t = raw.trim();
121+
if (!raw.equals(raw.trim())) {
122+
return "type must not contain leading/trailing whitespace";
123+
}
124+
String t = raw;
122125

123126
while (true) {
124127
Matcher m = ARRAY_SUFFIX.matcher(t);

framework/src/test/java/org/tron/common/runtime/TvmTestUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ private static SmartContract.ABI.Entry.EntryType getEntryType(String type) {
490490
return SmartContract.ABI.Entry.EntryType.Event;
491491
case "fallback":
492492
return SmartContract.ABI.Entry.EntryType.Fallback;
493+
case "receive":
494+
return SmartContract.ABI.Entry.EntryType.Receive;
493495
case "error":
494496
return SmartContract.ABI.Entry.EntryType.Error;
495497
default:
@@ -544,7 +546,8 @@ public static SmartContract.ABI jsonStr2Abi(String jsonStr) {
544546
logger.error("No type!");
545547
return null;
546548
}
547-
if (!type.equalsIgnoreCase("fallback") && null == inputs) {
549+
if (!type.equalsIgnoreCase("fallback") && !type.equalsIgnoreCase("receive")
550+
&& null == inputs) {
548551
logger.error("No inputs!");
549552
return null;
550553
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.tron.common.runtime;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;
7+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry;
8+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry.EntryType;
9+
10+
public class TvmTestUtilsTest {
11+
12+
@Test
13+
public void jsonStr2AbiAcceptsReceiveEntry() {
14+
String json = "[{\"stateMutability\":\"payable\",\"type\":\"receive\"}]";
15+
ABI abi = TvmTestUtils.jsonStr2Abi(json);
16+
assertEquals(1, abi.getEntrysCount());
17+
Entry entry = abi.getEntrys(0);
18+
assertEquals(EntryType.Receive, entry.getType());
19+
assertEquals(0, entry.getInputsCount());
20+
assertEquals(0, entry.getOutputsCount());
21+
}
22+
}

framework/src/test/java/org/tron/common/utils/PublicMethod.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public static SmartContractOuterClass.SmartContract.ABI jsonStr2Abi(String jsonS
131131
logger.error("No type!");
132132
return null;
133133
}
134-
if (!type.equalsIgnoreCase("fallback") && null == inputs) {
134+
if (!type.equalsIgnoreCase("fallback") && !type.equalsIgnoreCase("receive")
135+
&& null == inputs) {
135136
logger.error("No inputs!");
136137
return null;
137138
}
@@ -218,6 +219,8 @@ public static SmartContractOuterClass.SmartContract.ABI jsonStr2Abi(String jsonS
218219
return SmartContractOuterClass.SmartContract.ABI.Entry.EntryType.Event;
219220
case "fallback":
220221
return SmartContractOuterClass.SmartContract.ABI.Entry.EntryType.Fallback;
222+
case "receive":
223+
return SmartContractOuterClass.SmartContract.ABI.Entry.EntryType.Receive;
221224
case "error":
222225
return SmartContractOuterClass.SmartContract.ABI.Entry.EntryType.Error;
223226
default:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.tron.common.utils;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;
8+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry;
9+
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry.EntryType;
10+
11+
public class PublicMethodTest {
12+
13+
@Test
14+
public void jsonStr2AbiAcceptsReceiveEntry() {
15+
String json = "[{\"stateMutability\":\"payable\",\"type\":\"receive\"}]";
16+
ABI abi = PublicMethod.jsonStr2Abi(json);
17+
assertEquals(1, abi.getEntrysCount());
18+
Entry entry = abi.getEntrys(0);
19+
assertEquals(EntryType.Receive, entry.getType());
20+
assertEquals(0, entry.getInputsCount());
21+
assertEquals(0, entry.getOutputsCount());
22+
}
23+
24+
@Test
25+
public void getEntryTypeMapsReceive() {
26+
assertEquals(EntryType.Receive, PublicMethod.getEntryType("receive"));
27+
}
28+
29+
@Test
30+
public void getEntryTypeUnknownStaysUnrecognized() {
31+
assertTrue(PublicMethod.getEntryType("weirdo") == EntryType.UNRECOGNIZED);
32+
}
33+
}

framework/src/test/java/org/tron/core/utils/AbiValidatorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ public void emptyTypeIsRejected() {
107107
function("foo", param("x", ""))).build(), "type must not be empty");
108108
}
109109

110+
@Test
111+
public void whitespacePaddedTypeIsRejected() {
112+
expect(ABI.newBuilder().addEntrys(
113+
function("foo", param("x", " uint256 "))).build(), "leading/trailing whitespace");
114+
expect(ABI.newBuilder().addEntrys(
115+
function("foo", param("x", " uint256"))).build(), "leading/trailing whitespace");
116+
expect(ABI.newBuilder().addEntrys(
117+
function("foo", param("x", "uint256 "))).build(), "leading/trailing whitespace");
118+
expect(ABI.newBuilder().addEntrys(
119+
function("foo", param("x", " address[]"))).build(), "leading/trailing whitespace");
120+
}
121+
110122
@Test
111123
public void duplicateConstructorIsRejected() {
112124
ABI abi = ABI.newBuilder()

0 commit comments

Comments
 (0)