Skip to content

Commit ad871b6

Browse files
committed
No transfer asset or trx to yourself
1 parent 057894f commit ad871b6

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public boolean validate() throws ContractValidateException {
9797
throw new ContractValidateException("Trx Num must be positive!");
9898
}
9999

100-
if ( participateAssetIssueContract.getOwnerAddress().equals(participateAssetIssueContract.getToAddress())){
100+
if (participateAssetIssueContract.getOwnerAddress()
101+
.equals(participateAssetIssueContract.getToAddress())) {
101102
throw new ContractValidateException("Cannot participate asset Issue yourself !");
102103
}
103104

@@ -142,7 +143,8 @@ public boolean validate() throws ContractValidateException {
142143
if (exchangeAmount == 0) {
143144
throw new ContractValidateException("Can not process the exchange!");
144145
}
145-
AccountCapsule toAccount = this.dbManager.getAccountStore().get(participateAssetIssueContract.getToAddress().toByteArray());
146+
AccountCapsule toAccount = this.dbManager.getAccountStore()
147+
.get(participateAssetIssueContract.getToAddress().toByteArray());
146148
if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
147149
throw new ContractValidateException("Asset balance is not enough !");
148150
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,29 @@ public boolean validate() throws ContractValidateException {
5959
"contract type error,expected type [TransferContract],real type[" + contract
6060
.getClass() + "]");
6161
}
62-
6362
TransferContract transferContract = this.contract.unpack(TransferContract.class);
64-
AccountCapsule ownerAccount = dbManager.getAccountStore()
65-
.get(transferContract.getOwnerAddress().toByteArray());
6663
Preconditions.checkNotNull(transferContract.getOwnerAddress(), "OwnerAddress is null");
6764
Preconditions.checkNotNull(transferContract.getToAddress(), "ToAddress is null");
6865
Preconditions.checkNotNull(transferContract.getAmount(), "Amount is null");
6966

70-
AccountCapsule accountCapsule = dbManager.getAccountStore()
67+
if (transferContract.getOwnerAddress().equals(transferContract.getToAddress())) {
68+
throw new ContractValidateException("Cannot transfer trx to yourself.");
69+
}
70+
if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
71+
throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
72+
}
73+
74+
AccountCapsule ownerAccount = dbManager.getAccountStore()
7175
.get(transferContract.getOwnerAddress().toByteArray());
7276

73-
long balance = accountCapsule.getBalance();
74-
long laststOperationTime = accountCapsule.getLatestOperationTime();
77+
long balance = ownerAccount.getBalance();
78+
long laststOperationTime = ownerAccount.getLatestOperationTime();
7579
long now = System.currentTimeMillis();
76-
80+
//TODO:
7781
//if (now - laststOperationTime < balance) {
7882
//throw new ContractValidateException();
7983
//}
8084

81-
{
82-
if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
83-
throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
84-
}
85-
}
86-
8785
// if account with to_address is not existed, create it.
8886
ByteString toAddress = transferContract.getToAddress();
8987
if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package org.tron.core.actuator;
1717

18+
import com.google.common.base.Preconditions;
1819
import com.google.protobuf.Any;
1920
import com.google.protobuf.ByteString;
2021
import com.google.protobuf.InvalidProtocolBufferException;
@@ -57,7 +58,9 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
5758
long amount = transferAssetContract.getAmount();
5859

5960
AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
60-
ownerAccountCapsule.reduceAssetAmount(assertName, amount);
61+
if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
62+
throw new ContractExeException("reduceAssetAmount failed !");
63+
}
6164
accountStore.put(ownerKey, ownerAccountCapsule);
6265

6366
AccountCapsule toAccountCapsule = accountStore.get(toKey);
@@ -78,6 +81,13 @@ public boolean validate() throws ContractValidateException {
7881
TransferAssetContract transferAssetContract = this.contract
7982
.unpack(TransferAssetContract.class);
8083

84+
Preconditions.checkNotNull(transferAssetContract.getOwnerAddress(), "OwnerAddress is null");
85+
Preconditions.checkNotNull(transferAssetContract.getToAddress(), "ToAddress is null");
86+
Preconditions.checkNotNull(transferAssetContract.getAssetName(), "AssetName is null");
87+
Preconditions.checkNotNull(transferAssetContract.getAmount(), "Amount is null");
88+
if (transferAssetContract.getOwnerAddress().equals(transferAssetContract.getToAddress())) {
89+
throw new ContractValidateException("Cannot transfer asset to yourself.");
90+
}
8191
byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
8292
if (!this.dbManager.getAccountStore().has(ownerKey)) {
8393
throw new ContractValidateException();

src/test/java/org/tron/core/actuator/ParticipateAssetIssueActuatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Any getContract(long count) {
106106

107107
private void initAssetIssue(long startTimestmp, long endTimestmp) {
108108
AssetIssueContract assetIssueContract = AssetIssueContract.newBuilder()
109-
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
109+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS)))
110110
.setName(ByteString.copyFrom(ByteArray.fromString(ASSET_NAME)))
111111
.setTotalSupply(TOTAL_SUPPLY)
112112
.setTrxNum(TRX_NUM)

0 commit comments

Comments
 (0)