Skip to content

Commit a4b97a1

Browse files
authored
Merge pull request #322 from tronprotocol/develop-evan-ParticipateAssetIssue
Develop evan participate asset issue
2 parents d7bcae9 + ad871b6 commit a4b97a1

File tree

5 files changed

+68
-26
lines changed

5 files changed

+68
-26
lines changed

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.protobuf.InvalidProtocolBufferException;
2222
import lombok.extern.slf4j.Slf4j;
2323
import org.joda.time.DateTime;
24+
import org.tron.common.utils.ByteArray;
2425
import org.tron.core.capsule.AccountCapsule;
2526
import org.tron.core.capsule.AssetIssueCapsule;
2627
import org.tron.core.capsule.TransactionResultCapsule;
@@ -57,13 +58,16 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
5758
AssetIssueCapsule assetIssueCapsule =
5859
this.dbManager.getAssetIssueStore()
5960
.get(participateAssetIssueContract.getAssetName().toByteArray());
60-
long exchangeAmount = cost / assetIssueCapsule.getTrxNum() * assetIssueCapsule.getNum();
61+
long exchangeAmount = cost * assetIssueCapsule.getNum() / assetIssueCapsule.getTrxNum();
6162
ownerAccount.addAssetAmount(assetIssueCapsule.getName(), exchangeAmount);
6263
//add to to_address
6364
byte[] toAddressBytes = participateAssetIssueContract.getToAddress().toByteArray();
6465
AccountCapsule toAccount = this.dbManager.getAccountStore().get(toAddressBytes);
6566
toAccount.setBalance(toAccount.getBalance() + cost);
66-
toAccount.reduceAssetAmount(assetIssueCapsule.getName(), exchangeAmount);
67+
if (!toAccount.reduceAssetAmount(assetIssueCapsule.getName(), exchangeAmount)) {
68+
throw new ContractExeException("reduceAssetAmount failed !");
69+
}
70+
6771
//write to db
6872
dbManager.getAccountStore().put(ownerAddressBytes, ownerAccount);
6973
dbManager.getAccountStore().put(toAddressBytes, toAccount);
@@ -96,6 +100,11 @@ public boolean validate() throws ContractValidateException {
96100
throw new ContractValidateException("Trx Num must be positive!");
97101
}
98102

103+
if (participateAssetIssueContract.getOwnerAddress()
104+
.equals(participateAssetIssueContract.getToAddress())) {
105+
throw new ContractValidateException("Cannot participate asset Issue yourself !");
106+
}
107+
99108
byte[] addressBytes = participateAssetIssueContract.getOwnerAddress().toByteArray();
100109
//Whether the account exist
101110
if (!this.dbManager.getAccountStore().has(addressBytes)) {
@@ -106,33 +115,43 @@ public boolean validate() throws ContractValidateException {
106115
long fee = calcFee();
107116
//Whether the balance is enough
108117
if (ac.getBalance() < participateAssetIssueContract.getAmount() + fee) {
109-
throw new ContractValidateException();
118+
throw new ContractValidateException("No enough balance !");
110119
}
111120

112121
//Whether have the mapping
113122
if (!this.dbManager.getAssetIssueStore()
114123
.has(participateAssetIssueContract.getAssetName().toByteArray())) {
115-
throw new ContractValidateException();
124+
throw new ContractValidateException("No asset named " + ByteArray
125+
.toStr(participateAssetIssueContract.getAssetName().toByteArray()));
116126
}
117-
118-
//Whether the exchange can be processed: to see if the exchange can be the exact int
119-
long cost = participateAssetIssueContract.getAmount();
120127
AssetIssueCapsule assetIssueCapsule =
121128
this.dbManager.getAssetIssueStore()
122129
.get(participateAssetIssueContract.getAssetName().toByteArray());
130+
if (!participateAssetIssueContract.getToAddress()
131+
.equals(assetIssueCapsule.getOwnerAddress())) {
132+
throw new ContractValidateException("The asset is not issued by " + ByteArray
133+
.toHexString(participateAssetIssueContract.getToAddress().toByteArray()));
134+
}
135+
//Whether the exchange can be processed: to see if the exchange can be the exact int
136+
long cost = participateAssetIssueContract.getAmount();
137+
123138
DateTime now = DateTime.now();
124139
if (now.getMillis() >= assetIssueCapsule.getEndTime() || now.getMillis() < assetIssueCapsule
125140
.getStartTime()) {
126141
throw new ContractValidateException("No longer valid period!");
127142
}
128143
int trxNum = assetIssueCapsule.getTrxNum();
129144
int num = assetIssueCapsule.getNum();
130-
long exchangeAmount = cost / trxNum * num;
131-
float preciseExchangeAmount = (float) cost / (float) trxNum * (float) num;
132-
if (preciseExchangeAmount - exchangeAmount >= 0.000001f
133-
|| preciseExchangeAmount - exchangeAmount <= -0.000001f) {
145+
long exchangeAmount = cost * num / trxNum;
146+
if (exchangeAmount == 0) {
134147
throw new ContractValidateException("Can not process the exchange!");
135148
}
149+
AccountCapsule toAccount = this.dbManager.getAccountStore()
150+
.get(participateAssetIssueContract.getToAddress().toByteArray());
151+
if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
152+
throw new ContractValidateException("Asset balance is not enough !");
153+
}
154+
136155
} catch (InvalidProtocolBufferException e) {
137156
throw new ContractValidateException();
138157
}

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

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

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

75-
long balance = accountCapsule.getBalance();
76-
long laststOperationTime = accountCapsule.getLatestOperationTime();
79+
long balance = ownerAccount.getBalance();
80+
long laststOperationTime = ownerAccount.getLatestOperationTime();
7781
long now = System.currentTimeMillis();
78-
82+
//TODO:
7983
//if (now - laststOperationTime < balance) {
8084
//throw new ContractValidateException();
8185
//}
8286

83-
{
84-
if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
85-
throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
86-
}
87-
}
88-
8987
// if account with to_address is not existed, create it.
9088
ByteString toAddress = transferContract.getToAddress();
9189
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/main/java/org/tron/core/capsule/AccountCapsule.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ public long getShare() {
175175
return this.account.getBalance();
176176
}
177177

178+
/**
179+
* asset balance enough
180+
*/
181+
public boolean assetBalanceEnough(ByteString name, long amount) {
182+
Map<String, Long> assetMap = this.account.getAssetMap();
183+
String nameKey = ByteArray.toStr(name.toByteArray());
184+
Long currentAmount = assetMap.get(nameKey);
185+
186+
if (amount > 0 && null != currentAmount && amount <= currentAmount) {
187+
return true;
188+
}
189+
return false;
190+
}
191+
192+
178193
/**
179194
* reduce asset amount.
180195
*/

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)