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

func(vm): support shanghai upgrade #5175

Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -681,6 +681,17 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_TVM_SHANGHAI: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_2)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_TVM_SHANGHAI]");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_TVM_SHANGHAI] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -753,7 +764,8 @@ public enum ProposalType { // current value, value range
ALLOW_DYNAMIC_ENERGY(72), // 0, 1
DYNAMIC_ENERGY_THRESHOLD(73), // 0, [0, LONG]
DYNAMIC_ENERGY_INCREASE_FACTOR(74), // 0, [0, 10_000]
DYNAMIC_ENERGY_MAX_FACTOR(75); // 0, [0, 100_000]
DYNAMIC_ENERGY_MAX_FACTOR(75), // 0, [0, 100_000]
ALLOW_TVM_SHANGHAI(76); // 0, 1

private long code;

Expand Down
1 change: 1 addition & 0 deletions actuator/src/main/java/org/tron/core/vm/Op.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public class Op {

/* Push Operations */
// Place item on stack
public static final int PUSH0 = 0x5f;
public static final int PUSH1 = 0x60;
public static final int PUSH2 = 0x61;
public static final int PUSH3 = 0x62;
Expand Down
5 changes: 5 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@ public static void jumpDestAction(Program program) {
program.step();
}

public static void push0Action(Program program) {
program.stackPush(DataWord.ZERO());
program.step();
}

public static void pushAction(Program program) {
int n = program.getCurrentOpIntValue() - Op.PUSH1 + 1;
program.step();
Expand Down
20 changes: 19 additions & 1 deletion actuator/src/main/java/org/tron/core/vm/OperationRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum Version {
TRON_V1_0,
TRON_V1_1,
TRON_V1_2,
TRON_V1_3,
// add more
// TRON_V2,
// ETH
Expand All @@ -22,6 +23,7 @@ public enum Version {
tableMap.put(Version.TRON_V1_0, newTronV10OperationSet());
tableMap.put(Version.TRON_V1_1, newTronV11OperationSet());
tableMap.put(Version.TRON_V1_2, newTronV12OperationSet());
tableMap.put(Version.TRON_V1_3, newTronV13OperationSet());
}

public static JumpTable newTronV10OperationSet() {
Expand All @@ -47,12 +49,18 @@ public static JumpTable newTronV12OperationSet() {
return table;
}

public static JumpTable newTronV13OperationSet() {
JumpTable table = newTronV12OperationSet();
appendShangHaiOperations(table);
return table;
}

// Just for warming up class to avoid out_of_time
public static void init() {}

public static JumpTable getTable() {
// always get the table which has the newest version
JumpTable table = tableMap.get(Version.TRON_V1_2);
JumpTable table = tableMap.get(Version.TRON_V1_3);

// next make the corresponding changes, exclude activating opcode
if (VMConfig.allowHigherLimitForMaxCpuTimeOfOneTx()) {
Expand Down Expand Up @@ -617,4 +625,14 @@ public static void appendDelegateOperations(JumpTable table) {
OperationActions::unDelegateResourceAction,
proposal));
}

public static void appendShangHaiOperations(JumpTable table) {
BooleanSupplier proposal = VMConfig::allowTvmShanghai;

table.set(new Operation(
Op.PUSH0, 0, 1,
EnergyCost::getBaseTierCost,
OperationActions::push0Action,
proposal));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void load(StoreFactory storeFactory) {
VMConfig.initDynamicEnergyThreshold(ds.getDynamicEnergyThreshold());
VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor());
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
}
}
}
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 @@ -47,6 +47,8 @@ public class VMConfig {

private static long DYNAMIC_ENERGY_MAX_FACTOR = 0L;

private static boolean ALLOW_TVM_SHANGHAI = false;

private VMConfig() {
}

Expand Down Expand Up @@ -130,6 +132,10 @@ public static void initDynamicEnergyMaxFactor(long maxFactor) {
DYNAMIC_ENERGY_MAX_FACTOR = maxFactor;
}

public static void initAllowTvmShangHai(long allow) {
ALLOW_TVM_SHANGHAI = allow == 1;
}

public static boolean getEnergyLimitHardFork() {
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
}
Expand Down Expand Up @@ -201,4 +207,8 @@ public static long getDynamicEnergyIncreaseFactor() {
public static long getDynamicEnergyMaxFactor() {
return DYNAMIC_ENERGY_MAX_FACTOR;
}

public static boolean allowTvmShanghai() {
return ALLOW_TVM_SHANGHAI;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
private static final byte[] ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID =
"ALLOW_OPTIMIZED_RETURN_VALUE_OF_CHAIN_ID".getBytes();

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

@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
super(dbName);
Expand Down Expand Up @@ -2755,6 +2757,18 @@ public long getAllowOptimizedReturnValueOfChainId() {
() -> new IllegalArgumentException(msg));
}

public void saveAllowTvmShangHai(long allowTvmShangHai) {
this.put(DynamicPropertiesStore.ALLOW_TVM_SHANGHAI,
new BytesCapsule(ByteArray.fromLong(allowTvmShangHai)));
}

public long getAllowTvmShangHai() {
return Optional.ofNullable(getUnchecked(ALLOW_TVM_SHANGHAI))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElse(CommonParameter.getInstance().getAllowTvmShangHai());
}

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 @@ -6,7 +6,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import lombok.Getter;
import lombok.Setter;
import org.quartz.CronExpression;
Expand Down Expand Up @@ -642,6 +641,10 @@ public class CommonParameter {
@Setter
public long dynamicConfigCheckInterval;

@Getter
@Setter
public long allowTvmShangHai;

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 @@ -369,4 +369,6 @@ public class Constant {

public static final String DYNAMIC_CONFIG_ENABLE = "node.dynamicConfig.enable";
public static final String DYNAMIC_CONFIG_CHECK_INTERVAL = "node.dynamicConfig.checkInterval";

public static final String COMMITTEE_ALLOW_TVM_SHANGHAI = "committee.allowTvmShangHai";
}
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 @@ -21,7 +21,8 @@ public enum ForkBlockVersionEnum {
VERSION_4_5(24, 1596780000000L, 80),
VERSION_4_6(25, 1596780000000L, 80),
VERSION_4_7(26, 1596780000000L, 80),
VERSION_4_7_1(27, 1596780000000L, 80);
VERSION_4_7_1(27, 1596780000000L, 80),
VERSION_4_7_2(28, 1596780000000L, 80);
// if add a version, modify BLOCK_VERSION simultaneously

@Getter
Expand Down Expand Up @@ -70,7 +71,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 = 27;
public static final int BLOCK_VERSION = 28;
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 @@ -1316,6 +1316,11 @@ public Protocol.ChainParameters getChainParameters() {
.setValue(dbManager.getDynamicPropertiesStore().getDynamicEnergyMaxFactor())
.build());

builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
.setKey("getAllowTvmShangHai")
.setValue(dbManager.getDynamicPropertiesStore().getAllowTvmShangHai())
.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 @@ -231,6 +231,7 @@ public static void clearParam() {
PARAMETER.p2pDisable = false;
PARAMETER.dynamicConfigEnable = false;
PARAMETER.dynamicConfigCheckInterval = 600;
PARAMETER.allowTvmShangHai = 0;
}

/**
Expand Down Expand Up @@ -1166,6 +1167,10 @@ public static void setParam(final String[] args, final String confFileName) {
PARAMETER.dynamicConfigCheckInterval = 600;
}

PARAMETER.allowTvmShangHai =
config.hasPath(Constant.COMMITTEE_ALLOW_TVM_SHANGHAI) ? config
.getInt(Constant.COMMITTEE_ALLOW_TVM_SHANGHAI) : 0;

logConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule)
manager.getDynamicPropertiesStore().saveDynamicEnergyMaxFactor(entry.getValue());
break;
}
case ALLOW_TVM_SHANGHAI: {
manager.getDynamicPropertiesStore().saveAllowTvmShangHai(entry.getValue());
break;
}
default:
find = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.tron.core.config.args.Args;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.store.StoreFactory;
import org.tron.core.vm.EnergyCost;
import org.tron.core.vm.JumpTable;
import org.tron.core.vm.Op;
import org.tron.core.vm.Operation;
Expand All @@ -37,7 +38,7 @@ public class OperationsTest extends BaseTest {

private ProgramInvokeMockImpl invoke;
private Program program;
private final JumpTable jumpTable = OperationRegistry.newTronV10OperationSet();
private final JumpTable jumpTable = OperationRegistry.getTable();

@BeforeClass
public static void init() {
Expand Down Expand Up @@ -848,6 +849,25 @@ public void testComplexOperations() throws ContractValidateException {

}

@Test
public void testPush0() throws ContractValidateException {
VMConfig.initAllowTvmShangHai(1);

invoke = new ProgramInvokeMockImpl();
Protocol.Transaction trx = Protocol.Transaction.getDefaultInstance();
InternalTransaction interTrx =
new InternalTransaction(trx, InternalTransaction.TrxType.TRX_UNKNOWN_TYPE);

byte[] op = new byte[1];
op[0] = Op.PUSH0;
program = new Program(op, op, invoke, interTrx);
testOperations(program);
Assert.assertEquals(EnergyCost.getBaseTierCost(null), program.getResult().getEnergyUsed());
Assert.assertEquals(DataWord.ZERO(), program.getStack().pop());

VMConfig.initAllowTvmShangHai(0);
}

private void testOperations(Program program) {
try {
while (!program.isStopped()) {
Expand Down