Skip to content

Commit ce04da4

Browse files
authored
feat(proposal): prevent re-submission after ALLOW_OLD_REWARD_OPT is approved (#5683)
1 parent 4bb4922 commit ce04da4

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,10 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
733733
throw new ContractValidateException(
734734
"Bad chain parameter id [ALLOW_OLD_REWARD_OPT]");
735735
}
736+
if (dynamicPropertiesStore.allowOldRewardOpt()) {
737+
throw new ContractValidateException(
738+
"[ALLOW_OLD_REWARD_OPT] has been valid, no need to propose again");
739+
}
736740
if (value != 1) {
737741
throw new ContractValidateException(
738742
"This value[ALLOW_OLD_REWARD_OPT] is only allowed to be 1");

framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.List;
7+
import javax.annotation.Resource;
78
import lombok.extern.slf4j.Slf4j;
89
import org.junit.Assert;
910
import org.junit.BeforeClass;
@@ -12,6 +13,7 @@
1213
import org.tron.common.utils.ByteArray;
1314
import org.tron.common.utils.ForkController;
1415
import org.tron.core.Constant;
16+
import org.tron.core.capsule.BytesCapsule;
1517
import org.tron.core.config.Parameter;
1618
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
1719
import org.tron.core.config.args.Args;
@@ -27,6 +29,11 @@ public class ProposalUtilTest extends BaseTest {
2729
private static final String LONG_VALUE_ERROR =
2830
"Bad chain parameter value, valid range is [0," + LONG_VALUE + "]";
2931

32+
@Resource
33+
private DynamicPropertiesStore dynamicPropertiesStore;
34+
35+
ForkController forkUtils = ForkController.instance();
36+
3037
/**
3138
* Init .
3239
*/
@@ -60,8 +67,6 @@ public void validProposalTypeCheck() throws ContractValidateException {
6067

6168
@Test
6269
public void validateCheck() {
63-
DynamicPropertiesStore dynamicPropertiesStore = null;
64-
ForkController forkUtils = ForkController.instance();
6570
long invalidValue = -1;
6671

6772
try {
@@ -328,6 +333,66 @@ public void validateCheck() {
328333
e.getMessage());
329334
}
330335

336+
try {
337+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
338+
ProposalType.ALLOW_OLD_REWARD_OPT.getCode(), 2);
339+
Assert.fail();
340+
} catch (ContractValidateException e) {
341+
Assert.assertEquals(
342+
"Bad chain parameter id [ALLOW_OLD_REWARD_OPT]",
343+
e.getMessage());
344+
}
345+
hardForkTime =
346+
((ForkBlockVersionEnum.VERSION_4_7_4.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
347+
* maintenanceTimeInterval;
348+
forkUtils.getManager().getDynamicPropertiesStore()
349+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
350+
forkUtils.getManager().getDynamicPropertiesStore()
351+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_7_4.getValue(), stats);
352+
try {
353+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
354+
ProposalType.ALLOW_OLD_REWARD_OPT.getCode(), 2);
355+
Assert.fail();
356+
} catch (ContractValidateException e) {
357+
Assert.assertEquals(
358+
"This value[ALLOW_OLD_REWARD_OPT] is only allowed to be 1",
359+
e.getMessage());
360+
}
361+
try {
362+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
363+
ProposalType.ALLOW_OLD_REWARD_OPT.getCode(), 1);
364+
Assert.fail();
365+
} catch (ContractValidateException e) {
366+
Assert.assertEquals(
367+
"[ALLOW_NEW_REWARD] proposal must be approved "
368+
+ "before [ALLOW_OLD_REWARD_OPT] can be proposed",
369+
e.getMessage());
370+
}
371+
dynamicPropertiesStore.saveCurrentCycleNumber(0);
372+
dynamicPropertiesStore.saveNewRewardAlgorithmEffectiveCycle();
373+
dynamicPropertiesStore.saveAllowNewReward(1);
374+
try {
375+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
376+
ProposalType.ALLOW_OLD_REWARD_OPT.getCode(), 1);
377+
Assert.fail();
378+
} catch (ContractValidateException e) {
379+
Assert.assertEquals(
380+
"no need old reward opt, ALLOW_NEW_REWARD from start cycle 1",
381+
e.getMessage());
382+
}
383+
dynamicPropertiesStore.put("NEW_REWARD_ALGORITHM_EFFECTIVE_CYCLE".getBytes(),
384+
new BytesCapsule(ByteArray.fromLong(4000)));
385+
dynamicPropertiesStore.saveAllowOldRewardOpt(1);
386+
try {
387+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
388+
ProposalType.ALLOW_OLD_REWARD_OPT.getCode(), 1);
389+
Assert.fail();
390+
} catch (ContractValidateException e) {
391+
Assert.assertEquals(
392+
"[ALLOW_OLD_REWARD_OPT] has been valid, no need to propose again",
393+
e.getMessage());
394+
}
395+
331396
forkUtils.getManager().getDynamicPropertiesStore()
332397
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
333398
forkUtils.reset();

0 commit comments

Comments
 (0)