Skip to content

Commit

Permalink
Merge pull request #6141 from zeusoo001/consensus_logic_opt
Browse files Browse the repository at this point in the history
feat(consensus): add consensus logic optimization proposal
  • Loading branch information
CodeNinjaEvan authored Jan 20, 2025
2 parents 92ae447 + b4dea0d commit 1f0aa38
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 3 deletions.
18 changes: 17 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
5 changes: 3 additions & 2 deletions common/src/main/java/org/tron/core/config/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
5 changes: 5 additions & 0 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public static void clearParam() {
PARAMETER.allowOldRewardOpt = 0;
PARAMETER.allowEnergyAdjustment = 0;
PARAMETER.allowStrictMath = 0;
PARAMETER.consensusLogicOptimization = 0;
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ public void validateCheck() {

testEnergyAdjustmentProposal();

testConsensusLogicOptimizationProposal();

forkUtils.getManager().getDynamicPropertiesStore()
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
forkUtils.reset();
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

}

0 comments on commit 1f0aa38

Please sign in to comment.