33import static com .google .common .primitives .Longs .max ;
44import static com .google .common .primitives .Longs .min ;
55import static org .apache .commons .lang3 .ArrayUtils .isEmpty ;
6+ import static org .tron .common .runtime .utils .MUtil .convertToTronAddress ;
67import static org .tron .common .runtime .utils .MUtil .transfer ;
78import static org .tron .common .runtime .vm .VMUtils .saveProgramTraceFile ;
89import static org .tron .common .runtime .vm .VMUtils .zipAndEncode ;
2526import org .joda .time .DateTime ;
2627import org .spongycastle .util .encoders .Hex ;
2728import org .tron .common .runtime .config .SystemProperties ;
29+ import org .tron .common .runtime .vm .DataWord ;
2830import org .tron .common .runtime .vm .PrecompiledContracts ;
2931import org .tron .common .runtime .vm .VM ;
3032import org .tron .common .runtime .vm .program .InternalTransaction ;
@@ -338,7 +340,7 @@ private long getEnergyLimit(AccountCapsule creator, AccountCapsule caller,
338340
339341 if (creatorEnergyLimit * consumeUserResourcePercent
340342 >= (100 - consumeUserResourcePercent ) * callerEnergyLimit ) {
341- return 100 * Math .floorDiv (callerEnergyLimit , consumeUserResourcePercent );
343+ return Math .floorDiv (callerEnergyLimit * 100 , consumeUserResourcePercent );
342344 } else {
343345 return Math .addExact (callerEnergyLimit , creatorEnergyLimit );
344346 }
@@ -348,6 +350,10 @@ private long getEnergyLimit(AccountCapsule creator, AccountCapsule caller,
348350 **/
349351 private void create ()
350352 throws ContractExeException , ContractValidateException {
353+ if (!deposit .getDbManager ().getDynamicPropertiesStore ().supportVM ()) {
354+ throw new ContractExeException ("vm work is off, need to be opened by the committee" );
355+ }
356+
351357 CreateSmartContract contract = ContractCapsule .getSmartContractFromTransaction (trx );
352358 SmartContract newSmartContract = contract .getNewContract ();
353359
@@ -404,6 +410,7 @@ private void create()
404410 this .program = new Program (ops , programInvoke , internalTransaction , config );
405411 Program .setRootTransactionId (new TransactionCapsule (trx ).getTransactionId ().getBytes ());
406412 Program .resetNonce ();
413+ Program .setRootCallConstant (isCallConstant ());
407414 } catch (Exception e ) {
408415 logger .error (e .getMessage ());
409416 throw new ContractExeException (e .getMessage ());
@@ -432,6 +439,11 @@ private void create()
432439
433440 private void call ()
434441 throws ContractExeException , ContractValidateException {
442+
443+ if (!deposit .getDbManager ().getDynamicPropertiesStore ().supportVM ()) {
444+ throw new ContractExeException ("VM work is off, need to be opened by the committee" );
445+ }
446+
435447 Contract .TriggerSmartContract contract = ContractCapsule .getTriggerContractFromTransaction (trx );
436448 if (contract == null ) {
437449 return ;
@@ -480,6 +492,7 @@ private void call()
480492 this .program = new Program (null , code , programInvoke , internalTransaction , config );
481493 Program .setRootTransactionId (new TransactionCapsule (trx ).getTransactionId ().getBytes ());
482494 Program .resetNonce ();
495+ Program .setRootCallConstant (isCallConstant ());
483496 }
484497
485498 program .getResult ().setContractAddress (contractAddress );
@@ -502,6 +515,7 @@ public void go() throws OutOfSlotTimeException {
502515
503516 program .getResult ().setRet (result .getRet ());
504517 result = program .getResult ();
518+
505519 if (isCallConstant ()) {
506520 long callValue = TransactionCapsule .getCallValue (trx .getRawData ().getContract (0 ));
507521 if (callValue > 0 ) {
@@ -510,8 +524,6 @@ public void go() throws OutOfSlotTimeException {
510524 return ;
511525 }
512526
513- // todo: consume bandwidth for successful creating contract
514-
515527 if (result .getException () != null || result .isRevert ()) {
516528 result .getDeleteAccounts ().clear ();
517529 result .getLogInfoList ().clear ();
@@ -527,7 +539,6 @@ public void go() throws OutOfSlotTimeException {
527539 } else {
528540 deposit .commit ();
529541 }
530-
531542 } else {
532543 deposit .commit ();
533544 }
@@ -555,10 +566,14 @@ private long getEnergyFee(long callerEnergyUsage, long callerEnergyFrozen,
555566 .divide (BigInteger .valueOf (callerEnergyTotal )).longValue ();
556567 }
557568
558- private boolean isCallConstant () {
569+ public boolean isCallConstant () {
570+ TriggerSmartContract triggerContractFromTransaction = ContractCapsule
571+ .getTriggerContractFromTransaction (trx );
559572 if (TRX_CONTRACT_CALL_TYPE .equals (trxType )) {
560- ABI abi = deposit .getContract (result .getContractAddress ()).getInstance ().getAbi ();
561- if (Wallet .isConstant (abi , ContractCapsule .getTriggerContractFromTransaction (trx ))) {
573+ ABI abi = deposit
574+ .getContract (triggerContractFromTransaction .getContractAddress ().toByteArray ())
575+ .getInstance ().getAbi ();
576+ if (Wallet .isConstant (abi , triggerContractFromTransaction )) {
562577 return true ;
563578 }
564579 }
@@ -576,6 +591,10 @@ private boolean isCallConstant(byte[] address) {
576591 }
577592
578593 public void finalization () {
594+ for (DataWord contract : result .getDeleteAccounts ()) {
595+ deposit .deleteContract (convertToTronAddress ((contract .getLast20Bytes ())));
596+ }
597+
579598 if (config .vmTrace () && program != null && result != null ) {
580599 String trace = program .getTrace ()
581600 .result (result .getHReturn ())
0 commit comments