Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(consensus): add consensus logic optimization proposal #6141

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[CONSENSUS_LOGIC_OPTIMIZATION] has been valid, no need to propose again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have seen many proposals that do not implement this judgment logic, such as ALLOW_TVM_SHANGHAI, ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID

if (value != 1) {
throw new ContractValidateException(
"This value[CONSENSUS_LOGIC_OPTIMIZATION] is only allowed to be 1");
}
break;
}
Expand Down Expand Up @@ -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;

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,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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public boolean allowConsensusLogicOptimization() {
return getConsensusLogicOptimization() == 1L;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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();
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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +24,8 @@
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.WitnessCapsule;

import static org.tron.core.config.Parameter.ChainConstant.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check style do not allow *

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but the local check style is passed.


@Slf4j(topic = "consensus")
@Component
public class DposService implements ConsensusInterface {
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,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()) {
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,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);
}

}
Loading