Repository navigation
Expand file tree
/
Copy pathCommons.java
More file actions
172 lines (153 loc) · 6.56 KB
/
Copy pathCommons.java
File metadata and controls
172 lines (153 loc) · 6.56 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
package org.tron.common.utils;
import static org.tron.common.math.Maths.addExact;
import static org.tron.common.math.Maths.subtractExact;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.exception.BalanceInsufficientException;
import org.tron.core.store.AccountStore;
import org.tron.core.store.AssetIssueStore;
import org.tron.core.store.AssetIssueV2Store;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.store.ExchangeStore;
import org.tron.core.store.ExchangeV2Store;
@Slf4j(topic = "Commons")
public class Commons {
public static final int ASSET_ISSUE_COUNT_LIMIT_MAX = 1000;
public static final int BASE58_ADDRESS_LENGTH = 34;
public 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(CommonParameter.getInstance().isECKeyCryptoEngine(),
decodeData);
byte[] hash1 = Sha256Hash.hash(CommonParameter.getInstance().isECKeyCryptoEngine(),
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;
}
/**
* Decode a Base58Check address string to its 21-byte form.
*/
public static byte[] decodeFromBase58Check(String addressBase58) {
if (StringUtils.isEmpty(addressBase58)) {
logger.debug("address is empty !!");
return null;
}
if (addressBase58.length() != BASE58_ADDRESS_LENGTH) {
logger.debug("invalid Base58 address length");
return null;
}
byte[] address = decode58Check(addressBase58);
if (address == null) {
return null;
}
if (!DecodeUtil.addressValid(address)) {
return null;
}
return address;
}
public static void adjustBalance(AccountStore accountStore, byte[] accountAddress, long amount,
boolean useStrict)
throws BalanceInsufficientException {
AccountCapsule account = accountStore.getUnchecked(accountAddress);
adjustBalance(accountStore, account, amount, useStrict);
}
/**
* judge balance.
*/
public static void adjustBalance(AccountStore accountStore, AccountCapsule account, long amount,
boolean useStrict)
throws BalanceInsufficientException {
long balance = account.getBalance();
if (amount == 0) {
return;
}
if (amount < 0 && balance < -amount) {
throw new BalanceInsufficientException(
String.format("%s insufficient balance, balance: %d, amount: %d",
StringUtil.createReadableString(account.createDbKey()), balance, -amount));
}
account.setBalance(addExact(balance, amount, useStrict));
accountStore.put(account.getAddress().toByteArray(), account);
}
public static ExchangeStore getExchangeStoreFinal(DynamicPropertiesStore dynamicPropertiesStore,
ExchangeStore exchangeStore,
ExchangeV2Store exchangeV2Store) {
if (dynamicPropertiesStore.getAllowSameTokenName() == 0) {
return exchangeStore;
} else {
return exchangeV2Store;
}
}
public static void putExchangeCapsule(ExchangeCapsule exchangeCapsule,
DynamicPropertiesStore dynamicPropertiesStore, ExchangeStore exchangeStore,
ExchangeV2Store exchangeV2Store, AssetIssueStore assetIssueStore) {
if (dynamicPropertiesStore.getAllowSameTokenName() == 0) {
exchangeStore.put(exchangeCapsule.createDbKey(), exchangeCapsule);
ExchangeCapsule exchangeCapsuleV2 = new ExchangeCapsule(exchangeCapsule.getData());
exchangeCapsuleV2.resetTokenWithID(assetIssueStore, dynamicPropertiesStore);
exchangeV2Store.put(exchangeCapsuleV2.createDbKey(), exchangeCapsuleV2);
} else {
exchangeV2Store.put(exchangeCapsule.createDbKey(), exchangeCapsule);
}
}
public static AssetIssueStore getAssetIssueStoreFinal(
DynamicPropertiesStore dynamicPropertiesStore,
AssetIssueStore assetIssueStore, AssetIssueV2Store assetIssueV2Store) {
if (dynamicPropertiesStore.getAllowSameTokenName() == 0) {
return assetIssueStore;
} else {
return assetIssueV2Store;
}
}
public static void adjustAssetBalanceV2(AccountCapsule account, String AssetID, long amount,
AccountStore accountStore, AssetIssueStore assetIssueStore,
DynamicPropertiesStore dynamicPropertiesStore)
throws BalanceInsufficientException {
if (amount < 0) {
if (!account.reduceAssetAmountV2(AssetID.getBytes(), -amount, dynamicPropertiesStore,
assetIssueStore)) {
throw new BalanceInsufficientException(
String.format("reduceAssetAmount failed! account: %s",
StringUtil.encode58Check(account.createDbKey())));
}
} else if (amount > 0 &&
!account.addAssetAmountV2(AssetID.getBytes(), amount, dynamicPropertiesStore,
assetIssueStore)) {
throw new BalanceInsufficientException(
String.format("addAssetAmount failed! account: %s",
StringUtil.encode58Check(account.createDbKey())));
}
accountStore.put(account.getAddress().toByteArray(), account);
}
public static void adjustTotalShieldedPoolValue(long valueBalance,
DynamicPropertiesStore dynamicPropertiesStore) throws BalanceInsufficientException {
long totalShieldedPoolValue = subtractExact(
dynamicPropertiesStore.getTotalShieldedPoolValue(), valueBalance,
dynamicPropertiesStore.disableJavaLangMath());
if (totalShieldedPoolValue < 0) {
throw new BalanceInsufficientException(String.format(
"total shielded pool value can not below 0, actual: %d", totalShieldedPoolValue));
}
dynamicPropertiesStore.saveTotalShieldedPoolValue(totalShieldedPoolValue);
}
public static void adjustAssetBalanceV2(byte[] accountAddress, String AssetID, long amount
, AccountStore accountStore, AssetIssueStore assetIssueStore,
DynamicPropertiesStore dynamicPropertiesStore)
throws BalanceInsufficientException {
AccountCapsule account = accountStore.getUnchecked(accountAddress);
adjustAssetBalanceV2(account, AssetID, amount, accountStore, assetIssueStore,
dynamicPropertiesStore);
}
}