|
| 1 | +package org.tron.core.actuator; |
| 2 | + |
| 3 | +import static junit.framework.TestCase.fail; |
| 4 | + |
| 5 | +import com.google.protobuf.Any; |
| 6 | +import com.google.protobuf.ByteString; |
| 7 | +import java.io.File; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.junit.AfterClass; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.BeforeClass; |
| 12 | +import org.junit.Test; |
| 13 | +import org.tron.common.application.TronApplicationContext; |
| 14 | +import org.tron.common.utils.ByteArray; |
| 15 | +import org.tron.common.utils.FileUtil; |
| 16 | +import org.tron.core.Constant; |
| 17 | +import org.tron.core.Wallet; |
| 18 | +import org.tron.core.capsule.DeferredTransactionCapsule; |
| 19 | +import org.tron.core.capsule.TransactionCapsule; |
| 20 | +import org.tron.core.capsule.TransactionResultCapsule; |
| 21 | +import org.tron.core.config.DefaultConfig; |
| 22 | +import org.tron.core.config.args.Args; |
| 23 | +import org.tron.core.db.Manager; |
| 24 | +import org.tron.core.exception.ContractExeException; |
| 25 | +import org.tron.core.exception.ContractValidateException; |
| 26 | +import org.tron.protos.Contract; |
| 27 | +import org.tron.protos.Contract.TransferContract; |
| 28 | +import org.tron.protos.Protocol.DeferredTransaction; |
| 29 | +import org.tron.protos.Protocol.Transaction; |
| 30 | +import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
| 31 | +import org.tron.protos.Protocol.Transaction.Result.code; |
| 32 | +import org.tron.protos.Protocol.Transaction.raw.Builder; |
| 33 | + |
| 34 | +@Slf4j |
| 35 | +public class CancelDeferredTransactionContractActuatorTest { |
| 36 | + |
| 37 | + private static Manager dbManager; |
| 38 | + private static final String dbPath = "output_cancel_deferred_transaction_test"; |
| 39 | + private static TronApplicationContext context; |
| 40 | + private static final String OWNER_ADDRESS; |
| 41 | + private static final String TO_ADDRESS; |
| 42 | + private static final long AMOUNT = 100; |
| 43 | + private static final long OWNER_BALANCE = 9999999; |
| 44 | + private static final long TO_BALANCE = 100001; |
| 45 | + private static final String OWNER_ADDRESS_INVALID = "aaaa"; |
| 46 | + private static final String TO_ADDRESS_INVALID = "bbb"; |
| 47 | + private static final String OWNER_ACCOUNT_INVALID; |
| 48 | + private static final String OWNER_NO_BALANCE; |
| 49 | + private static final String To_ACCOUNT_INVALID; |
| 50 | + private static Transaction transaction = null; |
| 51 | + private static DeferredTransaction deferredTransaction = null; |
| 52 | + private static DeferredTransactionCapsule deferredTransactionCapsule = null; |
| 53 | + |
| 54 | + static { |
| 55 | + Args.setParam(new String[]{"--output-directory", dbPath}, Constant.TEST_CONF); |
| 56 | + context = new TronApplicationContext(DefaultConfig.class); |
| 57 | + OWNER_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; |
| 58 | + TO_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc"; |
| 59 | + OWNER_ACCOUNT_INVALID = |
| 60 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a3456"; |
| 61 | + OWNER_NO_BALANCE = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a3433"; |
| 62 | + To_ACCOUNT_INVALID = |
| 63 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a3422"; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Init data. |
| 68 | + */ |
| 69 | + @BeforeClass |
| 70 | + public static void init() { |
| 71 | + dbManager = context.getBean(Manager.class); |
| 72 | + initDeferredTransaction(); |
| 73 | + deferredTransaction = getBuildDeferredTransaction(transaction); |
| 74 | + deferredTransactionCapsule = new DeferredTransactionCapsule(deferredTransaction); |
| 75 | + dbManager.getDeferredTransactionIdIndexStore().put(deferredTransactionCapsule); |
| 76 | + dbManager.getDeferredTransactionStore().put(deferredTransactionCapsule); |
| 77 | + } |
| 78 | + |
| 79 | + private static void initDeferredTransaction() { |
| 80 | + transaction = getBuildTransaction( |
| 81 | + getBuildTransferContract(OWNER_ADDRESS, TO_ADDRESS), |
| 82 | + System.currentTimeMillis(), 100); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Release resources. |
| 87 | + */ |
| 88 | + @AfterClass |
| 89 | + public static void destroy() { |
| 90 | + Args.clearParam(); |
| 91 | + context.destroy(); |
| 92 | + if (FileUtil.deleteDir(new File(dbPath))) { |
| 93 | + logger.info("Release resources successful."); |
| 94 | + } else { |
| 95 | + logger.info("Release resources failure."); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static DeferredTransaction getBuildDeferredTransaction(Transaction transaction) { |
| 100 | + Builder rawData = transaction.getRawData().toBuilder().setDelaySeconds(100); |
| 101 | + transaction = transaction.toBuilder().setRawData(rawData).build(); |
| 102 | + DeferredTransaction.Builder deferredTransaction = DeferredTransaction.newBuilder(); |
| 103 | + TransactionCapsule transactionCapsule = new TransactionCapsule(transaction); |
| 104 | + deferredTransaction.setTransactionId(transactionCapsule.getTransactionId().getByteString()); |
| 105 | + deferredTransaction.setDelaySeconds(transactionCapsule.getDeferredSeconds()); |
| 106 | + deferredTransaction.setDelayUntil(System.currentTimeMillis() + 10000); |
| 107 | + ByteString senderAddress = transactionCapsule.getSenderAddress(); |
| 108 | + ByteString toAddress = transactionCapsule.getToAddress(); |
| 109 | + |
| 110 | + deferredTransaction.setSenderAddress(senderAddress); |
| 111 | + deferredTransaction.setReceiverAddress(toAddress); |
| 112 | + deferredTransaction.setTransaction(transactionCapsule.getInstance()); |
| 113 | + return deferredTransaction.build(); |
| 114 | + } |
| 115 | + |
| 116 | + private static Transaction getBuildTransaction( |
| 117 | + TransferContract transferContract, long transactionTimestamp, long refBlockNum) { |
| 118 | + return Transaction.newBuilder().setRawData( |
| 119 | + Transaction.raw.newBuilder().setTimestamp(transactionTimestamp).setRefBlockNum(refBlockNum) |
| 120 | + .addContract( |
| 121 | + Transaction.Contract.newBuilder().setType(ContractType.TransferContract) |
| 122 | + .setParameter(Any.pack(transferContract)).build()).build()).build(); |
| 123 | + } |
| 124 | + |
| 125 | + private static TransferContract getBuildTransferContract(String ownerAddress, String toAddress) { |
| 126 | + return TransferContract.newBuilder().setAmount(10) |
| 127 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ownerAddress))) |
| 128 | + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString(toAddress))).build(); |
| 129 | + } |
| 130 | + |
| 131 | + private Any getOwnerAddressContract() { |
| 132 | + return Any.pack( |
| 133 | + Contract.CancelDeferredTransactionContract.newBuilder() |
| 134 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) |
| 135 | + .setTransactionId(deferredTransactionCapsule.getTransactionId()) |
| 136 | + .build()); |
| 137 | + } |
| 138 | + |
| 139 | + private Any getToAddressContract() { |
| 140 | + return Any.pack( |
| 141 | + Contract.CancelDeferredTransactionContract.newBuilder() |
| 142 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS))) |
| 143 | + .setTransactionId(deferredTransactionCapsule.getTransactionId()) |
| 144 | + .build()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void perfectCancelDeferredTransaction() { |
| 149 | + CancelDeferredTransactionContractActuator actuator = new CancelDeferredTransactionContractActuator( |
| 150 | + getOwnerAddressContract(), dbManager); |
| 151 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 152 | + byte[] key = dbManager.getDeferredTransactionIdIndexStore().getDeferredTransactionKeyById(deferredTransaction.getTransactionId()); |
| 153 | + Assert.assertNotNull("perfect cancel deferred transaction", key); |
| 154 | + try { |
| 155 | + actuator.validate(); |
| 156 | + actuator.execute(ret); |
| 157 | + Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS); |
| 158 | + key = dbManager.getDeferredTransactionIdIndexStore().getDeferredTransactionKeyById(deferredTransaction.getTransactionId()); |
| 159 | + Assert.assertNull("perfect cancel deferred transaction", key); |
| 160 | + |
| 161 | + } catch (ContractValidateException e) { |
| 162 | + Assert.assertFalse(e instanceof ContractValidateException); |
| 163 | + } catch (ContractExeException e) { |
| 164 | + Assert.assertFalse(e instanceof ContractExeException); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void failedCancelDeferredTransaction() throws ContractValidateException { |
| 170 | + CancelDeferredTransactionContractActuator actuator = new CancelDeferredTransactionContractActuator( |
| 171 | + getToAddressContract(), dbManager); |
| 172 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 173 | + byte[] key = dbManager.getDeferredTransactionIdIndexStore().getDeferredTransactionKeyById(deferredTransaction.getTransactionId()); |
| 174 | + try { |
| 175 | + actuator.validate(); |
| 176 | + |
| 177 | + } catch (ContractValidateException e) { |
| 178 | + Assert.assertTrue(e instanceof ContractValidateException); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments