Skip to content

Commit 36e301b

Browse files
committed
improve spend cpu and storage in method go().
1 parent def1e3e commit 36e301b

3 files changed

Lines changed: 53 additions & 17 deletions

File tree

‎src/main/java/org/tron/common/runtime/Runtime.java‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ public void go() {
475475
if (isCallConstant()) {
476476
return;
477477
}
478-
spendUsage(true);
479478

480479
if (result.getException() != null || result.isRevert()) {
481480
result.getDeleteAccounts().clear();
@@ -500,8 +499,8 @@ public void go() {
500499
if (executerType == ET_NORMAL_TYPE) {
501500
deposit.commit();
502501
}
502+
spendUsage(useedStorageSize);
503503
}
504-
505504
} else {
506505
if (executerType == ET_NORMAL_TYPE) {
507506
deposit.commit();
@@ -511,7 +510,7 @@ public void go() {
511510
logger.error(e.getMessage());
512511
runtimeError = e.getMessage();
513512
} catch (TronException e) {
514-
spendUsage(false);
513+
spendUsage(0);
515514
logger.error(e.getMessage());
516515
runtimeError = e.getMessage();
517516
}
@@ -523,10 +522,10 @@ public void go() {
523522

524523
}
525524

526-
private void spendUsage(boolean spandStorage) {
525+
private void spendUsage(long useedStorageSize) {
527526
cpuProcessor = new CpuProcessor(deposit.getDbManager());
528527
long cpuUsage, storageUsage;
529-
storageUsage = 0;
528+
storageUsage = useedStorageSize;
530529
long now = System.nanoTime() / 1000;
531530
cpuUsage = now - program.getVmStartInUs();
532531
if (executerType == ET_NORMAL_TYPE) {
@@ -542,25 +541,47 @@ private void spendUsage(boolean spandStorage) {
542541
byte[] callerAddressBytes = TransactionCapsule.getOwner(trx.getRawData().getContract(0));
543542
AccountCapsule caller = deposit.getAccount(callerAddressBytes);
544543

545-
spendCpuUsage(cpuUsage, origin, caller);
546-
if (spandStorage) {
547-
spendStorageUsage(storageUsage, origin, caller);
548-
}
544+
spendCpuUsage(cpuUsage, origin, caller, contract.getInstance().getConsumeUserResourcePercent());
545+
546+
spendStorageUsage(storageUsage, origin, caller,
547+
contract.getInstance().getConsumeUserResourcePercent());
549548
}
550549

551-
private void spendCpuUsage(long cpuUsage, AccountCapsule origin, AccountCapsule caller) {
550+
private void spendCpuUsage(long cpuUsage, AccountCapsule origin, AccountCapsule caller,
551+
long consumeUserResourcePercent) {
552+
553+
if (cpuUsage <= 0) {
554+
return;
555+
}
552556

553-
this.cpuProcessor.useCpu(origin, cpuUsage * (100 - 36) / 100,
554-
deposit.getDbManager().getHeadBlockTimeStamp());
555-
this.cpuProcessor
556-
.useCpu(caller, cpuUsage * 36 / 100, deposit.getDbManager().getHeadBlockTimeStamp());
557+
long originUsage =
558+
cpuUsage * (Constant.MAX_CONSUME_USER_RESOURCE_PERCENT - consumeUserResourcePercent)
559+
/ Constant.MAX_CONSUME_USER_RESOURCE_PERCENT;
560+
this.cpuProcessor.useCpu(origin, originUsage, deposit.getDbManager().getHeadBlockTimeStamp());
561+
long callerUsage =
562+
cpuUsage * consumeUserResourcePercent / Constant.MAX_CONSUME_USER_RESOURCE_PERCENT;
563+
this.cpuProcessor.useCpu(caller, callerUsage, deposit.getDbManager().getHeadBlockTimeStamp());
557564

558565
}
559566

560-
private void spendStorageUsage(long storageUsage, AccountCapsule origin, AccountCapsule caller) {
567+
private void spendStorageUsage(long storageUsage, AccountCapsule origin, AccountCapsule caller,
568+
long consumeUserResourcePercent) {
569+
570+
if (storageUsage <= 0) {
571+
return;
572+
}
561573

562-
origin.setStorageUsage(storageUsage * (100 - 36) / 100);
563-
caller.setStorageUsage(storageUsage * 36 / 100);
574+
consumeUserResourcePercent = Long
575+
.max(consumeUserResourcePercent, Constant.MIN_CONSUME_USER_RESOURCE_PERCENT);
576+
consumeUserResourcePercent = Long
577+
.min(consumeUserResourcePercent, Constant.MAX_CONSUME_USER_RESOURCE_PERCENT);
578+
long originUsage =
579+
storageUsage * (Constant.MAX_CONSUME_USER_RESOURCE_PERCENT - consumeUserResourcePercent)
580+
/ Constant.MAX_CONSUME_USER_RESOURCE_PERCENT;
581+
origin.addStorageUsage(originUsage);
582+
long callerUsage =
583+
storageUsage * consumeUserResourcePercent / Constant.MAX_CONSUME_USER_RESOURCE_PERCENT;
584+
caller.addStorageUsage(callerUsage);
564585
}
565586

566587
private boolean isCallConstant() {

‎src/main/java/org/tron/core/Constant.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ public class Constant {
5151
public static final long CPU_LIMIT_IN_ONE_TX_OF_SMART_CONTRACT = 100000; // 100 ms = 100000 us
5252
public static final long STORAGE_LIMIT_IN_ONE_TX_OF_SMART_CONTRACT = 32 * 1024 * 1024L; // 32MB
5353
public static final long CPU_IN_US_PER_TRX = 1000000 / 30; // 1 us <-> 30 SUN <-> 30 * 10^-6 TRX
54+
public static final long MAX_CONSUME_USER_RESOURCE_PERCENT = 100L; // 1 us <-> 30 SUN <-> 30 * 10^-6 TRX
55+
public static final long MIN_CONSUME_USER_RESOURCE_PERCENT = 0L; // 1 us <-> 30 SUN <-> 30 * 10^-6 TRX
56+
5457
}

‎src/main/java/org/tron/core/capsule/AccountCapsule.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,16 @@ public void setLatestExchangeStorageTime(long time) {
544544
.build();
545545
}
546546

547+
public void addStorageUsage(long storageUsage) {
548+
if (storageUsage <= 0) {
549+
return;
550+
}
551+
AccountResource accountResource = this.account.getAccountResource();
552+
accountResource = accountResource.toBuilder()
553+
.setStorageUsage(accountResource.getStorageUsage() + storageUsage).build();
554+
555+
this.account = this.account.toBuilder()
556+
.setAccountResource(accountResource)
557+
.build();
558+
}
547559
}

0 commit comments

Comments
 (0)