33import static org .tron .core .actuator .ActuatorConstant .NOT_EXIST_STR ;
44import static org .tron .core .config .Parameter .ChainConstant .FROZEN_PERIOD ;
55import static org .tron .core .config .Parameter .ChainConstant .TRX_PRECISION ;
6+ import static org .tron .protos .contract .Common .ResourceCode ;
7+ import static org .tron .protos .contract .Common .ResourceCode .BANDWIDTH ;
8+ import static org .tron .protos .contract .Common .ResourceCode .ENERGY ;
9+ import static org .tron .protos .contract .Common .ResourceCode .TRON_POWER ;
610
711import com .google .protobuf .ByteString ;
812import com .google .protobuf .InvalidProtocolBufferException ;
@@ -72,44 +76,48 @@ public boolean execute(Object result) throws ContractExeException {
7276 byte [] ownerAddress = freezeBalanceContract .getOwnerAddress ().toByteArray ();
7377 byte [] receiverAddress = freezeBalanceContract .getReceiverAddress ().toByteArray ();
7478
79+ long increment ;
7580 switch (freezeBalanceContract .getResource ()) {
7681 case BANDWIDTH :
7782 if (!ArrayUtils .isEmpty (receiverAddress )
7883 && dynamicStore .supportDR ()) {
79- delegateResource (ownerAddress , receiverAddress , true ,
80- frozenBalance , expireTime );
84+ increment = delegateResource (ownerAddress , receiverAddress , true ,
85+ frozenBalance , expireTime );
8186 accountCapsule .addDelegatedFrozenBalanceForBandwidth (frozenBalance );
8287 } else {
88+ long oldNetWeight = accountCapsule .getFrozenBalance () / TRX_PRECISION ;
8389 long newFrozenBalanceForBandwidth =
8490 frozenBalance + accountCapsule .getFrozenBalance ();
8591 accountCapsule .setFrozenForBandwidth (newFrozenBalanceForBandwidth , expireTime );
92+ long newNetWeight = accountCapsule .getFrozenBalance () / TRX_PRECISION ;
93+ increment = newNetWeight - oldNetWeight ;
8694 }
87- dynamicStore
88- .addTotalNetWeight (frozenBalance / TRX_PRECISION );
95+ addTotalWeight (BANDWIDTH , dynamicStore , frozenBalance , increment );
8996 break ;
9097 case ENERGY :
9198 if (!ArrayUtils .isEmpty (receiverAddress )
9299 && dynamicStore .supportDR ()) {
93- delegateResource (ownerAddress , receiverAddress , false ,
94- frozenBalance , expireTime );
100+ increment = delegateResource (ownerAddress , receiverAddress , false ,
101+ frozenBalance , expireTime );
95102 accountCapsule .addDelegatedFrozenBalanceForEnergy (frozenBalance );
96103 } else {
104+ long oldEnergyWeight = accountCapsule .getEnergyFrozenBalance () / TRX_PRECISION ;
97105 long newFrozenBalanceForEnergy =
98- frozenBalance + accountCapsule .getAccountResource ()
99- .getFrozenBalanceForEnergy ()
100- .getFrozenBalance ();
106+ frozenBalance + accountCapsule .getEnergyFrozenBalance ();
101107 accountCapsule .setFrozenForEnergy (newFrozenBalanceForEnergy , expireTime );
108+ long newEnergyWeight = accountCapsule .getEnergyFrozenBalance () / TRX_PRECISION ;
109+ increment = newEnergyWeight - oldEnergyWeight ;
102110 }
103- dynamicStore
104- .addTotalEnergyWeight (frozenBalance / TRX_PRECISION );
111+ addTotalWeight (ENERGY , dynamicStore , frozenBalance , increment );
105112 break ;
106113 case TRON_POWER :
114+ long oldTPWeight = accountCapsule .getTronPowerFrozenBalance () / TRX_PRECISION ;
107115 long newFrozenBalanceForTronPower =
108116 frozenBalance + accountCapsule .getTronPowerFrozenBalance ();
109117 accountCapsule .setFrozenForTronPower (newFrozenBalanceForTronPower , expireTime );
110-
111- dynamicStore
112- . addTotalTronPowerWeight ( frozenBalance / TRX_PRECISION );
118+ long newTPWeight = accountCapsule . getTronPowerFrozenBalance () / TRX_PRECISION ;
119+ increment = newTPWeight - oldTPWeight ;
120+ addTotalWeight ( TRON_POWER , dynamicStore , frozenBalance , increment );
113121 break ;
114122 default :
115123 logger .debug ("Resource Code Error." );
@@ -123,6 +131,23 @@ public boolean execute(Object result) throws ContractExeException {
123131 return true ;
124132 }
125133
134+ private void addTotalWeight (ResourceCode resourceCode , DynamicPropertiesStore dynamicStore ,
135+ long frozenBalance , long increment ) {
136+ long weight = dynamicStore .allowNewReward () ? increment : frozenBalance / TRX_PRECISION ;
137+ switch (resourceCode ) {
138+ case BANDWIDTH :
139+ dynamicStore .addTotalNetWeight (weight );
140+ break ;
141+ case ENERGY :
142+ dynamicStore .addTotalEnergyWeight (weight );
143+ break ;
144+ case TRON_POWER :
145+ dynamicStore .addTotalTronPowerWeight (weight );
146+ break ;
147+ default :
148+ logger .debug ("Resource Code Error." );
149+ }
150+ }
126151
127152 @ Override
128153 public boolean validate () throws ContractValidateException {
@@ -175,11 +200,6 @@ public boolean validate() throws ContractValidateException {
175200 throw new ContractValidateException ("frozenBalance must be less than accountBalance" );
176201 }
177202
178- // long maxFrozenNumber = dbManager.getDynamicPropertiesStore().getMaxFrozenNumber();
179- // if (accountCapsule.getFrozenCount() >= maxFrozenNumber) {
180- // throw new ContractValidateException("max frozen number is: " + maxFrozenNumber);
181- // }
182-
183203 long frozenDuration = freezeBalanceContract .getFrozenDuration ();
184204 long minFrozenTime = dynamicStore .getMinFrozenTime ();
185205 long maxFrozenTime = dynamicStore .getMaxFrozenTime ();
@@ -262,9 +282,10 @@ public long calcFee() {
262282 return 0 ;
263283 }
264284
265- private void delegateResource (byte [] ownerAddress , byte [] receiverAddress , boolean isBandwidth ,
285+ private long delegateResource (byte [] ownerAddress , byte [] receiverAddress , boolean isBandwidth ,
266286 long balance , long expireTime ) {
267287 AccountStore accountStore = chainBaseManager .getAccountStore ();
288+ DynamicPropertiesStore dynamicPropertiesStore = chainBaseManager .getDynamicPropertiesStore ();
268289 DelegatedResourceStore delegatedResourceStore = chainBaseManager .getDelegatedResourceStore ();
269290 DelegatedResourceAccountIndexStore delegatedResourceAccountIndexStore = chainBaseManager
270291 .getDelegatedResourceAccountIndexStore ();
@@ -292,46 +313,56 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
292313 delegatedResourceStore .put (key , delegatedResourceCapsule );
293314
294315 //modify DelegatedResourceAccountIndexStore
295- {
296- DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
297- .get (ownerAddress );
298- if (delegatedResourceAccountIndexCapsule == null ) {
299- delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule (
316+ if (!dynamicPropertiesStore .supportAllowDelegateOptimization ()) {
317+
318+ DelegatedResourceAccountIndexCapsule ownerIndexCapsule =
319+ delegatedResourceAccountIndexStore .get (ownerAddress );
320+ if (ownerIndexCapsule == null ) {
321+ ownerIndexCapsule = new DelegatedResourceAccountIndexCapsule (
300322 ByteString .copyFrom (ownerAddress ));
301323 }
302- List <ByteString > toAccountsList = delegatedResourceAccountIndexCapsule .getToAccountsList ();
324+ List <ByteString > toAccountsList = ownerIndexCapsule .getToAccountsList ();
303325 if (!toAccountsList .contains (ByteString .copyFrom (receiverAddress ))) {
304- delegatedResourceAccountIndexCapsule .addToAccount (ByteString .copyFrom (receiverAddress ));
326+ ownerIndexCapsule .addToAccount (ByteString .copyFrom (receiverAddress ));
305327 }
306- delegatedResourceAccountIndexStore
307- .put (ownerAddress , delegatedResourceAccountIndexCapsule );
308- }
328+ delegatedResourceAccountIndexStore .put (ownerAddress , ownerIndexCapsule );
309329
310- {
311- DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
312- .get (receiverAddress );
313- if (delegatedResourceAccountIndexCapsule == null ) {
314- delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule (
330+ DelegatedResourceAccountIndexCapsule receiverIndexCapsule
331+ = delegatedResourceAccountIndexStore .get (receiverAddress );
332+ if (receiverIndexCapsule == null ) {
333+ receiverIndexCapsule = new DelegatedResourceAccountIndexCapsule (
315334 ByteString .copyFrom (receiverAddress ));
316335 }
317- List <ByteString > fromAccountsList = delegatedResourceAccountIndexCapsule
336+ List <ByteString > fromAccountsList = receiverIndexCapsule
318337 .getFromAccountsList ();
319338 if (!fromAccountsList .contains (ByteString .copyFrom (ownerAddress ))) {
320- delegatedResourceAccountIndexCapsule .addFromAccount (ByteString .copyFrom (ownerAddress ));
339+ receiverIndexCapsule .addFromAccount (ByteString .copyFrom (ownerAddress ));
321340 }
322- delegatedResourceAccountIndexStore
323- .put (receiverAddress , delegatedResourceAccountIndexCapsule );
341+ delegatedResourceAccountIndexStore .put (receiverAddress , receiverIndexCapsule );
342+
343+ } else {
344+ // modify DelegatedResourceAccountIndexStore new
345+ delegatedResourceAccountIndexStore .convert (ownerAddress );
346+ delegatedResourceAccountIndexStore .convert (receiverAddress );
347+ delegatedResourceAccountIndexStore .delegate (ownerAddress , receiverAddress ,
348+ dynamicPropertiesStore .getLatestBlockHeaderTimestamp ());
324349 }
325350
326351 //modify AccountStore
327352 AccountCapsule receiverCapsule = accountStore .get (receiverAddress );
353+ long oldWeight ;
354+ long newWeight ;
328355 if (isBandwidth ) {
356+ oldWeight = receiverCapsule .getAcquiredDelegatedFrozenBalanceForBandwidth () / TRX_PRECISION ;
329357 receiverCapsule .addAcquiredDelegatedFrozenBalanceForBandwidth (balance );
358+ newWeight = receiverCapsule .getAcquiredDelegatedFrozenBalanceForBandwidth () / TRX_PRECISION ;
330359 } else {
360+ oldWeight = receiverCapsule .getAcquiredDelegatedFrozenBalanceForEnergy () / TRX_PRECISION ;
331361 receiverCapsule .addAcquiredDelegatedFrozenBalanceForEnergy (balance );
362+ newWeight = receiverCapsule .getAcquiredDelegatedFrozenBalanceForEnergy () / TRX_PRECISION ;
332363 }
333-
334364 accountStore .put (receiverCapsule .createDbKey (), receiverCapsule );
365+ return newWeight - oldWeight ;
335366 }
336367
337368}
0 commit comments