|
1 | 1 | package org.tron.core.actuator; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertThrows; |
3 | 4 | import static org.junit.Assert.fail; |
4 | 5 | import static org.tron.core.config.Parameter.ChainSymbol.TRX_SYMBOL_BYTES; |
5 | 6 |
|
6 | 7 | import com.google.protobuf.Any; |
7 | 8 | import com.google.protobuf.ByteString; |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
8 | 11 | import java.util.Arrays; |
9 | 12 | import java.util.Map; |
10 | 13 | import junit.framework.TestCase; |
|
13 | 16 | import org.junit.Before; |
14 | 17 | import org.junit.Test; |
15 | 18 | import org.tron.common.BaseTest; |
| 19 | +import org.tron.common.crypto.ECKey; |
16 | 20 | import org.tron.common.utils.ByteArray; |
| 21 | +import org.tron.common.utils.ForkController; |
| 22 | +import org.tron.common.utils.PublicMethod; |
| 23 | +import org.tron.consensus.base.Param; |
| 24 | +import org.tron.consensus.base.Param.Miner; |
17 | 25 | import org.tron.core.Constant; |
18 | 26 | import org.tron.core.Wallet; |
19 | 27 | import org.tron.core.capsule.AccountCapsule; |
20 | 28 | import org.tron.core.capsule.AssetIssueCapsule; |
| 29 | +import org.tron.core.capsule.BlockCapsule; |
21 | 30 | import org.tron.core.capsule.ExchangeCapsule; |
| 31 | +import org.tron.core.capsule.TransactionCapsule; |
22 | 32 | import org.tron.core.capsule.TransactionResultCapsule; |
| 33 | +import org.tron.core.capsule.WitnessCapsule; |
| 34 | +import org.tron.core.config.Parameter; |
| 35 | +import org.tron.core.config.Parameter.ForkBlockVersionEnum; |
23 | 36 | import org.tron.core.config.args.Args; |
| 37 | +import org.tron.core.db.Manager; |
24 | 38 | import org.tron.core.exception.ContractExeException; |
25 | 39 | import org.tron.core.exception.ContractValidateException; |
26 | 40 | import org.tron.core.exception.ItemNotFoundException; |
27 | 41 | import org.tron.protos.Protocol.AccountType; |
| 42 | +import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
28 | 43 | import org.tron.protos.Protocol.Transaction.Result.code; |
29 | 44 | import org.tron.protos.contract.AssetIssueContractOuterClass; |
30 | 45 | import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract; |
@@ -1674,7 +1689,7 @@ public void noContract() { |
1674 | 1689 | public void invalidContractType() { |
1675 | 1690 | ExchangeTransactionActuator actuator = new ExchangeTransactionActuator(); |
1676 | 1691 | // create AssetIssueContract, not a valid ClearABI contract , which will throw e expectipon |
1677 | | - Any invalidContractTypes = Any.pack(AssetIssueContractOuterClass.AssetIssueContract.newBuilder() |
| 1692 | + Any invalidContractTypes = Any.pack(AssetIssueContract.newBuilder() |
1678 | 1693 | .build()); |
1679 | 1694 | actuator.setChainBaseManager(dbManager.getChainBaseManager()) |
1680 | 1695 | .setAny(invalidContractTypes); |
@@ -1725,4 +1740,92 @@ private void processAndCheckInvalid(ExchangeTransactionActuator actuator, |
1725 | 1740 | } |
1726 | 1741 | } |
1727 | 1742 |
|
| 1743 | + /** |
| 1744 | + * isExchangeTransaction |
| 1745 | + */ |
| 1746 | + @Test |
| 1747 | + public void isExchangeTransactionPush() { |
| 1748 | + try { |
| 1749 | + TransactionCapsule transactionCap = new TransactionCapsule( |
| 1750 | + ExchangeTransactionContract.newBuilder() |
| 1751 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) |
| 1752 | + .setExchangeId(1) |
| 1753 | + .setTokenId(ByteString.copyFrom("_".getBytes())) |
| 1754 | + .setQuant(1) |
| 1755 | + .setExpected(1) |
| 1756 | + .build(), ContractType.ExchangeTransactionContract); |
| 1757 | + dbManager.pushTransaction(transactionCap); |
| 1758 | + |
| 1759 | + } catch (Exception e) { |
| 1760 | + Assert.assertTrue(true); |
| 1761 | + } |
| 1762 | + } |
| 1763 | + |
| 1764 | + @Test |
| 1765 | + public void isExchangeTransactionGenerate() { |
| 1766 | + try { |
| 1767 | + |
| 1768 | + String key = PublicMethod.getRandomPrivateKey(); |
| 1769 | + byte[] privateKey = ByteArray.fromHexString(key); |
| 1770 | + final ECKey ecKey = ECKey.fromPrivate(privateKey); |
| 1771 | + byte[] address = ecKey.getAddress(); |
| 1772 | + WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address)); |
| 1773 | + |
| 1774 | + String OWNER_ADDRESS_SECOND = |
| 1775 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; |
| 1776 | + TransactionCapsule transactionCap = new TransactionCapsule( |
| 1777 | + ExchangeTransactionContract.newBuilder() |
| 1778 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) |
| 1779 | + .setExchangeId(1) |
| 1780 | + .setTokenId(ByteString.copyFrom("_".getBytes())) |
| 1781 | + .setQuant(1) |
| 1782 | + .setExpected(1) |
| 1783 | + .build(), ContractType.ExchangeTransactionContract); |
| 1784 | + dbManager.getPendingTransactions().add(transactionCap); |
| 1785 | + Param param = Param.getInstance(); |
| 1786 | + Miner miner = param.new Miner(privateKey, witnessCapsule.getAddress(), |
| 1787 | + witnessCapsule.getAddress()); |
| 1788 | + BlockCapsule blockCapsule = dbManager |
| 1789 | + .generateBlock(miner, 1533529947843L, System.currentTimeMillis() + 1000); |
| 1790 | + } catch (Exception e) { |
| 1791 | + Assert.assertTrue(false); |
| 1792 | + } |
| 1793 | + } |
| 1794 | + |
| 1795 | + @Test |
| 1796 | + public void rejectExchangeTransaction() { |
| 1797 | + try { |
| 1798 | + long maintenanceTimeInterval = dbManager.getDynamicPropertiesStore() |
| 1799 | + .getMaintenanceTimeInterval(); |
| 1800 | + long hardForkTime = |
| 1801 | + ((ForkBlockVersionEnum.VERSION_4_0_1.getHardForkTime() - 1) / maintenanceTimeInterval + 1) |
| 1802 | + * maintenanceTimeInterval; |
| 1803 | + dbManager.getDynamicPropertiesStore() |
| 1804 | + .saveLatestBlockHeaderTimestamp(hardForkTime + 1); |
| 1805 | + byte[] stats = new byte[27]; |
| 1806 | + Arrays.fill(stats, (byte) 1); |
| 1807 | + dbManager.getDynamicPropertiesStore() |
| 1808 | + .statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0_1.getValue(), stats); |
| 1809 | + boolean flag = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_0_1); |
| 1810 | + Assert.assertTrue(flag); |
| 1811 | + String OWNER_ADDRESS_SECOND = |
| 1812 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; |
| 1813 | + TransactionCapsule transactionCap = new TransactionCapsule( |
| 1814 | + ExchangeTransactionContract.newBuilder() |
| 1815 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS_SECOND))) |
| 1816 | + .setExchangeId(1) |
| 1817 | + .setTokenId(ByteString.copyFrom("_".getBytes())) |
| 1818 | + .setQuant(1) |
| 1819 | + .setExpected(1) |
| 1820 | + .build(), ContractType.ExchangeTransactionContract); |
| 1821 | + Method rejectExchangeTransaction = Manager.class.getDeclaredMethod( |
| 1822 | + "rejectExchangeTransaction", org.tron.protos.Protocol.Transaction.class); |
| 1823 | + rejectExchangeTransaction.setAccessible(true); |
| 1824 | + Exception ex = assertThrows(InvocationTargetException.class, () -> { |
| 1825 | + rejectExchangeTransaction.invoke(dbManager, transactionCap.getInstance()); |
| 1826 | + }); |
| 1827 | + } catch (Exception e) { |
| 1828 | + fail(); |
| 1829 | + } |
| 1830 | + } |
1728 | 1831 | } |
0 commit comments