Skip to content

Commit

Permalink
No transfer asset or trx to yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
zergweak committed Mar 29, 2018
1 parent 057894f commit ad871b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("Trx Num must be positive!");
}

if ( participateAssetIssueContract.getOwnerAddress().equals(participateAssetIssueContract.getToAddress())){
if (participateAssetIssueContract.getOwnerAddress()
.equals(participateAssetIssueContract.getToAddress())) {
throw new ContractValidateException("Cannot participate asset Issue yourself !");
}

Expand Down Expand Up @@ -142,7 +143,8 @@ public boolean validate() throws ContractValidateException {
if (exchangeAmount == 0) {
throw new ContractValidateException("Can not process the exchange!");
}
AccountCapsule toAccount = this.dbManager.getAccountStore().get(participateAssetIssueContract.getToAddress().toByteArray());
AccountCapsule toAccount = this.dbManager.getAccountStore()
.get(participateAssetIssueContract.getToAddress().toByteArray());
if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
throw new ContractValidateException("Asset balance is not enough !");
}
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/tron/core/actuator/TransferActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,29 @@ public boolean validate() throws ContractValidateException {
"contract type error,expected type [TransferContract],real type[" + contract
.getClass() + "]");
}

TransferContract transferContract = this.contract.unpack(TransferContract.class);
AccountCapsule ownerAccount = dbManager.getAccountStore()
.get(transferContract.getOwnerAddress().toByteArray());
Preconditions.checkNotNull(transferContract.getOwnerAddress(), "OwnerAddress is null");
Preconditions.checkNotNull(transferContract.getToAddress(), "ToAddress is null");
Preconditions.checkNotNull(transferContract.getAmount(), "Amount is null");

AccountCapsule accountCapsule = dbManager.getAccountStore()
if (transferContract.getOwnerAddress().equals(transferContract.getToAddress())) {
throw new ContractValidateException("Cannot transfer trx to yourself.");
}
if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
}

AccountCapsule ownerAccount = dbManager.getAccountStore()
.get(transferContract.getOwnerAddress().toByteArray());

long balance = accountCapsule.getBalance();
long laststOperationTime = accountCapsule.getLatestOperationTime();
long balance = ownerAccount.getBalance();
long laststOperationTime = ownerAccount.getLatestOperationTime();
long now = System.currentTimeMillis();

//TODO:
//if (now - laststOperationTime < balance) {
//throw new ContractValidateException();
//}

{
if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
}
}

// if account with to_address is not existed, create it.
ByteString toAddress = transferContract.getToAddress();
if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/tron/core/actuator/TransferAssetActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.tron.core.actuator;

import com.google.common.base.Preconditions;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -57,7 +58,9 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
long amount = transferAssetContract.getAmount();

AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
ownerAccountCapsule.reduceAssetAmount(assertName, amount);
if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}
accountStore.put(ownerKey, ownerAccountCapsule);

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

Preconditions.checkNotNull(transferAssetContract.getOwnerAddress(), "OwnerAddress is null");
Preconditions.checkNotNull(transferAssetContract.getToAddress(), "ToAddress is null");
Preconditions.checkNotNull(transferAssetContract.getAssetName(), "AssetName is null");
Preconditions.checkNotNull(transferAssetContract.getAmount(), "Amount is null");
if (transferAssetContract.getOwnerAddress().equals(transferAssetContract.getToAddress())) {
throw new ContractValidateException("Cannot transfer asset to yourself.");
}
byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
if (!this.dbManager.getAccountStore().has(ownerKey)) {
throw new ContractValidateException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private Any getContract(long count) {

private void initAssetIssue(long startTimestmp, long endTimestmp) {
AssetIssueContract assetIssueContract = AssetIssueContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS)))
.setName(ByteString.copyFrom(ByteArray.fromString(ASSET_NAME)))
.setTotalSupply(TOTAL_SUPPLY)
.setTrxNum(TRX_NUM)
Expand Down

0 comments on commit ad871b6

Please sign in to comment.