|
| 1 | +package org.tron.core.db; |
| 2 | + |
| 3 | +import com.google.protobuf.ByteString; |
| 4 | +import java.io.File; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.junit.After; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.tron.api.GrpcAPI; |
| 11 | +import org.tron.api.GrpcAPI.Return.response_code; |
| 12 | +import org.tron.common.application.TronApplicationContext; |
| 13 | +import org.tron.common.parameter.CommonParameter; |
| 14 | +import org.tron.common.utils.ByteArray; |
| 15 | +import org.tron.common.utils.FileUtil; |
| 16 | +import org.tron.common.utils.Sha256Hash; |
| 17 | +import org.tron.core.Constant; |
| 18 | +import org.tron.core.Wallet; |
| 19 | +import org.tron.core.capsule.BlockCapsule; |
| 20 | +import org.tron.core.capsule.TransactionCapsule; |
| 21 | +import org.tron.core.config.DefaultConfig; |
| 22 | +import org.tron.core.config.args.Args; |
| 23 | +import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
| 24 | +import org.tron.protos.contract.BalanceContract.TransferContract; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +public class TransactionExpireTest { |
| 28 | + |
| 29 | + private String dbPath = "output_expire_test"; |
| 30 | + private TronApplicationContext context; |
| 31 | + private Wallet wallet; |
| 32 | + private Manager dbManager; |
| 33 | + private BlockCapsule blockCapsule; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void init() { |
| 37 | + Args.setParam(new String[] {"--output-directory", dbPath}, Constant.TEST_CONF); |
| 38 | + CommonParameter.PARAMETER.setMinEffectiveConnection(0); |
| 39 | + |
| 40 | + context = new TronApplicationContext(DefaultConfig.class); |
| 41 | + wallet = context.getBean(Wallet.class); |
| 42 | + dbManager = context.getBean(Manager.class); |
| 43 | + |
| 44 | + blockCapsule = new BlockCapsule( |
| 45 | + 1, |
| 46 | + Sha256Hash.wrap(ByteString.copyFrom( |
| 47 | + ByteArray.fromHexString( |
| 48 | + "0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), |
| 49 | + 1, |
| 50 | + ByteString.copyFromUtf8("testAddress")); |
| 51 | + dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderNumber(blockCapsule.getNum()); |
| 52 | + dbManager.getDynamicPropertiesStore() |
| 53 | + .saveLatestBlockHeaderTimestamp(blockCapsule.getTimeStamp()); |
| 54 | + dbManager.updateRecentBlock(blockCapsule); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void removeDb() { |
| 59 | + Args.clearParam(); |
| 60 | + context.destroy(); |
| 61 | + if (FileUtil.deleteDir(new File(dbPath))) { |
| 62 | + logger.info("Release resources successful."); |
| 63 | + } else { |
| 64 | + logger.info("Release resources failure."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testExpireTransaction() { |
| 70 | + TransferContract transferContract = TransferContract.newBuilder() |
| 71 | + .setAmount(1L) |
| 72 | + .setOwnerAddress(ByteString.copyFrom(Args.getLocalWitnesses() |
| 73 | + .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()))) |
| 74 | + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString( |
| 75 | + (Wallet.getAddressPreFixString() + "A389132D6639FBDA4FBC8B659264E6B7C90DB086")))) |
| 76 | + .build(); |
| 77 | + TransactionCapsule transactionCapsule = |
| 78 | + new TransactionCapsule(transferContract, ContractType.TransferContract); |
| 79 | + transactionCapsule.setReference(blockCapsule.getNum(), blockCapsule.getBlockId().getBytes()); |
| 80 | + Assert.assertEquals(1, blockCapsule.getTimeStamp()); |
| 81 | + |
| 82 | + long blockTimeStamp = blockCapsule.getTimeStamp(); |
| 83 | + transactionCapsule.setExpiration(blockTimeStamp - 1); |
| 84 | + transactionCapsule.sign(ByteArray.fromHexString(Args.getLocalWitnesses().getPrivateKey())); |
| 85 | + |
| 86 | + GrpcAPI.Return result = wallet.broadcastTransaction(transactionCapsule.getInstance()); |
| 87 | + Assert.assertEquals(response_code.TRANSACTION_EXPIRATION_ERROR, result.getCode()); |
| 88 | + } |
| 89 | +} |
0 commit comments