From 021a5c1c2f4dd17542c434ca6e849d5c16dbf026 Mon Sep 17 00:00:00 2001 From: zeusoo001 Date: Thu, 18 Jul 2024 15:58:44 +0800 Subject: [PATCH 1/2] feat(consensus): add consensus logic optimization proposal --- .../org/tron/core/utils/ProposalUtil.java | 20 ++++++++-- .../core/store/DynamicPropertiesStore.java | 15 +++++++ .../common/parameter/CommonParameter.java | 4 ++ .../src/main/java/org/tron/core/Constant.java | 3 ++ .../java/org/tron/core/config/Parameter.java | 5 ++- .../org/tron/consensus/dpos/DposService.java | 5 +-- .../src/main/java/org/tron/core/Wallet.java | 5 +++ .../java/org/tron/core/config/args/Args.java | 5 +++ .../tron/core/consensus/ProposalService.java | 5 +++ .../core/actuator/utils/ProposalUtilTest.java | 39 +++++++++++++++++++ .../org/tron/core/config/args/ArgsTest.java | 1 + .../core/services/ProposalServiceTest.java | 18 +++++++++ 12 files changed, 116 insertions(+), 9 deletions(-) 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..fc60316eb40 100644 --- a/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java +++ b/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java @@ -782,15 +782,26 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore, case ALLOW_STRICT_MATH: { if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_7)) { throw new ContractValidateException( - "Bad chain parameter id [ALLOW_STRICT_MATH]"); + "Bad chain parameter id [ALLOW_STRICT_MATH]"); } if (dynamicPropertiesStore.allowStrictMath()) { throw new ContractValidateException( - "[ALLOW_STRICT_MATH] has been valid, no need to propose again"); + "[ALLOW_STRICT_MATH] has been valid, no need to propose again"); } if (value != 1) { throw new ContractValidateException( - "This value[ALLOW_STRICT_MATH] is only allowed to be 1"); + "This value[ALLOW_STRICT_MATH] is only allowed to be 1"); + } + break; + } + case CONSENSUS_LOGIC_OPTIMIZATION: { + if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_0)) { + throw new ContractValidateException( + "Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]"); + } + if (value != 1) { + throw new ContractValidateException( + "This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1"); } break; } @@ -873,7 +884,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..261b4462f55 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,18 @@ 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()); + } + 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/consensus/src/main/java/org/tron/consensus/dpos/DposService.java b/consensus/src/main/java/org/tron/consensus/dpos/DposService.java index 5ec6c7c554b..bc46345da08 100644 --- a/consensus/src/main/java/org/tron/consensus/dpos/DposService.java +++ b/consensus/src/main/java/org/tron/consensus/dpos/DposService.java @@ -1,8 +1,5 @@ package org.tron.consensus.dpos; -import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM; -import static org.tron.core.config.Parameter.ChainConstant.SOLIDIFIED_THRESHOLD; - import com.google.protobuf.ByteString; import java.util.ArrayList; import java.util.Comparator; @@ -27,6 +24,8 @@ import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.WitnessCapsule; +import static org.tron.core.config.Parameter.ChainConstant.*; + @Slf4j(topic = "consensus") @Component public class DposService implements ConsensusInterface { 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..cc82ed88cc8 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,43 @@ 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()); + } + } + @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..5975c83b88f 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,21 @@ public void testUpdateTransactionFee() { Assert.assertEquals(expResult, currentHistory); } + @Test + public void testUpdateConsensusLogicOptimization() { + long v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization(); + Assert.assertEquals(v, 0); + + 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); + } + } \ No newline at end of file From 46826a70a97bef3dcc3c5bfaebdb367775d8b75e Mon Sep 17 00:00:00 2001 From: zeusoo001 Date: Tue, 14 Jan 2025 21:19:53 +0800 Subject: [PATCH 2/2] feat(consensus): modify the review comments --- .../java/org/tron/core/utils/ProposalUtil.java | 14 +++++++++----- .../tron/core/store/DynamicPropertiesStore.java | 4 ++++ .../java/org/tron/consensus/dpos/DposService.java | 5 +++-- .../tron/core/actuator/utils/ProposalUtilTest.java | 12 ++++++++++++ .../tron/core/services/ProposalServiceTest.java | 3 +++ 5 files changed, 31 insertions(+), 7 deletions(-) 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 fc60316eb40..081b91dfd6c 100644 --- a/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java +++ b/actuator/src/main/java/org/tron/core/utils/ProposalUtil.java @@ -782,26 +782,30 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore, case ALLOW_STRICT_MATH: { if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_7)) { throw new ContractValidateException( - "Bad chain parameter id [ALLOW_STRICT_MATH]"); + "Bad chain parameter id [ALLOW_STRICT_MATH]"); } if (dynamicPropertiesStore.allowStrictMath()) { throw new ContractValidateException( - "[ALLOW_STRICT_MATH] has been valid, no need to propose again"); + "[ALLOW_STRICT_MATH] has been valid, no need to propose again"); } if (value != 1) { throw new ContractValidateException( - "This value[ALLOW_STRICT_MATH] is only allowed to be 1"); + "This value[ALLOW_STRICT_MATH] is only allowed to be 1"); } break; } case CONSENSUS_LOGIC_OPTIMIZATION: { if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_0)) { throw new ContractValidateException( - "Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]"); + "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"); + "This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1"); } break; } 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 261b4462f55..85f958ada8b 100644 --- a/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java +++ b/chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java @@ -2906,6 +2906,10 @@ public long getConsensusLogicOptimization() { .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/consensus/src/main/java/org/tron/consensus/dpos/DposService.java b/consensus/src/main/java/org/tron/consensus/dpos/DposService.java index bc46345da08..5ec6c7c554b 100644 --- a/consensus/src/main/java/org/tron/consensus/dpos/DposService.java +++ b/consensus/src/main/java/org/tron/consensus/dpos/DposService.java @@ -1,5 +1,8 @@ package org.tron.consensus.dpos; +import static org.tron.core.config.Parameter.ChainConstant.MAX_ACTIVE_WITNESS_NUM; +import static org.tron.core.config.Parameter.ChainConstant.SOLIDIFIED_THRESHOLD; + import com.google.protobuf.ByteString; import java.util.ArrayList; import java.util.Comparator; @@ -24,8 +27,6 @@ import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.WitnessCapsule; -import static org.tron.core.config.Parameter.ChainConstant.*; - @Slf4j(topic = "consensus") @Component public class DposService implements ConsensusInterface { 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 cc82ed88cc8..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 @@ -537,6 +537,18 @@ private void testConsensusLogicOptimizationProposal() { "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 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 5975c83b88f..cda9bfe4186 100644 --- a/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java +++ b/framework/src/test/java/org/tron/core/services/ProposalServiceTest.java @@ -111,6 +111,7 @@ public void testUpdateTransactionFee() { public void testUpdateConsensusLogicOptimization() { long v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization(); Assert.assertEquals(v, 0); + Assert.assertTrue(!dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization()); long value = 1; Proposal proposal = @@ -122,6 +123,8 @@ public void testUpdateConsensusLogicOptimization() { v = dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization(); Assert.assertEquals(v, value); + + Assert.assertTrue(dbManager.getDynamicPropertiesStore().allowConsensusLogicOptimization()); } } \ No newline at end of file