Skip to content

Commit 9da13ac

Browse files
committed
func(test): add unit test for energy adjustment proposal
1 parent 7491af6 commit 9da13ac

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
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;
@@ -193,7 +194,7 @@ public void execute(Object object) throws ContractExeException {
193194
// If the last op consumed too much execution time, the CPU time limit for the whole tx can be exceeded.
194195
// This is not fair for other txs in the same block.
195196
// So when allowFairEnergyAdjustment is on, the CPU time limit will be checked at the end of tx execution.
196-
program.checkCPUTimeLimit("TX_LAST_OP");
197+
program.checkCPUTimeLimit(Op.getNameOf(program.getLastOp()) + "(TX_LAST_OP)");
197198
}
198199

199200
if (TrxType.TRX_CONTRACT_CREATION_TYPE == trxType && !result.isRevert()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static JumpTable getTable() {
6868
}
6969

7070
if (VMConfig.allowEnergyAdjustment()) {
71-
ajustForFairEnergy(table);
71+
adjustForFairEnergy(table);
7272
}
7373

7474
return table;
@@ -640,7 +640,7 @@ public static void appendShangHaiOperations(JumpTable table) {
640640
proposal));
641641
}
642642

643-
public static void ajustForFairEnergy(JumpTable table) {
643+
public static void adjustForFairEnergy(JumpTable table) {
644644
table.set(new Operation(
645645
Op.VOTEWITNESS, 4, 1,
646646
EnergyCost::getVoteWitnessCost2,

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

Lines changed: 4 additions & 0 deletions
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
*/

framework/src/test/java/org/tron/common/runtime/vm/OperationsTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.junit.Assert.assertEquals;
44

55
import java.util.List;
6+
import java.util.Random;
7+
68
import lombok.SneakyThrows;
79
import lombok.extern.slf4j.Slf4j;
810
import org.bouncycastle.util.encoders.Hex;
@@ -15,6 +17,7 @@
1517
import org.tron.common.BaseTest;
1618
import org.tron.common.parameter.CommonParameter;
1719
import org.tron.common.runtime.InternalTransaction;
20+
import org.tron.common.utils.DecodeUtil;
1821
import org.tron.core.Constant;
1922
import org.tron.core.config.args.Args;
2023
import org.tron.core.exception.ContractValidateException;
@@ -864,6 +867,50 @@ public void testPush0() throws ContractValidateException {
864867
VMConfig.initAllowTvmShangHai(0);
865868
}
866869

870+
@Test
871+
public void testSuicideCost() throws ContractValidateException {
872+
invoke = new ProgramInvokeMockImpl(StoreFactory.getInstance(), new byte[0], new byte[21]);
873+
program = new Program(null, null, invoke,
874+
new InternalTransaction(
875+
Protocol.Transaction.getDefaultInstance(),
876+
InternalTransaction.TrxType.TRX_UNKNOWN_TYPE));
877+
878+
byte[] receiver1 = generateRandomAddress();
879+
program.stackPush(new DataWord(receiver1));
880+
Assert.assertEquals(0, EnergyCost.getSuicideCost(program));
881+
invoke.getDeposit().createAccount(receiver1, Protocol.AccountType.Normal);
882+
Assert.assertEquals(0, EnergyCost.getSuicideCost(program));
883+
884+
byte[] receiver2 = generateRandomAddress();
885+
program.stackPush(new DataWord(receiver2));
886+
Assert.assertEquals(25000, EnergyCost.getSuicideCost2(program));
887+
invoke.getDeposit().createAccount(receiver2, Protocol.AccountType.Normal);
888+
Assert.assertEquals(0, EnergyCost.getSuicideCost2(program));
889+
}
890+
891+
@Test
892+
public void testVoteWitnessCost() throws ContractValidateException {
893+
// Build stack environment, the stack from top to bottom is 0x00, 0x80, 0x00, 0x80
894+
program = new Program(null, null, new ProgramInvokeMockImpl(),
895+
new InternalTransaction(
896+
Protocol.Transaction.getDefaultInstance(),
897+
InternalTransaction.TrxType.TRX_UNKNOWN_TYPE));
898+
program.stackPush(DataWord.of((byte) 0x80));
899+
program.stackPush(DataWord.of((byte) 0x00));
900+
program.stackPush(DataWord.of((byte) 0x80));
901+
program.stackPush(DataWord.of((byte) 0x00));
902+
903+
// Test VoteWitness before EnergyAdjustment, should not have memory expand energy
904+
Assert.assertEquals(30000, EnergyCost.getVoteWitnessCost(program));
905+
906+
// Test VoteWitness after EnergyAdjustment, should have memory expand energy
907+
VMConfig.initAllowEnergyAdjustment(1);
908+
909+
long memWords = (0x80 + 32) / 32;
910+
long memoryExpandEnergy = 3 * memWords + memWords * memWords / 512;
911+
Assert.assertEquals(30000 + memoryExpandEnergy, EnergyCost.getVoteWitnessCost2(program));
912+
}
913+
867914
private void testOperations(Program program) {
868915
try {
869916
while (!program.isStopped()) {
@@ -954,4 +1001,10 @@ private byte[] compile(String code) {
9541001
return new BytecodeCompiler().compile(code);
9551002
}
9561003

1004+
private byte[] generateRandomAddress() {
1005+
byte[] address = new byte[21];
1006+
new Random().nextBytes(address);
1007+
address[0] = DecodeUtil.addressPreFixByte;
1008+
return address;
1009+
}
9571010
}

0 commit comments

Comments
 (0)