Skip to content

Commit 4058702

Browse files
committed
func(test): add unit test for getAllowEnergyAdjustment proposal
1 parent eb2b880 commit 4058702

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.protobuf.ByteString;
44
import java.util.ArrayList;
55
import java.util.Arrays;
6+
import java.util.HashMap;
67
import java.util.List;
78
import javax.annotation.Resource;
89
import lombok.extern.slf4j.Slf4j;
@@ -12,13 +13,18 @@
1213
import org.tron.common.BaseTest;
1314
import org.tron.common.utils.ByteArray;
1415
import org.tron.common.utils.ForkController;
16+
import org.tron.core.ChainBaseManager;
1517
import org.tron.core.Constant;
1618
import org.tron.core.capsule.BytesCapsule;
19+
import org.tron.core.capsule.ProposalCapsule;
1720
import org.tron.core.config.Parameter;
1821
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
1922
import org.tron.core.config.args.Args;
23+
import org.tron.core.consensus.ProposalService;
24+
import org.tron.core.db.Manager;
2025
import org.tron.core.exception.ContractValidateException;
2126
import org.tron.core.store.DynamicPropertiesStore;
27+
import org.tron.core.store.StoreFactory;
2228
import org.tron.core.utils.ProposalUtil;
2329
import org.tron.core.utils.ProposalUtil.ProposalType;
2430

@@ -381,11 +387,76 @@ public void validateCheck() {
381387
e.getMessage());
382388
}
383389

390+
testEnergyAdjustmentProposal();
391+
384392
forkUtils.getManager().getDynamicPropertiesStore()
385393
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
386394
forkUtils.reset();
387395
}
388396

397+
private void testEnergyAdjustmentProposal() {
398+
// Should fail because cannot pass the fork controller check
399+
try {
400+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
401+
ProposalType.ALLOW_ENERGY_ADJUSTMENT.getCode(), 1);
402+
Assert.fail();
403+
} catch (ContractValidateException e) {
404+
Assert.assertEquals(
405+
"Bad chain parameter id [ALLOW_ENERGY_ADJUSTMENT]",
406+
e.getMessage());
407+
}
408+
409+
long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore()
410+
.getMaintenanceTimeInterval();
411+
412+
long hardForkTime =
413+
((ForkBlockVersionEnum.VERSION_4_7_5.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
414+
* maintenanceTimeInterval;
415+
forkUtils.getManager().getDynamicPropertiesStore()
416+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
417+
418+
byte[] stats = new byte[27];
419+
Arrays.fill(stats, (byte) 1);
420+
forkUtils.getManager().getDynamicPropertiesStore()
421+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_7_5.getValue(), stats);
422+
423+
// Should fail because the proposal value is invalid
424+
try {
425+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
426+
ProposalType.ALLOW_ENERGY_ADJUSTMENT.getCode(), 2);
427+
Assert.fail();
428+
} catch (ContractValidateException e) {
429+
Assert.assertEquals(
430+
"This value[ALLOW_ENERGY_ADJUSTMENT] is only allowed to be 1",
431+
e.getMessage());
432+
}
433+
434+
// Should succeed
435+
try {
436+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
437+
ProposalType.ALLOW_ENERGY_ADJUSTMENT.getCode(), 1);
438+
} catch (Throwable t) {
439+
Assert.fail();
440+
}
441+
442+
ProposalCapsule proposalCapsule = new ProposalCapsule(ByteString.empty(), 0);
443+
proposalCapsule.setParameters(new HashMap<Long, Long>(){{
444+
put(81L, 1L);
445+
}});
446+
447+
ProposalService.process(dbManager, proposalCapsule);
448+
449+
try {
450+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
451+
ProposalType.ALLOW_ENERGY_ADJUSTMENT.getCode(), 1);
452+
Assert.fail();
453+
} catch (ContractValidateException e) {
454+
Assert.assertEquals(
455+
"[ALLOW_ENERGY_ADJUSTMENT] has been valid, no need to propose again",
456+
e.getMessage());
457+
}
458+
}
459+
389460
@Test
390461
public void blockVersionCheck() {
391462
for (ForkBlockVersionEnum forkVersion : ForkBlockVersionEnum.values()) {

0 commit comments

Comments
 (0)