eth: add debug_clearTxpool api

This commit is contained in:
cuiweixie 2025-12-02 21:01:09 +08:00
parent a9eaf2ffd8
commit 4532988fe3
No known key found for this signature in database
GPG key ID: 16DF64EE15E495A3
3 changed files with 88 additions and 0 deletions

View file

@ -526,3 +526,15 @@ func (api *DebugAPI) ExecutionWitnessByHash(hash common.Hash) (*stateless.ExtWit
}
return result.Witness().ToExtWitness(), nil
}
// ClearTxpool clears all transactions from the transaction pool.
// This method removes all pending and queued transactions from both the
// legacy transaction pool and the blob transaction pool.
//
// Note: This operation invokes Sync() internally and should be used with
// caution as it can be susceptible to DoS vectors. It's primarily intended
// for testing and debugging purposes.
func (api *DebugAPI) ClearTxpool() error {
api.eth.TxPool().Clear()
return nil
}

View file

@ -29,11 +29,15 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/blobpool"
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
@ -335,3 +339,70 @@ func TestGetModifiedAccounts(t *testing.T) {
}
})
}
func TestDebugAPI_ClearTxpool(t *testing.T) {
// Create test key and genesis
testKey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddress := crypto.PubkeyToAddress(testKey.PublicKey)
testFunds := big.NewInt(1000_000_000_000_000)
testGspec := &core.Genesis{
Config: params.MergedTestChainConfig,
Alloc: types.GenesisAlloc{
testAddress: {Balance: testFunds},
},
Difficulty: common.Big0,
BaseFee: big.NewInt(params.InitialBaseFee),
}
testSigner := types.LatestSignerForChainID(testGspec.Config.ChainID)
// Initialize backend
db := rawdb.NewMemoryDatabase()
engine := beacon.New(ethash.NewFaker())
chain, _ := core.NewBlockChain(db, testGspec, engine, nil)
txconfig := legacypool.DefaultConfig
txconfig.Journal = "" // Don't litter the disk with test journals
blobPool := blobpool.New(blobpool.Config{Datadir: ""}, chain, nil)
legacyPool := legacypool.New(txconfig, chain)
pool, _ := txpool.New(txconfig.PriceLimit, chain, []txpool.SubPool{legacyPool, blobPool})
eth := &Ethereum{
blockchain: chain,
txPool: pool,
}
// Create debug API
api := NewDebugAPI(eth)
// Create and add a test transaction
tx := types.NewTransaction(0, common.Address{1}, big.NewInt(1000), params.TxGas, big.NewInt(params.InitialBaseFee), nil)
signedTx, err := types.SignTx(tx, testSigner, testKey)
if err != nil {
t.Fatalf("Failed to sign transaction: %v", err)
}
// Add transaction to pool
errs := pool.Add([]*types.Transaction{signedTx}, true)
if errs[0] != nil {
t.Logf("Note: Transaction addition returned: %v (this may be expected)", errs[0])
}
// Verify we tried to add a transaction
t.Logf("Transaction added to pool from: %s", testAddress.Hex())
// Clear the transaction pool
err = api.ClearTxpool()
if err != nil {
t.Fatalf("ClearTxpool failed: %v", err)
}
// Verify the pool is empty after clear
pool.Sync()
pending := pool.Pending(txpool.PendingFilter{})
if len(pending) > 0 {
t.Errorf("Expected empty pool after clear, but found %d accounts with pending transactions", len(pending))
}
t.Log("Successfully cleared transaction pool")
}

View file

@ -158,6 +158,11 @@ web3._extend({
name: 'stopWS',
call: 'admin_stopWS'
}),
new web3._extend.Method({
name: 'clearTxpool',
call: 'debug_clearTxpool',
params: 0
}),
],
properties: [
new web3._extend.Property({