Skip to content

Commit 17d69e0

Browse files
committed
test(vm): cover constant-call timeout policy and config
VmTimeoutPolicy: deadline math, max-of semantics (never shrink below network), and cap-engagement gate against the runtime network value. VmConfig: 0 sentinel, [80, 500] range, cap > 0.
1 parent c8ccd9f commit 17d69e0

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

common/src/test/java/org/tron/core/config/args/VmConfigTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,66 @@ public void testEstimateEnergyMaxRetryBoundaryValues() {
8888
assertEquals(3, VmConfig.fromConfig(
8989
withRef("vm { estimateEnergyMaxRetry = 3 }")).getEstimateEnergyMaxRetry());
9090
}
91+
92+
// ===========================================================================
93+
// Boundary tests for constant-call timeout (issue #6681)
94+
// ===========================================================================
95+
96+
@Test
97+
public void testConstantCallTimeoutDefaults() {
98+
VmConfig vm = VmConfig.fromConfig(withRef());
99+
assertEquals(0L, vm.getConstantCallTimeoutMs());
100+
assertEquals(10, vm.getConstantCallMaxConcurrency());
101+
}
102+
103+
@Test
104+
public void testConstantCallTimeoutValidOverride() {
105+
VmConfig vm = VmConfig.fromConfig(
106+
withRef("vm { constantCallTimeoutMs = 200, constantCallMaxConcurrency = 4 }"));
107+
assertEquals(200L, vm.getConstantCallTimeoutMs());
108+
assertEquals(4, vm.getConstantCallMaxConcurrency());
109+
}
110+
111+
@Test
112+
public void testConstantCallTimeoutBoundaryValues() {
113+
assertEquals(80L, VmConfig.fromConfig(
114+
withRef("vm { constantCallTimeoutMs = 80 }")).getConstantCallTimeoutMs());
115+
assertEquals(500L, VmConfig.fromConfig(
116+
withRef("vm { constantCallTimeoutMs = 500 }")).getConstantCallTimeoutMs());
117+
assertEquals(0L, VmConfig.fromConfig(
118+
withRef("vm { constantCallTimeoutMs = 0 }")).getConstantCallTimeoutMs());
119+
}
120+
121+
@Test
122+
public void testConstantCallTimeoutBelowBaselineRejected() {
123+
try {
124+
VmConfig.fromConfig(withRef("vm { constantCallTimeoutMs = 50 }"));
125+
org.junit.Assert.fail("expected IllegalArgumentException for ms < baseline");
126+
} catch (IllegalArgumentException ex) {
127+
org.junit.Assert.assertTrue(ex.getMessage(),
128+
ex.getMessage().contains("constantCallTimeoutMs"));
129+
}
130+
}
131+
132+
@Test
133+
public void testConstantCallTimeoutAboveMaxRejected() {
134+
try {
135+
VmConfig.fromConfig(withRef("vm { constantCallTimeoutMs = 1000 }"));
136+
org.junit.Assert.fail("expected IllegalArgumentException for ms > max");
137+
} catch (IllegalArgumentException ex) {
138+
org.junit.Assert.assertTrue(ex.getMessage(),
139+
ex.getMessage().contains("constantCallTimeoutMs"));
140+
}
141+
}
142+
143+
@Test
144+
public void testConstantCallMaxConcurrencyZeroRejected() {
145+
try {
146+
VmConfig.fromConfig(withRef("vm { constantCallMaxConcurrency = 0 }"));
147+
org.junit.Assert.fail("expected IllegalArgumentException for cap = 0");
148+
} catch (IllegalArgumentException ex) {
149+
org.junit.Assert.assertTrue(ex.getMessage(),
150+
ex.getMessage().contains("constantCallMaxConcurrency"));
151+
}
152+
}
91153
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.tron.core.vm;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class VmTimeoutPolicyTest {
8+
9+
private static final long START = 1_000_000L; // arbitrary microseconds
10+
private static final long MAX_CPU_MS = 80L;
11+
private static final double RATIO = 1.0;
12+
13+
@Test
14+
public void blockProcessingUsesMaxCpuTimes1000ByRatio() {
15+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
16+
false, START, MAX_CPU_MS, RATIO,
17+
VmTimeoutPolicy.CONSTANT_CALL_TIMEOUT_UNSET);
18+
assertEquals(START + 80_000L, deadline);
19+
}
20+
21+
@Test
22+
public void constantCallWithUnsetFallsBackToMaxCpu() {
23+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
24+
true, START, MAX_CPU_MS, RATIO,
25+
VmTimeoutPolicy.CONSTANT_CALL_TIMEOUT_UNSET);
26+
assertEquals(START + 80_000L, deadline);
27+
}
28+
29+
@Test
30+
public void constantCallWithOverrideUsesConfiguredTimeout() {
31+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
32+
true, START, MAX_CPU_MS, RATIO, 200L);
33+
assertEquals(START + 200_000L, deadline);
34+
}
35+
36+
@Test
37+
public void constantCallNeverShrinksBelowNetwork() {
38+
// Network raised to 100 ms (committee proposal), operator configures 80.
39+
// Effective budget must remain at network value, not 80.
40+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
41+
true, START, 100L, RATIO, 80L);
42+
assertEquals(START + 100_000L, deadline);
43+
}
44+
45+
@Test
46+
public void blockProcessingIgnoresConstantCallOverride() {
47+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
48+
false, START, MAX_CPU_MS, RATIO, 500L);
49+
// Even if operator sets a long constant-call timeout, block processing
50+
// remains gated by maxCpuTimeOfOneTx * ratio. Issue #6681 makes this
51+
// a hard guarantee, not a convention.
52+
assertEquals(START + 80_000L, deadline);
53+
}
54+
55+
@Test
56+
public void blockProcessingAppliesRatio() {
57+
long deadline = VmTimeoutPolicy.computeVmShouldEndInUs(
58+
false, START, MAX_CPU_MS, 0.5, 200L);
59+
assertEquals(START + 40_000L, deadline);
60+
}
61+
62+
// ===========================================================================
63+
// Cap-engagement gate (issue #6681). The cap is engaged strictly when the
64+
// operator's configured timeout exceeds the network's current
65+
// maxCpuTimeOfOneTx; raising the network value shifts the threshold.
66+
// ===========================================================================
67+
68+
@Test
69+
public void capDisengagedAtSentinel() {
70+
org.junit.Assert.assertFalse(VmTimeoutPolicy.isCapEngaged(0L, 80L));
71+
}
72+
73+
@Test
74+
public void capDisengagedWhenConfiguredEqualsNetwork() {
75+
org.junit.Assert.assertFalse(VmTimeoutPolicy.isCapEngaged(80L, 80L));
76+
}
77+
78+
@Test
79+
public void capEngagedJustAboveNetwork() {
80+
org.junit.Assert.assertTrue(VmTimeoutPolicy.isCapEngaged(81L, 80L));
81+
}
82+
83+
@Test
84+
public void capDisengagedWhenNetworkRaisedAboveConfigured() {
85+
// Network committee raised maxCpuTimeOfOneTx to 100; operator's
86+
// previously-set 80 is now below the network value -> no longer
87+
// an "extension" -> cap stays disengaged.
88+
org.junit.Assert.assertFalse(VmTimeoutPolicy.isCapEngaged(80L, 100L));
89+
}
90+
91+
@Test
92+
public void capEngagedAboveRaisedNetwork() {
93+
// Network at 100; operator configures 120 to extend further -> cap on.
94+
org.junit.Assert.assertTrue(VmTimeoutPolicy.isCapEngaged(120L, 100L));
95+
}
96+
}

0 commit comments

Comments
 (0)