Skip to content

Commit aa9a834

Browse files
mmsqevladjdkAlex | Interchain Labs
authored
chore: replace GlobalEVMMempool by passing to JSONRPC on initiate (#467)
* chore: ensure SetGlobalEVMMempool is thread-safe and only set once keep singleton behavior on concurrent initialization * doc * keep reset * pass in mempool * cleanup --------- Co-authored-by: Vlad J <[email protected]> Co-authored-by: Alex | Interchain Labs <[email protected]>
1 parent dda0fb1 commit aa9a834

File tree

11 files changed

+34
-81
lines changed

11 files changed

+34
-81
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [\#398](https://github.com/cosmos/evm/pull/398) Post-audit security fixes (batch 4)
4646
- [\#442](https://github.com/cosmos/evm/pull/442) Prevent nil pointer by checking error in gov precompile FromResponse.
4747
- [\#387](https://github.com/cosmos/evm/pull/387) (Experimental) EVM-compatible appside mempool
48+
- [\#467](https://github.com/cosmos/evm/pull/467) Ensure SetGlobalEVMMempool is thread-safe and only sets global mempool instance once.
4849

4950
### FEATURES
5051

evmd/app.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,6 @@ func NewExampleApp(
773773
evmMempool := evmmempool.NewExperimentalEVMMempool(app.CreateQueryContext, logger, app.EVMKeeper, app.FeeMarketKeeper, app.txConfig, app.clientCtx, mempoolConfig)
774774
app.EVMMempool = evmMempool
775775

776-
// Set the global mempool for RPC access
777-
if err := evmmempool.SetGlobalEVMMempool(evmMempool); err != nil {
778-
panic(err)
779-
}
780776
app.SetMempool(evmMempool)
781777
checkTxHandler := evmmempool.NewCheckTxHandler(evmMempool)
782778
app.SetCheckTxHandler(checkTxHandler)

evmd/tests/network/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func startInProcess(cfg Config, val *Validator) error {
138138
val.AppConfig,
139139
nil,
140140
app.(server.AppWithPendingTxStream),
141+
nil,
141142
)
142143
if err != nil {
143144
return err

mempool/registry_production.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

mempool/registry_testing.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

rpc/apis.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ethereum/go-ethereum/rpc"
77

8+
evmmempool "github.com/cosmos/evm/mempool"
89
"github.com/cosmos/evm/rpc/backend"
910
"github.com/cosmos/evm/rpc/namespaces/ethereum/debug"
1011
"github.com/cosmos/evm/rpc/namespaces/ethereum/eth"
@@ -47,6 +48,7 @@ type APICreator = func(
4748
stream *stream.RPCStream,
4849
allowUnprotectedTxs bool,
4950
indexer types.EVMTxIndexer,
51+
mempool *evmmempool.ExperimentalEVMMempool,
5052
) []rpc.API
5153

5254
// apiCreators defines the JSON-RPC API namespaces.
@@ -59,8 +61,9 @@ func init() {
5961
stream *stream.RPCStream,
6062
allowUnprotectedTxs bool,
6163
indexer types.EVMTxIndexer,
64+
mempool *evmmempool.ExperimentalEVMMempool,
6265
) []rpc.API {
63-
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
66+
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, mempool)
6467
return []rpc.API{
6568
{
6669
Namespace: EthNamespace,
@@ -76,7 +79,7 @@ func init() {
7679
},
7780
}
7881
},
79-
Web3Namespace: func(*server.Context, client.Context, *stream.RPCStream, bool, types.EVMTxIndexer) []rpc.API {
82+
Web3Namespace: func(*server.Context, client.Context, *stream.RPCStream, bool, types.EVMTxIndexer, *evmmempool.ExperimentalEVMMempool) []rpc.API {
8083
return []rpc.API{
8184
{
8285
Namespace: Web3Namespace,
@@ -86,7 +89,7 @@ func init() {
8689
},
8790
}
8891
},
89-
NetNamespace: func(ctx *server.Context, clientCtx client.Context, _ *stream.RPCStream, _ bool, _ types.EVMTxIndexer) []rpc.API {
92+
NetNamespace: func(ctx *server.Context, clientCtx client.Context, _ *stream.RPCStream, _ bool, _ types.EVMTxIndexer, _ *evmmempool.ExperimentalEVMMempool) []rpc.API {
9093
return []rpc.API{
9194
{
9295
Namespace: NetNamespace,
@@ -101,8 +104,9 @@ func init() {
101104
_ *stream.RPCStream,
102105
allowUnprotectedTxs bool,
103106
indexer types.EVMTxIndexer,
107+
mempool *evmmempool.ExperimentalEVMMempool,
104108
) []rpc.API {
105-
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
109+
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, mempool)
106110
return []rpc.API{
107111
{
108112
Namespace: PersonalNamespace,
@@ -117,8 +121,9 @@ func init() {
117121
_ *stream.RPCStream,
118122
allowUnprotectedTxs bool,
119123
indexer types.EVMTxIndexer,
124+
mempool *evmmempool.ExperimentalEVMMempool,
120125
) []rpc.API {
121-
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
126+
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, mempool)
122127
return []rpc.API{
123128
{
124129
Namespace: TxPoolNamespace,
@@ -133,8 +138,9 @@ func init() {
133138
_ *stream.RPCStream,
134139
allowUnprotectedTxs bool,
135140
indexer types.EVMTxIndexer,
141+
mempool *evmmempool.ExperimentalEVMMempool,
136142
) []rpc.API {
137-
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
143+
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, mempool)
138144
return []rpc.API{
139145
{
140146
Namespace: DebugNamespace,
@@ -149,8 +155,9 @@ func init() {
149155
_ *stream.RPCStream,
150156
allowUnprotectedTxs bool,
151157
indexer types.EVMTxIndexer,
158+
mempool *evmmempool.ExperimentalEVMMempool,
152159
) []rpc.API {
153-
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer)
160+
evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer, mempool)
154161
return []rpc.API{
155162
{
156163
Namespace: MinerNamespace,
@@ -170,12 +177,13 @@ func GetRPCAPIs(ctx *server.Context,
170177
allowUnprotectedTxs bool,
171178
indexer types.EVMTxIndexer,
172179
selectedAPIs []string,
180+
mempool *evmmempool.ExperimentalEVMMempool,
173181
) []rpc.API {
174182
var apis []rpc.API
175183

176184
for _, ns := range selectedAPIs {
177185
if creator, ok := apiCreators[ns]; ok {
178-
apis = append(apis, creator(ctx, clientCtx, stream, allowUnprotectedTxs, indexer)...)
186+
apis = append(apis, creator(ctx, clientCtx, stream, allowUnprotectedTxs, indexer, mempool)...)
179187
} else {
180188
ctx.Logger.Error("invalid namespace value", "namespace", ns)
181189
}

rpc/backend/backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
1818
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
1919

20+
evmmempool "github.com/cosmos/evm/mempool"
2021
rpctypes "github.com/cosmos/evm/rpc/types"
2122
"github.com/cosmos/evm/server/config"
2223
cosmosevmtypes "github.com/cosmos/evm/types"
@@ -166,6 +167,7 @@ type Backend struct {
166167
AllowUnprotectedTxs bool
167168
Indexer cosmosevmtypes.EVMTxIndexer
168169
ProcessBlocker ProcessBlocker
170+
Mempool *evmmempool.ExperimentalEVMMempool
169171
}
170172

171173
func (b *Backend) GetConfig() config.Config {
@@ -179,6 +181,7 @@ func NewBackend(
179181
clientCtx client.Context,
180182
allowUnprotectedTxs bool,
181183
indexer cosmosevmtypes.EVMTxIndexer,
184+
mempool *evmmempool.ExperimentalEVMMempool,
182185
) *Backend {
183186
appConf, err := config.GetConfig(ctx.Viper)
184187
if err != nil {
@@ -200,6 +203,7 @@ func NewBackend(
200203
Cfg: appConf,
201204
AllowUnprotectedTxs: allowUnprotectedTxs,
202205
Indexer: indexer,
206+
Mempool: mempool,
203207
}
204208
b.ProcessBlocker = b.ProcessBlock
205209
return b

rpc/backend/tx_pool.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/ethereum/go-ethereum/common/hexutil"
1010
ethtypes "github.com/ethereum/go-ethereum/core/types"
1111

12-
"github.com/cosmos/evm/mempool"
1312
"github.com/cosmos/evm/rpc/types"
1413
)
1514

@@ -28,7 +27,7 @@ func (b *Backend) Content() (map[string]map[string]map[string]*types.RPCTransact
2827
}
2928

3029
// Get the global mempool instance
31-
evmMempool := mempool.GetGlobalEVMMempool()
30+
evmMempool := b.Mempool
3231
if evmMempool == nil {
3332
return content, nil
3433
}
@@ -78,7 +77,7 @@ func (b *Backend) ContentFrom(addr common.Address) (map[string]map[string]*types
7877
content := make(map[string]map[string]*types.RPCTransaction, 2)
7978

8079
// Get the global mempool instance
81-
evmMempool := mempool.GetGlobalEVMMempool()
80+
evmMempool := b.Mempool
8281
if evmMempool == nil {
8382
return content, nil
8483
}
@@ -121,7 +120,7 @@ func (b *Backend) Inspect() (map[string]map[string]map[string]string, error) {
121120
}
122121

123122
// Get the global mempool instance
124-
evmMempool := mempool.GetGlobalEVMMempool()
123+
evmMempool := b.Mempool
125124
if evmMempool == nil {
126125
return inspect, nil
127126
}
@@ -163,7 +162,7 @@ func (b *Backend) Inspect() (map[string]map[string]map[string]string, error) {
163162
// Status returns the number of pending and queued transaction in the pool.
164163
func (b *Backend) Status() (map[string]hexutil.Uint, error) {
165164
// Get the global mempool instance
166-
evmMempool := mempool.GetGlobalEVMMempool()
165+
evmMempool := b.Mempool
167166
if evmMempool == nil {
168167
return map[string]hexutil.Uint{
169168
StatusPending: hexutil.Uint(0),

server/json_rpc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
rpcclient "github.com/cometbft/cometbft/rpc/client"
1616

17+
evmmempool "github.com/cosmos/evm/mempool"
1718
"github.com/cosmos/evm/rpc"
1819
"github.com/cosmos/evm/rpc/stream"
1920
serverconfig "github.com/cosmos/evm/server/config"
@@ -36,6 +37,7 @@ func StartJSONRPC(
3637
config *serverconfig.Config,
3738
indexer cosmosevmtypes.EVMTxIndexer,
3839
app AppWithPendingTxStream,
40+
mempool *evmmempool.ExperimentalEVMMempool,
3941
) (*http.Server, error) {
4042
logger := srvCtx.Logger.With("module", "geth")
4143

@@ -57,7 +59,7 @@ func StartJSONRPC(
5759
allowUnprotectedTxs := config.JSONRPC.AllowUnprotectedTxs
5860
rpcAPIArr := config.JSONRPC.API
5961

60-
apis := rpc.GetRPCAPIs(srvCtx, clientCtx, stream, allowUnprotectedTxs, indexer, rpcAPIArr)
62+
apis := rpc.GetRPCAPIs(srvCtx, clientCtx, stream, allowUnprotectedTxs, indexer, rpcAPIArr, mempool)
6163

6264
for _, api := range apis {
6365
if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {

server/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
dbm "github.com/cosmos/cosmos-db"
3030
"github.com/cosmos/evm/indexer"
31+
evmmempool "github.com/cosmos/evm/mempool"
3132
ethdebug "github.com/cosmos/evm/rpc/namespaces/ethereum/debug"
3233
cosmosevmserverconfig "github.com/cosmos/evm/server/config"
3334
srvflags "github.com/cosmos/evm/server/flags"
@@ -48,6 +49,7 @@ import (
4849
servercmtlog "github.com/cosmos/cosmos-sdk/server/log"
4950
"github.com/cosmos/cosmos-sdk/server/types"
5051
"github.com/cosmos/cosmos-sdk/telemetry"
52+
sdkmempool "github.com/cosmos/cosmos-sdk/types/mempool"
5153
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
5254
)
5355

@@ -57,6 +59,7 @@ type DBOpener func(opts types.AppOptions, rootDir string, backend dbm.BackendTyp
5759
type Application interface {
5860
types.Application
5961
AppWithPendingTxStream
62+
GetMempool() sdkmempool.ExtMempool
6063
SetClientCtx(clientCtx client.Context)
6164
}
6265

@@ -502,7 +505,7 @@ func startInProcess(svrCtx *server.Context, clientCtx client.Context, opts Start
502505
if !ok {
503506
return fmt.Errorf("json-rpc server requires AppWithPendingTxStream")
504507
}
505-
_, err = StartJSONRPC(ctx, svrCtx, clientCtx, g, &config, idxer, txApp)
508+
_, err = StartJSONRPC(ctx, svrCtx, clientCtx, g, &config, idxer, txApp, evmApp.GetMempool().(*evmmempool.ExperimentalEVMMempool))
506509
if err != nil {
507510
return err
508511
}

0 commit comments

Comments
 (0)