11package org .tron .core .actuator ;
22
33import static org .tron .core .actuator .ActuatorConstant .NOT_EXIST_STR ;
4+ import static org .tron .core .config .Parameter .ChainConstant .BLOCK_PRODUCED_INTERVAL ;
45import static org .tron .core .config .Parameter .ChainConstant .DELEGATE_PERIOD ;
56import static org .tron .core .config .Parameter .ChainConstant .TRX_PRECISION ;
67import static org .tron .protos .contract .Common .ResourceCode ;
@@ -66,7 +67,8 @@ public boolean execute(Object result) throws ContractExeException {
6667 DynamicPropertiesStore dynamicStore = chainBaseManager .getDynamicPropertiesStore ();
6768 long delegateBalance = delegateResourceContract .getBalance ();
6869 boolean lock = delegateResourceContract .getLock ();
69- long lockPeriod = getLockPeriod (dynamicStore , delegateResourceContract );
70+ long lockPeriod = getLockPeriod (dynamicStore .supportMaxDelegateLockPeriod (),
71+ delegateResourceContract );
7072 byte [] receiverAddress = delegateResourceContract .getReceiverAddress ().toByteArray ();
7173
7274 // delegate resource to receiver
@@ -155,14 +157,12 @@ public boolean validate() throws ContractValidateException {
155157
156158 long accountNetUsage = ownerCapsule .getNetUsage ();
157159 if (null != this .getTx () && this .getTx ().isTransactionCreate ()) {
158- accountNetUsage += TransactionUtil .estimateConsumeBandWidthSize (
159- ownerCapsule .getBalance ());
160+ accountNetUsage += TransactionUtil .estimateConsumeBandWidthSize (dynamicStore ,
161+ ownerCapsule .getBalance ());
160162 }
161163 long netUsage = (long ) (accountNetUsage * TRX_PRECISION * ((double )
162164 (dynamicStore .getTotalNetWeight ()) / dynamicStore .getTotalNetLimit ()));
163-
164165 long v2NetUsage = getV2NetUsage (ownerCapsule , netUsage );
165-
166166 if (ownerCapsule .getFrozenV2BalanceForBandwidth () - v2NetUsage < delegateBalance ) {
167167 throw new ContractValidateException (
168168 "delegateBalance must be less than or equal to available FreezeBandwidthV2 balance" );
@@ -175,9 +175,7 @@ public boolean validate() throws ContractValidateException {
175175
176176 long energyUsage = (long ) (ownerCapsule .getEnergyUsage () * TRX_PRECISION * ((double )
177177 (dynamicStore .getTotalEnergyWeight ()) / dynamicStore .getTotalEnergyCurrentLimit ()));
178-
179178 long v2EnergyUsage = getV2EnergyUsage (ownerCapsule , energyUsage );
180-
181179 if (ownerCapsule .getFrozenV2BalanceForEnergy () - v2EnergyUsage < delegateBalance ) {
182180 throw new ContractValidateException (
183181 "delegateBalance must be less than or equal to available FreezeEnergyV2 balance" );
@@ -211,7 +209,7 @@ public boolean validate() throws ContractValidateException {
211209
212210 boolean lock = delegateResourceContract .getLock ();
213211 if (lock && dynamicStore .supportMaxDelegateLockPeriod ()) {
214- long lockPeriod = getLockPeriod (dynamicStore , delegateResourceContract );
212+ long lockPeriod = getLockPeriod (true , delegateResourceContract );
215213 long maxDelegateLockPeriod = dynamicStore .getMaxDelegateLockPeriod ();
216214 if (lockPeriod < 0 || lockPeriod > maxDelegateLockPeriod ) {
217215 throw new ContractValidateException (
@@ -249,20 +247,20 @@ public boolean validate() throws ContractValidateException {
249247 return true ;
250248 }
251249
252- private long getLockPeriod (DynamicPropertiesStore dynamicStore ,
250+ private long getLockPeriod (boolean supportMaxDelegateLockPeriod ,
253251 DelegateResourceContract delegateResourceContract ) {
254252 long lockPeriod = delegateResourceContract .getLockPeriod ();
255- if (dynamicStore . supportMaxDelegateLockPeriod () ) {
256- return lockPeriod == 0 ? DELEGATE_PERIOD / 3000 : lockPeriod ;
253+ if (supportMaxDelegateLockPeriod ) {
254+ return lockPeriod == 0 ? DELEGATE_PERIOD / BLOCK_PRODUCED_INTERVAL : lockPeriod ;
257255 } else {
258- return 0 ;
256+ return DELEGATE_PERIOD / BLOCK_PRODUCED_INTERVAL ;
259257 }
260258 }
261259
262260 private void validRemainTime (ResourceCode resourceCode , long lockPeriod , long expireTime ,
263261 long now ) throws ContractValidateException {
264262 long remainTime = expireTime - now ;
265- if (lockPeriod * 3 * 1000 < remainTime ) {
263+ if (lockPeriod * BLOCK_PRODUCED_INTERVAL < remainTime ) {
266264 throw new ContractValidateException (
267265 "The lock period for " + resourceCode .name () + " this time cannot be less than the "
268266 + "remaining time[" + remainTime + "ms] of the last lock period for "
@@ -295,11 +293,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
295293 //modify DelegatedResourceStore
296294 long expireTime = 0 ;
297295 if (lock ) {
298- if (dynamicPropertiesStore .supportMaxDelegateLockPeriod ()) {
299- expireTime = now + lockPeriod * 3 * 1000 ;
300- } else {
301- expireTime = now + DELEGATE_PERIOD ;
302- }
296+ expireTime = now + lockPeriod * BLOCK_PRODUCED_INTERVAL ;
303297 }
304298 byte [] key = DelegatedResourceCapsule .createDbKeyV2 (ownerAddress , receiverAddress , lock );
305299 DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore .get (key );
0 commit comments