Skip to content

Commit ad7d152

Browse files
authored
Merge pull request #1360 from tronprotocol/develop
update exchange
2 parents c93f6c0 + 263960e commit ad7d152

35 files changed

+2781
-120
lines changed

src/main/java/org/tron/common/runtime/Runtime.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,16 @@ public void go() throws OutOfSlotTimeException {
532532
deposit.commit();
533533
}
534534
} catch (OutOfResourceException e) {
535-
logger.error(e.getMessage());
535+
logger.error("runtime error is :{}", e.getMessage());
536536
throw new OutOfSlotTimeException(e.getMessage());
537537
} catch (Throwable e) {
538-
result.setException(new RuntimeException("Unknown Throwable"));
539-
logger.error(e.getMessage());
538+
if (Objects.isNull(result.getException())) {
539+
result.setException(new RuntimeException("Unknown Throwable"));
540+
}
540541
if (StringUtils.isEmpty(runtimeError)) {
541-
runtimeError = e.getMessage();
542+
runtimeError = result.getException().getMessage();
542543
}
544+
logger.error("runtime error is :{}", result.getException().getMessage());
543545
}
544546
trace.setBill(result.getEnergyUsed());
545547
}

src/main/java/org/tron/common/runtime/vm/program/Program.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.tron.common.utils.ByteArraySet;
6262
import org.tron.common.utils.ByteUtil;
6363
import org.tron.common.utils.FastByteComparisons;
64-
import org.tron.common.utils.Sha256Hash;
6564
import org.tron.common.utils.Utils;
6665
import org.tron.core.Wallet;
6766
import org.tron.core.actuator.TransferActuator;
@@ -469,7 +468,7 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
469468
this.increaseNonce();
470469
//this.transactionHash = Sha256Hash.hash(transactionHash);
471470
byte[] newAddress = Wallet
472-
.generateContractAddress(rootTransactionId,nonce);
471+
.generateContractAddress(rootTransactionId, nonce);
473472

474473
AccountCapsule existingAddr = getContractState().getAccount(newAddress);
475474
//boolean contractAlreadyExists = existingAddr != null && existingAddr.isContractExist(blockchainConfig);
@@ -744,12 +743,12 @@ this, new DataWord(contextAddress),
744743
}
745744
}
746745

