@@ -121,16 +121,20 @@ public boolean execute(Object result) throws ContractExeException {
121
121
accountStore .put (receiverCapsule .createDbKey (), receiverCapsule );
122
122
}
123
123
124
+ // transfer lock delegate to unlock
125
+ delegatedResourceStore .unLockExpireResource (ownerAddress , receiverAddress ,
126
+ dynamicStore .getLatestBlockHeaderTimestamp ());
127
+
128
+ byte [] unlockKey = DelegatedResourceCapsule
129
+ .createDbKeyV2 (ownerAddress , receiverAddress );
130
+ DelegatedResourceCapsule unlockResource = delegatedResourceStore
131
+ .get (unlockKey );
132
+
124
133
// modify owner Account
125
134
AccountCapsule ownerCapsule = accountStore .get (ownerAddress );
126
- byte [] key = DelegatedResourceCapsule
127
- .createDbKeyV2 (unDelegateResourceContract .getOwnerAddress ().toByteArray (),
128
- unDelegateResourceContract .getReceiverAddress ().toByteArray ());
129
- DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore
130
- .get (key );
131
135
switch (unDelegateResourceContract .getResource ()) {
132
136
case BANDWIDTH : {
133
- delegatedResourceCapsule .addFrozenBalanceForBandwidth (-unDelegateBalance , 0 );
137
+ unlockResource .addFrozenBalanceForBandwidth (-unDelegateBalance , 0 );
134
138
135
139
ownerCapsule .addDelegatedFrozenV2BalanceForBandwidth (-unDelegateBalance );
136
140
ownerCapsule .addFrozenBalanceForBandwidthV2 (unDelegateBalance );
@@ -146,7 +150,7 @@ public boolean execute(Object result) throws ContractExeException {
146
150
}
147
151
break ;
148
152
case ENERGY : {
149
- delegatedResourceCapsule .addFrozenBalanceForEnergy (-unDelegateBalance , 0 );
153
+ unlockResource .addFrozenBalanceForEnergy (-unDelegateBalance , 0 );
150
154
151
155
ownerCapsule .addDelegatedFrozenV2BalanceForEnergy (-unDelegateBalance );
152
156
ownerCapsule .addFrozenBalanceForEnergyV2 (unDelegateBalance );
@@ -166,14 +170,21 @@ public boolean execute(Object result) throws ContractExeException {
166
170
break ;
167
171
}
168
172
169
- if (delegatedResourceCapsule .getFrozenBalanceForBandwidth () == 0
170
- && delegatedResourceCapsule .getFrozenBalanceForEnergy () == 0 ) {
171
- delegatedResourceStore .delete (key );
173
+ if (unlockResource .getFrozenBalanceForBandwidth () == 0
174
+ && unlockResource .getFrozenBalanceForEnergy () == 0 ) {
175
+ delegatedResourceStore .delete (unlockKey );
176
+ unlockResource = null ;
177
+ } else {
178
+ delegatedResourceStore .put (unlockKey , unlockResource );
179
+ }
172
180
181
+ byte [] lockKey = DelegatedResourceCapsule
182
+ .createLockDbKeyV2 (ownerAddress , receiverAddress );
183
+ DelegatedResourceCapsule lockResource = delegatedResourceStore
184
+ .get (lockKey );
185
+ if (lockResource == null && unlockResource == null ) {
173
186
//modify DelegatedResourceAccountIndexStore
174
187
delegatedResourceAccountIndexStore .unDelegateV2 (ownerAddress , receiverAddress );
175
- } else {
176
- delegatedResourceStore .put (key , delegatedResourceCapsule );
177
188
}
178
189
179
190
accountStore .put (ownerAddress , ownerCapsule );
@@ -244,11 +255,12 @@ public boolean validate() throws ContractValidateException {
244
255
// "Receiver Account[" + readableReceiverAddress + "] does not exist");
245
256
// }
246
257
247
- byte [] key = DelegatedResourceCapsule
248
- .createDbKeyV2 (unDelegateResourceContract .getOwnerAddress ().toByteArray (),
249
- unDelegateResourceContract .getReceiverAddress ().toByteArray ());
258
+ long now = dynamicStore .getLatestBlockHeaderTimestamp ();
259
+ byte [] key = DelegatedResourceCapsule .createDbKeyV2 (ownerAddress , receiverAddress );
250
260
DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore .get (key );
251
- if (delegatedResourceCapsule == null ) {
261
+ byte [] lockKey = DelegatedResourceCapsule .createLockDbKeyV2 (ownerAddress , receiverAddress );
262
+ DelegatedResourceCapsule lockResourceCapsule = delegatedResourceStore .get (lockKey );
263
+ if (delegatedResourceCapsule == null && lockResourceCapsule == null ) {
252
264
throw new ContractValidateException (
253
265
"delegated Resource does not exist" );
254
266
}
@@ -258,24 +270,42 @@ public boolean validate() throws ContractValidateException {
258
270
throw new ContractValidateException ("unDelegateBalance must be more than 0 TRX" );
259
271
}
260
272
switch (unDelegateResourceContract .getResource ()) {
261
- case BANDWIDTH :
262
- if ( delegatedResourceCapsule . getFrozenBalanceForBandwidth () < unDelegateBalance ) {
263
- throw new ContractValidateException ( "insufficient delegatedFrozenBalance(BANDWIDTH), request="
264
- + unDelegateBalance + ", balance=" + delegatedResourceCapsule .getFrozenBalanceForBandwidth () );
273
+ case BANDWIDTH : {
274
+ long delegateBalance = 0 ;
275
+ if ( delegatedResourceCapsule != null ) {
276
+ delegateBalance += delegatedResourceCapsule .getFrozenBalanceForBandwidth ();
265
277
}
266
- break ;
267
- case ENERGY :
268
- if (delegatedResourceCapsule .getFrozenBalanceForEnergy () < unDelegateBalance ) {
278
+ if (lockResourceCapsule != null
279
+ && lockResourceCapsule .getExpireTimeForBandwidth () < now ) {
280
+ delegateBalance += lockResourceCapsule .getFrozenBalanceForBandwidth ();
281
+ }
282
+ if (delegateBalance < unDelegateBalance ) {
283
+ throw new ContractValidateException (
284
+ "insufficient delegatedFrozenBalance(BANDWIDTH), request="
285
+ + unDelegateBalance + ", unlock_balance=" + delegateBalance );
286
+ }
287
+ }
288
+ break ;
289
+ case ENERGY : {
290
+ long delegateBalance = 0 ;
291
+ if (delegatedResourceCapsule != null ) {
292
+ delegateBalance += delegatedResourceCapsule .getFrozenBalanceForEnergy ();
293
+ }
294
+ if (lockResourceCapsule != null
295
+ && lockResourceCapsule .getExpireTimeForEnergy () < now ) {
296
+ delegateBalance += lockResourceCapsule .getFrozenBalanceForEnergy ();
297
+ }
298
+ if (delegateBalance < unDelegateBalance ) {
269
299
throw new ContractValidateException ("insufficient delegateFrozenBalance(Energy), request="
270
- + unDelegateBalance + ", balance =" + delegatedResourceCapsule . getFrozenBalanceForEnergy () );
300
+ + unDelegateBalance + ", unlock_balance =" + delegateBalance );
271
301
}
272
- break ;
302
+ }
303
+ break ;
273
304
default :
274
305
throw new ContractValidateException (
275
306
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]" );
276
307
}
277
308
278
-
279
309
return true ;
280
310
}
281
311
0 commit comments