Skip to content

Commit 1f0aa38

Browse files
Merge pull request #6141 from zeusoo001/consensus_logic_opt
feat(consensus): add consensus logic optimization proposal
2 parents 92ae447 + b4dea0d commit 1f0aa38

File tree

11 files changed

+134
-3
lines changed

11 files changed

+134
-3
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
794794
}
795795
break;
796796
}
797+
case CONSENSUS_LOGIC_OPTIMIZATION: {
798+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_0)) {
799+
throw new ContractValidateException(
800+
"Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]");
801+
}
802+
if (dynamicPropertiesStore.getConsensusLogicOptimization() == 1) {
803+
throw new ContractValidateException(
804+
"[CONSENSUS_LOGIC_OPTIMIZATION] has been valid, no need to propose again");
805+
}
806+
if (value != 1) {
807+
throw new ContractValidateException(
808+
"This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1");
809+
}
810+
break;
811+
}
797812
default:
798813
break;
799814
}
@@ -873,7 +888,8 @@ public enum ProposalType { // current value, value range
873888
ALLOW_OLD_REWARD_OPT(79), // 0, 1
874889
ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1
875890
MAX_CREATE_ACCOUNT_TX_SIZE(82), // [500, 10000]
876-
ALLOW_STRICT_MATH(87); // 0, 1
891+
ALLOW_STRICT_MATH(87), // 0, 1
892+
CONSENSUS_LOGIC_OPTIMIZATION(88); // 0, 1
877893

878894
private long code;
879895

chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
224224
private static final byte[] MAX_CREATE_ACCOUNT_TX_SIZE = "MAX_CREATE_ACCOUNT_TX_SIZE".getBytes();
225225
private static final byte[] ALLOW_STRICT_MATH = "ALLOW_STRICT_MATH".getBytes();
226226

227+
private static final byte[] CONSENSUS_LOGIC_OPTIMIZATION
228+
= "CONSENSUS_LOGIC_OPTIMIZATION".getBytes();
229+
227230
@Autowired
228231
private DynamicPropertiesStore(@Value("properties") String dbName) {
229232
super(dbName);
@@ -2891,6 +2894,22 @@ public boolean allowStrictMath() {
28912894
return getAllowStrictMath() == 1L;
28922895
}
28932896

2897+
public void saveConsensusLogicOptimization(long value) {
2898+
this.put(CONSENSUS_LOGIC_OPTIMIZATION,
2899+
new BytesCapsule(ByteArray.fromLong(value)));
2900+
}
2901+
2902+
public long getConsensusLogicOptimization() {
2903+
return Optional.ofNullable(getUnchecked(CONSENSUS_LOGIC_OPTIMIZATION))
2904+
.map(BytesCapsule::getData)
2905+
.map(ByteArray::toLong)
2906+
.orElse(CommonParameter.getInstance().getConsensusLogicOptimization());
2907+
}
2908+
2909+
public boolean allowConsensusLogicOptimization() {
2910+
return getConsensusLogicOptimization() == 1L;
2911+
}
2912+
28942913
private static class DynamicResourceProperties {
28952914

28962915
private static final byte[] ONE_DAY_NET_LIMIT = "ONE_DAY_NET_LIMIT".getBytes();

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,10 @@ public class CommonParameter {
681681
@Setter
682682
public long allowStrictMath;
683683

684+
@Getter
685+
@Setter
686+
public long consensusLogicOptimization;
687+
684688
private static double calcMaxTimeRatio() {
685689
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
686690
return 5.0;

common/src/main/java/org/tron/core/Constant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,7 @@ public class Constant {
387387

388388
public static final String COMMITTEE_ALLOW_ENERGY_ADJUSTMENT = "committee.allowEnergyAdjustment";
389389
public static final String COMMITTEE_ALLOW_STRICT_MATH = "committee.allowStrictMath";
390+
391+
public static final String COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION
392+
= "committee.consensusLogicOptimization";
390393
}

common/src/main/java/org/tron/core/config/Parameter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public enum ForkBlockVersionEnum {
2525
VERSION_4_7_2(28, 1596780000000L, 80),
2626
VERSION_4_7_4(29, 1596780000000L, 80),
2727
VERSION_4_7_5(30, 1596780000000L, 80),
28-
VERSION_4_7_7(31, 1596780000000L, 80);
28+
VERSION_4_7_7(31, 1596780000000L, 80),
29+
VERSION_4_8_0(32, 1596780000000L, 80);
2930
// if add a version, modify BLOCK_VERSION simultaneously
3031

3132
@Getter
@@ -74,7 +75,7 @@ public class ChainConstant {
7475
public static final int SINGLE_REPEAT = 1;
7576
public static final int BLOCK_FILLED_SLOTS_NUMBER = 128;
7677
public static final int MAX_FROZEN_NUMBER = 1;
77-
public static final int BLOCK_VERSION = 31;
78+
public static final int BLOCK_VERSION = 32;
7879
public static final long FROZEN_PERIOD = 86_400_000L;
7980
public static final long DELEGATE_PERIOD = 3 * 86_400_000L;
8081
public static final long TRX_PRECISION = 1000_000L;

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,11 @@ public Protocol.ChainParameters getChainParameters() {
13481348
.setValue(dbManager.getDynamicPropertiesStore().getAllowStrictMath())
13491349
.build());
13501350

1351+
builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
1352+
.setKey("getConsensusLogicOptimization")
1353+
.setValue(dbManager.getDynamicPropertiesStore().getConsensusLogicOptimization())
1354+
.build());
1355+
13511356
return builder.build();
13521357
}
13531358

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public static void clearParam() {
235235
PARAMETER.allowOldRewardOpt = 0;
236236
PARAMETER.allowEnergyAdjustment = 0;
237237
PARAMETER.allowStrictMath = 0;
238+
PARAMETER.consensusLogicOptimization = 0;
238239
}
239240

240241
/**
@@ -1222,6 +1223,10 @@ public static void setParam(final String[] args, final String confFileName) {
12221223
config.hasPath(Constant.COMMITTEE_ALLOW_STRICT_MATH) ? config
12231224
.getInt(Constant.COMMITTEE_ALLOW_STRICT_MATH) : 0;
12241225

1226+
PARAMETER.consensusLogicOptimization =
1227+
config.hasPath(Constant.COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION) ? config
1228+
.getInt(Constant.COMMITTEE_CONSENSUS_LOGIC_OPTIMIZATION) : 0;
1229+
12251230
logConfig();
12261231
}
12271232

framework/src/main/java/org/tron/core/consensus/ProposalService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule)
371371
manager.getDynamicPropertiesStore().saveAllowStrictMath(entry.getValue());
372372
break;
373373
}
374+
case CONSENSUS_LOGIC_OPTIMIZATION: {
375+
manager.getDynamicPropertiesStore()
376+
.saveConsensusLogicOptimization(entry.getValue());
377+
break;
378+
}
374379
default:
375380
find = false;
376381
break;

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ public void validateCheck() {
433433

434434
testEnergyAdjustmentProposal();
435435

436+
testConsensusLogicOptimizationProposal();
437+
436438
forkUtils.getManager().getDynamicPropertiesStore()
437439
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
438440
forkUtils.reset();
@@ -500,6 +502,55 @@ private void testEnergyAdjustmentProposal() {
500502
}
501503
}
502504

505+
private void testConsensusLogicOptimizationProposal() {
506+
try {
507+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
508+
ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 1);
509+
Assert.fail();
510+
} catch (ContractValidateException e) {
511+
Assert.assertEquals(
512+
"Bad chain parameter id [CONSENSUS_LOGIC_OPTIMIZATION]",
513+
e.getMessage());
514+
}
515+
516+
long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore()
517+
.getMaintenanceTimeInterval();
518+
519+
long hardForkTime =
520+
((ForkBlockVersionEnum.VERSION_4_8_0.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
521+
* maintenanceTimeInterval;
522+
forkUtils.getManager().getDynamicPropertiesStore()
523+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
524+
525+
byte[] stats = new byte[27];
526+
Arrays.fill(stats, (byte) 1);
527+
forkUtils.getManager().getDynamicPropertiesStore()
528+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats);
529+
530+
// Should fail because the proposal value is invalid
531+
try {
532+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
533+
ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 2);
534+
Assert.fail();
535+
} catch (ContractValidateException e) {
536+
Assert.assertEquals(
537+
"This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1",
538+
e.getMessage());
539+
}
540+
541+
dynamicPropertiesStore.saveConsensusLogicOptimization(1);
542+
try {
543+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
544+
ProposalType.CONSENSUS_LOGIC_OPTIMIZATION.getCode(), 1);
545+
Assert.fail();
546+
} catch (ContractValidateException e) {
547+
Assert.assertEquals(
548+
"[CONSENSUS_LOGIC_OPTIMIZATION] has been valid, no need to propose again",
549+
e.getMessage());
550+
}
551+
552+
}
553+
503554
@Test
504555
public void blockVersionCheck() {
505556
for (ForkBlockVersionEnum forkVersion : ForkBlockVersionEnum.values()) {

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public void get() {
115115
Assert.assertEquals(GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE, parameter.getMaxMessageSize());
116116
Assert.assertEquals(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, parameter.getMaxHeaderListSize());
117117
Assert.assertEquals(1L, parameter.getAllowCreationOfContracts());
118+
Assert.assertEquals(0, parameter.getConsensusLogicOptimization());
118119

119120
Assert.assertEquals(privateKey,
120121
Args.getLocalWitnesses().getPrivateKey());

0 commit comments

Comments
 (0)