747-
public static void increaseNonce(){
746+
public static void increaseNonce() {
748747
nonce++;
749748
}
750749

751-
public static void resetNonce(){
752-
nonce=0;
750+
public static void resetNonce() {
751+
nonce = 0;
753752
}
754753

755754
public void spendEnergy(long energyValue, String opName) {
@@ -916,7 +915,7 @@ public DataWord getNumber() {
916915
}
917916

918917
public DataWord getDifficulty() {
919-
return new DataWord(0); //invoke.getDifficulty().clone();
918+
return invoke.getDifficulty().clone();
920919
}
921920

922921
public boolean isStaticCall() {

src/main/java/org/tron/common/runtime/vm/program/invoke/ProgramInvokeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public DataWord getNumber() {
225225

226226
/* DIFFICULTY op */
227227
public DataWord getDifficulty() {
228-
return null; //difficulty;
228+
return new DataWord(0); //difficulty;
229229
}
230230

231231
public long getVmShouldEndInUs() {

src/main/java/org/tron/core/actuator/ExchangeCreateActuator.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import com.google.protobuf.Any;
44
import com.google.protobuf.ByteString;
55
import com.google.protobuf.InvalidProtocolBufferException;
6+
import java.util.Arrays;
67
import lombok.extern.slf4j.Slf4j;
78
import org.tron.common.utils.StringUtil;
89
import org.tron.core.Wallet;
910
import org.tron.core.capsule.AccountCapsule;
1011
import org.tron.core.capsule.ExchangeCapsule;
1112
import org.tron.core.capsule.TransactionResultCapsule;
12-
import org.tron.core.config.Parameter.ChainParameters;
1313
import org.tron.core.db.Manager;
1414
import org.tron.core.exception.ContractExeException;
1515
import org.tron.core.exception.ContractValidateException;
@@ -39,13 +39,15 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
3939

4040
long newBalance = accountCapsule.getBalance() - calcFee();
4141

42-
if (firstTokenID == "_".getBytes()) {
42+
accountCapsule.setBalance(newBalance);
43+
44+
if (Arrays.equals(firstTokenID, "_".getBytes())) {
4345
accountCapsule.setBalance(newBalance - firstTokenBalance);
4446
} else {
4547
accountCapsule.reduceAssetAmount(firstTokenID, firstTokenBalance);
4648
}
4749

48-
if (secondTokenID == "_".getBytes()) {
50+
if (Arrays.equals(secondTokenID, "_".getBytes())) {
4951
accountCapsule.setBalance(newBalance - secondTokenBalance);
5052
} else {
5153
accountCapsule.reduceAssetAmount(secondTokenID, secondTokenBalance);
@@ -119,7 +121,7 @@ public boolean validate() throws ContractValidateException {
119121
long firstTokenBalance = contract.getFirstTokenBalance();
120122
long secondTokenBalance = contract.getSecondTokenBalance();
121123

122-
if (firstTokenID == secondTokenID) {
124+
if (Arrays.equals(firstTokenID, secondTokenID)) {
123125
throw new ContractValidateException("cannot exchange same tokens");
124126
}
125127

@@ -132,7 +134,7 @@ public boolean validate() throws ContractValidateException {
132134
throw new ContractValidateException("token balance must less than " + balanceLimit);
133135
}
134136

135-
if (firstTokenID == "_".getBytes()) {
137+
if (Arrays.equals(firstTokenID, "_".getBytes())) {
136138
if (accountCapsule.getBalance() < (firstTokenBalance + calcFee())) {
137139
throw new ContractValidateException("balance is not enough");
138140
}
@@ -142,7 +144,7 @@ public boolean validate() throws ContractValidateException {
142144
}
143145
}
144146

145-
if (secondTokenID == "_".getBytes()) {
147+
if (Arrays.equals(secondTokenID, "_".getBytes())) {
146148
if (accountCapsule.getBalance() < (secondTokenBalance + calcFee())) {
147149
throw new ContractValidateException("balance is not enough");
148150
}
@@ -166,8 +168,4 @@ public long calcFee() {
166168
return dbManager.getDynamicPropertiesStore().getExchangeCreateFee();
167169
}
168170

169-
private boolean validKey(long idx) {
170-
return idx >= 0 && idx < ChainParameters.values().length;
171-
}
172-
173171
}

src/main/java/org/tron/core/actuator/ExchangeInjectActuator.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.protobuf.Any;
44
import com.google.protobuf.ByteString;
55
import com.google.protobuf.InvalidProtocolBufferException;
6+
import java.math.BigInteger;
67
import java.util.Arrays;
78
import lombok.extern.slf4j.Slf4j;
89
import org.tron.common.utils.ByteArray;
@@ -11,7 +12,6 @@
1112
import org.tron.core.capsule.AccountCapsule;
1213
import org.tron.core.capsule.ExchangeCapsule;
1314
import org.tron.core.capsule.TransactionResultCapsule;
14-
import org.tron.core.config.Parameter.ChainParameters;
1515
import org.tron.core.db.Manager;
1616
import org.tron.core.exception.ContractExeException;
1717
import org.tron.core.exception.ContractValidateException;
@@ -65,13 +65,13 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
6565

6666
long newBalance = accountCapsule.getBalance() - calcFee();
6767

68-
if (tokenID == "_".getBytes()) {
68+
if (Arrays.equals(tokenID, "_".getBytes())) {
6969
accountCapsule.setBalance(newBalance - tokenQuant);
7070
} else {
7171
accountCapsule.reduceAssetAmount(tokenID, tokenQuant);
7272
}
7373

74-
if (anotherTokenID == "_".getBytes()) {
74+
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
7575
accountCapsule.setBalance(newBalance - anotherTokenQuant);
7676
} else {
7777
accountCapsule.reduceAssetAmount(anotherTokenID, anotherTokenQuant);
@@ -164,34 +164,41 @@ public boolean validate() throws ContractValidateException {
164164
}
165165

166166
if (tokenQuant <= 0) {
167-
throw new ContractValidateException("injected token balance must greater than zero");
167+
throw new ContractValidateException("injected token quant must greater than zero");
168168
}
169169

170+
BigInteger bigFirstTokenBalance = new BigInteger(String.valueOf(firstTokenBalance));
171+
BigInteger bigSecondTokenBalance = new BigInteger(String.valueOf(secondTokenBalance));
172+
BigInteger bigTokenQuant = new BigInteger(String.valueOf(tokenQuant));
170173
long newTokenBalance, newAnotherTokenBalance;
171174
if (Arrays.equals(tokenID, firstTokenID)) {
172175
anotherTokenID = secondTokenID;
173-
anotherTokenQuant = Math
174-
.floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
176+
// anotherTokenQuant = Math
177+
// .floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
178+
anotherTokenQuant = bigSecondTokenBalance.multiply(bigTokenQuant)
179+
.divide(bigFirstTokenBalance).longValueExact();
175180
newTokenBalance = firstTokenBalance + tokenQuant;
176181
newAnotherTokenBalance = secondTokenBalance + anotherTokenQuant;
177182
} else {
178183
anotherTokenID = firstTokenID;
179-
anotherTokenQuant = Math
180-
.floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
184+
// anotherTokenQuant = Math
185+
// .floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
186+
anotherTokenQuant = bigFirstTokenBalance.multiply(bigTokenQuant)
187+
.divide(bigSecondTokenBalance).longValueExact();
181188
newTokenBalance = secondTokenBalance + tokenQuant;
182189
newAnotherTokenBalance = firstTokenBalance + anotherTokenQuant;
183190
}
184191

185192
if (anotherTokenQuant <= 0) {
186-
throw new ContractValidateException(" The calculated Token Quant must be larger than 0");
193+
throw new ContractValidateException("the calculated token quant must be greater than 0");
187194
}
188195

189196
long balanceLimit = dbManager.getDynamicPropertiesStore().getExchangeBalanceLimit();
190197
if (newTokenBalance > balanceLimit || newAnotherTokenBalance > balanceLimit) {
191198
throw new ContractValidateException("token balance must less than " + balanceLimit);
192199
}
193200

194-
if (tokenID == "_".getBytes()) {
201+
if (Arrays.equals(tokenID, "_".getBytes())) {
195202
if (accountCapsule.getBalance() < (tokenQuant + calcFee())) {
196203
throw new ContractValidateException("balance is not enough");
197204
}
@@ -201,7 +208,7 @@ public boolean validate() throws ContractValidateException {
201208
}
202209
}
203210

204-
if (anotherTokenID == "_".getBytes()) {
211+
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
205212
if (accountCapsule.getBalance() < (anotherTokenQuant + calcFee())) {
206213
throw new ContractValidateException("balance is not enough");
207214
}
@@ -225,8 +232,4 @@ public long calcFee() {
225232
return 0;
226233
}
227234

228-
private boolean validKey(long idx) {
229-
return idx >= 0 && idx < ChainParameters.values().length;
230-
}
231-
232235
}

src/main/java/org/tron/core/actuator/ExchangeTransactionActuator.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.tron.core.capsule.AccountCapsule;
1212
import org.tron.core.capsule.ExchangeCapsule;
1313
import org.tron.core.capsule.TransactionResultCapsule;
14-
import org.tron.core.config.Parameter.ChainParameters;
1514
import org.tron.core.db.Manager;
1615
import org.tron.core.exception.ContractExeException;
1716
import org.tron.core.exception.ContractValidateException;
@@ -55,13 +54,13 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
5554

5655
long newBalance = accountCapsule.getBalance() - calcFee();
5756

58-
if (tokenID == "_".getBytes()) {
57+
if (Arrays.equals(tokenID, "_".getBytes())) {
5958
accountCapsule.setBalance(newBalance - tokenQuant);
6059
} else {
6160
accountCapsule.reduceAssetAmount(tokenID, tokenQuant);
6261
}
6362

64-
if (anotherTokenID == "_".getBytes()) {
63+
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
6564
accountCapsule.setBalance(newBalance + anotherTokenQuant);
6665
} else {
6766
accountCapsule.addAssetAmount(anotherTokenID, anotherTokenQuant);
@@ -145,14 +144,20 @@ public boolean validate() throws ContractValidateException {
145144
throw new ContractValidateException("transaction token balance must greater than zero");
146145
}
147146

147+
if (firstTokenBalance == 0 || secondTokenBalance == 0) {
148+
throw new ContractValidateException("Token balance in exchange is equal with 0,"
149+
+ "the exchange has been closed");
150+
}
151+
148152
long balanceLimit = dbManager.getDynamicPropertiesStore().getExchangeBalanceLimit();
149-
long tokenBalance = (tokenID == firstTokenID ? firstTokenBalance : secondTokenBalance);
153+
long tokenBalance = (Arrays.equals(tokenID, firstTokenID) ? firstTokenBalance
154+
: secondTokenBalance);
150155
tokenBalance += tokenQuant;
151156
if (tokenBalance > balanceLimit) {
152157
throw new ContractValidateException("token balance must less than " + balanceLimit);
153158
}
154159

155-
if (tokenID == "_".getBytes()) {
160+
if (Arrays.equals(tokenID, "_".getBytes())) {
156161
if (accountCapsule.getBalance() < (tokenQuant + calcFee())) {
157162
throw new ContractValidateException("balance is not enough");
158163
}
@@ -181,8 +186,4 @@ public long calcFee() {
181186
return 0;
182187
}
183188

184-
private boolean validKey(long idx) {
185-
return idx >= 0 && idx < ChainParameters.values().length;
186-
}
187-
188189
}

0 commit comments

Comments
 (0)