|
| 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