Skip to content

Commit

Permalink
feat(api_simulation): use call tracer instead of the custom tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennampham23 committed Nov 17, 2023
1 parent 916c464 commit 75d48cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 241 deletions.
25 changes: 6 additions & 19 deletions core/types/simulated_tx.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
package types

import "math/big"
import (
"encoding/json"
"math/big"
)

type SimulationDebugInfoResponse struct {
StartSimulateMs int64 `json:"start_simulate_ms"`
EndSimulateMs int64 `json:"end_simulate_ms"`
}

type InternalTxResponse struct {
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
Gas uint64 `json:"gas"`
GasUsed uint64 `json:"gas_used"`
Input string `json:"input"`
Value string `json:"value"`
}

type EventLogResponse struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
}

type SimulationTxResponse struct {
PendingBlockNumber uint64 `json:"pending_block_number"`
BaseFee *big.Int `json:"base_fee"`
InternalTxs []InternalTxResponse `json:"internal_transactions"`
CurrentBlockTime uint64 `json:"current_block_time"`
DebugInfo SimulationDebugInfoResponse `json:"debug_info"`
Logs []EventLogResponse `json:"logs"`
CallFrame json.RawMessage `json:"call_frame"`
}
82 changes: 30 additions & 52 deletions eth/api_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"math"
"strings"
"sync"
"sync/atomic"
"time"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/native"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
)
Expand All @@ -46,6 +44,20 @@ func (l *list) findSnapshotByNonce(nonce uint64) *state.StateDB {
return l.snapshots[nonce]
}

const (
enableCheckpointFlag = false
)

var (
tracerCfgBytes []byte
)

func init() {
tracerCfgBytes, _ = json.Marshal(map[string]interface{}{
"withLog": true,
})
}

// SimulationAPIBackend creates a new simulation API
type SimulationAPIBackend struct {
eth Backend
Expand Down Expand Up @@ -195,18 +207,20 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
)
// load the checkpoint db if exists
var currentList *list
if enc, found := b.stateDbCheckpoint.Load(msg.From.Hex()); found && enc != nil {
list, ok := enc.(*list)
if ok && list != nil {
currentList = list
checkpointStateDb := list.findSnapshotByNonce(msg.Nonce)
if checkpointStateDb != nil {
stateDb = checkpointStateDb.Copy()
if enableCheckpointFlag {
if enc, found := b.stateDbCheckpoint.Load(msg.From.Hex()); found && enc != nil {
list, ok := enc.(*list)
if ok && list != nil {
currentList = list
checkpointStateDb := list.findSnapshotByNonce(msg.Nonce)
if checkpointStateDb != nil {
stateDb = checkpointStateDb.Copy()
}
}
}
}

internalTransactionTracer, err := tracers.DefaultDirectory.New(native.InternalTransactionTracerName, tracerCtx, json.RawMessage{})
internalTransactionTracer, err := tracers.DefaultDirectory.New("callTracer", tracerCtx, tracerCfgBytes)
if err != nil {
log.Error("Failed to create call tracer", "error", err)
return nil, err
Expand All @@ -224,7 +238,10 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
log.Error("Failed to apply the message", "hash", tx.Hash().String(), "number", currentBlock.NumberU64(), "err", err)
return nil, err
}
b.storeSnapshot(stateDb, currentList, msg.Nonce, msg.From)

if enableCheckpointFlag {
b.storeSnapshot(stateDb, currentList, msg.Nonce, msg.From)
}

if executionResult == nil {
log.Warn("Simulation result is empty", "tx_hash", tx.Hash().String())
Expand All @@ -246,54 +263,15 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
return nil, nil
}

var internalTxTracerOutput native.InternalTxTracerOutput

if err := json.Unmarshal(tracerResultBytes, &internalTxTracerOutput); err != nil {
log.Error("Failed to unmarshal the internal transactions tracers", "err", err)
return nil, err
}

internalTxsResponse := make([]types.InternalTxResponse, 0, len(internalTxTracerOutput.InternalTxs))
for _, internalTx := range internalTxTracerOutput.InternalTxs {
to := ""
value := "0"
if internalTx.To != nil {
to = internalTx.To.String()
}
if internalTx.Value != nil {
value = internalTx.Value.String()
}
internalTxsResponse = append(internalTxsResponse, types.InternalTxResponse{
Type: internalTx.Type.String(),
From: strings.ToLower(internalTx.From.String()),
To: strings.ToLower(to),
Gas: internalTx.Gas,
GasUsed: internalTx.GasUsed,
Input: internalTx.Input,
Value: value,
})
}
eventLogs := make([]types.EventLogResponse, 0, len(internalTxTracerOutput.EventLogs))
for _, eventLog := range internalTxTracerOutput.EventLogs {
topics := make([]string, 0, len(eventLog.Topics))
for _, topic := range eventLog.Topics {
topics = append(topics, topic.Hex())
}
eventLogs = append(eventLogs, types.EventLogResponse{
Data: hexutil.Encode(eventLog.Data),
Address: eventLog.Address.String(),
Topics: topics,
})
}
return &types.SimulationTxResponse{
InternalTxs: internalTxsResponse,
CallFrame: tracerResultBytes,
DebugInfo: types.SimulationDebugInfoResponse{
StartSimulateMs: startTraceTimeMs,
EndSimulateMs: time.Now().UnixMilli(),
},
PendingBlockNumber: currentBlock.NumberU64() + 1,
CurrentBlockTime: currentBlock.Time(),
BaseFee: currentBlock.BaseFee(),
Logs: eventLogs,
}, nil
}

Expand Down
170 changes: 0 additions & 170 deletions eth/tracers/native/internal_transaction.go

This file was deleted.

0 comments on commit 75d48cf

Please sign in to comment.