diff --git a/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java b/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java index fb4d6f76228..081b91dfd6c 100644 --- a/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java +++ b/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java @@ -794,6 +794,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore, } break; } + case CONSENSUS_LOGIC_OPTIMIZATION: { + if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_0)) { + throw new ContractValidateException( + "Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]"); + } + if (dynamicPropertiesStore.getConsensusLogicOptimization() == 1) { + throw new ContractValidateException( + "[CONSENSUS_LOGIC_OPTIMIZATION] has been valid, no need to propose again"); + } + if (value != 1) { + throw new ContractValidateException( + "This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1"); + } + break; + } default: break; } @@ -873,7 +888,8 @@ public enum ProposalType { // current value, value range ALLOW_OLD_REWARD_OPT(79), // 0, 1 ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1 MAX_CREATE_ACCOUNT_TX_SIZE(82), // [500, 10000] - ALLOW_STRICT_MATH(87); // 0, 1 + ALLOW_STRICT_MATH(87), // 0, 1 + CONSENSUS_LOGIC_OPTIMIZATION(88); // 0, 1 private long code; diff --git a/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java b/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java index 4af338f09bb..85f958ada8b 100644 --- a/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java +++ b/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java @@ -224,6 +224,9 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking private static final byte[] MAX_CREATE_ACCOUNT_TX_SIZE = "MAX_CREATE_ACCOUNT_TX_SIZE".getBytes(); private static final byte[] ALLOW_STRICT_MATH = "ALLOW_STRICT_MATH".getBytes(); + private static final byte[] CONSENSUS_LOGIC_OPTIMIZATION + = "CONSENSUS_LOGIC_OPTIMIZATION".getBytes(); + @Autowired private DynamicPropertiesStore(@Value("properties") String dbName) { super(dbName); @@ -2891,6 +2894,22 @@ public boolean allowStrictMath() { return getAllowStrictMath() == 1L; } + public void saveConsensusLogicOptimization(long value) { + this.put(CONSENSUS_LOGIC_OPTIMIZATION, + new BytesCapsule(ByteArray.fromLong(value))); + } + + public long getConsensusLogicOptimization() { + return Optional.ofNullable(getUnchecked(CONSENSUS_LOGIC_OPTIMIZATION)) + .map(BytesCapsule::getData) + .map(ByteArray::toLong) + .orElse(CommonParameter.getInstance().getConsensusLogicOptimization()); + } + + public boolean allowConsensusLogicOptimization() { + return getConsensusLogicOptimization() == 1L; + } + private static class DynamicResourceProperties { private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes(); diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 1aa3befe8aa..d4a7b064cbb 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -681,6 +681,10 @@ public class CommonParameter { @Setter public long allowStrictMath; + @Getter + @Setter + public long consensusLogicOptimization; + private static double calcMaxTimeRatio() { //return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1))); return 5.0; diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 96ff41b91da..3bdbf2113af 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -387,4 +387,7 @@ public class Constant { public static final String COMMITTEE_ALLOW_ENERGY_ADJUSTMENT = "committee.allowEnergyAdjustment"; public static final String COMMITTEE_ALLOW_STRICT_MATH = "committee.allowStrictMath"; + + public static final String COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION + = "committee.consensusLogicOptimization"; } diff --git a/common/src/main/java/org/tron/core/config/Parameter.java b/common/src/main/java/org/tron/core/config/Parameter.java index 247c5dd4fe2..a71dc58e8bd 100644 --- a/common/src/main/java/org/tron/core/config/Parameter.java +++ b/common/src/main/java/org/tron/core/config/Parameter.java @@ -25,7 +25,8 @@ public enum ForkBlockVersionEnum { VERSION_4_7_2(28, 1596780000000L, 80), VERSION_4_7_4(29, 1596780000000L, 80), VERSION_4_7_5(30, 1596780000000L, 80), - VERSION_4_7_7(31, 1596780000000L, 80); + VERSION_4_7_7(31, 1596780000000L, 80), + VERSION_4_8_0(32, 1596780000000L, 80); // if add a version, modify BLOCK_VERSION simultaneously @Getter @@ -74,7 +75,7 @@ public class ChainConstant { public static final int SINGLE_REPEAT = 1; public static final int BLOCK_FILLED_SLOTS_NUMBER = 128; public static final int MAX_FROZEN_NUMBER = 1; - public static final int BLOCK_VERSION = 31; + public static final int BLOCK_VERSION = 32; public static final long FROZEN_PERIOD = 86_400_000L; public static final long DELEGATE_PERIOD = 3 * 86_400_000L; public static final long TRX_PRECISION = 1000_000L; diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 0943723f2f4..e2eea8700d1 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -1348,6 +1348,11 @@ public Protocol.ChainParameters getChainParameters() { .setValue(dbManager.getDynamicPropertiesStore().getAllowStrictMath()) .build()); + builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder() + .setKey("getConsensusLogicOptimization") + .setValue(dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization()) + .build()); + return builder.build(); } diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 8853971a5f8..7397b69cde3 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -235,6 +235,7 @@ public static void clearParam() { PARAMETER.allowOldRewardOpt = 0; PARAMETER.allowEnergyAdjustment = 0; PARAMETER.allowStrictMath = 0; + PARAMETER.consensusLogicOptimization = 0; } /** @@ -1222,6 +1223,10 @@ public static void setParam(final String[] args, final String confFileName) { config.hasPath(Constant.COMMITTEE_ALLOW_STRICT_MATH) ? config .getInt(Constant.COMMITTEE_ALLOW_STRICT_MATH) : 0; + PARAMETER.consensusLogicOptimization = + config.hasPath(Constant.COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION) ? config + .getInt(Constant.COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION) : 0; + logConfig(); } diff --git a/framework/src/main/java/org/tron/core/consensus/ProposalService.java b/framework/src/main/java/org/tron/core/consensus/ProposalService.java index 29eef1c3cb3..120394de1be 100644 --- a/framework/src/main/java/org/tron/core/consensus/ProposalService.java +++ b/framework/src/main/java/org/tron/core/consensus/ProposalService.java @@ -371,6 +371,11 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule) manager.getDynamicPropertiesStore().saveAllowStrictMath(entry.getValue()); break; } + case CONSENSUS_LOGIC_OPTIMIZATION: { + manager.getDynamicPropertiesStore() + .saveConsensusLogicOptimization(entry.getValue()); + break; + } default: find = false; break; diff --git a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java index 52f8cdacc00..1cfc4d1887f 100644 --- a/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java +++ b/framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java @@ -433,6 +433,8 @@ public void validateCheck() { testEnergyAdjustmentProposal(); + testConsensusLogicOptimizationProposal(); + forkUtils.getManager().getDynamicPropertiesStore() .statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats); forkUtils.reset(); @@ -500,6 +502,55 @@ private void testEnergyAdjustmentProposal() { } } + private void testConsensusLogicOptimizationProposal() { + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 1); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]", + e.getMessage()); + } + + long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore() + .getMaintenanceTimeInterval(); + + long hardForkTime = + ((ForkBlockVersionEnum.VERSION_4_8_0.getHardForkTime() - 1) / maintenanceTimeInterval + 1) + * maintenanceTimeInterval; + forkUtils.getManager().getDynamicPropertiesStore() + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); + + byte[] stats = new byte[27]; + Arrays.fill(stats, (byte) 1); + forkUtils.getManager().getDynamicPropertiesStore() + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats); + + // Should fail because the proposal value is invalid + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 2); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1", + e.getMessage()); + } + + dynamicPropertiesStore.saveConsensusLogicOptimization(1); + try { + ProposalUtil.validator(dynamicPropertiesStore, forkUtils, + ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 1); + Assert.fail(); + } catch (ContractValidateException e) { + Assert.assertEquals( + "[CONSENSUS_LOGIC_OPTIMIZATION] has been valid, no need to propose again", + e.getMessage()); + } + + } + @Test public void blockVersionCheck() { for (ForkBlockVersionEnum forkVersion : ForkBlockVersionEnum.values()) { diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 5bbf08fd96e..61f34b647c5 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -115,6 +115,7 @@ public void get() { Assert.assertEquals(GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE, parameter.getMaxMessageSize()); Assert.assertEquals(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, parameter.getMaxHeaderListSize()); Assert.assertEquals(1L, parameter.getAllowCreationOfContracts()); + Assert.assertEquals(0, parameter.getConsensusLogicOptimization()); Assert.assertEquals(privateKey, Args.getLocalWitnesses().getPrivateKey()); diff --git a/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java b/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java index 0ba32b27f2e..cda9bfe4186 100644 --- a/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java +++ b/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java @@ -1,5 +1,6 @@ package org.tron.core.services; +import static org.tron.core.utils.ProposalUtil.ProposalType.CONSENSUS_LOGIC_OPTIMIZATION; import static org.tron.core.utils.ProposalUtil.ProposalType.ENERGY_FEE; import static org.tron.core.utils.ProposalUtil.ProposalType.TRANSACTION_FEE; import static org.tron.core.utils.ProposalUtil.ProposalType.WITNESS_127_PAY_PER_BLOCK; @@ -106,4 +107,24 @@ public void testUpdateTransactionFee() { Assert.assertEquals(expResult, currentHistory); } + @Test + public void testUpdateConsensusLogicOptimization() { + long v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization(); + Assert.assertEquals(v, 0); + Assert.assertTrue(!dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization()); + + long value = 1; + Proposal proposal = + Proposal.newBuilder().putParameters(CONSENSUS_LOGIC_OPTIMIZATION.getCode(), value).build(); + ProposalCapsule proposalCapsule = new ProposalCapsule(proposal); + proposalCapsule.setExpirationTime(1627279200000L); + boolean result = ProposalService.process(dbManager, proposalCapsule); + Assert.assertTrue(result); + + v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization(); + Assert.assertEquals(v, value); + + Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization()); + } + } \ No newline at end of file