forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWallet.java
More file actions
executable file
·885 lines (780 loc) · 32.5 KB
/
Wallet.java
File metadata and controls
executable file
·885 lines (780 loc) · 32.5 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
* 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 com.google.protobuf.ByteString;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.tron.api.GrpcAPI;
import org.tron.api.GrpcAPI.AccountNetMessage;
import org.tron.api.GrpcAPI.AccountResourceMessage;
import org.tron.api.GrpcAPI.Address;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.BlockList;
import org.tron.api.GrpcAPI.Node;
import org.tron.api.GrpcAPI.NodeList;
import org.tron.api.GrpcAPI.NumberMessage;
import org.tron.api.GrpcAPI.ProposalList;
import org.tron.api.GrpcAPI.Return.response_code;
import org.tron.api.GrpcAPI.TransactionExtention;
import org.tron.api.GrpcAPI.WitnessList;
import org.tron.common.crypto.ECKey;
import org.tron.common.crypto.Hash;
import org.tron.common.overlay.discover.node.NodeHandler;
import org.tron.common.overlay.discover.node.NodeManager;
import org.tron.common.overlay.message.Message;
import org.tron.common.runtime.Runtime;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.runtime.vm.program.invoke.ProgramInvokeFactoryImpl;
import org.tron.common.storage.DepositImpl;
import org.tron.common.utils.Base58;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Sha256Hash;
import org.tron.common.utils.Utils;
import org.tron.core.actuator.Actuator;
import org.tron.core.actuator.ActuatorFactory;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.ProposalCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.db.AccountIdIndexStore;
import org.tron.core.db.AccountStore;
import org.tron.core.db.BandwidthProcessor;
import org.tron.core.db.ContractStore;
import org.tron.core.db.CpuProcessor;
import org.tron.core.db.DynamicPropertiesStore;
import org.tron.core.db.Manager;
import org.tron.core.db.PendingManager;
import org.tron.core.exception.AccountResourceInsufficientException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.DupTransactionException;
import org.tron.core.exception.HeaderNotFound;
import org.tron.core.exception.StoreException;
import org.tron.core.exception.TaposException;
import org.tron.core.exception.TooBigTransactionException;
import org.tron.core.exception.TransactionExpirationException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.net.message.TransactionMessage;
import org.tron.core.net.node.NodeImpl;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.CreateSmartContract;
import org.tron.protos.Contract.TransferContract;
import org.tron.protos.Contract.TriggerSmartContract;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.Proposal;
import org.tron.protos.Protocol.SmartContract;
import org.tron.protos.Protocol.SmartContract.ABI;
import org.tron.protos.Protocol.SmartContract.ABI.Entry.StateMutabilityType;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.Protocol.TransactionSign;
@Slf4j
@Component
public class Wallet {
@Getter
private final ECKey ecKey;
@Autowired
private NodeImpl p2pNode;
@Autowired
private Manager dbManager;
@Autowired
private NodeManager nodeManager;
private static String addressPreFixString = Constant.ADD_PRE_FIX_STRING_TESTNET; //default testnet
private static byte addressPreFixByte = Constant.ADD_PRE_FIX_BYTE_TESTNET;
/**
* Creates a new Wallet with a random ECKey.
*/
public Wallet() {
this.ecKey = new ECKey(Utils.getRandom());
}
/**
* Creates a Wallet with an existing ECKey.
*/
public Wallet(final ECKey ecKey) {
this.ecKey = ecKey;
logger.info("wallet address: {}", ByteArray.toHexString(this.ecKey.getAddress()));
}
public static boolean isConstant(ABI abi, TriggerSmartContract triggerSmartContract) {
try {
return isConstant(abi, getSelector(triggerSmartContract.getData().toByteArray()));
} catch (Exception e) {
return false;
}
}
public byte[] getAddress() {
return ecKey.getAddress();
}
public static String getAddressPreFixString() {
return addressPreFixString;
}
public static void setAddressPreFixString(String addressPreFixString) {
Wallet.addressPreFixString = addressPreFixString;
}
public static byte getAddressPreFixByte() {
return addressPreFixByte;
}
public static void setAddressPreFixByte(byte addressPreFixByte) {
Wallet.addressPreFixByte = addressPreFixByte;
}
public static boolean addressValid(byte[] address) {
if (ArrayUtils.isEmpty(address)) {
logger.warn("Warning: Address is empty !!");
return false;
}
if (address.length != Constant.ADDRESS_SIZE / 2) {
logger.warn(
"Warning: Address length need " + Constant.ADDRESS_SIZE + " but " + address.length
+ " !!");
return false;
}
if (address[0] != addressPreFixByte) {
logger.warn("Warning: Address need prefix with " + addressPreFixByte + " but "
+ address[0] + " !!");
return false;
}
//Other rule;
return true;
}
public static String encode58Check(byte[] input) {
byte[] hash0 = Sha256Hash.hash(input);
byte[] hash1 = Sha256Hash.hash(hash0);
byte[] inputCheck = new byte[input.length + 4];
System.arraycopy(input, 0, inputCheck, 0, input.length);
System.arraycopy(hash1, 0, inputCheck, input.length, 4);
return Base58.encode(inputCheck);
}
private static byte[] decode58Check(String input) {
byte[] decodeCheck = Base58.decode(input);
if (decodeCheck.length <= 4) {
return null;
}
byte[] decodeData = new byte[decodeCheck.length - 4];
System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
byte[] hash0 = Sha256Hash.hash(decodeData);
byte[] hash1 = Sha256Hash.hash(hash0);
if (hash1[0] == decodeCheck[decodeData.length] &&
hash1[1] == decodeCheck[decodeData.length + 1] &&
hash1[2] == decodeCheck[decodeData.length + 2] &&
hash1[3] == decodeCheck[decodeData.length + 3]) {
return decodeData;
}
return null;
}
public static byte[] generateContractAddress(Transaction trx) {
CreateSmartContract contract = ContractCapsule.getSmartContractFromTransaction(trx);
byte[] ownerAddress = contract.getOwnerAddress().toByteArray();
TransactionCapsule trxCap = new TransactionCapsule(trx);
byte[] txRawDataHash = trxCap.getTransactionId().getBytes();
byte[] combined = new byte[txRawDataHash.length + ownerAddress.length];
System.arraycopy(txRawDataHash, 0, combined, 0, txRawDataHash.length);
System.arraycopy(ownerAddress, 0, combined, txRawDataHash.length, ownerAddress.length);
return Hash.sha3omit12(combined);
}
public static byte[] decodeFromBase58Check(String addressBase58) {
if (StringUtils.isEmpty(addressBase58)) {
logger.warn("Warning: Address is empty !!");
return null;
}
byte[] address = decode58Check(addressBase58);
if (address == null) {
return null;
}
if (!addressValid(address)) {
return null;
}
return address;
}
public Account getAccount(Account account) {
AccountStore accountStore = dbManager.getAccountStore();
AccountCapsule accountCapsule = accountStore.get(account.getAddress().toByteArray());
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
CpuProcessor cpuProcessor = new CpuProcessor(dbManager);
cpuProcessor.updateUsage(accountCapsule);
return accountCapsule.getInstance();
}
public Account getAccountById(Account account) {
AccountStore accountStore = dbManager.getAccountStore();
AccountIdIndexStore accountIdIndexStore = dbManager.getAccountIdIndexStore();
byte[] address = accountIdIndexStore.get(account.getAccountId());
if (address == null) {
return null;
}
AccountCapsule accountCapsule = accountStore.get(address);
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
CpuProcessor cpuProcessor = new CpuProcessor(dbManager);
cpuProcessor.updateUsage(accountCapsule);
return accountCapsule.getInstance();
}
/**
* Create a transaction.
*/
/*public Transaction createTransaction(byte[] address, String to, long amount) {
long balance = getBalance(address);
return new TransactionCapsule(address, to, amount, balance, utxoStore).getInstance();
} */
/**
* Create a transaction by contract.
*/
@Deprecated
public Transaction createTransaction(TransferContract contract) {
AccountStore accountStore = dbManager.getAccountStore();
return new TransactionCapsule(contract, accountStore).getInstance();
}
public TransactionCapsule createTransactionCapsule(com.google.protobuf.Message message,
ContractType contractType) throws ContractValidateException {
TransactionCapsule trx = new TransactionCapsule(message, contractType);
if (contractType != ContractType.CreateSmartContract
&& contractType != ContractType.TriggerSmartContract) {
List<Actuator> actList = ActuatorFactory.createActuator(trx, dbManager);
for (Actuator act : actList) {
act.validate();
}
}
if (contractType == ContractType.CreateSmartContract) {
// insure one owner just have one contract
CreateSmartContract contract = ContractCapsule
.getSmartContractFromTransaction(trx.getInstance());
byte[] ownerAddress = contract.getOwnerAddress().toByteArray();
if (dbManager.getAccountContractIndexStore().get(ownerAddress) != null) {
throw new ContractValidateException(
"Trying to create second contract with one account: address: " + Wallet
.encode58Check(ownerAddress));
}
// // insure the new contract address haven't exist
// if (deposit.getAccount(contractAddress) != null) {
// logger.error("Trying to create a contract with existing contract address: " + Wallet
// .encode58Check(contractAddress));
// return;
// }
}
try {
BlockCapsule headBlock = null;
List<BlockCapsule> blockList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockList)) {
throw new HeaderNotFound("latest block not found");
} else {
headBlock = blockList.get(0);
}
trx.setReference(headBlock.getNum(), headBlock.getBlockId().getBytes());
long expiration = headBlock.getTimeStamp() + Constant.TRANSACTION_DEFAULT_EXPIRATION_TIME;
trx.setExpiration(expiration);
trx.setTimestamp();
} catch (HeaderNotFound headerNotFound) {
headerNotFound.printStackTrace();
}
return trx;
}
/**
* Broadcast a transaction.
*/
public GrpcAPI.Return broadcastTransaction(Transaction signaturedTransaction) {
GrpcAPI.Return.Builder builder = GrpcAPI.Return.newBuilder();
try {
TransactionCapsule trx = new TransactionCapsule(signaturedTransaction);
Message message = new TransactionMessage(signaturedTransaction);
if (dbManager.isTooManyPending()) {
logger.debug(
"Manager is busy, pending transaction count:{}, discard the new coming transaction",
(dbManager.getPendingTransactions().size() + PendingManager.getTmpTransactions()
.size()));
return builder.setResult(false).setCode(response_code.SERVER_BUSY).build();
}
if (dbManager.isGeneratingBlock()) {
logger.debug("Manager is generating block, discard the new coming transaction");
return builder.setResult(false).setCode(response_code.SERVER_BUSY).build();
}
if (dbManager.getTransactionIdCache().getIfPresent(trx.getTransactionId()) != null) {
logger.debug("This transaction has been processed, discard the transaction");
return builder.setResult(false).setCode(response_code.DUP_TRANSACTION_ERROR).build();
} else {
dbManager.getTransactionIdCache().put(trx.getTransactionId(), true);
}
dbManager.pushTransactions(trx);
p2pNode.broadcast(message);
return builder.setResult(true).setCode(response_code.SUCCESS).build();
} catch (ValidateSignatureException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.SIGERROR)
.setMessage(ByteString.copyFromUtf8("validate signature error"))
.build();
} catch (ContractValidateException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.CONTRACT_VALIDATE_ERROR)
.setMessage(ByteString.copyFromUtf8("contract validate error : " + e.getMessage()))
.build();
} catch (ContractExeException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.CONTRACT_EXE_ERROR)
.setMessage(ByteString.copyFromUtf8("contract execute error : " + e.getMessage()))
.build();
} catch (AccountResourceInsufficientException e) {
logger.info(e.getMessage());
return builder.setResult(false).setCode(response_code.BANDWITH_ERROR)
.setMessage(ByteString.copyFromUtf8("AccountResourceInsufficient error"))
.build();
} catch (DupTransactionException e) {
logger.info("dup trans" + e.getMessage());
return builder.setResult(false).setCode(response_code.DUP_TRANSACTION_ERROR)
.setMessage(ByteString.copyFromUtf8("dup transaction"))
.build();
} catch (TaposException e) {
logger.info("tapos error" + e.getMessage());
return builder.setResult(false).setCode(response_code.TAPOS_ERROR)
.setMessage(ByteString.copyFromUtf8("Tapos check error"))
.build();
} catch (TooBigTransactionException e) {
logger.info("transaction error" + e.getMessage());
return builder.setResult(false).setCode(response_code.TOO_BIG_TRANSACTION_ERROR)
.setMessage(ByteString.copyFromUtf8("transaction size is too big"))
.build();
} catch (TransactionExpirationException e) {
logger.info("transaction expired" + e.getMessage());
return builder.setResult(false).setCode(response_code.TRANSACTION_EXPIRATION_ERROR)
.setMessage(ByteString.copyFromUtf8("transaction expired"))
.build();
} catch (Exception e) {
logger.info("exception caught" + e.getMessage());
return builder.setResult(false).setCode(response_code.OTHER_ERROR)
.setMessage(ByteString.copyFromUtf8("other error : " + e.getMessage()))
.build();
}
}
public TransactionCapsule getTransactionSign(TransactionSign transactionSign) {
byte[] privateKey = transactionSign.getPrivateKey().toByteArray();
TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction());
trx.sign(privateKey);
return trx;
}
public byte[] pass2Key(byte[] passPhrase) {
return Sha256Hash.hash(passPhrase);
}
public byte[] createAdresss(byte[] passPhrase) {
byte[] privateKey = pass2Key(passPhrase);
ECKey ecKey = ECKey.fromPrivate(privateKey);
return ecKey.getAddress();
}
public Block getNowBlock() {
List<BlockCapsule> blockList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockList)) {
return null;
} else {
return blockList.get(0).getInstance();
}
}
public Block getBlockByNum(long blockNum) {
try {
return dbManager.getBlockByNum(blockNum).getInstance();
} catch (StoreException e) {
logger.info(e.getMessage());
return null;
}
}
public WitnessList getWitnessList() {
WitnessList.Builder builder = WitnessList.newBuilder();
List<WitnessCapsule> witnessCapsuleList = dbManager.getWitnessStore().getAllWitnesses();
witnessCapsuleList
.forEach(witnessCapsule -> builder.addWitnesses(witnessCapsule.getInstance()));
return builder.build();
}
public ProposalList getProposalList() {
ProposalList.Builder builder = ProposalList.newBuilder();
List<ProposalCapsule> proposalCapsuleList = dbManager.getProposalStore().getAllProposals();
proposalCapsuleList
.forEach(proposalCapsule -> builder.addProposals(proposalCapsule.getInstance()));
return builder.build();
}
public Protocol.ChainParameters getChainParameters() {
Protocol.ChainParameters.Builder builder = Protocol.ChainParameters.newBuilder();
DynamicPropertiesStore dynamicPropertiesStore = dbManager.getDynamicPropertiesStore();
Protocol.ChainParameters.ChainParameter.Builder builder1
= Protocol.ChainParameters.ChainParameter.newBuilder();
builder.addChainParameter(builder1
.setKey(ChainParameters.MAINTENANCE_TIME_INTERVAL.name())
.setValue(
dynamicPropertiesStore.getMaintenanceTimeInterval())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.ACCOUNT_UPGRADE_COST.name())
.setValue(
dynamicPropertiesStore.getAccountUpgradeCost())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.CREATE_ACCOUNT_FEE.name())
.setValue(
dynamicPropertiesStore.getCreateAccountFee())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.TRANSACTION_FEE.name())
.setValue(
dynamicPropertiesStore.getTransactionFee())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.ASSET_ISSUE_FEE.name())
.setValue(
dynamicPropertiesStore.getAssetIssueFee())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.WITNESS_PAY_PER_BLOCK.name())
.setValue(
dynamicPropertiesStore.getWitnessPayPerBlock())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.WITNESS_STANDBY_ALLOWANCE.name())
.setValue(
dynamicPropertiesStore.getWitnessStandbyAllowance())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.CREATE_NEW_ACCOUNT_FEE_IN_SYSTEM_CONTRACT.name())
.setValue(
dynamicPropertiesStore.getCreateNewAccountFeeInSystemContract())
.build());
builder.addChainParameter(builder1
.setKey(ChainParameters.CREATE_NEW_ACCOUNT_BANDWIDTH_RATE.name())
.setValue(
dynamicPropertiesStore.getCreateNewAccountBandwidthRate())
.build());
return builder.build();
}
public AssetIssueList getAssetIssueList() {
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
dbManager.getAssetIssueStore().getAllAssetIssues()
.forEach(issueCapsule -> builder.addAssetIssue(issueCapsule.getInstance()));
return builder.build();
}
public AssetIssueList getAssetIssueList(long offset, long limit) {
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
List<AssetIssueCapsule> assetIssueList = dbManager.getAssetIssueStore()
.getAssetIssuesPaginated(offset, limit);
if (CollectionUtils.isEmpty(assetIssueList)) {
return null;
}
assetIssueList.forEach(issueCapsule -> builder.addAssetIssue(issueCapsule.getInstance()));
return builder.build();
}
public AssetIssueList getAssetIssueByAccount(ByteString accountAddress) {
if (accountAddress == null || accountAddress.isEmpty()) {
return null;
}
List<AssetIssueCapsule> assetIssueCapsuleList = dbManager.getAssetIssueStore()
.getAllAssetIssues();
AssetIssueList.Builder builder = AssetIssueList.newBuilder();
assetIssueCapsuleList.stream()
.filter(assetIssueCapsule -> assetIssueCapsule.getOwnerAddress().equals(accountAddress))
.forEach(issueCapsule -> {
builder.addAssetIssue(issueCapsule.getInstance());
});
return builder.build();
}
public AccountNetMessage getAccountNet(ByteString accountAddress) {
if (accountAddress == null || accountAddress.isEmpty()) {
return null;
}
AccountNetMessage.Builder builder = AccountNetMessage.newBuilder();
AccountCapsule accountCapsule = dbManager.getAccountStore().get(accountAddress.toByteArray());
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
Map<String, Long> assetNetLimitMap = new HashMap<>();
accountCapsule.getAllFreeAssetNetUsage().keySet().forEach(asset -> {
byte[] key = ByteArray.fromString(asset);
assetNetLimitMap.put(asset, dbManager.getAssetIssueStore().get(key).getFreeAssetNetLimit());
});
builder.setFreeNetUsed(accountCapsule.getFreeNetUsage())
.setFreeNetLimit(freeNetLimit)
.setNetUsed(accountCapsule.getNetUsage())
.setNetLimit(netLimit)
.setTotalNetLimit(totalNetLimit)
.setTotalNetWeight(totalNetWeight)
.putAllAssetNetUsed(accountCapsule.getAllFreeAssetNetUsage())
.putAllAssetNetLimit(assetNetLimitMap);
return builder.build();
}
public AccountResourceMessage getAccountResource(ByteString accountAddress) {
if (accountAddress == null || accountAddress.isEmpty()) {
return null;
}
AccountResourceMessage.Builder builder = AccountResourceMessage.newBuilder();
AccountCapsule accountCapsule = dbManager.getAccountStore().get(accountAddress.toByteArray());
if (accountCapsule == null) {
return null;
}
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);
CpuProcessor cpuProcessor = new CpuProcessor(dbManager);
cpuProcessor.updateUsage(accountCapsule);
long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
long cpuLimit = cpuProcessor.calculateGlobalCpuLimit(accountCapsule.getCpuFrozenBalance());
long totalCpuLimit = dbManager.getDynamicPropertiesStore().getTotalCpuLimit();
long totalCpuWeight = dbManager.getDynamicPropertiesStore().getTotalCpuWeight();
long storageLimit = accountCapsule.getAccountResource().getStorageLimit();
long storageUsage = accountCapsule.getAccountResource().getStorageUsage();
Map<String, Long> assetNetLimitMap = new HashMap<>();
accountCapsule.getAllFreeAssetNetUsage().keySet().forEach(asset -> {
byte[] key = ByteArray.fromString(asset);
assetNetLimitMap.put(asset, dbManager.getAssetIssueStore().get(key).getFreeAssetNetLimit());
});
builder.setFreeNetUsed(accountCapsule.getFreeNetUsage())
.setFreeNetLimit(freeNetLimit)
.setNetUsed(accountCapsule.getNetUsage())
.setNetLimit(netLimit)
.setTotalNetLimit(totalNetLimit)
.setTotalNetWeight(totalNetWeight)
.setCpuLimit(cpuLimit)
.setCpuUsed(accountCapsule.getAccountResource().getCpuUsage())
.setTotalCpuLimit(totalCpuLimit)
.setTotalCpuWeight(totalCpuWeight)
.setStorageLimit(storageLimit)
.setStorageUsed(storageUsage)
.putAllAssetNetUsed(accountCapsule.getAllFreeAssetNetUsage())
.putAllAssetNetLimit(assetNetLimitMap);
return builder.build();
}
public AssetIssueContract getAssetIssueByName(ByteString assetName) {
if (assetName == null || assetName.isEmpty()) {
return null;
}
AssetIssueCapsule assetIssueCapsule = dbManager.getAssetIssueStore()
.get(assetName.toByteArray());
return assetIssueCapsule != null ? assetIssueCapsule.getInstance() : null;
}
public NumberMessage totalTransaction() {
NumberMessage.Builder builder = NumberMessage.newBuilder()
.setNum(dbManager.getTransactionStore().getTotalTransactions());
return builder.build();
}
public NumberMessage getNextMaintenanceTime() {
NumberMessage.Builder builder = NumberMessage.newBuilder()
.setNum(dbManager.getDynamicPropertiesStore().getNextMaintenanceTime());
return builder.build();
}
public Block getBlockById(ByteString BlockId) {
if (Objects.isNull(BlockId)) {
return null;
}
Block block = null;
try {
block = dbManager.getBlockStore().get(BlockId.toByteArray()).getInstance();
} catch (StoreException e) {
}
return block;
}
public BlockList getBlocksByLimitNext(long number, long limit) {
if (limit <= 0) {
return null;
}
BlockList.Builder blockListBuilder = BlockList.newBuilder();
dbManager.getBlockStore().getLimitNumber(number, limit).forEach(
blockCapsule -> blockListBuilder.addBlock(blockCapsule.getInstance()));
return blockListBuilder.build();
}
public BlockList getBlockByLatestNum(long getNum) {
BlockList.Builder blockListBuilder = BlockList.newBuilder();
dbManager.getBlockStore().getBlockByLatestNum(getNum).forEach(
blockCapsule -> blockListBuilder.addBlock(blockCapsule.getInstance()));
return blockListBuilder.build();
}
public Transaction getTransactionById(ByteString transactionId) {
if (Objects.isNull(transactionId)) {
return null;
}
TransactionCapsule transactionCapsule = null;
try {
transactionCapsule = dbManager.getTransactionStore()
.get(transactionId.toByteArray());
} catch (StoreException e) {
}
if (transactionCapsule != null) {
return transactionCapsule.getInstance();
}
return null;
}
public Proposal getProposalById(ByteString proposalId) {
if (Objects.isNull(proposalId)) {
return null;
}
ProposalCapsule proposalCapsule = null;
try {
proposalCapsule = dbManager.getProposalStore()
.get(proposalId.toByteArray());
} catch (StoreException e) {
}
if (proposalCapsule != null) {
return proposalCapsule.getInstance();
}
return null;
}
public NodeList listNodes() {
List<NodeHandler> handlerList = nodeManager.dumpActiveNodes();
Map<String, NodeHandler> nodeHandlerMap = new HashMap<>();
for (NodeHandler handler : handlerList) {
String key = handler.getNode().getHexId() + handler.getNode().getHost();
nodeHandlerMap.put(key, handler);
}
NodeList.Builder nodeListBuilder = NodeList.newBuilder();
nodeHandlerMap.entrySet().stream()
.forEach(v -> {
org.tron.common.overlay.discover.node.Node node = v.getValue().getNode();
nodeListBuilder.addNodes(Node.newBuilder().setAddress(
Address.newBuilder()
.setHost(ByteString.copyFrom(ByteArray.fromString(node.getHost())))
.setPort(node.getPort())));
});
return nodeListBuilder.build();
}
public Transaction deployContract(CreateSmartContract createSmartContract,
TransactionCapsule trxCap) {
// do nothing, so can add some useful function later
// trxcap contract para cacheUnpackValue has value
return trxCap.getInstance();
}
public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
TransactionCapsule trxCap, TransactionExtention.Builder builder) {
ContractStore contractStore = dbManager.getContractStore();
byte[] contractAddress = triggerSmartContract.getContractAddress().toByteArray();
SmartContract.ABI abi = contractStore.getABI(contractAddress);
if (abi == null) {
return null;
}
try {
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
if (selector == null) {
return null;
}
if (!isConstant(abi, selector)) {
return trxCap.getInstance();
} else {
DepositImpl deposit = DepositImpl.createRoot(dbManager);
Block headBlock;
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockCapsuleList)) {
throw new HeaderNotFound("latest block not found");
} else {
headBlock = blockCapsuleList.get(0).getInstance();
}
Runtime runtime = new Runtime(trxCap.getInstance(), headBlock, deposit,
new ProgramInvokeFactoryImpl());
runtime.init();
runtime.execute();
runtime.go();
if (runtime.getResult().getException() != null) {
throw new RuntimeException("Runtime exe failed!");
}
ProgramResult result = runtime.getResult();
TransactionResultCapsule ret = new TransactionResultCapsule();
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
//ret.setConstantResult(result.getHReturn());
ret.setStatus(0, code.SUCESS);
trxCap.setResult(ret);
return trxCap.getInstance();
}
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
public SmartContract getContract(GrpcAPI.BytesMessage bytesMessage) {
byte[] address = bytesMessage.getValue().toByteArray();
AccountCapsule accountCapsule = dbManager.getAccountStore().get(address);
if (accountCapsule == null) {
logger.error(
"Get contract failed, the account is not exist or the account does not have code hash!");
return null;
}
ContractCapsule contractCapsule = dbManager.getContractStore()
.get(bytesMessage.getValue().toByteArray());
return contractCapsule.getInstance();
}
private static byte[] getSelector(byte[] data) {
if (data == null ||
data.length < 4) {
return null;
}
byte[] ret = new byte[4];
System.arraycopy(data, 0, ret, 0, 4);
return ret;
}
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) throws Exception {
if (selector == null || selector.length != 4) {
throw new Exception("Selector's length or selector itself is invalid");
}
for (int i = 0; i < abi.getEntrysCount(); i++) {
ABI.Entry entry = abi.getEntrys(i);
if (entry.getType() != ABI.Entry.EntryType.Function) {
continue;
}
int inputCount = entry.getInputsCount();
StringBuffer sb = new StringBuffer();
sb.append(entry.getName());
sb.append("(");
for (int k = 0; k < inputCount; k++) {
ABI.Entry.Param param = entry.getInputs(k);
sb.append(param.getType());
if (k + 1 < inputCount) {
sb.append(",");
}
}
sb.append(")");
byte[] funcSelector = new byte[4];
System.arraycopy(Hash.sha3(sb.toString().getBytes()), 0, funcSelector, 0, 4);
if (Arrays.equals(funcSelector, selector)) {
if (entry.getConstant() == true || entry.getStateMutability()
.equals(StateMutabilityType.View)) {
return true;
} else {
return false;
}
}
}
throw new Exception("There is no the selector!");
}
}