forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.java
More file actions
659 lines (554 loc) · 22.9 KB
/
Runtime.java
File metadata and controls
659 lines (554 loc) · 22.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package org.tron.common.runtime;
import static com.google.common.primitives.Longs.max;
import static com.google.common.primitives.Longs.min;
import static org.apache.commons.lang3.ArrayUtils.isEmpty;
import static org.tron.common.runtime.utils.MUtil.transfer;
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_CONSTANT_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_NORMAL_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_PRE_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_UNKNOWN_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.TrxType.TRX_CONTRACT_CALL_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.TrxType.TRX_CONTRACT_CREATION_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.TrxType.TRX_PRECOMPILED_TYPE;
import static org.tron.common.runtime.vm.program.InternalTransaction.TrxType.TRX_UNKNOWN_TYPE;
import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.tron.common.runtime.config.SystemProperties;
import org.tron.common.runtime.vm.PrecompiledContracts;
import org.tron.common.runtime.vm.VM;
import org.tron.common.runtime.vm.program.InternalTransaction;
import org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType;
import org.tron.common.runtime.vm.program.Program;
import org.tron.common.runtime.vm.program.Program.OutOfResourceException;
import org.tron.common.runtime.vm.program.ProgramPrecompile;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.runtime.vm.program.invoke.ProgramInvoke;
import org.tron.common.runtime.vm.program.invoke.ProgramInvokeFactory;
import org.tron.common.storage.Deposit;
import org.tron.common.storage.DepositImpl;
import org.tron.core.Constant;
import org.tron.core.Wallet;
import org.tron.core.actuator.Actuator;
import org.tron.core.actuator.ActuatorFactory;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BytesCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.db.CpuProcessor;
import org.tron.core.db.StorageMarket;
import org.tron.core.db.TransactionTrace;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.OutOfSlotTimeException;
import org.tron.protos.Contract;
import org.tron.protos.Contract.CreateSmartContract;
import org.tron.protos.Contract.TriggerSmartContract;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.SmartContract;
import org.tron.protos.Protocol.SmartContract.ABI;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
/**
* @author Guo Yonggang
* @since 28.04.2018
*/
@Slf4j(topic = "Runtime")
public class Runtime {
SystemProperties config;
private Transaction trx;
private Block block = null;
private Deposit deposit;
private ProgramInvokeFactory programInvokeFactory = null;
private String runtimeError;
private boolean readyToExecute = false;
private CpuProcessor cpuProcessor = null;
private StorageMarket storageMarket = null;
PrecompiledContracts.PrecompiledContract precompiledContract = null;
private ProgramResult result = new ProgramResult();
private VM vm = null;
private Program program = null;
private InternalTransaction.TrxType trxType = TRX_UNKNOWN_TYPE;
private ExecutorType executorType = ET_UNKNOWN_TYPE;
//tx trace
private TransactionTrace trace;
/**
* For block's trx run
*/
public Runtime(TransactionTrace trace, Block block, Deposit deosit,
ProgramInvokeFactory programInvokeFactory) {
this.trace = trace;
this.trx = trace.getTrx().getInstance();
if (Objects.nonNull(block)) {
this.block = block;
this.executorType = ET_NORMAL_TYPE;
} else {
this.block = Block.newBuilder().build();
this.executorType = ET_PRE_TYPE;
}
this.deposit = deosit;
this.programInvokeFactory = programInvokeFactory;
this.cpuProcessor = new CpuProcessor(deposit.getDbManager());
this.storageMarket = new StorageMarket(deposit.getDbManager());
Transaction.Contract.ContractType contractType = this.trx.getRawData().getContract(0).getType();
switch (contractType.getNumber()) {
case ContractType.TriggerSmartContract_VALUE:
trxType = TRX_CONTRACT_CALL_TYPE;
break;
case ContractType.CreateSmartContract_VALUE:
trxType = TRX_CONTRACT_CREATION_TYPE;
break;
default:
trxType = TRX_PRECOMPILED_TYPE;
}
}
/**
* For pre trx run
*/
@Deprecated
public Runtime(Transaction tx, DepositImpl deposit, ProgramInvokeFactory programInvokeFactory) {
this.trx = tx;
this.deposit = deposit;
this.programInvokeFactory = programInvokeFactory;
this.executorType = ET_PRE_TYPE;
Transaction.Contract.ContractType contractType = tx.getRawData().getContract(0).getType();
switch (contractType.getNumber()) {
case Transaction.Contract.ContractType.TriggerSmartContract_VALUE:
trxType = TRX_CONTRACT_CALL_TYPE;
break;
case Transaction.Contract.ContractType.CreateSmartContract_VALUE:
trxType = TRX_CONTRACT_CREATION_TYPE;
break;
default:
trxType = TRX_PRECOMPILED_TYPE;
}
}
/**
* For constant trx
*/
@Deprecated
public Runtime(Transaction tx, ProgramInvokeFactory programInvokeFactory, Deposit deposit) {
trx = tx;
this.deposit = deposit;
this.programInvokeFactory = programInvokeFactory;
executorType = ET_CONSTANT_TYPE;
trxType = TRX_CONTRACT_CALL_TYPE;
}
/**
* For constant trx with latest block.
*/
public Runtime(Transaction tx, Block block, DepositImpl deposit,
ProgramInvokeFactory programInvokeFactory) {
this.trx = tx;
this.deposit = deposit;
this.programInvokeFactory = programInvokeFactory;
this.executorType = ET_PRE_TYPE;
this.block = block;
this.cpuProcessor = new CpuProcessor(deposit.getDbManager());
this.storageMarket = new StorageMarket(deposit.getDbManager());
Transaction.Contract.ContractType contractType = tx.getRawData().getContract(0).getType();
switch (contractType.getNumber()) {
case Transaction.Contract.ContractType.TriggerSmartContract_VALUE:
trxType = TRX_CONTRACT_CALL_TYPE;
break;
case Transaction.Contract.ContractType.CreateSmartContract_VALUE:
trxType = TRX_CONTRACT_CREATION_TYPE;
break;
default:
trxType = TRX_PRECOMPILED_TYPE;
}
}
public void precompiled() throws ContractValidateException, ContractExeException {
TransactionCapsule trxCap = new TransactionCapsule(trx);
final List<Actuator> actuatorList = ActuatorFactory
.createActuator(trxCap, deposit.getDbManager());
for (Actuator act : actuatorList) {
act.validate();
act.execute(result.getRet());
}
}
/**
*/
public void init() {
readyToExecute = true;
// switch (trxType) {
// case TRX_PRECOMPILED_TYPE:
// readyToExecute = true;
// break;
// case TRX_CONTRACT_CREATION_TYPE:
// case TRX_CONTRACT_CALL_TYPE:
// // if (!curCPULimitReachedBlockCPULimit()) {
// // readyToExecute = true;
// // }
// readyToExecute = true;
// break;
// default:
// readyToExecute = true;
// break;
// }
}
public BigInteger getBlockCPULeftInUs() {
// insure block is not null
BigInteger curBlockHaveElapsedCPUInUs =
BigInteger.valueOf(
1000 * (DateTime.now().getMillis() - block.getBlockHeader().getRawData()
.getTimestamp())); // us
BigInteger curBlockCPULimitInUs = BigInteger.valueOf((long)
(1000 * ChainConstant.BLOCK_PRODUCED_INTERVAL * 0.5
* ChainConstant.BLOCK_PRODUCED_TIME_OUT
/ 100)); // us
return curBlockCPULimitInUs.subtract(curBlockHaveElapsedCPUInUs);
}
public boolean curCPULimitReachedBlockCPULimit() {
if (executorType == ET_NORMAL_TYPE) {
BigInteger blockCPULeftInUs = getBlockCPULeftInUs();
BigInteger oneTxCPULimitInUs = BigInteger
.valueOf(Constant.MAX_CPU_TIME_OF_ONE_TX);
// TODO get from account
BigInteger increasedStorageLimit = BigInteger.valueOf(10000000);
boolean cumulativeCPUReached =
oneTxCPULimitInUs.compareTo(blockCPULeftInUs) > 0;
if (cumulativeCPUReached) {
logger.error("cumulative CPU Reached");
return true;
}
}
return false;
}
private long getAccountCPULimitInUs(AccountCapsule account,
long limitInDrop, long maxCpuInUsByAccount) {
CpuProcessor cpuProcessor = new CpuProcessor(this.deposit.getDbManager());
long cpuInUsFromFreeze = cpuProcessor.getAccountLeftCpuInUsFromFreeze(account);
long cpuInUsFromDrop = Math.floorDiv(limitInDrop, Constant.SUN_PER_GAS);
return min(maxCpuInUsByAccount, max(cpuInUsFromFreeze, cpuInUsFromDrop)); // us
}
private long getAccountCPULimitInUsByPercent(AccountCapsule creator, AccountCapsule sender,
TriggerSmartContract contract, long maxCpuInUsBySender, long limitInDrop) {
long senderCpuLimit = getAccountCPULimitInUs(sender, limitInDrop,
maxCpuInUsBySender);
if (Arrays.equals(creator.getAddress().toByteArray(), sender.getAddress().toByteArray())) {
return senderCpuLimit;
}
CpuProcessor cpuProcessor = new CpuProcessor(this.deposit.getDbManager());
long creatorCpuFromFrozen = cpuProcessor.getAccountLeftCpuInUsFromFreeze(creator);
SmartContract smartContract = this.deposit
.getContract(contract.getContractAddress().toByteArray()).getInstance();
double consumeUserResourcePercent = smartContract.getConsumeUserResourcePercent() * 1.0 / 100;
if (consumeUserResourcePercent >= 1.0) {
consumeUserResourcePercent = 1.0;
}
if (consumeUserResourcePercent <= 0.0) {
consumeUserResourcePercent = 0.0;
}
if (consumeUserResourcePercent <= 0.0) {
return creatorCpuFromFrozen;
}
if (creatorCpuFromFrozen * consumeUserResourcePercent
>= (1 - consumeUserResourcePercent) * senderCpuLimit) {
return (long) (senderCpuLimit / consumeUserResourcePercent);
} else {
return Math.addExact(senderCpuLimit, creatorCpuFromFrozen);
}
}
public void execute() throws ContractValidateException, ContractExeException {
if (!readyToExecute) {
return;
}
switch (trxType) {
case TRX_PRECOMPILED_TYPE:
precompiled();
break;
case TRX_CONTRACT_CREATION_TYPE:
create();
break;
case TRX_CONTRACT_CALL_TYPE:
call();
break;
default:
break;
}
}
private long getGasLimit(AccountCapsule account, long feeLimit) {
// will change the name from us to gas
// can change the calc way
long cpuGasFromFreeze = cpuProcessor.getAccountLeftCpuInUsFromFreeze(account);
long cpuGasFromBalance = Math.floorDiv(account.getBalance(), Constant.SUN_PER_GAS);
long cpuGasFromFeeLimit;
long balanceForCpuFreeze = account.getAccountResource().getFrozenBalanceForCpu()
.getFrozenBalance();
if (0 == balanceForCpuFreeze) {
cpuGasFromFeeLimit = feeLimit / Constant.SUN_PER_GAS;
} else {
long totalCpuGasFromFreeze = cpuProcessor.calculateGlobalCpuLimit(balanceForCpuFreeze);
long leftBalanceForCpuFreeze =
Math.multiplyExact(cpuGasFromFreeze, balanceForCpuFreeze) / totalCpuGasFromFreeze;
if (leftBalanceForCpuFreeze >= feeLimit) {
cpuGasFromFeeLimit =
Math.multiplyExact(totalCpuGasFromFreeze, feeLimit) / balanceForCpuFreeze;
} else {
cpuGasFromFeeLimit = Math
.addExact(cpuGasFromFreeze,
(feeLimit - leftBalanceForCpuFreeze) / Constant.SUN_PER_GAS);
}
}
return min(Math.addExact(cpuGasFromFreeze, cpuGasFromBalance), cpuGasFromFeeLimit);
}
private long getGasLimit(AccountCapsule creator, AccountCapsule caller,
TriggerSmartContract contract, long feeLimit) {
long callerGasLimit = getGasLimit(caller, feeLimit);
if (Arrays.equals(creator.getAddress().toByteArray(), caller.getAddress().toByteArray())) {
return callerGasLimit;
}
// creatorCpuGasFromFreeze
long creatorGasLimit = cpuProcessor.getAccountLeftCpuInUsFromFreeze(creator);
SmartContract smartContract = this.deposit
.getContract(contract.getContractAddress().toByteArray()).getInstance();
long consumeUserResourcePercent = smartContract.getConsumeUserResourcePercent();
if (consumeUserResourcePercent >= 100) {
consumeUserResourcePercent = 100;
}
if (consumeUserResourcePercent <= 0) {
consumeUserResourcePercent = 0;
}
if (consumeUserResourcePercent <= 0) {
return creatorGasLimit;
}
if (creatorGasLimit * consumeUserResourcePercent
>= (100 - consumeUserResourcePercent) * callerGasLimit) {
return 100 * Math.floorDiv(callerGasLimit, consumeUserResourcePercent);
} else {
return Math.addExact(callerGasLimit, creatorGasLimit);
}
}
/*
**/
private void create()
throws ContractExeException, ContractValidateException {
CreateSmartContract contract = ContractCapsule.getSmartContractFromTransaction(trx);
SmartContract newSmartContract = contract.getNewContract();
byte[] code = newSmartContract.getBytecode().toByteArray();
byte[] contractAddress = Wallet.generateContractAddress(trx);
byte[] ownerAddress = contract.getOwnerAddress().toByteArray();
long percent = contract.getNewContract().getConsumeUserResourcePercent();
if (percent < 0 || percent > 100) {
throw new ContractExeException("percent must be >= 0 and <= 100");
}
// insure one owner just have one contract
// if (this.deposit.getContractByNormalAccount(ownerAddress) != null) {
// logger.error("Trying to create second contract with one account: address: " + Wallet
// .encode58Check(ownerAddress));
// return;
// }
// insure the new contract address haven't exist
if (deposit.getAccount(contractAddress) != null) {
logger.error("Trying to create a contract with existing contract address: " + Wallet
.encode58Check(contractAddress));
return;
}
newSmartContract = newSmartContract.toBuilder()
.setContractAddress(ByteString.copyFrom(contractAddress)).build();
// create vm to constructor smart contract
try {
AccountCapsule creator = this.deposit
.getAccount(newSmartContract.getOriginAddress().toByteArray());
// if (executorType == ET_NORMAL_TYPE) {
// long blockCPULeftInUs = getBlockCPULeftInUs().longValue();
// thisTxCPULimitInUs = min(blockCPULeftInUs,
// Constant.CPU_LIMIT_IN_ONE_TX_OF_SMART_CONTRACT);
// } else {
// thisTxCPULimitInUs = Constant.CPU_LIMIT_IN_ONE_TX_OF_SMART_CONTRACT;
// }
long thisTxCPULimitInUs;
if (ET_NORMAL_TYPE == executorType) {
thisTxCPULimitInUs = Constant.MAX_CPU_TIME_OF_ONE_TX_WHEN_VERIFY_BLOCK;
} else {
thisTxCPULimitInUs = Constant.MAX_CPU_TIME_OF_ONE_TX;
}
long vmStartInUs = System.nanoTime() / 1000;
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;
long feeLimit = trx.getRawData().getFeeLimit();
long gasLimit = getGasLimit(creator, feeLimit);
byte[] ops = newSmartContract.getBytecode().toByteArray();
InternalTransaction internalTransaction = new InternalTransaction(trx);
// todo: callvalue should pass into this function
ProgramInvoke programInvoke = programInvokeFactory
.createProgramInvoke(TRX_CONTRACT_CREATION_TYPE, executorType, trx,
block, deposit, vmStartInUs, vmShouldEndInUs, gasLimit);
this.vm = new VM(config);
this.program = new Program(ops, programInvoke, internalTransaction, config);
} catch (Exception e) {
logger.error(e.getMessage());
return;
}
program.getResult().setContractAddress(contractAddress);
deposit.createAccount(contractAddress, newSmartContract.getName(),
Protocol.AccountType.Contract);
deposit.createContract(contractAddress, new ContractCapsule(newSmartContract));
deposit.saveCode(contractAddress, ProgramPrecompile.getCode(code));
deposit.createContractByNormalAccountIndex(ownerAddress, new BytesCapsule(contractAddress));
// transfer from callerAddress to contractAddress according to callValue
byte[] callerAddress = contract.getOwnerAddress().toByteArray();
long callValue = newSmartContract.getCallValue();
if (callValue > 0) {
transfer(this.deposit, callerAddress, contractAddress, callValue);
}
}
/**
* **
*/
private void call()
throws ContractValidateException {
Contract.TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
if (contract == null) {
return;
}
byte[] contractAddress = contract.getContractAddress().toByteArray();
byte[] code = this.deposit.getCode(contractAddress);
if (isEmpty(code)) {
} else {
AccountCapsule caller = this.deposit.getAccount(contract.getOwnerAddress().toByteArray());
AccountCapsule creator = this.deposit.getAccount(
this.deposit.getContract(contractAddress).getInstance()
.getOriginAddress().toByteArray());
long thisTxCPULimitInUs;
if (ET_NORMAL_TYPE == executorType) {
thisTxCPULimitInUs = Constant.MAX_CPU_TIME_OF_ONE_TX_WHEN_VERIFY_BLOCK;
} else {
thisTxCPULimitInUs = Constant.MAX_CPU_TIME_OF_ONE_TX;
}
long vmStartInUs = System.nanoTime() / 1000;
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;
long feeLimit = trx.getRawData().getFeeLimit();
long gasLimit = getGasLimit(creator, caller, contract, feeLimit);
if (isCallConstant(contractAddress)) {
gasLimit = Constant.MAX_GAS_IN_TX;
}
ProgramInvoke programInvoke = programInvokeFactory
.createProgramInvoke(TRX_CONTRACT_CALL_TYPE, executorType, trx,
block, deposit, vmStartInUs, vmShouldEndInUs, gasLimit);
this.vm = new VM(config);
InternalTransaction internalTransaction = new InternalTransaction(trx);
this.program = new Program(null, code, programInvoke, internalTransaction, config);
}
program.getResult().setContractAddress(contractAddress);
//transfer from callerAddress to targetAddress according to callValue
byte[] callerAddress = contract.getOwnerAddress().toByteArray();
long callValue = contract.getCallValue();
if (callValue > 0) {
transfer(this.deposit, callerAddress, contractAddress, callValue);
}
}
public void go() throws OutOfSlotTimeException {
if (!readyToExecute) {
return;
}
try {
if (vm != null) {
vm.play(program);
result = program.getResult();
if (isCallConstant()) {
long callValue = TransactionCapsule.getCallValue(trx.getRawData().getContract(0));
if (callValue > 0) {
runtimeError = "constant cannot set call value.";
}
return;
}
// todo: consume bandwidth for successful creating contract
if (result.getException() != null || result.isRevert()) {
result.getDeleteAccounts().clear();
result.getLogInfoList().clear();
result.resetFutureRefund();
program.spendAllGas();
spendUsage(0);
if (result.getException() != null) {
runtimeError = result.getException().getMessage();
throw result.getException();
} else {
runtimeError = "REVERT opcode executed";
}
} else {
long usedStorageSize =
deposit.computeAfterRunStorageSize() - deposit.getBeforeRunStorageSize();
if (!spendUsage(usedStorageSize)) {
throw Program.Exception.notEnoughStorage();
}
deposit.commit();
}
} else {
deposit.commit();
}
} catch (OutOfResourceException e) {
logger.error(e.getMessage());
throw new OutOfSlotTimeException(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}
}
private boolean spendUsage(long useedStorageSize) {
long cpuUsage = result.getGasUsed();
ContractCapsule contract = deposit.getContract(result.getContractAddress());
ByteString originAddress = contract.getInstance().getOriginAddress();
AccountCapsule origin = deposit.getAccount(originAddress.toByteArray());
long originResourcePercent = 100 - contract.getConsumeUserResourcePercent();
originResourcePercent = min(originResourcePercent, 100);
originResourcePercent = max(originResourcePercent, 0);
long originCpuUsage = cpuUsage * originResourcePercent / 100;
originCpuUsage = min(originCpuUsage, cpuProcessor.getAccountLeftCpuInUsFromFreeze(origin));
long callerCpuUsage = cpuUsage - originCpuUsage;
if (useedStorageSize <= 0) {
trace.setBill(callerCpuUsage, 0);
return true;
}
long originStorageUsage = useedStorageSize * originResourcePercent / 100;
originStorageUsage = min(originCpuUsage, origin.getStorageLeft());
long callerStorageUsage = useedStorageSize - originStorageUsage;
byte[] callerAddressBytes = TransactionCapsule.getOwner(trx.getRawData().getContract(0));
AccountCapsule caller = deposit.getAccount(callerAddressBytes);
long storageFee = trx.getRawData().getFeeLimit();
long cpuFee = (callerCpuUsage - cpuProcessor.getAccountLeftCpuInUsFromFreeze(caller))
* Constant.SUN_PER_GAS;
if (cpuFee > 0) {
storageFee -= cpuFee;
}
long tryBuyStorage = storageMarket.tryBuyStorage(storageFee);
if (tryBuyStorage + caller.getStorageLeft() < callerStorageUsage) {
trace.setBill(callerCpuUsage, 0);
return false;
}
trace.setBill(callerCpuUsage, callerStorageUsage);
return true;
}
private boolean isCallConstant() {
if (TRX_CONTRACT_CALL_TYPE.equals(trxType)) {
ABI abi = deposit.getContract(result.getContractAddress()).getInstance().getAbi();
if (Wallet.isConstant(abi, ContractCapsule.getTriggerContractFromTransaction(trx))) {
return true;
}
}
return false;
}
private boolean isCallConstant(byte[] address) {
if (TRX_CONTRACT_CALL_TYPE.equals(trxType)) {
ABI abi = deposit.getContract(address).getInstance().getAbi();
if (Wallet.isConstant(abi, ContractCapsule.getTriggerContractFromTransaction(trx))) {
return true;
}
}
return false;
}
public RuntimeSummary finalization() {
return null;
}
public ProgramResult getResult() {
return result;
}
public String getRuntimeError() {
return runtimeError;
}
}