Repository navigation
Expand file tree
/
Copy pathTransactionTrace.java
More file actions
298 lines (262 loc) · 9.65 KB
/
Copy pathTransactionTrace.java
File metadata and controls
298 lines (262 loc) · 9.65 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
package org.tron.core.db;
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 java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.spongycastle.util.encoders.Hex;
import org.springframework.util.StringUtils;
import org.tron.common.runtime.Runtime;
import org.tron.common.runtime.RuntimeImpl;
import org.tron.common.runtime.vm.program.InternalTransaction;
import org.tron.common.runtime.vm.program.Program.BadJumpDestinationException;
import org.tron.common.runtime.vm.program.Program.IllegalOperationException;
import org.tron.common.runtime.vm.program.Program.JVMStackOverFlowException;
import org.tron.common.runtime.vm.program.Program.OutOfEnergyException;
import org.tron.common.runtime.vm.program.Program.OutOfMemoryException;
import org.tron.common.runtime.vm.program.Program.OutOfTimeException;
import org.tron.common.runtime.vm.program.Program.PrecompiledContractException;
import org.tron.common.runtime.vm.program.Program.StackTooLargeException;
import org.tron.common.runtime.vm.program.Program.StackTooSmallException;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.runtime.vm.program.invoke.ProgramInvokeFactoryImpl;
import org.tron.common.storage.DepositImpl;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.ReceiptCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.exception.BalanceInsufficientException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.ReceiptCheckErrException;
import org.tron.core.exception.VMIllegalException;
import org.tron.protos.Contract.TriggerSmartContract;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.contractResult;
@Slf4j(topic = "TransactionTrace")
public class TransactionTrace {
private TransactionCapsule trx;
private ReceiptCapsule receipt;
private Manager dbManager;
private Runtime runtime;
private EnergyProcessor energyProcessor;
private InternalTransaction.TrxType trxType;
private long txStartTimeInMs;
public TransactionCapsule getTrx() {
return trx;
}
public enum TimeResultType {
NORMAL,
LONG_RUNNING,
OUT_OF_TIME
}
@Getter
@Setter
private TimeResultType timeResultType = TimeResultType.NORMAL;
public TransactionTrace(TransactionCapsule trx, Manager dbManager) {
this.trx = trx;
Transaction.Contract.ContractType contractType = this.trx.getInstance().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;
}
this.dbManager = dbManager;
this.receipt = new ReceiptCapsule(Sha256Hash.ZERO_HASH);
this.energyProcessor = new EnergyProcessor(this.dbManager);
}
private boolean needVM() {
return this.trxType == TRX_CONTRACT_CALL_TYPE || this.trxType == TRX_CONTRACT_CREATION_TYPE;
}
public void init(BlockCapsule blockCap) {
init(blockCap, false);
}
//pre transaction check
public void init(BlockCapsule blockCap, boolean eventPluginLoaded) {
txStartTimeInMs = System.currentTimeMillis();
DepositImpl deposit = DepositImpl.createRoot(dbManager);
runtime = new RuntimeImpl(this, blockCap, deposit, new ProgramInvokeFactoryImpl());
runtime.setEnableEventLinstener(eventPluginLoaded);
}
public void checkIsConstant() throws ContractValidateException, VMIllegalException {
if (runtime.isCallConstant()) {
throw new VMIllegalException("cannot call constant method ");
}
}
//set bill
public void setBill(long energyUsage) {
if (energyUsage < 0) {
energyUsage = 0L;
}
receipt.setEnergyUsageTotal(energyUsage);
}
//set net bill
public void setNetBill(long netUsage, long netFee) {
receipt.setNetUsage(netUsage);
receipt.setNetFee(netFee);
}
public void addNetBill(long netFee) {
receipt.addNetFee(netFee);
}
public void exec()
throws ContractExeException, ContractValidateException, VMIllegalException {
/* VM execute */
runtime.execute();
runtime.go();
if (TRX_PRECOMPILED_TYPE != runtime.getTrxType()) {
if (contractResult.OUT_OF_TIME
.equals(receipt.getResult())) {
setTimeResultType(TimeResultType.OUT_OF_TIME);
} else if (System.currentTimeMillis() - txStartTimeInMs
> Args.getInstance().getLongRunningTime()) {
setTimeResultType(TimeResultType.LONG_RUNNING);
}
}
}
public void finalization() throws ContractExeException {
try {
pay();
} catch (BalanceInsufficientException e) {
throw new ContractExeException(e.getMessage());
}
runtime.finalization();
}
/**
* pay actually bill(include ENERGY and storage).
*/
public void pay() throws BalanceInsufficientException {
byte[] originAccount;
byte[] callerAccount;
long percent = 0;
long originEnergyLimit = 0;
switch (trxType) {
case TRX_CONTRACT_CREATION_TYPE:
callerAccount = TransactionCapsule.getOwner(trx.getInstance().getRawData().getContract(0));
originAccount = callerAccount;
break;
case TRX_CONTRACT_CALL_TYPE:
TriggerSmartContract callContract = ContractCapsule
.getTriggerContractFromTransaction(trx.getInstance());
ContractCapsule contractCapsule =
dbManager.getContractStore().get(callContract.getContractAddress().toByteArray());
callerAccount = callContract.getOwnerAddress().toByteArray();
originAccount = contractCapsule.getOriginAddress();
percent = Math
.max(Constant.ONE_HUNDRED - contractCapsule.getConsumeUserResourcePercent(), 0);
percent = Math.min(percent, Constant.ONE_HUNDRED);
originEnergyLimit = contractCapsule.getOriginEnergyLimit();
break;
default:
return;
}
// originAccount Percent = 30%
AccountCapsule origin = dbManager.getAccountStore().get(originAccount);
AccountCapsule caller = dbManager.getAccountStore().get(callerAccount);
receipt.payEnergyBill(
dbManager,
origin,
caller,
percent, originEnergyLimit,
energyProcessor,
dbManager.getWitnessController().getHeadSlot());
}
public boolean checkNeedRetry() {
if (!needVM()) {
return false;
}
return trx.getContractRet() != contractResult.OUT_OF_TIME && receipt.getResult()
== contractResult.OUT_OF_TIME;
}
public void check() throws ReceiptCheckErrException {
if (!needVM()) {
return;
}
if (Objects.isNull(trx.getContractRet())) {
throw new ReceiptCheckErrException("null resultCode");
}
if (!trx.getContractRet().equals(receipt.getResult())) {
logger.info(
"this tx id: {}, the resultCode in received block: {}, the resultCode in self: {}",
Hex.toHexString(trx.getTransactionId().getBytes()), trx.getContractRet(),
receipt.getResult());
throw new ReceiptCheckErrException("Different resultCode");
}
}
public ReceiptCapsule getReceipt() {
return receipt;
}
public void setResult() {
if (!needVM()) {
return;
}
RuntimeException exception = runtime.getResult().getException();
if (Objects.isNull(exception) && StringUtils
.isEmpty(runtime.getRuntimeError()) && !runtime.getResult().isRevert()) {
receipt.setResult(contractResult.SUCCESS);
return;
}
if (runtime.getResult().isRevert()) {
receipt.setResult(contractResult.REVERT);
return;
}
if (exception instanceof IllegalOperationException) {
receipt.setResult(contractResult.ILLEGAL_OPERATION);
return;
}
if (exception instanceof OutOfEnergyException) {
receipt.setResult(contractResult.OUT_OF_ENERGY);
return;
}
if (exception instanceof BadJumpDestinationException) {
receipt.setResult(contractResult.BAD_JUMP_DESTINATION);
return;
}
if (exception instanceof OutOfTimeException) {
receipt.setResult(contractResult.OUT_OF_TIME);
return;
}
if (exception instanceof OutOfMemoryException) {
receipt.setResult(contractResult.OUT_OF_MEMORY);
return;
}
if (exception instanceof PrecompiledContractException) {
receipt.setResult(contractResult.PRECOMPILED_CONTRACT);
return;
}
if (exception instanceof StackTooSmallException) {
receipt.setResult(contractResult.STACK_TOO_SMALL);
return;
}
if (exception instanceof StackTooLargeException) {
receipt.setResult(contractResult.STACK_TOO_LARGE);
return;
}
if (exception instanceof JVMStackOverFlowException) {
receipt.setResult(contractResult.JVM_STACK_OVER_FLOW);
return;
}
receipt.setResult(contractResult.UNKNOWN);
}
public String getRuntimeError() {
return runtime.getRuntimeError();
}
public ProgramResult getRuntimeResult() {
return runtime.getResult();
}
public Runtime getRuntime() {
return runtime;
}
}