From 28a95cf4443cb8b76e94307e2a0b90fd6bb6c226 Mon Sep 17 00:00:00 2001 From: "(Nam) Pham Tien" Date: Thu, 22 Feb 2024 14:49:37 +0700 Subject: [PATCH] feat: check devnet mode when simulating the bundle --- eth/api_simulation.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/eth/api_simulation.go b/eth/api_simulation.go index 0bdd49583065..35876b4e965c 100644 --- a/eth/api_simulation.go +++ b/eth/api_simulation.go @@ -8,6 +8,7 @@ import ( "fmt" "math" "math/big" + "os" "sync" "sync/atomic" "time" @@ -29,6 +30,10 @@ import ( "github.com/ethereum/go-ethereum/log" ) +var ( + enableDevnet = os.Getenv("ENABLE_DEVNET") +) + type TraceInternalTransactionArgs struct { Tx hexutil.Bytes `json:"tx"` } @@ -172,9 +177,9 @@ func (b *SimulationAPIBackend) loop() error { func (b *SimulationAPIBackend) setBlock(currentBlock *types.Block) error { log.Info("Receive new head", "block", currentBlock.NumberU64()) - + isEnableDevNet := enableDevnet == "true" blockTime := int64(currentBlock.Time()) - if !b.isLatestBlock(blockTime) { + if !b.isLatestBlock(blockTime) && !isEnableDevNet { b.isCatchUpLatestBlock.Store(false) log.Warn("The state of the block isn't up-to-date", "block", currentBlock.NumberU64(), "time", currentBlock.Time()) return nil @@ -293,6 +298,22 @@ func (b *SimulationAPIBackend) isLatestBlock(blockTime int64) bool { // The sender is responsible for signing the transactions and using the correct // nonce and ensuring validity func (b *SimulationAPIBackend) CallBundle(ctx context.Context, args CallBundleArgs) (map[string]interface{}, error) { + if b.currentBlock == nil { + return nil, fmt.Errorf("the current block is empty") + } + + if b.stateDb == nil { + return nil, fmt.Errorf("stateDb is empty") + } + + if isCatchUpLatestBlock := b.isCatchUpLatestBlock.Load(); !isCatchUpLatestBlock { + var blockNumber uint64 + if b.currentBlock != nil { + blockNumber = b.currentBlock.NumberU64() + } + return nil, fmt.Errorf("failed to simulate the bundle because of the state isn't up to date, block_number: %d", blockNumber) + } + if len(args.Txs) == 0 { return nil, errors.New("bundle missing txs") }