Skip to content

Commit be35b0f

Browse files
committed
eth: add admin_clearTxpool api
1 parent da3822d commit be35b0f

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

eth/api_admin.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,15 @@ func (api *AdminAPI) ImportChain(file string) (bool, error) {
141141
}
142142
return true, nil
143143
}
144+
145+
// ClearTxpool clears all transactions from the transaction pool.
146+
// This method removes all pending and queued transactions from both the
147+
// legacy transaction pool and the blob transaction pool.
148+
//
149+
// Note: This operation invokes Sync() internally and should be used with
150+
// caution as it can be susceptible to DoS vectors. It's primarily intended
151+
// for testing and administrative purposes.
152+
func (api *AdminAPI) ClearTxpool() error {
153+
api.eth.TxPool().Clear()
154+
return nil
155+
}

eth/api_admin_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2024 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package eth
18+
19+
import (
20+
"math/big"
21+
"testing"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/consensus/beacon"
25+
"github.com/ethereum/go-ethereum/consensus/ethash"
26+
"github.com/ethereum/go-ethereum/core"
27+
"github.com/ethereum/go-ethereum/core/rawdb"
28+
"github.com/ethereum/go-ethereum/core/txpool"
29+
"github.com/ethereum/go-ethereum/core/txpool/blobpool"
30+
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
31+
"github.com/ethereum/go-ethereum/core/types"
32+
"github.com/ethereum/go-ethereum/crypto"
33+
"github.com/ethereum/go-ethereum/params"
34+
)
35+
36+
func TestAdminAPI_ClearTxpool(t *testing.T) {
37+
// Create test key and genesis
38+
testKey, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
39+
testAddress := crypto.PubkeyToAddress(testKey.PublicKey)
40+
testFunds := big.NewInt(1000_000_000_000_000)
41+
testGspec := &core.Genesis{
42+
Config: params.MergedTestChainConfig,
43+
Alloc: types.GenesisAlloc{
44+
testAddress: {Balance: testFunds},
45+
},
46+
Difficulty: common.Big0,
47+
BaseFee: big.NewInt(params.InitialBaseFee),
48+
}
49+
testSigner := types.LatestSignerForChainID(testGspec.Config.ChainID)
50+
51+
// Initialize backend
52+
db := rawdb.NewMemoryDatabase()
53+
engine := beacon.New(ethash.NewFaker())
54+
chain, _ := core.NewBlockChain(db, testGspec, engine, nil)
55+
56+
txconfig := legacypool.DefaultConfig
57+
txconfig.Journal = "" // Don't litter the disk with test journals
58+
59+
blobPool := blobpool.New(blobpool.Config{Datadir: ""}, chain, nil)
60+
legacyPool := legacypool.New(txconfig, chain)
61+
pool, _ := txpool.New(txconfig.PriceLimit, chain, []txpool.SubPool{legacyPool, blobPool})
62+
63+
eth := &Ethereum{
64+
blockchain: chain,
65+
txPool: pool,
66+
}
67+
68+
// Create admin API
69+
api := NewAdminAPI(eth)
70+
71+
// Create and add a test transaction
72+
tx := types.NewTransaction(0, common.Address{1}, big.NewInt(1000), params.TxGas, big.NewInt(params.InitialBaseFee), nil)
73+
signedTx, err := types.SignTx(tx, testSigner, testKey)
74+
if err != nil {
75+
t.Fatalf("Failed to sign transaction: %v", err)
76+
}
77+
78+
// Add transaction to pool
79+
errs := pool.Add([]*types.Transaction{signedTx}, true)
80+
if errs[0] != nil {
81+
t.Logf("Note: Transaction addition returned: %v (this may be expected)", errs[0])
82+
}
83+
84+
// Verify we tried to add a transaction
85+
t.Logf("Transaction added to pool from: %s", testAddress.Hex())
86+
87+
// Clear the transaction pool
88+
err = api.ClearTxpool()
89+
if err != nil {
90+
t.Fatalf("ClearTxpool failed: %v", err)
91+
}
92+
93+
// Verify the pool is empty after clear
94+
pool.Sync()
95+
pending := pool.Pending(txpool.PendingFilter{})
96+
if len(pending) > 0 {
97+
t.Errorf("Expected empty pool after clear, but found %d accounts with pending transactions", len(pending))
98+
}
99+
100+
t.Log("Successfully cleared transaction pool")
101+
}

internal/web3ext/web3ext.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ web3._extend({
158158
name: 'stopWS',
159159
call: 'admin_stopWS'
160160
}),
161+
new web3._extend.Method({
162+
name: 'clearTxpool',
163+
call: 'admin_clearTxpool',
164+
params: 0
165+
}),
161166
],
162167
properties: [
163168
new web3._extend.Property({

0 commit comments

Comments
 (0)