|
1 | 1 | package org.tron.core.db;
|
2 | 2 |
|
| 3 | +import com.google.protobuf.ByteString; |
3 | 4 | import java.io.File;
|
4 | 5 | import java.util.Random;
|
5 | 6 | import org.junit.AfterClass;
|
| 7 | +import org.junit.Assert; |
6 | 8 | import org.junit.BeforeClass;
|
7 |
| -import org.junit.Ignore; |
8 | 9 | import org.junit.Test;
|
9 | 10 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 11 | +import org.tron.common.utils.ByteArray; |
10 | 12 | import org.tron.common.utils.FileUtil;
|
11 | 13 | import org.tron.core.Constant;
|
| 14 | +import org.tron.core.Wallet; |
| 15 | +import org.tron.core.capsule.AccountCapsule; |
12 | 16 | import org.tron.core.capsule.TransactionCapsule;
|
13 | 17 | import org.tron.core.config.DefaultConfig;
|
14 | 18 | import org.tron.core.config.args.Args;
|
| 19 | +import org.tron.protos.Contract.AccountCreateContract; |
| 20 | +import org.tron.protos.Contract.TransferContract; |
| 21 | +import org.tron.protos.Contract.VoteWitnessContract; |
| 22 | +import org.tron.protos.Contract.VoteWitnessContract.Vote; |
| 23 | +import org.tron.protos.Contract.WitnessCreateContract; |
| 24 | +import org.tron.protos.Protocol.AccountType; |
15 | 25 |
|
16 | 26 | public class TransactionStoreTest {
|
17 | 27 |
|
18 | 28 | private static String dbPath = "output_TransactionStore_test";
|
19 | 29 | private static TransactionStore transactionStore;
|
20 | 30 | private static AnnotationConfigApplicationContext context;
|
21 |
| - private static final byte[] data = TransactionStoreTest.randomBytes(21); |
22 |
| - private static final byte[] key = TransactionStoreTest.randomBytes(21); |
23 |
| - private static final long value = 111L; |
| 31 | + private static final byte[] key1 = TransactionStoreTest.randomBytes(21); |
| 32 | + private static Manager dbManager; |
| 33 | + private static final byte[] key2 = TransactionStoreTest.randomBytes(21); |
| 34 | + |
| 35 | + |
| 36 | + private static final String URL = "https://tron.network"; |
| 37 | + |
| 38 | + private static final String ACCOUNT_NAME = "ownerF"; |
| 39 | + private static final String OWNER_ADDRESS = |
| 40 | + Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc"; |
| 41 | + private static final String TO_ADDRESS = |
| 42 | + Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc"; |
| 43 | + private static final long AMOUNT = 100; |
| 44 | + private static final String WITNESS_ADDRESS = |
| 45 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; |
24 | 46 |
|
25 | 47 | static {
|
26 | 48 | Args.setParam(new String[]{"-d", dbPath, "-w"}, Constant.TEST_CONF);
|
27 | 49 | context = new AnnotationConfigApplicationContext(DefaultConfig.class);
|
28 | 50 | }
|
29 | 51 |
|
30 |
| - @AfterClass |
31 |
| - public static void destroy() { |
32 |
| - Args.clearParam(); |
33 |
| - FileUtil.deleteDir(new File(dbPath)); |
34 |
| - context.destroy(); |
35 |
| - } |
36 |
| - |
| 52 | + /** |
| 53 | + * Init data. |
| 54 | + */ |
37 | 55 | @BeforeClass
|
38 | 56 | public static void init() {
|
39 |
| - transactionStore = context.getBean(TransactionStore.class); |
40 |
| - /* TransactionCapsule transactionCapsule = new TransactionCapsule(key, value);*/ |
41 |
| - TransactionCapsule transactionCapsule = null; |
42 |
| - transactionStore.put(data, transactionCapsule); |
| 57 | + dbManager = context.getBean(Manager.class); |
| 58 | + transactionStore = dbManager.getTransactionStore(); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * get AccountCreateContract. |
| 64 | + */ |
| 65 | + private AccountCreateContract getContract(String name, String address) { |
| 66 | + return AccountCreateContract.newBuilder() |
| 67 | + .setAccountName(ByteString.copyFromUtf8(name)) |
| 68 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(address))) |
| 69 | + .build(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * get TransferContract. |
| 74 | + */ |
| 75 | + private TransferContract getContract(long count, String owneraddress, String toaddress) { |
| 76 | + return TransferContract.newBuilder() |
| 77 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(owneraddress))) |
| 78 | + .setToAddress(ByteString.copyFrom(ByteArray.fromHexString(toaddress))) |
| 79 | + .setAmount(count) |
| 80 | + .build(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * get WitnessCreateContract. |
| 85 | + */ |
| 86 | + private WitnessCreateContract getWitnessContract(String address, String url) { |
| 87 | + return WitnessCreateContract.newBuilder() |
| 88 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(address))) |
| 89 | + .setUrl(ByteString.copyFrom(ByteArray.fromString(url))) |
| 90 | + .build(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * get VoteWitnessContract. |
| 95 | + */ |
| 96 | + private VoteWitnessContract getVoteWitnessContract(String address, String voteaddress, |
| 97 | + Long value) { |
| 98 | + return |
| 99 | + VoteWitnessContract.newBuilder() |
| 100 | + .setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(address))) |
| 101 | + .addVotes(Vote.newBuilder() |
| 102 | + .setVoteAddress(ByteString.copyFrom(ByteArray.fromHexString(voteaddress))) |
| 103 | + .setVoteCount(value).build()) |
| 104 | + .build(); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * put and get CreateAccountTransaction. |
| 110 | + */ |
| 111 | + @Test |
| 112 | + public void CreateAccountTransactionStoreTest() { |
| 113 | + AccountCreateContract accountCreateContract = getContract(ACCOUNT_NAME, |
| 114 | + OWNER_ADDRESS); |
| 115 | + TransactionCapsule ret = new TransactionCapsule(accountCreateContract, |
| 116 | + dbManager.getAccountStore()); |
| 117 | + transactionStore.put(key1, ret); |
| 118 | + Assert.assertEquals("Store CreateAccountTransaction is error", |
| 119 | + transactionStore.get(key1).getInstance(), |
| 120 | + ret.getInstance()); |
| 121 | + Assert.assertTrue(transactionStore.has(key1)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * put and get CreateWitnessTransaction. |
| 126 | + */ |
| 127 | + @Test |
| 128 | + public void CreateWitnessTransactionStoreTest() { |
| 129 | + WitnessCreateContract witnessContract = getWitnessContract(OWNER_ADDRESS, URL); |
| 130 | + TransactionCapsule transactionCapsule = new TransactionCapsule(witnessContract); |
| 131 | + transactionStore.put(key1, transactionCapsule); |
| 132 | + Assert.assertEquals("Store CreateWitnessTransaction is error", |
| 133 | + transactionStore.get(key1).getInstance(), |
| 134 | + transactionCapsule.getInstance()); |
43 | 135 | }
|
44 | 136 |
|
45 |
| - @Ignore |
| 137 | + /** |
| 138 | + * put and get TransferTransaction. |
| 139 | + */ |
46 | 140 | @Test
|
47 |
| - public void testGet() { |
48 |
| - // test get and has method |
49 |
| - /* try { |
50 |
| - Assert.assertTrue(transactionStore.has(data)); |
51 |
| - Assert.assertArrayEquals( |
52 |
| - key, |
53 |
| - transactionStore |
54 |
| - .get(data) |
55 |
| - .getInstance() |
56 |
| - .getRawData() |
57 |
| - .getContractList() |
58 |
| - .get(0) |
59 |
| - .getParameter() |
60 |
| - .unpack(TransferContract.class) |
61 |
| - .getToAddress() |
62 |
| - .toByteArray()); |
63 |
| - Assert.assertEquals( |
64 |
| - value, |
65 |
| - transactionStore |
66 |
| - .get(data) |
67 |
| - .getInstance() |
68 |
| - .getRawData() |
69 |
| - .getContractList() |
70 |
| - .get(0) |
71 |
| - .getParameter() |
72 |
| - .unpack(TransferContract.class) |
73 |
| - .getAmount()); |
74 |
| - } catch (InvalidProtocolBufferException e) { |
75 |
| - e.printStackTrace(); |
76 |
| - } */ |
| 141 | + public void TransferTransactionStorenTest() { |
| 142 | + AccountCapsule ownerCapsule = |
| 143 | + new AccountCapsule( |
| 144 | + ByteString.copyFromUtf8(ACCOUNT_NAME), |
| 145 | + ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), |
| 146 | + AccountType.AssetIssue, |
| 147 | + 1000000L |
| 148 | + ); |
| 149 | + dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule); |
| 150 | + TransferContract transferContract = getContract(AMOUNT, OWNER_ADDRESS, TO_ADDRESS); |
| 151 | + TransactionCapsule transactionCapsule = new TransactionCapsule(transferContract, |
| 152 | + dbManager.getAccountStore()); |
| 153 | + transactionStore.put(key1, transactionCapsule); |
| 154 | + Assert.assertEquals("Store TransferTransaction is error", |
| 155 | + transactionStore.get(key1).getInstance(), |
| 156 | + transactionCapsule.getInstance()); |
77 | 157 | }
|
78 | 158 |
|
79 |
| - /*@Test |
80 |
| - public void findTransactionByHash() { |
81 |
| - //test findTransactionByHash method |
82 |
| - TransactionCapsule transactionCapsuleByHash = new TransactionCapsule( |
83 |
| - transactionStore.findTransactionByHash(data)); |
| 159 | + /** |
| 160 | + * put and get VoteWitnessTransaction. |
| 161 | + */ |
| 162 | + |
| 163 | + @Test |
| 164 | + public void voteWitnessTransactionTest() { |
| 165 | + |
| 166 | + AccountCapsule ownerAccountFirstCapsule = |
| 167 | + new AccountCapsule( |
| 168 | + ByteString.copyFromUtf8(ACCOUNT_NAME), |
| 169 | + ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), |
| 170 | + AccountType.Normal, |
| 171 | + 1_000_000_000_000L); |
| 172 | + long frozenBalance = 1_000_000_000_000L; |
| 173 | + long duration = 3; |
| 174 | + ownerAccountFirstCapsule.setFrozen(frozenBalance, duration); |
| 175 | + dbManager.getAccountStore() |
| 176 | + .put(ownerAccountFirstCapsule.getAddress().toByteArray(), ownerAccountFirstCapsule); |
| 177 | + VoteWitnessContract actuator = getVoteWitnessContract(OWNER_ADDRESS, WITNESS_ADDRESS, 1L); |
| 178 | + TransactionCapsule transactionCapsule = new TransactionCapsule(actuator); |
| 179 | + transactionStore.put(key1, transactionCapsule); |
| 180 | + Assert.assertEquals("Store VoteWitnessTransaction is error", |
| 181 | + transactionStore.get(key1).getInstance(), |
| 182 | + transactionCapsule.getInstance()); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * put value is null and get it. |
| 187 | + */ |
| 188 | + @Test |
| 189 | + public void TransactionValueNullTest() { |
| 190 | + TransactionCapsule transactionCapsule = null; |
| 191 | + transactionStore.put(key2, transactionCapsule); |
| 192 | + Assert.assertNull("put value is null", transactionStore.get(key2)); |
| 193 | + |
| 194 | + } |
84 | 195 |
|
85 |
| - Assert.assertEquals(key, ByteArray.toHexString( |
86 |
| - transactionCapsuleByHash.getInstance().getRawData().getVoutList().get(0).getPubKeyHash() |
87 |
| - .toByteArray())); |
88 |
| - Assert.assertEquals(value, |
89 |
| - transactionCapsuleByHash.getInstance().getRawData().getVoutList().get(0).getValue()); |
| 196 | + /** |
| 197 | + * put key is null and get it. |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void TransactionKeyNullTest() { |
| 201 | + AccountCreateContract accountCreateContract = getContract(ACCOUNT_NAME, |
| 202 | + OWNER_ADDRESS); |
| 203 | + TransactionCapsule ret = new TransactionCapsule(accountCreateContract, |
| 204 | + dbManager.getAccountStore()); |
| 205 | + byte[] key = null; |
| 206 | + transactionStore.put(key, ret); |
| 207 | + try { |
| 208 | + transactionStore.get(key); |
| 209 | + } catch (RuntimeException e) { |
| 210 | + Assert.assertEquals("The key argument cannot be null", e.getMessage()); |
| 211 | + } |
| 212 | + } |
| 213 | + @AfterClass |
| 214 | + public static void destroy() { |
| 215 | + Args.clearParam(); |
| 216 | + FileUtil.deleteDir(new File(dbPath)); |
| 217 | + context.destroy(); |
| 218 | + } |
90 | 219 |
|
91 |
| - } */ |
92 | 220 |
|
93 | 221 | public static byte[] randomBytes(int length) {
|
94 | 222 | // generate the random number
|
|
0 commit comments