Skip to content

Commit 7251c60

Browse files
authored
fix(acurator): add more check for AssetIssueActuator (#6525)
1 parent 02b28c4 commit 7251c60

5 files changed

Lines changed: 66 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import java.util.List;
2525
import java.util.Objects;
2626
import lombok.extern.slf4j.Slf4j;
27+
import org.tron.common.math.StrictMathWrapper;
2728
import org.tron.common.utils.DecodeUtil;
2829
import org.tron.core.capsule.AccountCapsule;
2930
import org.tron.core.capsule.AssetIssueCapsule;
3031
import org.tron.core.capsule.TransactionResultCapsule;
32+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
3133
import org.tron.core.exception.BalanceInsufficientException;
3234
import org.tron.core.exception.ContractExeException;
3335
import org.tron.core.exception.ContractValidateException;
@@ -263,6 +265,16 @@ public boolean validate() throws ContractValidateException {
263265
"frozenDuration must be less than " + maxFrozenSupplyTime + " days "
264266
+ "and more than " + minFrozenSupplyTime + " days");
265267
}
268+
// make sure FrozenSupply.expireTime not overflow
269+
if (chainBaseManager.getForkController().pass(ForkBlockVersionEnum.VERSION_4_8_1)) {
270+
long frozenPeriod = next.getFrozenDays() * FROZEN_PERIOD;
271+
try {
272+
StrictMathWrapper.addExact(assetIssueContract.getStartTime(), frozenPeriod);
273+
} catch (ArithmeticException e) {
274+
throw new ContractValidateException(
275+
"Start time and frozen days would cause expire time overflow");
276+
}
277+
}
266278
remainSupply -= next.getFrozenAmount();
267279
}
268280

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class CommonParameter {
6767
public boolean debug = false;
6868
@Getter
6969
@Setter
70-
@Parameter(names = {"--min-time-ratio"}, description = "Maximum CPU tolerance when executing "
70+
@Parameter(names = {"--min-time-ratio"}, description = "Minimum CPU tolerance when executing "
7171
+ "timeout transactions while synchronizing blocks. (default: 0.0)")
7272
public double minTimeRatio = 0.0;
7373
@Getter

framework/src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.actuator;
22

33
import static org.junit.Assert.fail;
4+
import static org.tron.core.config.Parameter.ChainConstant.FROZEN_PERIOD;
45

56
import com.google.protobuf.Any;
67
import com.google.protobuf.ByteString;
@@ -15,12 +16,14 @@
1516
import org.junit.Test;
1617
import org.tron.common.BaseTest;
1718
import org.tron.common.utils.ByteArray;
19+
import org.tron.common.utils.ForkController;
1820
import org.tron.core.Constant;
1921
import org.tron.core.Wallet;
2022
import org.tron.core.capsule.AccountCapsule;
2123
import org.tron.core.capsule.AssetIssueCapsule;
2224
import org.tron.core.capsule.TransactionResultCapsule;
2325
import org.tron.core.config.Parameter.ForkBlockVersionConsts;
26+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
2427
import org.tron.core.config.args.Args;
2528
import org.tron.core.exception.ContractExeException;
2629
import org.tron.core.exception.ContractValidateException;
@@ -1868,6 +1871,53 @@ public void SameTokenNameCloseInvalidAccount() {
18681871

18691872
}
18701873

1874+
@Test
1875+
public void issueStartTimeTooBig() {
1876+
long maintenanceTimeInterval = dbManager.getDynamicPropertiesStore()
1877+
.getMaintenanceTimeInterval();
1878+
long hardForkTime =
1879+
((ForkBlockVersionEnum.VERSION_4_0_1.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
1880+
* maintenanceTimeInterval;
1881+
dbManager.getDynamicPropertiesStore()
1882+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
1883+
1884+
// add more check after 4.8.1
1885+
byte[] stats = new byte[27];
1886+
Arrays.fill(stats, (byte) 1);
1887+
dbManager.getDynamicPropertiesStore()
1888+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_1.getValue(), stats);
1889+
boolean flag = ForkController.instance().pass(ForkBlockVersionEnum.VERSION_4_8_1);
1890+
Assert.assertTrue(flag);
1891+
1892+
TransactionResultCapsule ret = new TransactionResultCapsule();
1893+
1894+
// Start time is too big. If it's to large, the account.frozen_supply.expireTime will overflow
1895+
FrozenSupply frozenSupply = FrozenSupply.newBuilder().setFrozenDays(20).setFrozenAmount(100)
1896+
.build();
1897+
long startTime = Long.MAX_VALUE - frozenSupply.getFrozenDays() * FROZEN_PERIOD + 1;
1898+
Any any = Any.pack(
1899+
AssetIssueContract.newBuilder()
1900+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
1901+
.setName(ByteString.copyFromUtf8(NAME)).setTotalSupply(TOTAL_SUPPLY).setTrxNum(TRX_NUM)
1902+
.setNum(NUM)
1903+
.setStartTime(startTime)
1904+
.setEndTime(startTime + 24 * 3600 * 1000)
1905+
.setDescription(ByteString.copyFromUtf8(DESCRIPTION))
1906+
.setUrl(ByteString.copyFromUtf8(URL))
1907+
.setPrecision(3)
1908+
.addFrozenSupply(frozenSupply)
1909+
.build());
1910+
AssetIssueActuator actuator = new AssetIssueActuator();
1911+
actuator.setChainBaseManager(dbManager.getChainBaseManager()).setAny(any);
1912+
1913+
AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
1914+
owner.setBalance(10_000_000_000L);
1915+
dbManager.getAccountStore().put(owner.createDbKey(), owner);
1916+
1917+
processAndCheckInvalid(actuator, ret,
1918+
"Start time and frozen days would cause expire time overflow",
1919+
"Start time and frozen days would cause expire time overflow");
1920+
}
18711921

18721922
@Test
18731923
public void commonErrorCheck() {

protocol/src/main/protos/core/Tron.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ message ChainParameters {
132132
message Account {
133133
/* frozen balance */
134134
message Frozen {
135-
int64 frozen_balance = 1; // the frozen trx balance
135+
int64 frozen_balance = 1; // the frozen trx or asset balance
136136
int64 expire_time = 2; // the expire time
137137
}
138138
// account nick name

protocol/src/main/protos/core/contract/asset_issue_contract.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ message AssetIssueContract {
1010
string id = 41;
1111

1212
message FrozenSupply {
13-
int64 frozen_amount = 1;
13+
int64 frozen_amount = 1; // asset amount
1414
int64 frozen_days = 2;
1515
}
1616
bytes owner_address = 1;
1717
bytes name = 2;
1818
bytes abbr = 3;
1919
int64 total_supply = 4;
2020
repeated FrozenSupply frozen_supply = 5;
21-
int32 trx_num = 6;
21+
int32 trx_num = 6; // The fields trx_num and num define the exchange rate: num tokens can be purchased with trx_num TRX. This avoids using decimals.
2222
int32 precision = 7;
2323
int32 num = 8;
2424
int64 start_time = 9;

0 commit comments

Comments
 (0)