Skip to content

Commit

Permalink
Merge pull request #322 from tronprotocol/develop-evan-ParticipateAss…
Browse files Browse the repository at this point in the history
…etIssue

Develop evan participate asset issue
  • Loading branch information
zhaohong authored Mar 29, 2018
2 parents d7bcae9 + ad871b6 commit a4b97a1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.tron.common.utils.ByteArray;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
Expand Down Expand Up @@ -57,13 +58,16 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
AssetIssueCapsule assetIssueCapsule =
this.dbManager.getAssetIssueStore()
.get(participateAssetIssueContract.getAssetName().toByteArray());
long exchangeAmount = cost / assetIssueCapsule.getTrxNum() * assetIssueCapsule.getNum();
long exchangeAmount = cost * assetIssueCapsule.getNum() / assetIssueCapsule.getTrxNum();
ownerAccount.addAssetAmount(assetIssueCapsule.getName(), exchangeAmount);
//add to to_address
byte[] toAddressBytes = participateAssetIssueContract.getToAddress().toByteArray();
AccountCapsule toAccount = this.dbManager.getAccountStore().get(toAddressBytes);
toAccount.setBalance(toAccount.getBalance() + cost);
toAccount.reduceAssetAmount(assetIssueCapsule.getName(), exchangeAmount);
if (!toAccount.reduceAssetAmount(assetIssueCapsule.getName(), exchangeAmount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}

//write to db
dbManager.getAccountStore().put(ownerAddressBytes, ownerAccount);
dbManager.getAccountStore().put(toAddressBytes, toAccount);
Expand Down Expand Up @@ -96,6 +100,11 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("Trx Num must be positive!");
}

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

byte[] addressBytes = participateAssetIssueContract.getOwnerAddress().toByteArray();
//Whether the account exist
if (!this.dbManager.getAccountStore().has(addressBytes)) {
Expand All @@ -106,33 +115,43 @@ public boolean validate() throws ContractValidateException {
long fee = calcFee();
//Whether the balance is enough
if (ac.getBalance() < participateAssetIssueContract.getAmount() + fee) {
throw new ContractValidateException();
throw new ContractValidateException("No enough balance !");
}

//Whether have the mapping
if (!this.dbManager.getAssetIssueStore()
.has(participateAssetIssueContract.getAssetName().toByteArray())) {
throw new ContractValidateException();
throw new ContractValidateException("No asset named " + ByteArray
.toStr(participateAssetIssueContract.getAssetName().toByteArray()));
}

//Whether the exchange can be processed: to see if the exchange can be the exact int
long cost = participateAssetIssueContract.getAmount();
AssetIssueCapsule assetIssueCapsule =
this.dbManager.getAssetIssueStore()
.get(participateAssetIssueContract.getAssetName().toByteArray());
if (!participateAssetIssueContract.getToAddress()
.equals(assetIssueCapsule.getOwnerAddress())) {
throw new ContractValidateException("The asset is not issued by " + ByteArray
.toHexString(participateAssetIssueContract.getToAddress().toByteArray()));
}
//Whether the exchange can be processed: to see if the exchange can be the exact int
long cost = participateAssetIssueContract.getAmount();

DateTime now = DateTime.now();
if (now.getMillis() >= assetIssueCapsule.getEndTime() || now.getMillis() < assetIssueCapsule
.getStartTime()) {
throw new ContractValidateException("No longer valid period!");
}
int trxNum = assetIssueCapsule.getTrxNum();
int num = assetIssueCapsule.getNum();
long exchangeAmount = cost / trxNum * num;
float preciseExchangeAmount = (float) cost / (float) trxNum * (float) num;
if (preciseExchangeAmount - exchangeAmount >= 0.000001f
|| preciseExchangeAmount - exchangeAmount <= -0.000001f) {
long exchangeAmount = cost * num / trxNum;
if (exchangeAmount == 0) {
throw new ContractValidateException("Can not process the exchange!");
}
AccountCapsule toAccount = this.dbManager.getAccountStore()
.get(participateAssetIssueContract.getToAddress().toByteArray());
if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
throw new ContractValidateException("Asset balance is not enough !");
}

} catch (InvalidProtocolBufferException e) {
throw new ContractValidateException();
}
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 @@ -61,31 +61,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
15 changes: 15 additions & 0 deletions src/main/java/org/tron/core/capsule/AccountCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ public long getShare() {
return this.account.getBalance();
}

/**
* asset balance enough
*/
public boolean assetBalanceEnough(ByteString name, long amount) {
Map<String, Long> assetMap = this.account.getAssetMap();
String nameKey = ByteArray.toStr(name.toByteArray());
Long currentAmount = assetMap.get(nameKey);

if (amount > 0 && null != currentAmount && amount <= currentAmount) {
return true;
}
return false;
}


/**
* reduce asset amount.
*/
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 a4b97a1

Please sign in to comment.