Skip to content

Commit e925fbd

Browse files
committed
re-enable specific transfer validation for vm function
1 parent ddd3014 commit e925fbd

3 files changed

Lines changed: 59 additions & 17 deletions

File tree

‎src/main/java/org/tron/common/runtime/utils/MUtil.java‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.tron.core.Wallet;
99
import org.tron.core.actuator.Actuator;
1010
import org.tron.core.actuator.ActuatorFactory;
11+
import org.tron.core.actuator.TransferActuator;
1112
import org.tron.core.capsule.TransactionCapsule;
1213
import org.tron.core.exception.ContractExeException;
1314
import org.tron.core.exception.ContractValidateException;
@@ -23,7 +24,10 @@ public class MUtil {
2324

2425
public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddress, long amount)
2526
throws ContractValidateException {
26-
transferValidate(deposit, fromAddress, toAddress, amount);
27+
if (!TransferActuator.validate(deposit, fromAddress, toAddress, amount)){
28+
throw new RuntimeException(
29+
Hex.toHexString(fromAddress).toUpperCase() + " transfer to" +Hex.toHexString(toAddress).toUpperCase() +" validation fail!");
30+
}
2731
if (deposit.getBalance(fromAddress) < amount) {
2832
throw new RuntimeException(
2933
Hex.toHexString(fromAddress).toUpperCase() + " not enough balance!");
@@ -35,19 +39,6 @@ public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddres
3539
deposit.addBalance(fromAddress, -amount);
3640
}
3741

38-
public static void transferValidate(Deposit deposit, byte[] fromAddress, byte[] toAddress,
39-
long amount)
40-
throws ContractValidateException {
41-
Contract.TransferContract.Builder builder = Contract.TransferContract.newBuilder();
42-
builder.setAmount(amount);
43-
builder.setOwnerAddress(ByteString.copyFrom(fromAddress));
44-
builder.setToAddress(ByteString.copyFrom(toAddress));
45-
TransferContract contract = builder.build();
46-
TransactionCapsule trx = new TransactionCapsule(contract,
47-
ContractType.TransferContract);
48-
Actuator actuator = ActuatorFactory.createActuator(trx, deposit.getDbManager()).get(0);
49-
actuator.validate();
50-
}
5142

5243
public static void burn(Deposit deposit, byte[] address, long amount) {
5344
if (deposit.getBalance(address) < amount) {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static org.apache.commons.lang3.ArrayUtils.nullToEmpty;
2727
import static org.tron.common.runtime.utils.MUtil.convertToTronAddress;
2828
import static org.tron.common.runtime.utils.MUtil.transfer;
29-
import static org.tron.common.runtime.utils.MUtil.transferValidate;
3029
import static org.tron.common.utils.BIUtil.isPositive;
3130
import static org.tron.common.utils.BIUtil.toBI;
3231

@@ -65,6 +64,7 @@
6564
import org.tron.common.utils.FastByteComparisons;
6665
import org.tron.common.utils.Sha256Hash;
6766
import org.tron.common.utils.Utils;
67+
import org.tron.core.actuator.TransferActuator;
6868
import org.tron.core.capsule.AccountCapsule;
6969
import org.tron.core.capsule.BlockCapsule;
7070
import org.tron.core.config.args.Args;
@@ -462,7 +462,10 @@ public void createContract(DataWord value, DataWord memStart, DataWord memSize)
462462
// [4] TRANSFER THE BALANCE
463463
long newBalance = 0L;
464464
if (!byTestingSuite()) {
465-
transferValidate(deposit, senderAddress, newAddress, endowment);
465+
if (!TransferActuator.validate(deposit, senderAddress, newAddress, endowment)){
466+
throw new RuntimeException(
467+
Hex.toHexString(senderAddress).toUpperCase() + " transfer to" +Hex.toHexString(newAddress).toUpperCase() +" validation fail!");
468+
}
466469
deposit.addBalance(senderAddress, -endowment);
467470
newBalance = deposit.addBalance(newAddress, endowment);
468471
}
@@ -617,7 +620,10 @@ public void callToAddress(MessageCall msg)
617620
msg.getGas().getNoLeadZeroesData(),
618621
msg.getEndowment().getNoLeadZeroesData());
619622
} else if(!ArrayUtils.isEmpty(senderAddress) && !ArrayUtils.isEmpty(contextAddress) && senderAddress != contextAddress && endowment > 0) {
620-
transferValidate(deposit,senderAddress,contextAddress,endowment);
623+
if (!TransferActuator.validate(deposit, senderAddress, contextAddress, endowment)){
624+
throw new RuntimeException(
625+
Hex.toHexString(senderAddress).toUpperCase() + " transfer to" +Hex.toHexString(contextAddress).toUpperCase() +" validation fail!");
626+
}
621627
deposit.addBalance(senderAddress, -endowment);
622628
contextBalance = deposit.addBalance(contextAddress, endowment);
623629
}

‎src/main/java/org/tron/core/actuator/TransferActuator.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,51 @@ public boolean validate() throws ContractValidateException {
134134
return true;
135135
}
136136

137+
public static boolean validate(Deposit deposit, byte[] ownerAddress, byte[] toAddress, long amount) throws ContractValidateException {
138+
if (!Wallet.addressValid(ownerAddress)) {
139+
throw new ContractValidateException("Invalid ownerAddress");
140+
}
141+
if (!Wallet.addressValid(toAddress)) {
142+
throw new ContractValidateException("Invalid toAddress");
143+
}
144+
145+
if (Arrays.equals(toAddress, ownerAddress)) {
146+
throw new ContractValidateException("Cannot transfer trx to yourself.");
147+
}
148+
149+
AccountCapsule ownerAccount = deposit.getAccount(ownerAddress);
150+
if (ownerAccount == null) {
151+
throw new ContractValidateException("Validate InternalTransfer error, no OwnerAccount.");
152+
}
153+
154+
AccountCapsule toAccount = deposit.getAccount(toAddress);
155+
if (toAccount == null) {
156+
throw new ContractValidateException("Validate InternalTransfer error, no ToAccount. And not allowed to create account in smart contract.");
157+
}
158+
159+
long balance = ownerAccount.getBalance();
160+
161+
if (amount <= 0) {
162+
throw new ContractValidateException("Amount must greater than 0.");
163+
}
164+
165+
try {
166+
if (balance < amount) {
167+
throw new ContractValidateException(
168+
"Validate InternalTransfer error, balance is not sufficient.");
169+
}
170+
171+
if (toAccount != null) {
172+
long toAddressBalance = Math.addExact(toAccount.getBalance(), amount);
173+
}
174+
} catch (ArithmeticException e) {
175+
logger.debug(e.getMessage(), e);
176+
throw new ContractValidateException(e.getMessage());
177+
}
178+
179+
return true;
180+
}
181+
137182
@Override
138183
public ByteString getOwnerAddress() throws InvalidProtocolBufferException {
139184
return contract.unpack(TransferContract.class).getOwnerAddress();

0 commit comments

Comments
 (0)