Skip to content

Commit

Permalink
func(proposal,chainid): optimize the return value of chainid
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghang8612 committed Dec 21, 2022
1 parent eb0065c commit 716ab53
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 4 deletions.
14 changes: 13 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 @@ -608,6 +608,17 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID]");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID] is only allowed to be 1");
}
break;
}

default:
break;
Expand Down Expand Up @@ -676,7 +687,8 @@ public enum ProposalType { // current value, value range
ALLOW_NEW_REWARD(67), // 0, 1
MEMO_FEE(68), // 0, [0, 1000_000_000]
ALLOW_DELEGATE_OPTIMIZATION(69),
UNFREEZE_DELAY_DAYS(70); // 0, [1, 365]
UNFREEZE_DELAY_DAYS(70), // 0, [1, 365]
ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID(71); // 0, 1

private long code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initAllowHigherLimitForMaxCpuTimeOfOneTx(
ds.getAllowHigherLimitForMaxCpuTimeOfOneTx());
VMConfig.initAllowTvmFreezeV2(ds.supportUnfreezeDelay() ? 1 : 0);
VMConfig.initAllowOptimizedReturnValueOfChainId(
ds.getAllowOptimizedReturnValueOfChainId());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/config/VMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class VMConfig {

private static boolean ALLOW_TVM_FREEZE_V2 = false;

private static boolean ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID = false;

private VMConfig() {
}

Expand Down Expand Up @@ -100,6 +102,10 @@ public static void initAllowTvmFreezeV2(long allow) {
ALLOW_TVM_FREEZE_V2 = allow == 1;
}

public static void initAllowOptimizedReturnValueOfChainId(long allow) {
ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID = allow == 1;
}

public static boolean getEnergyLimitHardFork() {
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
}
Expand Down Expand Up @@ -151,4 +157,8 @@ public static boolean allowHigherLimitForMaxCpuTimeOfOneTx() {
public static boolean allowTvmFreezeV2() {
return ALLOW_TVM_FREEZE_V2;
}

public static boolean allowOptimizedReturnValueOfChainId() {
return ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ public DataWord getCallerAddress() {

public DataWord getChainId() {
byte[] chainId = getContractState().getBlockByNum(0).getBlockId().getBytes();
if (VMConfig.allowTvmCompatibleEvm()) {
if (VMConfig.allowTvmCompatibleEvm() || VMConfig.allowOptimizedReturnValueOfChainId()) {
chainId = Arrays.copyOfRange(chainId, chainId.length - 4, chainId.length);
}
return new DataWord(chainId).clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
private static final byte[] ALLOW_DELEGATE_OPTIMIZATION =
"ALLOW_DELEGATE_OPTIMIZATION".getBytes();


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

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

@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
super(dbName);
Expand Down Expand Up @@ -899,6 +901,14 @@ private DynamicPropertiesStore(@Value("properties") String dbName) {
CommonParameter.getInstance().getUnfreezeDelayDays()
);
}

try {
this.getAllowOptimizedReturnValueOfChainId();
} catch (IllegalArgumentException e) {
this.saveAllowOptimizedReturnValueOfChainId(
CommonParameter.getInstance().getAllowOptimizedReturnValueOfChainId()
);
}
}

public String intArrayToString(int[] a) {
Expand Down Expand Up @@ -2647,6 +2657,20 @@ public void saveUnfreezeDelayDays(long value) {
this.put(UNFREEZE_DELAY_DAYS, new BytesCapsule(ByteArray.fromLong(value)));
}

public void saveAllowOptimizedReturnValueOfChainId(long value) {
this.put(ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID,
new BytesCapsule(ByteArray.fromLong(value)));
}

public long getAllowOptimizedReturnValueOfChainId() {
String msg = "not found ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID";
return Optional.ofNullable(getUnchecked(ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElseThrow(
() -> new IllegalArgumentException(msg));
}

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
@@ -1,7 +1,6 @@
package org.tron.common.parameter;

import com.beust.jcommander.Parameter;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
Expand Down Expand Up @@ -577,6 +576,10 @@ public class CommonParameter {
@Setter
public long unfreezeDelayDays = 0L;

@Getter
@Setter
public long allowOptimizedReturnValueOfChainId = 0L;

private static double calcMaxTimeRatio() {
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
return 5.0;
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public class Constant {
public static final String COMMITTEE_ALLOW_HIGHER_LIMIT_FOR_MAX_CPU_TIME_OF_ONE_TX =
"committee.allowHigherLimitForMaxCpuTimeOfOneTx";
public static final String COMMITTEE_ALLOW_NEW_REWARD_ALGORITHM = "committee.allowNewRewardAlgorithm";
public static final String COMMITTEE_ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID =
"committee.allowOptimizedReturnValueOfChainId";


public static final String METRICS_STORAGE_ENABLE = "node.metrics.storageEnable";
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 @@ -1284,6 +1284,11 @@ public Protocol.ChainParameters getChainParameters() {
.setValue(dbManager.getDynamicPropertiesStore().getUnfreezeDelayDays())
.build());

builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
.setKey("getAllowOptimizedReturnValueOfChainId")
.setValue(dbManager.getDynamicPropertiesStore().getAllowOptimizedReturnValueOfChainId())
.build());

return builder.build();
}

Expand Down
4 changes: 4 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 @@ -952,6 +952,10 @@ public static void setParam(final String[] args, final String confFileName) {
config.hasPath(Constant.COMMITTEE_ALLOW_NEW_REWARD_ALGORITHM) ? config
.getInt(Constant.COMMITTEE_ALLOW_NEW_REWARD_ALGORITHM) : 0;

PARAMETER.allowOptimizedReturnValueOfChainId =
config.hasPath(Constant.COMMITTEE_ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID) ? config
.getInt(Constant.COMMITTEE_ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID) : 0;

initBackupProperty(config);
if (Constant.ROCKSDB.equalsIgnoreCase(CommonParameter
.getInstance().getStorage().getDbEngine())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule)
manager.getDynamicPropertiesStore().saveAllowDelegateOptimization(entry.getValue());
break;
}
case ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID: {
manager.getDynamicPropertiesStore()
.saveAllowOptimizedReturnValueOfChainId(entry.getValue());
break;
}
default:
find = false;
break;
Expand Down

0 comments on commit 716ab53

Please sign in to comment.