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 .allowNewRewardEnable () ? 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,7 +282,7 @@ 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 ();
268288 DelegatedResourceStore delegatedResourceStore = chainBaseManager .getDelegatedResourceStore ();
@@ -272,13 +292,16 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
272292 //modify DelegatedResourceStore
273293 DelegatedResourceCapsule delegatedResourceCapsule = delegatedResourceStore
274294 .get (key );
295+ long oldWeight ;
275296 if (delegatedResourceCapsule != null ) {
297+ oldWeight = delegatedResourceCapsule .getFrozenBalance (isBandwidth ) / TRX_PRECISION ;
276298 if (isBandwidth ) {
277299 delegatedResourceCapsule .addFrozenBalanceForBandwidth (balance , expireTime );
278300 } else {
279301 delegatedResourceCapsule .addFrozenBalanceForEnergy (balance , expireTime );
280302 }
281303 } else {
304+ oldWeight = 0 ;
282305 delegatedResourceCapsule = new DelegatedResourceCapsule (
283306 ByteString .copyFrom (ownerAddress ),
284307 ByteString .copyFrom (receiverAddress ));
@@ -289,6 +312,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
289312 }
290313
291314 }
315+ long newWeight = delegatedResourceCapsule .getFrozenBalance (isBandwidth ) / TRX_PRECISION ;
292316 delegatedResourceStore .put (key , delegatedResourceCapsule );
293317
294318 //modify DelegatedResourceAccountIndexStore
@@ -332,6 +356,7 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
332356 }
333357
334358 accountStore .put (receiverCapsule .createDbKey (), receiverCapsule );
359+ return newWeight - oldWeight ;
335360 }
336361
337362}
0 commit comments