|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 |
|
5 | 5 | import java.util.List; |
| 6 | +import java.util.Random; |
| 7 | + |
6 | 8 | import lombok.SneakyThrows; |
7 | 9 | import lombok.extern.slf4j.Slf4j; |
8 | 10 | import org.bouncycastle.util.encoders.Hex; |
|
15 | 17 | import org.tron.common.BaseTest; |
16 | 18 | import org.tron.common.parameter.CommonParameter; |
17 | 19 | import org.tron.common.runtime.InternalTransaction; |
| 20 | +import org.tron.common.utils.DecodeUtil; |
18 | 21 | import org.tron.core.Constant; |
19 | 22 | import org.tron.core.config.args.Args; |
20 | 23 | import org.tron.core.exception.ContractValidateException; |
@@ -864,6 +867,50 @@ public void testPush0() throws ContractValidateException { |
864 | 867 | VMConfig.initAllowTvmShangHai(0); |
865 | 868 | } |
866 | 869 |
|
| 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 | + |
867 | 914 | private void testOperations(Program program) { |
868 | 915 | try { |
869 | 916 | while (!program.isStopped()) { |
@@ -954,4 +1001,10 @@ private byte[] compile(String code) { |
954 | 1001 | return new BytecodeCompiler().compile(code); |
955 | 1002 | } |
956 | 1003 |
|
| 1004 | + private byte[] generateRandomAddress() { |
| 1005 | + byte[] address = new byte[21]; |
| 1006 | + new Random().nextBytes(address); |
| 1007 | + address[0] = DecodeUtil.addressPreFixByte; |
| 1008 | + return address; |
| 1009 | + } |
957 | 1010 | } |
0 commit comments