Skip to content

Commit 382a405

Browse files
authored
Merge pull request #5175 from yanghang8612/feature/support_shanghai
func(vm): support shanghai upgrade
2 parents 619a20f + 5758cdd commit 382a405

File tree

14 files changed

+107
-6
lines changed

14 files changed

+107
-6
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,17 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
681681
}
682682
break;
683683
}
684+
case ALLOW_TVM_SHANGHAI: {
685+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_2)) {
686+
throw new ContractValidateException(
687+
"Bad chain parameter id [ALLOW_TVM_SHANGHAI]");
688+
}
689+
if (value != 1) {
690+
throw new ContractValidateException(
691+
"This value[ALLOW_TVM_SHANGHAI] is only allowed to be 1");
692+
}
693+
break;
694+
}
684695
default:
685696
break;
686697
}
@@ -753,7 +764,8 @@ public enum ProposalType { // current value, value range
753764
ALLOW_DYNAMIC_ENERGY(72), // 0, 1
754765
DYNAMIC_ENERGY_THRESHOLD(73), // 0, [0, LONG]
755766
DYNAMIC_ENERGY_INCREASE_FACTOR(74), // 0, [0, 10_000]
756-
DYNAMIC_ENERGY_MAX_FACTOR(75); // 0, [0, 100_000]
767+
DYNAMIC_ENERGY_MAX_FACTOR(75), // 0, [0, 100_000]
768+
ALLOW_TVM_SHANGHAI(76); // 0, 1
757769

758770
private long code;
759771

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public class Op {
148148

149149
/* Push Operations */
150150
// Place item on stack
151+
public static final int PUSH0 = 0x5f;
151152
public static final int PUSH1 = 0x60;
152153
public static final int PUSH2 = 0x61;
153154
public static final int PUSH3 = 0x62;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,11 @@ public static void jumpDestAction(Program program) {
643643
program.step();
644644
}
645645

646+
public static void push0Action(Program program) {
647+
program.stackPush(DataWord.ZERO());
648+
program.step();
649+
}
650+
646651
public static void pushAction(Program program) {
647652
int n = program.getCurrentOpIntValue() - Op.PUSH1 + 1;
648653
program.step();

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public enum Version {
1111
TRON_V1_0,
1212
TRON_V1_1,
1313
TRON_V1_2,
14+
TRON_V1_3,
1415
// add more
1516
// TRON_V2,
1617
// ETH
@@ -22,6 +23,7 @@ public enum Version {
2223
tableMap.put(Version.TRON_V1_0, newTronV10OperationSet());
2324
tableMap.put(Version.TRON_V1_1, newTronV11OperationSet());
2425
tableMap.put(Version.TRON_V1_2, newTronV12OperationSet());
26+
tableMap.put(Version.TRON_V1_3, newTronV13OperationSet());
2527
}
2628

2729
public static JumpTable newTronV10OperationSet() {
@@ -47,12 +49,18 @@ public static JumpTable newTronV12OperationSet() {
4749
return table;
4850
}
4951

52+
public static JumpTable newTronV13OperationSet() {
53+
JumpTable table = newTronV12OperationSet();
54+
appendShangHaiOperations(table);
55+
return table;
56+
}
57+
5058
// Just for warming up class to avoid out_of_time
5159
public static void init() {}
5260

5361
public static JumpTable getTable() {
5462
// always get the table which has the newest version
55-
JumpTable table = tableMap.get(Version.TRON_V1_2);
63+
JumpTable table = tableMap.get(Version.TRON_V1_3);
5664

5765
// next make the corresponding changes, exclude activating opcode
5866
if (VMConfig.allowHigherLimitForMaxCpuTimeOfOneTx()) {
@@ -617,4 +625,14 @@ public static void appendDelegateOperations(JumpTable table) {
617625
OperationActions::unDelegateResourceAction,
618626
proposal));
619627
}
628+
629+
public static void appendShangHaiOperations(JumpTable table) {
630+
BooleanSupplier proposal = VMConfig::allowTvmShanghai;
631+
632+
table.set(new Operation(
633+
Op.PUSH0, 0, 1,
634+
EnergyCost::getBaseTierCost,
635+
OperationActions::push0Action,
636+
proposal));
637+
}
620638
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static void load(StoreFactory storeFactory) {
3838
VMConfig.initDynamicEnergyThreshold(ds.getDynamicEnergyThreshold());
3939
VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor());
4040
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
41+
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
4142
}
4243
}
4344
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class VMConfig {
4747

4848
private static long DYNAMIC_ENERGY_MAX_FACTOR = 0L;
4949

50+
private static boolean ALLOW_TVM_SHANGHAI = false;
51+
5052
private VMConfig() {
5153
}
5254

@@ -130,6 +132,10 @@ public static void initDynamicEnergyMaxFactor(long maxFactor) {
130132
DYNAMIC_ENERGY_MAX_FACTOR = maxFactor;
131133
}
132134

135+
public static void initAllowTvmShangHai(long allow) {
136+
ALLOW_TVM_SHANGHAI = allow == 1;
137+
}
138+
133139
public static boolean getEnergyLimitHardFork() {
134140
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
135141
}
@@ -201,4 +207,8 @@ public static long getDynamicEnergyIncreaseFactor() {
201207
public static long getDynamicEnergyMaxFactor() {
202208
return DYNAMIC_ENERGY_MAX_FACTOR;
203209
}
210+
211+
public static boolean allowTvmShanghai() {
212+
return ALLOW_TVM_SHANGHAI;
213+
}
204214
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
206206
private static final byte[] ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID =
207207
"ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID".getBytes();
208208

209+
private static final byte[] ALLOW_TVM_SHANGHAI = "ALLOW_TVM_SHANGHAI".getBytes();
210+
209211
@Autowired
210212
private DynamicPropertiesStore(@Value("properties") String dbName) {
211213
super(dbName);
@@ -2755,6 +2757,18 @@ public long getAllowOptimizedReturnValueOfChainId() {
27552757
() -> new IllegalArgumentException(msg));
27562758
}
27572759

2760+
public void saveAllowTvmShangHai(long allowTvmShangHai) {
2761+
this.put(DynamicPropertiesStore.ALLOW_TVM_SHANGHAI,
2762+
new BytesCapsule(ByteArray.fromLong(allowTvmShangHai)));
2763+
}
2764+
2765+
public long getAllowTvmShangHai() {
2766+
return Optional.ofNullable(getUnchecked(ALLOW_TVM_SHANGHAI))
2767+
.map(BytesCapsule::getData)
2768+
.map(ByteArray::toLong)
2769+
.orElse(CommonParameter.getInstance().getAllowTvmShangHai());
2770+
}
2771+
27582772
private static class DynamicResourceProperties {
27592773

27602774
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.ArrayList;
77
import java.util.List;
88
import java.util.Set;
9-
109
import lombok.Getter;
1110
import lombok.Setter;
1211
import org.quartz.CronExpression;
@@ -648,6 +647,10 @@ public class CommonParameter {
648647
@Setter
649648
public long dynamicConfigCheckInterval;
650649

650+
@Getter
651+
@Setter
652+
public long allowTvmShangHai;
653+
651654
private static double calcMaxTimeRatio() {
652655
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
653656
return 5.0;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,6 @@ public class Constant {
371371

372372
public static final String DYNAMIC_CONFIG_ENABLE = "node.dynamicConfig.enable";
373373
public static final String DYNAMIC_CONFIG_CHECK_INTERVAL = "node.dynamicConfig.checkInterval";
374+
375+
public static final String COMMITTEE_ALLOW_TVM_SHANGHAI = "committee.allowTvmShangHai";
374376
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public enum ForkBlockVersionEnum {
2121
VERSION_4_5(24, 1596780000000L, 80),
2222
VERSION_4_6(25, 1596780000000L, 80),
2323
VERSION_4_7(26, 1596780000000L, 80),
24-
VERSION_4_7_1(27, 1596780000000L, 80);
24+
VERSION_4_7_1(27, 1596780000000L, 80),
25+
VERSION_4_7_2(28, 1596780000000L, 80);
2526
// if add a version, modify BLOCK_VERSION simultaneously
2627

2728
@Getter
@@ -70,7 +71,7 @@ public class ChainConstant {
7071
public static final int SINGLE_REPEAT = 1;
7172
public static final int BLOCK_FILLED_SLOTS_NUMBER = 128;
7273
public static final int MAX_FROZEN_NUMBER = 1;
73-
public static final int BLOCK_VERSION = 27;
74+
public static final int BLOCK_VERSION = 28;
7475
public static final long FROZEN_PERIOD = 86_400_000L;
7576
public static final long DELEGATE_PERIOD = 3 * 86_400_000L;
7677
public static final long TRX_PRECISION = 1000_000L;

0 commit comments

Comments
 (0)