Skip to content

Commit 0aa7f6a

Browse files
committed
implement eth_simulateV1 JSON-RPC method for trading-flow case
1 parent 381d369 commit 0aa7f6a

18 files changed

Lines changed: 1730 additions & 22 deletions

actuator/src/main/java/org/tron/core/actuator/VMActuator.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.tron.core.vm.program.ProgramPrecompile;
5555
import org.tron.core.vm.program.invoke.ProgramInvoke;
5656
import org.tron.core.vm.program.invoke.ProgramInvokeFactory;
57+
import org.tron.core.vm.program.listener.SimulationTracer;
5758
import org.tron.core.vm.repository.Repository;
5859
import org.tron.core.vm.repository.RepositoryImpl;
5960
import org.tron.core.vm.utils.MUtil;
@@ -95,6 +96,12 @@ public class VMActuator implements Actuator2 {
9596
@Setter
9697
private boolean enableEventListener;
9798

99+
@Setter
100+
private Repository injectedRootRepository;
101+
102+
@Setter
103+
private SimulationTracer simulationTracer;
104+
98105
private LogInfoTriggerParser logInfoTriggerParser;
99106

100107
public VMActuator(boolean isConstantCall) {
@@ -138,7 +145,9 @@ public void validate(Object object) throws ContractValidateException {
138145
//Route Type
139146
ContractType contractType = this.trx.getRawData().getContract(0).getType();
140147
//Prepare Repository
141-
rootRepository = RepositoryImpl.createRoot(context.getStoreFactory());
148+
rootRepository = injectedRootRepository != null
149+
? injectedRootRepository
150+
: RepositoryImpl.createRoot(context.getStoreFactory());
142151

143152
enableEventListener = context.isEventPluginLoaded();
144153

@@ -414,6 +423,7 @@ private void create()
414423
if (VMConfig.allowTvmCompatibleEvm()) {
415424
this.program.setContractVersion(1);
416425
}
426+
this.program.setSimulationTracer(simulationTracer);
417427
byte[] txId = TransactionUtil.getTransactionId(trx).getBytes();
418428
this.program.setRootTransactionId(txId);
419429
if (enableEventListener && isCheckTransaction()) {
@@ -437,10 +447,18 @@ private void create()
437447
// transfer from callerAddress to contractAddress according to callValue
438448
if (callValue > 0) {
439449
MUtil.transfer(rootRepository, callerAddress, contractAddress, callValue);
450+
if (simulationTracer != null) {
451+
simulationTracer.onTransfer(stripTronPrefix(callerAddress),
452+
stripTronPrefix(contractAddress), callValue);
453+
}
440454
}
441455
if (VMConfig.allowTvmTransferTrc10() && tokenValue > 0) {
442456
MUtil.transferToken(rootRepository, callerAddress, contractAddress, String.valueOf(tokenId),
443457
tokenValue);
458+
if (simulationTracer != null) {
459+
simulationTracer.onTokenTransfer(stripTronPrefix(callerAddress),
460+
stripTronPrefix(contractAddress), tokenId, tokenValue);
461+
}
444462
}
445463

446464
}
@@ -529,6 +547,7 @@ private void call()
529547
if (VMConfig.allowTvmCompatibleEvm()) {
530548
this.program.setContractVersion(deployedContract.getContractVersion());
531549
}
550+
this.program.setSimulationTracer(simulationTracer);
532551
byte[] txId = TransactionUtil.getTransactionId(trx).getBytes();
533552
this.program.setRootTransactionId(txId);
534553

@@ -543,14 +562,31 @@ private void call()
543562

544563
if (callValue > 0) {
545564
MUtil.transfer(rootRepository, callerAddress, contractAddress, callValue);
565+
if (simulationTracer != null) {
566+
simulationTracer.onTransfer(stripTronPrefix(callerAddress),
567+
stripTronPrefix(contractAddress), callValue);
568+
}
546569
}
547570
if (VMConfig.allowTvmTransferTrc10() && tokenValue > 0) {
548571
MUtil.transferToken(rootRepository, callerAddress, contractAddress, String.valueOf(tokenId),
549572
tokenValue);
573+
if (simulationTracer != null) {
574+
simulationTracer.onTokenTransfer(stripTronPrefix(callerAddress),
575+
stripTronPrefix(contractAddress), tokenId, tokenValue);
576+
}
550577
}
551578

552579
}
553580

581+
private static byte[] stripTronPrefix(byte[] tronAddress) {
582+
if (tronAddress == null || tronAddress.length != 21) {
583+
return tronAddress;
584+
}
585+
byte[] evm = new byte[20];
586+
System.arraycopy(tronAddress, 1, evm, 0, 20);
587+
return evm;
588+
}
589+
554590
public long getAccountEnergyLimitWithFixRatio(AccountCapsule account, long feeLimit,
555591
long callValue) {
556592

actuator/src/main/java/org/tron/core/vm/OperationActions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,9 @@ public static void logAction(Program program) {
754754
new LogInfo(address.getLast20Bytes(), topics, data);
755755

756756
program.getResult().addLogInfo(logInfo);
757+
if (program.getSimulationTracer() != null) {
758+
program.getSimulationTracer().onLog(logInfo);
759+
}
757760
program.step();
758761
}
759762

0 commit comments

Comments
 (0)