Skip to content

Commit 58f6e64

Browse files
authored
fix(vm): isolate constant-call config, self-dispatch vote-witness cost (#6857)
* fix(vm): self-dispatch vote-witness cost by proposal flag getVoteWitnessCost3 falls back to cost2 when Osaka is off, and cost2 to the legacy cost when energy adjustment is off. The energy then stays correct even if a stale cost function lingers in the shared jump table after a reorg, or is read by a constant call whose view has the proposal off. * fix(vm): isolate constant-call config view to prevent global pollution Constant calls bound to a lagging solidity/PBFT snapshot loaded their flags into the process-global VMConfig, racing block processing during a proposal's activation-to-solidification window and risking a fork. Route a constant call's config into a thread-local snapshot; the block/broadcast path keeps writing (and reading) the global.
1 parent 3a9ccfe commit 58f6e64

7 files changed

Lines changed: 339 additions & 135 deletions

File tree

actuator/src/main/java/org/tron/core/actuator/VMActuator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void validate(Object object) throws ContractValidateException {
120120
}
121121

122122
// Load Config
123-
ConfigLoader.load(context.getStoreFactory());
123+
ConfigLoader.load(context.getStoreFactory(), isConstantCall);
124124
// Warm up registry class
125125
OperationRegistry.init();
126126
trx = context.getTrxCap().getInstance();

actuator/src/main/java/org/tron/core/vm/EnergyCost.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ public static long getVoteWitnessCost(Program program) {
365365
}
366366

367367
public static long getVoteWitnessCost2(Program program) {
368+
if (!VMConfig.allowEnergyAdjustment()) {
369+
return getVoteWitnessCost(program);
370+
}
371+
368372
Stack stack = program.getStack();
369373
long oldMemSize = program.getMemSize();
370374
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
@@ -388,6 +392,10 @@ public static long getVoteWitnessCost2(Program program) {
388392
}
389393

390394
public static long getVoteWitnessCost3(Program program) {
395+
if (!VMConfig.allowTvmOsaka()) {
396+
return getVoteWitnessCost2(program);
397+
}
398+
391399
Stack stack = program.getStack();
392400
long oldMemSize = program.getMemSize();
393401
BigInteger amountArrayLength = stack.get(stack.size() - 1).value();

actuator/src/main/java/org/tron/core/vm/config/ConfigLoader.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,48 @@ public class ConfigLoader {
1313
//only for unit test
1414
public static boolean disable = false;
1515

16-
public static void load(StoreFactory storeFactory) {
16+
// isolate=true: a constant call bound to a non-HEAD (solidity/PBFT) snapshot installs its
17+
// snapshot into a thread-local view instead of the process-wide global, so it cannot pollute
18+
// the flags the block-processing path reads concurrently.
19+
public static void load(StoreFactory storeFactory, boolean isolate) {
1720
if (!disable) {
1821
DynamicPropertiesStore ds = storeFactory.getChainBaseManager().getDynamicPropertiesStore();
1922
VMConfig.setVmTrace(CommonParameter.getInstance().isVmTrace());
2023
if (ds != null) {
2124
VMConfig.initVmHardFork(checkForEnergyLimit(ds));
22-
VMConfig.initAllowMultiSign(ds.getAllowMultiSign());
23-
VMConfig.initAllowTvmTransferTrc10(ds.getAllowTvmTransferTrc10());
24-
VMConfig.initAllowTvmConstantinople(ds.getAllowTvmConstantinople());
25-
VMConfig.initAllowTvmSolidity059(ds.getAllowTvmSolidity059());
26-
VMConfig.initAllowShieldedTRC20Transaction(ds.getAllowShieldedTRC20Transaction());
27-
VMConfig.initAllowTvmIstanbul(ds.getAllowTvmIstanbul());
28-
VMConfig.initAllowTvmFreeze(ds.getAllowTvmFreeze());
29-
VMConfig.initAllowTvmVote(ds.getAllowTvmVote());
30-
VMConfig.initAllowTvmLondon(ds.getAllowTvmLondon());
31-
VMConfig.initAllowTvmCompatibleEvm(ds.getAllowTvmCompatibleEvm());
32-
VMConfig.initAllowHigherLimitForMaxCpuTimeOfOneTx(
33-
ds.getAllowHigherLimitForMaxCpuTimeOfOneTx());
34-
VMConfig.initAllowTvmFreezeV2(ds.supportUnfreezeDelay() ? 1 : 0);
35-
VMConfig.initAllowOptimizedReturnValueOfChainId(
36-
ds.getAllowOptimizedReturnValueOfChainId());
37-
VMConfig.initAllowDynamicEnergy(ds.getAllowDynamicEnergy());
38-
VMConfig.initDynamicEnergyThreshold(ds.getDynamicEnergyThreshold());
39-
VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor());
40-
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
41-
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
42-
VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment());
43-
VMConfig.initAllowStrictMath(ds.getAllowStrictMath());
44-
VMConfig.initAllowTvmCancun(ds.getAllowTvmCancun());
45-
VMConfig.initDisableJavaLangMath(ds.getConsensusLogicOptimization());
46-
VMConfig.initAllowTvmBlob(ds.getAllowTvmBlob());
47-
VMConfig.initAllowTvmSelfdestructRestriction(ds.getAllowTvmSelfdestructRestriction());
48-
VMConfig.initAllowTvmOsaka(ds.getAllowTvmOsaka());
49-
VMConfig.initAllowHardenResourceCalculation(ds.getAllowHardenResourceCalculation());
25+
VMConfig.Snapshot snapshot = new VMConfig.Snapshot();
26+
snapshot.allowMultiSign = ds.getAllowMultiSign() == 1;
27+
snapshot.allowTvmTransferTrc10 = ds.getAllowTvmTransferTrc10() == 1;
28+
snapshot.allowTvmConstantinople = ds.getAllowTvmConstantinople() == 1;
29+
snapshot.allowTvmSolidity059 = ds.getAllowTvmSolidity059() == 1;
30+
snapshot.allowShieldedTRC20Transaction = ds.getAllowShieldedTRC20Transaction() == 1;
31+
snapshot.allowTvmIstanbul = ds.getAllowTvmIstanbul() == 1;
32+
snapshot.allowTvmFreeze = ds.getAllowTvmFreeze() == 1;
33+
snapshot.allowTvmVote = ds.getAllowTvmVote() == 1;
34+
snapshot.allowTvmLondon = ds.getAllowTvmLondon() == 1;
35+
snapshot.allowTvmCompatibleEvm = ds.getAllowTvmCompatibleEvm() == 1;
36+
snapshot.allowHigherLimitForMaxCpuTimeOfOneTx =
37+
ds.getAllowHigherLimitForMaxCpuTimeOfOneTx() == 1;
38+
snapshot.allowTvmFreezeV2 = ds.supportUnfreezeDelay();
39+
snapshot.allowOptimizedReturnValueOfChainId = ds.getAllowOptimizedReturnValueOfChainId() == 1;
40+
snapshot.allowDynamicEnergy = ds.getAllowDynamicEnergy() == 1;
41+
snapshot.dynamicEnergyThreshold = ds.getDynamicEnergyThreshold();
42+
snapshot.dynamicEnergyIncreaseFactor = ds.getDynamicEnergyIncreaseFactor();
43+
snapshot.dynamicEnergyMaxFactor = ds.getDynamicEnergyMaxFactor();
44+
snapshot.allowTvmShanghai = ds.getAllowTvmShangHai() == 1;
45+
snapshot.allowEnergyAdjustment = ds.getAllowEnergyAdjustment() == 1;
46+
snapshot.allowStrictMath = ds.getAllowStrictMath() == 1;
47+
snapshot.allowTvmCancun = ds.getAllowTvmCancun() == 1;
48+
snapshot.disableJavaLangMath = ds.getConsensusLogicOptimization() == 1;
49+
snapshot.allowTvmBlob = ds.getAllowTvmBlob() == 1;
50+
snapshot.allowTvmSelfdestructRestriction = ds.getAllowTvmSelfdestructRestriction() == 1;
51+
snapshot.allowTvmOsaka = ds.getAllowTvmOsaka() == 1;
52+
snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1;
53+
if (isolate) {
54+
VMConfig.setLocalSnapshot(snapshot);
55+
} else {
56+
VMConfig.setGlobalSnapshot(snapshot);
57+
}
5058
}
5159
}
5260
}

0 commit comments

Comments
 (0)