Skip to content

Commit eb8ffa1

Browse files
committed
feat(freezeV2): optimize proposal validation
1 parent f4acc52 commit eb8ffa1

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

actuator/src/main/java/org/tron/core/utils/ProposalUtil.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,11 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
713713
throw new ContractValidateException(
714714
"Bad chain parameter id [MAX_DELEGATE_LOCK_PERIOD]");
715715
}
716-
if (value <= 0) {
716+
long maxDelegateLockPeriod = dynamicPropertiesStore.getMaxDelegateLockPeriod();
717+
if (value <= maxDelegateLockPeriod || value > 10512000L) {
717718
throw new ContractValidateException(
718-
"This value[MAX_DELEGATE_LOCK_PERIOD] is only allowed to be "
719-
+ "greater than 0");
719+
"This value[MAX_DELEGATE_LOCK_PERIOD] is only allowed to be greater than "
720+
+ maxDelegateLockPeriod + "and less than or equal to 10512000 !");
720721
}
721722
if (dynamicPropertiesStore.getUnfreezeDelayDays() == 0) {
722723
throw new ContractValidateException(
@@ -800,7 +801,7 @@ public enum ProposalType { // current value, value range
800801
DYNAMIC_ENERGY_MAX_FACTOR(75), // 0, [0, 100_000]
801802
ALLOW_TVM_SHANGHAI(76), // 0, 1
802803
ALLOW_CANCEL_ALL_UNFREEZE_V2(77), // 0, 1
803-
MAX_DELEGATE_LOCK_PERIOD(78); // 0, 1
804+
MAX_DELEGATE_LOCK_PERIOD(78); // (86400, 10512000]
804805

805806
private long code;
806807

chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.core.store;
22

3+
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
4+
35
import com.google.protobuf.ByteString;
46
import java.util.Arrays;
57
import java.util.Optional;
@@ -2808,11 +2810,11 @@ public long getMaxDelegateLockPeriod() {
28082810
return Optional.ofNullable(getUnchecked(MAX_DELEGATE_LOCK_PERIOD))
28092811
.map(BytesCapsule::getData)
28102812
.map(ByteArray::toLong)
2811-
.orElse(CommonParameter.getInstance().getMaxDelegateLockPeriod());
2813+
.orElse(DELEGATE_PERIOD / 3000);
28122814
}
28132815

28142816
public boolean supportMaxDelegateLockPeriod() {
2815-
return getMaxDelegateLockPeriod() > 0 && getUnfreezeDelayDays() > 0;
2817+
return (getMaxDelegateLockPeriod() > DELEGATE_PERIOD / 3000) && getUnfreezeDelayDays() > 0;
28162818
}
28172819

28182820
private static class DynamicResourceProperties {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,6 @@ public class CommonParameter {
655655
@Setter
656656
public long allowCancelAllUnfreezeV2;
657657

658-
@Getter
659-
@Setter
660-
public long maxDelegateLockPeriod;
661-
662658
private static double calcMaxTimeRatio() {
663659
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
664660
return 5.0;

framework/src/test/java/org/tron/core/WalletTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertNotEquals;
2424
import static org.junit.Assert.assertNotNull;
25+
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
2526
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
2627
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
2728
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
@@ -792,19 +793,19 @@ public void testGetCanDelegatedMaxSizeBandWidth() {
792793
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
793794
BANDWIDTH.getNumber());
794795
Assert.assertEquals(initBalance - 280L, message.getMaxSize());
795-
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
796+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
796797
}
797798

798799
@Test
799800
public void testGetCanDelegatedMaxSizeBandWidth2() {
800801
chainBaseManager.getDynamicPropertiesStore().saveUnfreezeDelayDays(14);
801-
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(1);
802+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(86401);
802803
freezeBandwidthForOwner();
803804
GrpcAPI.CanDelegatedMaxSizeResponseMessage message = wallet.getCanDelegatedMaxSize(
804805
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
805806
BANDWIDTH.getNumber());
806-
Assert.assertEquals(initBalance - 282L, message.getMaxSize());
807-
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
807+
Assert.assertEquals(initBalance - 284L, message.getMaxSize());
808+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
808809
}
809810

810811
@Test

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertThrows;
55
import static org.junit.Assert.assertTrue;
66
import static org.junit.Assert.fail;
7+
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
78
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
89
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
910
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
@@ -435,7 +436,7 @@ public void testLockedDelegateResourceForBandwidth() {
435436

436437
@Test
437438
public void testMaxDelegateLockPeriodForBandwidthWrongLockPeriod1() {
438-
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(1);
439+
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(86401);
439440
freezeBandwidthForOwner();
440441
long delegateBalance = 1_000_000_000L;
441442
DelegateResourceActuator actuator = new DelegateResourceActuator();
@@ -444,7 +445,7 @@ public void testMaxDelegateLockPeriodForBandwidthWrongLockPeriod1() {
444445
delegateBalance, 370 * 24 * 3600));
445446
assertThrows("The lock period of delegate resources cannot exceed 1 year!",
446447
ContractValidateException.class, actuator::validate);
447-
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
448+
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
448449
}
449450

450451
@Test
@@ -473,7 +474,7 @@ public void testMaxDelegateLockPeriodForBandwidthWrongLockPeriod2() {
473474
assertThrows("The lock period for bandwidth this time cannot be less than the remaining"
474475
+ " time[60000s] of the last lock period for bandwidth!",
475476
ContractValidateException.class, actuator1::validate);
476-
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
477+
dbManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
477478
}
478479

479480
@Test

framework/src/test/java/org/tron/core/actuator/utils/TransactionUtilTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertTrue;
66
import static org.tron.core.capsule.utils.TransactionUtil.isNumber;
7+
import static org.tron.core.config.Parameter.ChainConstant.DELEGATE_PERIOD;
78
import static org.tron.core.utils.TransactionUtil.validAccountId;
89
import static org.tron.core.utils.TransactionUtil.validAccountName;
910
import static org.tron.core.utils.TransactionUtil.validAssetName;
@@ -149,7 +150,7 @@ public void testEstimateConsumeBandWidthSize() {
149150
long estimateConsumeBandWidthSize = TransactionUtil.estimateConsumeBandWidthSize(ownerCapsule,
150151
dbManager.getChainBaseManager());
151152
assertEquals(275L, estimateConsumeBandWidthSize);
152-
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
153+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
153154
}
154155

155156
@Test
@@ -161,7 +162,7 @@ public void testEstimateConsumeBandWidthSize2() {
161162
long estimateConsumeBandWidthSize = TransactionUtil.estimateConsumeBandWidthSize(ownerCapsule,
162163
dbManager.getChainBaseManager());
163164
assertEquals(277L, estimateConsumeBandWidthSize);
164-
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(0);
165+
chainBaseManager.getDynamicPropertiesStore().saveMaxDelegateLockPeriod(DELEGATE_PERIOD / 3000);
165166
}
166167

167168
}

0 commit comments

Comments
 (0)