Skip to content

Commit

Permalink
Merge pull request #2 from KyberNetwork/feat/remove-unused-code
Browse files Browse the repository at this point in the history
feat: remove unused code
  • Loading branch information
tiennampham23 authored Dec 6, 2023
2 parents 12b3b93 + 9dca9d3 commit 04fdc52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 94 deletions.
3 changes: 0 additions & 3 deletions core/types/simulated_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

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

type SimulationDebugInfoResponse struct {
Expand All @@ -12,8 +11,6 @@ type SimulationDebugInfoResponse struct {

type SimulationTxResponse struct {
PendingBlockNumber uint64 `json:"pending_block_number"`
BaseFee *big.Int `json:"base_fee"`
CurrentBlockTime uint64 `json:"current_block_time"`
DebugInfo SimulationDebugInfoResponse `json:"debug_info"`
CallFrame json.RawMessage `json:"call_frame"`
}
101 changes: 10 additions & 91 deletions eth/api_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"math"
"sync"
"sync/atomic"
Expand All @@ -32,22 +31,6 @@ type Backend interface {
TxPool() *txpool.TxPool
}

// list is a "list" of the statedb belonging to an account, sorted by account nonce
type list struct {
snapshots map[uint64]*state.StateDB
mu sync.Mutex
}

func (l *list) findSnapshotByNonce(nonce uint64) *state.StateDB {
l.mu.Lock()
defer l.mu.Unlock()
return l.snapshots[nonce]
}

const (
enableCheckpointFlag = false
)

var (
tracerCfgBytes []byte
)
Expand Down Expand Up @@ -75,8 +58,6 @@ type SimulationAPIBackend struct {
stateDb *state.StateDB // current stateDb of the blockchain
currentSigner types.Signer // current signer according to the current block
currentBlockCtx vm.BlockContext // current block context according to the current block

stateDbCheckpoint sync.Map // store "list" of checkpoint belonging to an account address
}

func NewSimulationAPI(eth Backend) *SimulationAPIBackend {
Expand All @@ -97,7 +78,7 @@ func NewSimulationAPI(eth Backend) *SimulationAPIBackend {
return simulationAPIBackend
}

func (b *SimulationAPIBackend) TraceInternalTransaction(ctx context.Context, args TraceInternalTransactionArgs) (*types.SimulationTxResponse, error) {
func (b *SimulationAPIBackend) TraceInternalTransaction(_ context.Context, args TraceInternalTransactionArgs) (*types.SimulationTxResponse, error) {
if len(args.Tx) == 0 {
return nil, errors.New("missing transaction")
}
Expand Down Expand Up @@ -145,27 +126,23 @@ func (b *SimulationAPIBackend) loop() error {
blockTime := int64(currentBlock.Time())
if !b.isLatestBlock(blockTime) {
b.isCatchUpLatestBlock.Store(false)
log.Warn("The state of block isn't up-to-date", "block", currentBlock.NumberU64(), "time", currentBlock.Time())
log.Warn("The state of the block isn't up-to-date", "block", currentBlock.NumberU64(), "time", currentBlock.Time())
continue
}

signer := types.MakeSigner(b.eth.BlockChain().Config(), currentBlock.Number(), currentBlock.Time())
blockCtx := core.NewEVMBlockContext(currentBlock.Header(), b.eth.BlockChain(), nil)

readOnlyStateDb, err := b.eth.BlockChain().StateAt(currentBlock.Root())
stateDb, err := b.eth.BlockChain().StateAt(currentBlock.Root())
if err != nil {
log.Error("Failed to get read-only state of the blockchain", "hash", currentBlock.Hash().String(), "error", err)
log.Error("Failed to get the read-only state of the blockchain", "hash", currentBlock.Hash().String(), "error", err)
return err
}
b.stateDb = readOnlyStateDb
b.stateDb = stateDb
b.currentBlock = currentBlock
b.currentBlockCtx = blockCtx
b.currentSigner = signer
b.isCatchUpLatestBlock.Store(true)

// clear the checkpoint of snapshots if the states are stale
b.clearStaleSnapshots(readOnlyStateDb.Copy())

case err := <-b.chainHeadSub.Err():
return err
case <-b.exitCh:
Expand All @@ -185,11 +162,11 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
startTraceTimeMs := time.Now().UnixMilli()

if currentBlock == nil || currentBlock.NumberU64() <= 0 {
return nil, fmt.Errorf("current block is empty")
return nil, fmt.Errorf("the current block is empty")
}

if stateDb == nil {
return nil, fmt.Errorf("stateDb is empty")
return nil, fmt.Errorf("the stateDb is empty")
}

chainConfig := b.eth.BlockChain().Config()
Expand All @@ -205,24 +182,10 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
TxHash: tx.Hash(),
}
)
// load the checkpoint db if exists
var currentList *list
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("callTracer", tracerCtx, tracerCfgBytes)
if err != nil {
log.Error("Failed to create call tracer", "error", err)
log.Error("Failed to create call the tracer", "error", err)
return nil, err
}

Expand All @@ -239,10 +202,6 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
return nil, err
}

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

if executionResult == nil {
log.Warn("Simulation result is empty", "tx_hash", tx.Hash().String())
return nil, nil
Expand Down Expand Up @@ -270,53 +229,13 @@ func (b *SimulationAPIBackend) simulate(tx *types.Transaction, stateDb *state.St
EndSimulateMs: time.Now().UnixMilli(),
},
PendingBlockNumber: currentBlock.NumberU64() + 1,
CurrentBlockTime: currentBlock.Time(),
BaseFee: currentBlock.BaseFee(),
}, nil
}

func (b *SimulationAPIBackend) isLatestBlock(blockTime int64) bool {
secondsNow := time.Now().Unix()
if blockTime <= secondsNow && blockTime >= secondsNow-12 {
if blockTime <= secondsNow && blockTime+12 >= secondsNow {
return true
}
return false
}

func (b *SimulationAPIBackend) storeSnapshot(stateDb *state.StateDB, l *list, nonce uint64, from common.Address) {
if l == nil {
l = &list{
snapshots: make(map[uint64]*state.StateDB),
}
}
nextNonce := nonce + 1
l.snapshots[nextNonce] = stateDb
b.stateDbCheckpoint.Store(from.Hex(), l)
}

func (b *SimulationAPIBackend) clearStaleSnapshots(stateDb *state.StateDB) {
b.stateDbCheckpoint.Range(func(k, v any) bool {
l := v.(*list)

address := k.(string)

if l == nil {
b.stateDbCheckpoint.Delete(address)
return true
}

pendingNonce := stateDb.GetNonce(common.HexToAddress(address))

for nonce := range l.snapshots {
if pendingNonce > nonce-1 {
delete(l.snapshots, nonce)
}
}

if len(l.snapshots) == 0 {
b.stateDbCheckpoint.Delete(address)
return true
}
return true
})
}
}

0 comments on commit 04fdc52

Please sign in to comment.