Skip to content

Commit 83a2950

Browse files
authored
Merge pull request tronprotocol#5837 from yanghang8612/feature/implement-TIP653
feat(proposal,tvm): implement TIP-653
2 parents b089ca0 + 4412177 commit 83a2950

File tree

16 files changed

+273
-3
lines changed

16 files changed

+273
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.tron.core.utils.TransactionUtil;
3939
import org.tron.core.vm.EnergyCost;
4040
import org.tron.core.vm.LogInfoTriggerParser;
41+
import org.tron.core.vm.Op;
4142
import org.tron.core.vm.OperationRegistry;
4243
import org.tron.core.vm.VM;
4344
import org.tron.core.vm.VMConstant;
@@ -189,6 +190,13 @@ public void execute(Object object) throws ContractExeException {
189190
VM.play(program, OperationRegistry.getTable());
190191
result = program.getResult();
191192

193+
if (VMConfig.allowEnergyAdjustment()) {
194+
// If the last op consumed too much execution time, the CPU time limit for the whole tx can be exceeded.
195+
// This is not fair for other txs in the same block.
196+
// So when allowFairEnergyAdjustment is on, the CPU time limit will be checked at the end of tx execution.
197+
program.checkCPUTimeLimit(Op.getNameOf(program.getLastOp()) + "(TX_LAST_OP)");
198+
}
199+
192200
if (TrxType.TRX_CONTRACT_CREATION_TYPE == trxType && !result.isRevert()) {
193201
byte[] code = program.getResult().getHReturn();
194202
if (code.length != 0 && VMConfig.allowTvmLondon() && code[0] == (byte) 0xEF) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
750750
}
751751
break;
752752
}
753+
case ALLOW_ENERGY_ADJUSTMENT: {
754+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_5)) {
755+
throw new ContractValidateException(
756+
"Bad chain parameter id [ALLOW_ENERGY_ADJUSTMENT]");
757+
}
758+
if (dynamicPropertiesStore.getAllowEnergyAdjustment() == 1) {
759+
throw new ContractValidateException(
760+
"[ALLOW_ENERGY_ADJUSTMENT] has been valid, no need to propose again");
761+
}
762+
if (value != 1) {
763+
throw new ContractValidateException(
764+
"This value[ALLOW_ENERGY_ADJUSTMENT] is only allowed to be 1");
765+
}
766+
break;
767+
}
753768
case MAX_CREATE_ACCOUNT_TX_SIZE: {
754769
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_7_5)) {
755770
throw new ContractValidateException(
@@ -841,6 +856,7 @@ public enum ProposalType { // current value, value range
841856
ALLOW_CANCEL_ALL_UNFREEZE_V2(77), // 0, 1
842857
MAX_DELEGATE_LOCK_PERIOD(78), // (86400, 10512000]
843858
ALLOW_OLD_REWARD_OPT(79), // 0, 1
859+
ALLOW_ENERGY_ADJUSTMENT(81), // 0, 1
844860
MAX_CREATE_ACCOUNT_TX_SIZE(82); // [500, 10000]
845861

846862
private long code;

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,19 @@ public static long getSuicideCost(Program ignored) {
259259
return SUICIDE;
260260
}
261261

262+
public static long getSuicideCost2(Program program) {
263+
DataWord inheritorAddress = program.getStack().peek();
264+
if (isDeadAccount(program, inheritorAddress)) {
265+
return getSuicideCost(program) + NEW_ACCT_CALL;
266+
}
267+
return getSuicideCost(program);
268+
}
269+
262270
public static long getBalanceCost(Program ignored) {
263271
return BALANCE;
264272
}
265273

266274
public static long getFreezeCost(Program program) {
267-
268275
Stack stack = program.getStack();
269276
DataWord receiverAddressWord = stack.get(stack.size() - 3);
270277
if (isDeadAccount(program, receiverAddressWord)) {
@@ -306,7 +313,27 @@ public static long getUnDelegateResourceCost(Program ignored) {
306313
}
307314

308315
public static long getVoteWitnessCost(Program program) {
316+
Stack stack = program.getStack();
317+
long oldMemSize = program.getMemSize();
318+
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
319+
DataWord amountArrayOffset = stack.get(stack.size() - 2);
320+
DataWord witnessArrayLength = stack.get(stack.size() - 3).clone();
321+
DataWord witnessArrayOffset = stack.get(stack.size() - 4);
322+
323+
DataWord wordSize = new DataWord(DataWord.WORD_SIZE);
324+
325+
amountArrayLength.mul(wordSize);
326+
BigInteger amountArrayMemoryNeeded = memNeeded(amountArrayOffset, amountArrayLength);
327+
328+
witnessArrayLength.mul(wordSize);
329+
BigInteger witnessArrayMemoryNeeded = memNeeded(witnessArrayOffset, witnessArrayLength);
330+
331+
return VOTE_WITNESS + calcMemEnergy(oldMemSize,
332+
(amountArrayMemoryNeeded.compareTo(witnessArrayMemoryNeeded) > 0
333+
? amountArrayMemoryNeeded : witnessArrayMemoryNeeded), 0, Op.VOTEWITNESS);
334+
}
309335

336+
public static long getVoteWitnessCost2(Program program) {
310337
Stack stack = program.getStack();
311338
long oldMemSize = program.getMemSize();
312339
DataWord amountArrayLength = stack.get(stack.size() - 1).clone();
@@ -317,9 +344,11 @@ public static long getVoteWitnessCost(Program program) {
317344
DataWord wordSize = new DataWord(DataWord.WORD_SIZE);
318345

319346
amountArrayLength.mul(wordSize);
347+
amountArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
320348
BigInteger amountArrayMemoryNeeded = memNeeded(amountArrayOffset, amountArrayLength);
321349

322350
witnessArrayLength.mul(wordSize);
351+
witnessArrayLength.add(wordSize); // dynamic array length is at least 32 bytes
323352
BigInteger witnessArrayMemoryNeeded = memNeeded(witnessArrayOffset, witnessArrayLength);
324353

325354
return VOTE_WITNESS + calcMemEnergy(oldMemSize,

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public static JumpTable getTable() {
6767
adjustMemOperations(table);
6868
}
6969

70+
if (VMConfig.allowEnergyAdjustment()) {
71+
adjustForFairEnergy(table);
72+
}
73+
7074
return table;
7175
}
7276

@@ -635,4 +639,17 @@ public static void appendShangHaiOperations(JumpTable table) {
635639
OperationActions::push0Action,
636640
proposal));
637641
}
642+
643+
public static void adjustForFairEnergy(JumpTable table) {
644+
table.set(new Operation(
645+
Op.VOTEWITNESS, 4, 1,
646+
EnergyCost::getVoteWitnessCost2,
647+
OperationActions::voteWitnessAction,
648+
VMConfig::allowTvmVote));
649+
650+
table.set(new Operation(
651+
Op.SUICIDE, 1, 0,
652+
EnergyCost::getSuicideCost2,
653+
OperationActions::suicideAction));
654+
}
638655
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import java.io.InputStream;
1313
import java.io.OutputStream;
1414
import java.util.Arrays;
15-
import java.util.Map;
1615
import java.util.zip.Deflater;
1716
import java.util.zip.DeflaterOutputStream;
1817
import lombok.extern.slf4j.Slf4j;
1918
import org.tron.common.utils.ByteArray;
2019
import org.tron.common.utils.ByteUtil;
2120
import org.tron.common.utils.Commons;
2221
import org.tron.common.utils.DecodeUtil;
22+
import org.tron.core.Constant;
2323
import org.tron.core.capsule.AccountCapsule;
2424
import org.tron.core.exception.ContractValidateException;
2525
import org.tron.core.vm.config.VMConfig;
@@ -33,6 +33,11 @@ public final class VMUtils {
3333
private VMUtils() {
3434
}
3535

36+
public static int getAddressSize() {
37+
return VMConfig.allowEnergyAdjustment() ?
38+
Constant.TRON_ADDRESS_SIZE : Constant.STANDARD_ADDRESS_SIZE;
39+
}
40+
3641
public static void closeQuietly(Closeable closeable) {
3742
try {
3843
if (closeable != null) {

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
@@ -39,6 +39,7 @@ public static void load(StoreFactory storeFactory) {
3939
VMConfig.initDynamicEnergyIncreaseFactor(ds.getDynamicEnergyIncreaseFactor());
4040
VMConfig.initDynamicEnergyMaxFactor(ds.getDynamicEnergyMaxFactor());
4141
VMConfig.initAllowTvmShangHai(ds.getAllowTvmShangHai());
42+
VMConfig.initAllowEnergyAdjustment(ds.getAllowEnergyAdjustment());
4243
}
4344
}
4445
}

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
@@ -49,6 +49,8 @@ public class VMConfig {
4949

5050
private static boolean ALLOW_TVM_SHANGHAI = false;
5151

52+
private static boolean ALLOW_ENERGY_ADJUSTMENT = false;
53+
5254
private VMConfig() {
5355
}
5456

@@ -136,6 +138,10 @@ public static void initAllowTvmShangHai(long allow) {
136138
ALLOW_TVM_SHANGHAI = allow == 1;
137139
}
138140

141+
public static void initAllowEnergyAdjustment(long allow) {
142+
ALLOW_ENERGY_ADJUSTMENT = allow == 1;
143+
}
144+
139145
public static boolean getEnergyLimitHardFork() {
140146
return CommonParameter.ENERGY_LIMIT_HARD_FORK;
141147
}
@@ -211,4 +217,8 @@ public static long getDynamicEnergyMaxFactor() {
211217
public static boolean allowTvmShanghai() {
212218
return ALLOW_TVM_SHANGHAI;
213219
}
220+
221+
public static boolean allowEnergyAdjustment() {
222+
return ALLOW_ENERGY_ADJUSTMENT;
223+
}
214224
}

actuator/src/main/java/org/tron/core/vm/program/Program.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ public void setLastOp(byte op) {
275275
this.lastOp = op;
276276
}
277277

278+
public byte getLastOp() {
279+
return this.lastOp;
280+
}
281+
278282
/**
279283
* Returns the last fully executed OP.
280284
*/
@@ -457,7 +461,8 @@ public void suicide(DataWord obtainerAddress) {
457461
InternalTransaction internalTx = addInternalTx(null, owner, obtainer, balance, null,
458462
"suicide", nonce, getContractState().getAccount(owner).getAssetMapV2());
459463

460-
if (FastByteComparisons.compareTo(owner, 0, 20, obtainer, 0, 20) == 0) {
464+
int ADDRESS_SIZE = VMUtils.getAddressSize();
465+
if (FastByteComparisons.compareTo(owner, 0, ADDRESS_SIZE, obtainer, 0, ADDRESS_SIZE) == 0) {
461466
// if owner == obtainer just zeroing account according to Yellow Paper
462467
getContractState().addBalance(owner, -balance);
463468
byte[] blackHoleAddress = getContractState().getBlackHoleAddress();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
219219

220220
private static final byte[] ALLOW_OLD_REWARD_OPT = "ALLOW_OLD_REWARD_OPT".getBytes();
221221

222+
private static final byte[] ALLOW_ENERGY_ADJUSTMENT = "ALLOW_ENERGY_ADJUSTMENT".getBytes();
223+
222224
private static final byte[] MAX_CREATE_ACCOUNT_TX_SIZE = "MAX_CREATE_ACCOUNT_TX_SIZE".getBytes();
223225

224226
@Autowired
@@ -2852,6 +2854,17 @@ public long getAllowOldRewardOpt() {
28522854
.orElse(CommonParameter.getInstance().getAllowOldRewardOpt());
28532855
}
28542856

2857+
public void saveAllowEnergyAdjustment(long allowEnergyAdjustment) {
2858+
this.put(ALLOW_ENERGY_ADJUSTMENT, new BytesCapsule(ByteArray.fromLong(allowEnergyAdjustment)));
2859+
}
2860+
2861+
public long getAllowEnergyAdjustment() {
2862+
return Optional.ofNullable(getUnchecked(ALLOW_ENERGY_ADJUSTMENT))
2863+
.map(BytesCapsule::getData)
2864+
.map(ByteArray::toLong)
2865+
.orElse(CommonParameter.getInstance().getAllowEnergyAdjustment());
2866+
}
2867+
28552868
public void saveMaxCreateAccountTxSize(long maxCreateAccountTxSize) {
28562869
this.put(MAX_CREATE_ACCOUNT_TX_SIZE,
28572870
new BytesCapsule(ByteArray.fromLong(maxCreateAccountTxSize)));

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ public class CommonParameter {
666666
@Setter
667667
public long allowOldRewardOpt;
668668

669+
@Getter
670+
@Setter
671+
public long allowEnergyAdjustment;
672+
669673
@Getter
670674
@Setter
671675
public long maxCreateAccountTxSize = 1000L;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Constant {
1818
public static final String ADD_PRE_FIX_STRING_MAINNET = "41";
1919
public static final byte ADD_PRE_FIX_BYTE_TESTNET = (byte) 0xa0; //a0 + address
2020
public static final String ADD_PRE_FIX_STRING_TESTNET = "a0";
21+
public static final int STANDARD_ADDRESS_SIZE = 20;
22+
public static final int TRON_ADDRESS_SIZE = 21;
2123

2224
public static final int NODE_TYPE_FULL_NODE = 0;
2325
public static final int NODE_TYPE_LIGHT_NODE = 1;
@@ -379,4 +381,6 @@ public class Constant {
379381

380382
public static final String MAX_UNSOLIDIFIED_BLOCKS = "node.maxUnsolidifiedBlocks";
381383
public static final String COMMITTEE_ALLOW_OLD_REWARD_OPT = "committee.allowOldRewardOpt";
384+
385+
public static final String COMMITTEE_ALLOW_ENERGY_ADJUSTMENT = "committee.allowEnergyAdjustment";
382386
}

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,11 @@ public Protocol.ChainParameters getChainParameters() {
13341334
.setValue(dbManager.getDynamicPropertiesStore().getAllowOldRewardOpt())
13351335
.build());
13361336

1337+
builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
1338+
.setKey("getAllowEnergyAdjustment")
1339+
.setValue(dbManager.getDynamicPropertiesStore().getAllowEnergyAdjustment())
1340+
.build());
1341+
13371342
builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
13381343
.setKey("getMaxCreateAccountTxSize")
13391344
.setValue(dbManager.getDynamicPropertiesStore().getMaxCreateAccountTxSize())

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public static void clearParam() {
232232
PARAMETER.unsolidifiedBlockCheck = false;
233233
PARAMETER.maxUnsolidifiedBlocks = 54;
234234
PARAMETER.allowOldRewardOpt = 0;
235+
PARAMETER.allowEnergyAdjustment = 0;
235236
}
236237

237238
/**
@@ -1205,6 +1206,10 @@ public static void setParam(final String[] args, final String confFileName) {
12051206
}
12061207
PARAMETER.allowOldRewardOpt = allowOldRewardOpt;
12071208

1209+
PARAMETER.allowEnergyAdjustment =
1210+
config.hasPath(Constant.COMMITTEE_ALLOW_ENERGY_ADJUSTMENT) ? config
1211+
.getInt(Constant.COMMITTEE_ALLOW_ENERGY_ADJUSTMENT) : 0;
1212+
12081213
logConfig();
12091214
}
12101215

framework/src/main/java/org/tron/core/consensus/ProposalService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule)
359359
manager.getDynamicPropertiesStore().saveAllowOldRewardOpt(entry.getValue());
360360
break;
361361
}
362+
case ALLOW_ENERGY_ADJUSTMENT: {
363+
manager.getDynamicPropertiesStore().saveAllowEnergyAdjustment(entry.getValue());
364+
break;
365+
}
362366
case MAX_CREATE_ACCOUNT_TX_SIZE: {
363367
manager.getDynamicPropertiesStore().saveMaxCreateAccountTxSize(entry.getValue());
364368
break;

0 commit comments

Comments
 (0)