forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletTest.java
More file actions
455 lines (406 loc) · 18.9 KB
/
WalletTest.java
File metadata and controls
455 lines (406 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.core;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import java.io.File;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.BlockList;
import org.tron.api.GrpcAPI.ExchangeList;
import org.tron.api.GrpcAPI.ProposalList;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.Utils;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.ProposalCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.config.args.Args;
import org.tron.core.db.DynamicPropertiesStore;
import org.tron.core.db.Manager;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.TransferContract;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.BlockHeader;
import org.tron.protos.Protocol.BlockHeader.raw;
//import org.tron.protos.Protocol.DeferredTransaction;
import org.tron.protos.Protocol.Exchange;
import org.tron.protos.Protocol.Proposal;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
@Slf4j
public class WalletTest {
private static TronApplicationContext context;
private static Wallet wallet;
private static Manager manager;
private static String dbPath = "output_wallet_test";
public static final String ACCOUNT_ADDRESS_ONE = "121212a9cf";
public static final String ACCOUNT_ADDRESS_TWO = "232323a9cf";
public static final String ACCOUNT_ADDRESS_THREE = "343434a9cf";
public static final String ACCOUNT_ADDRESS_FOUR = "454545a9cf";
public static final String ACCOUNT_ADDRESS_FIVE = "565656a9cf";
public static final String ACCOUNT_ADDRESS_SIX = "12344349cf";
private static Block block1;
private static Block block2;
private static Block block3;
private static Block block4;
private static Block block5;
public static final long BLOCK_NUM_ONE = 1;
public static final long BLOCK_NUM_TWO = 2;
public static final long BLOCK_NUM_THREE = 3;
public static final long BLOCK_NUM_FOUR = 4;
public static final long BLOCK_NUM_FIVE = 5;
public static final long BLOCK_TIMESTAMP_ONE = DateTime.now().minusDays(4).getMillis();
public static final long BLOCK_TIMESTAMP_TWO = DateTime.now().minusDays(3).getMillis();
public static final long BLOCK_TIMESTAMP_THREE = DateTime.now().minusDays(2).getMillis();
public static final long BLOCK_TIMESTAMP_FOUR = DateTime.now().minusDays(1).getMillis();
public static final long BLOCK_TIMESTAMP_FIVE = DateTime.now().getMillis();
public static final long BLOCK_WITNESS_ONE = 12;
public static final long BLOCK_WITNESS_TWO = 13;
public static final long BLOCK_WITNESS_THREE = 14;
public static final long BLOCK_WITNESS_FOUR = 15;
public static final long BLOCK_WITNESS_FIVE = 16;
private static Transaction transaction1;
private static Transaction transaction2;
private static Transaction transaction3;
private static Transaction transaction4;
private static Transaction transaction5;
private static Transaction transaction6;
//private static DeferredTransaction deferredTransaction;
public static final long TRANSACTION_TIMESTAMP_ONE = DateTime.now().minusDays(4).getMillis();
public static final long TRANSACTION_TIMESTAMP_TWO = DateTime.now().minusDays(3).getMillis();
public static final long TRANSACTION_TIMESTAMP_THREE = DateTime.now().minusDays(2).getMillis();
public static final long TRANSACTION_TIMESTAMP_FOUR = DateTime.now().minusDays(1).getMillis();
public static final long TRANSACTION_TIMESTAMP_FIVE = DateTime.now().getMillis();
private static AssetIssueCapsule Asset1;
static {
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
context = new TronApplicationContext(DefaultConfig.class);
}
@BeforeClass
public static void init() {
wallet = context.getBean(Wallet.class);
manager = context.getBean(Manager.class);
initTransaction();
initBlock();
manager.getDynamicPropertiesStore().saveLatestBlockHeaderNumber(5);
}
/**
* initTransaction.
*/
private static void initTransaction() {
transaction1 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_ONE, ACCOUNT_ADDRESS_TWO),
TRANSACTION_TIMESTAMP_ONE, BLOCK_NUM_ONE);
addTransactionToStore(transaction1);
transaction2 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_TWO, ACCOUNT_ADDRESS_THREE),
TRANSACTION_TIMESTAMP_TWO, BLOCK_NUM_TWO);
addTransactionToStore(transaction2);
transaction3 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_THREE, ACCOUNT_ADDRESS_FOUR),
TRANSACTION_TIMESTAMP_THREE, BLOCK_NUM_THREE);
addTransactionToStore(transaction3);
transaction4 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_FOUR, ACCOUNT_ADDRESS_FIVE),
TRANSACTION_TIMESTAMP_FOUR, BLOCK_NUM_FOUR);
addTransactionToStore(transaction4);
transaction5 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_FIVE, ACCOUNT_ADDRESS_ONE),
TRANSACTION_TIMESTAMP_FIVE, BLOCK_NUM_FIVE);
addTransactionToStore(transaction5);
transaction6 = getBuildTransaction(
getBuildTransferContract(ACCOUNT_ADDRESS_ONE, ACCOUNT_ADDRESS_SIX),
TRANSACTION_TIMESTAMP_FIVE, BLOCK_NUM_FIVE);
addTransactionToStore(transaction5);
}
private static void addTransactionToStore(Transaction transaction) {
TransactionCapsule transactionCapsule = new TransactionCapsule(transaction);
manager.getTransactionStore()
.put(transactionCapsule.getTransactionId().getBytes(), transactionCapsule);
}
private static Transaction getBuildTransaction(
TransferContract transferContract, long transactionTimestamp, long refBlockNum) {
return Transaction.newBuilder().setRawData(
Transaction.raw.newBuilder().setTimestamp(transactionTimestamp)
.setRefBlockNum(refBlockNum)
.addContract(
Contract.newBuilder().setType(ContractType.TransferContract)
.setParameter(Any.pack(transferContract)).build()).build())
.build();
}
private static TransferContract getBuildTransferContract(String ownerAddress, String toAddress) {
return TransferContract.newBuilder().setAmount(10)
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ownerAddress)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(toAddress))).build();
}
/**
* initBlock.
*/
private static void initBlock() {
block1 = getBuildBlock(BLOCK_TIMESTAMP_ONE, BLOCK_NUM_ONE, BLOCK_WITNESS_ONE,
ACCOUNT_ADDRESS_ONE, transaction1, transaction2);
addBlockToStore(block1);
block2 = getBuildBlock(BLOCK_TIMESTAMP_TWO, BLOCK_NUM_TWO, BLOCK_WITNESS_TWO,
ACCOUNT_ADDRESS_TWO, transaction2, transaction3);
addBlockToStore(block2);
block3 = getBuildBlock(BLOCK_TIMESTAMP_THREE, BLOCK_NUM_THREE, BLOCK_WITNESS_THREE,
ACCOUNT_ADDRESS_THREE, transaction2, transaction4);
addBlockToStore(block3);
block4 = getBuildBlock(BLOCK_TIMESTAMP_FOUR, BLOCK_NUM_FOUR, BLOCK_WITNESS_FOUR,
ACCOUNT_ADDRESS_FOUR, transaction4, transaction5);
addBlockToStore(block4);
block5 = getBuildBlock(BLOCK_TIMESTAMP_FIVE, BLOCK_NUM_FIVE, BLOCK_WITNESS_FIVE,
ACCOUNT_ADDRESS_FIVE, transaction5, transaction3);
addBlockToStore(block5);
}
private static void addBlockToStore(Block block) {
BlockCapsule blockCapsule = new BlockCapsule(block);
manager.getBlockStore().put(blockCapsule.getBlockId().getBytes(), blockCapsule);
}
private static Block getBuildBlock(long timestamp, long num, long witnessId,
String witnessAddress, Transaction transaction, Transaction transactionNext) {
return Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(
raw.newBuilder().setTimestamp(timestamp).setNumber(num).setWitnessId(witnessId)
.setWitnessAddress(ByteString.copyFrom(ByteArray.fromHexString(witnessAddress)))
.build()).build()).addTransactions(transaction).addTransactions(transactionNext)
.build();
}
private static void buildAssetIssue() {
AssetIssueContract.Builder builder = AssetIssueContract.newBuilder();
builder.setName(ByteString.copyFromUtf8("Asset1"));
Asset1 = new AssetIssueCapsule(builder.build());
manager.getAssetIssueStore().put(Asset1.createDbKey(), Asset1);
}
private static void buildProposal() {
Proposal.Builder builder = Proposal.newBuilder();
builder.setProposalId(1L).setProposerAddress(ByteString.copyFromUtf8("Address1"));
ProposalCapsule proposalCapsule = new ProposalCapsule(builder.build());
manager.getProposalStore().put(proposalCapsule.createDbKey(), proposalCapsule);
builder.setProposalId(2L).setProposerAddress(ByteString.copyFromUtf8("Address2"));
proposalCapsule = new ProposalCapsule(builder.build());
manager.getProposalStore().put(proposalCapsule.createDbKey(), proposalCapsule);
manager.getDynamicPropertiesStore().saveLatestProposalNum(2L);
}
private static void buildExchange() {
Exchange.Builder builder = Exchange.newBuilder();
builder.setExchangeId(1L).setCreatorAddress(ByteString.copyFromUtf8("Address1"));
ExchangeCapsule ExchangeCapsule = new ExchangeCapsule(builder.build());
manager.getExchangeStore().put(ExchangeCapsule.createDbKey(), ExchangeCapsule);
builder.setExchangeId(2L).setCreatorAddress(ByteString.copyFromUtf8("Address2"));
ExchangeCapsule = new ExchangeCapsule(builder.build());
manager.getExchangeStore().put(ExchangeCapsule.createDbKey(), ExchangeCapsule);
manager.getDynamicPropertiesStore().saveLatestExchangeNum(2L);
}
@AfterClass
public static void removeDb() {
Args.clearParam();
context.destroy();
FileUtil.deleteDir(new File(dbPath));
}
@Test
public void testWallet() {
Wallet wallet1 = new Wallet();
Wallet wallet2 = new Wallet();
logger.info("wallet address = {}", ByteArray.toHexString(wallet1
.getAddress()));
logger.info("wallet2 address = {}", ByteArray.toHexString(wallet2
.getAddress()));
assertFalse(wallet1.getAddress().equals(wallet2.getAddress()));
}
@Test
public void testGetAddress() {
ECKey ecKey = new ECKey(Utils.getRandom());
Wallet wallet1 = new Wallet(ecKey);
logger.info("ecKey address = {}", ByteArray.toHexString(ecKey
.getAddress()));
logger.info("wallet address = {}", ByteArray.toHexString(wallet1
.getAddress()));
assertArrayEquals(wallet1.getAddress(), ecKey.getAddress());
}
@Test
public void testGetEcKey() {
ECKey ecKey = new ECKey(Utils.getRandom());
ECKey ecKey2 = new ECKey(Utils.getRandom());
Wallet wallet1 = new Wallet(ecKey);
logger.info("ecKey address = {}", ByteArray.toHexString(ecKey
.getAddress()));
logger.info("wallet address = {}", ByteArray.toHexString(wallet1
.getAddress()));
assertEquals("Wallet ECKey should match provided ECKey", wallet1.getEcKey(), ecKey);
}
@Test
public void ss() {
for (int i = 0; i < 4; i++) {
ECKey ecKey = new ECKey(Utils.getRandom());
System.out.println(i + 1);
System.out.println("privateKey:" + ByteArray.toHexString(ecKey.getPrivKeyBytes()));
System.out.println("publicKey:" + ByteArray.toHexString(ecKey.getPubKey()));
System.out.println("address:" + ByteArray.toHexString(ecKey.getAddress()));
System.out.println();
}
}
@Test
public void getBlockById() {
Block blockById = wallet
.getBlockById(ByteString.copyFrom(new BlockCapsule(block1).getBlockId().getBytes()));
Assert.assertEquals("getBlockById1", block1, blockById);
blockById = wallet
.getBlockById(ByteString.copyFrom(new BlockCapsule(block2).getBlockId().getBytes()));
Assert.assertEquals("getBlockById2", block2, blockById);
blockById = wallet
.getBlockById(ByteString.copyFrom(new BlockCapsule(block3).getBlockId().getBytes()));
Assert.assertEquals("getBlockById3", block3, blockById);
blockById = wallet
.getBlockById(ByteString.copyFrom(new BlockCapsule(block4).getBlockId().getBytes()));
Assert.assertEquals("getBlockById4", block4, blockById);
blockById = wallet
.getBlockById(ByteString.copyFrom(new BlockCapsule(block5).getBlockId().getBytes()));
Assert.assertEquals("getBlockById5", block5, blockById);
}
@Test
public void getBlocksByLimit() {
BlockList blocksByLimit = wallet.getBlocksByLimitNext(3, 2);
Assert.assertTrue("getBlocksByLimit1", blocksByLimit.getBlockList().contains(block3));
Assert.assertTrue("getBlocksByLimit2", blocksByLimit.getBlockList().contains(block4));
blocksByLimit = wallet.getBlocksByLimitNext(0, 5);
Assert.assertTrue("getBlocksByLimit3",
blocksByLimit.getBlockList().contains(manager.getGenesisBlock().getInstance()));
Assert.assertTrue("getBlocksByLimit4", blocksByLimit.getBlockList().contains(block1));
Assert.assertTrue("getBlocksByLimit5", blocksByLimit.getBlockList().contains(block2));
Assert.assertTrue("getBlocksByLimit6", blocksByLimit.getBlockList().contains(block3));
Assert.assertTrue("getBlocksByLimit7", blocksByLimit.getBlockList().contains(block4));
Assert.assertFalse("getBlocksByLimit8", blocksByLimit.getBlockList().contains(block5));
}
@Ignore
@Test
public void getTransactionById() {
Transaction transactionById = wallet.getTransactionById(
ByteString
.copyFrom(new TransactionCapsule(transaction1).getTransactionId().getBytes()));
Assert.assertEquals("getTransactionById1", transaction1, transactionById);
transactionById = wallet.getTransactionById(
ByteString
.copyFrom(new TransactionCapsule(transaction2).getTransactionId().getBytes()));
Assert.assertEquals("getTransactionById2", transaction2, transactionById);
transactionById = wallet.getTransactionById(
ByteString
.copyFrom(new TransactionCapsule(transaction3).getTransactionId().getBytes()));
Assert.assertEquals("getTransactionById3", transaction3, transactionById);
transactionById = wallet.getTransactionById(
ByteString
.copyFrom(new TransactionCapsule(transaction4).getTransactionId().getBytes()));
Assert.assertEquals("getTransactionById4", transaction4, transactionById);
transactionById = wallet.getTransactionById(
ByteString
.copyFrom(new TransactionCapsule(transaction5).getTransactionId().getBytes()));
Assert.assertEquals("getTransactionById5", transaction5, transactionById);
}
@Test
public void getBlockByLatestNum() {
BlockList blockByLatestNum = wallet.getBlockByLatestNum(2);
Assert.assertTrue("getBlockByLatestNum1", blockByLatestNum.getBlockList().contains(block5));
Assert.assertTrue("getBlockByLatestNum2", blockByLatestNum.getBlockList().contains(block4));
}
@Test
public void getPaginatedAssetIssueList() {
buildAssetIssue();
AssetIssueList assetList1 = wallet.getAssetIssueList(0, 100);
Assert.assertTrue("get Asset1", assetList1.getAssetIssue(0).getName().equals(Asset1.getName()));
try {
assetList1.getAssetIssue(1);
} catch (Exception e) {
Assert.assertTrue("AssetIssueList1 size should be 1", true);
}
AssetIssueList assetList2 = wallet.getAssetIssueList(0, 0);
try {
assetList2.getAssetIssue(0);
} catch (Exception e) {
Assert.assertTrue("AssetIssueList2 size should be 0", true);
}
}
@Test
public void getPaginatedProposalList() {
buildProposal();
//
ProposalList proposalList = wallet.getPaginatedProposalList(0, 100);
Assert.assertEquals(2, proposalList.getProposalsCount());
Assert.assertEquals("Address1",
proposalList.getProposalsList().get(0).getProposerAddress().toStringUtf8());
Assert.assertEquals("Address2",
proposalList.getProposalsList().get(1).getProposerAddress().toStringUtf8());
//
proposalList = wallet.getPaginatedProposalList(1, 100);
Assert.assertEquals(1, proposalList.getProposalsCount());
Assert.assertEquals("Address2",
proposalList.getProposalsList().get(0).getProposerAddress().toStringUtf8());
//
proposalList = wallet.getPaginatedProposalList(-1, 100);
Assert.assertNull(proposalList);
//
proposalList = wallet.getPaginatedProposalList(0, -1);
Assert.assertNull(proposalList);
//
proposalList = wallet.getPaginatedProposalList(0, 1000000000L);
Assert.assertEquals(2, proposalList.getProposalsCount());
}
@Test
public void getPaginatedExchangeList() {
buildExchange();
ExchangeList exchangeList = wallet.getPaginatedExchangeList(0, 100);
Assert.assertEquals("Address1",
exchangeList.getExchangesList().get(0).getCreatorAddress().toStringUtf8());
Assert.assertEquals("Address2",
exchangeList.getExchangesList().get(1).getCreatorAddress().toStringUtf8());
}
//@Test
public void testChainParameters() {
Protocol.ChainParameters.Builder builder = Protocol.ChainParameters.newBuilder();
Arrays.stream(ChainParameters.values()).forEach(parameters -> {
String methodName = Wallet.makeUpperCamelMethod(parameters.name());
try {
builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
.setKey(methodName)
.setValue((long) DynamicPropertiesStore.class.getDeclaredMethod(methodName)
.invoke(manager.getDynamicPropertiesStore()))
.build());
} catch (Exception ex) {
Assert.fail("get chainParameter : " + methodName + ", error : " + ex.getMessage());
}
});
System.out.printf(builder.build().toString());
}
}