Skip to content

Commit 997953c

Browse files
committed
Make the TxPool run as a plugin
1 parent 01a0c69 commit 997953c

File tree

14 files changed

+167
-31
lines changed

14 files changed

+167
-31
lines changed

tests/p2p/test_tx_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
force_bytes_to_address
1414
)
1515

16-
from trinity.tx_pool.pool import (
16+
from trinity.plugins.builtin.tx_pool.pool import (
1717
TxPool,
1818
)
19-
from trinity.tx_pool.validators import (
19+
from trinity.plugins.builtin.tx_pool.validators import (
2020
DefaultTransactionValidator
2121
)
2222

trinity/main.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from argparse import Namespace
12
import asyncio
23
import logging
34
import signal
@@ -33,6 +34,15 @@
3334
from trinity.config import (
3435
ChainConfig,
3536
)
37+
from trinity.extensibility import (
38+
PluginManager,
39+
)
40+
from trinity.extensibility.events import (
41+
TrinityStartupEvent
42+
)
43+
from trinity.plugins.registry import (
44+
ENABLED_PLUGINS
45+
)
3646
from trinity.utils.ipc import (
3747
wait_for_ipc,
3848
kill_process_gracefully,
@@ -81,6 +91,8 @@
8191

8292

8393
def main() -> None:
94+
plugin_manager = setup_plugins()
95+
plugin_manager.amend_argparser_config(parser)
8496
args = parser.parse_args()
8597

8698
log_level = getattr(logging, args.log_level.upper())
@@ -154,7 +166,7 @@ def main() -> None:
154166

155167
networking_process = ctx.Process(
156168
target=launch_node,
157-
args=(chain_config, ),
169+
args=(args, chain_config, ),
158170
kwargs=extra_kwargs,
159171
)
160172

@@ -230,9 +242,21 @@ async def exit_on_signal(service_to_exit: BaseService) -> None:
230242

231243
@setup_cprofiler('launch_node')
232244
@with_queued_logging
233-
def launch_node(chain_config: ChainConfig) -> None:
245+
def launch_node(args: Namespace, chain_config: ChainConfig) -> None:
234246
NodeClass = chain_config.node_class
235-
node = NodeClass(chain_config)
247+
# Temporary hack: We setup a second instance of the PluginManager.
248+
# The first instance was only to configure the ArgumentParser whereas
249+
# for now, the second instance that lives inside the networking process
250+
# performs the bulk of the work. In the future, the PluginManager
251+
# should probably live in its own process and manage whether plugins
252+
# run in the shared plugin process or spawn their own.
253+
plugin_manager = setup_plugins()
254+
plugin_manager.broadcast(TrinityStartupEvent(
255+
args,
256+
chain_config
257+
))
258+
259+
node = NodeClass(plugin_manager, chain_config)
236260

237261
run_service_until_quit(node)
238262

@@ -250,3 +274,11 @@ def run_service_until_quit(service: BaseService) -> None:
250274
asyncio.ensure_future(service.run())
251275
loop.run_forever()
252276
loop.close()
277+
278+
279+
def setup_plugins() -> PluginManager:
280+
plugin_manager = PluginManager()
281+
# TODO: Implement auto-discovery of plugins based on some convention/configuration scheme
282+
plugin_manager.register(ENABLED_PLUGINS)
283+
284+
return plugin_manager

trinity/nodes/base.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
from trinity.config import (
4242
ChainConfig,
4343
)
44-
from trinity.tx_pool.pool import (
45-
TxPool
44+
from trinity.extensibility import (
45+
PluginManager,
4646
)
47-
from trinity.tx_pool.validators import (
48-
DefaultTransactionValidator
47+
from trinity.extensibility.events import (
48+
ResourceAvailableEvent
4949
)
5050

5151

@@ -55,11 +55,10 @@ class Node(BaseService):
5555
unset attributes.
5656
"""
5757
chain_class: Type[BaseChain] = None
58-
initial_tx_validation_block_number: int = None
5958

60-
def __init__(self, chain_config: ChainConfig) -> None:
59+
def __init__(self, plugin_manager: PluginManager, chain_config: ChainConfig) -> None:
6160
super().__init__()
62-
61+
self._plugin_manager = plugin_manager
6362
self._db_manager = create_db_manager(chain_config.database_ipc_path)
6463
self._db_manager.connect() # type: ignore
6564
self._headerdb = self._db_manager.get_headerdb() # type: ignore
@@ -100,13 +99,22 @@ def add_service(self, service: BaseService) -> None:
10099
else:
101100
self._auxiliary_services.append(service)
102101

103-
def create_and_add_tx_pool(self) -> None:
104-
self.tx_pool = TxPool(
105-
self.get_peer_pool(),
106-
DefaultTransactionValidator(self.get_chain(), self.initial_tx_validation_block_number),
107-
self.cancel_token
108-
)
109-
self.add_service(self.tx_pool)
102+
def notify_resource_available(self) -> None:
103+
104+
# We currently need this to give plugins the chance to start as soon
105+
# as the `PeerPool` is available. In the long term, the peer pool may become
106+
# a plugin itself and we can get rid of this.
107+
self._plugin_manager.broadcast(ResourceAvailableEvent(
108+
resource=(self.get_peer_pool(), self.cancel_token),
109+
resource_type=PeerPool
110+
))
111+
112+
# This broadcasts the *local* chain, which is suited for tasks that aren't blocking
113+
# for too long. There may be value in also broadcasting the proxied chain.
114+
self._plugin_manager.broadcast(ResourceAvailableEvent(
115+
resource=self.get_chain(),
116+
resource_type=BaseChain
117+
))
110118

111119
def make_ipc_server(self) -> Union[IPCServer, EmptyService]:
112120
if self._jsonrpc_ipc_path:

trinity/nodes/full.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@
1313
from trinity.config import (
1414
ChainConfig,
1515
)
16+
from trinity.extensibility import (
17+
PluginManager
18+
)
1619

1720

1821
class FullNode(Node):
1922
_chain: BaseChain = None
2023
_p2p_server: Server = None
2124

22-
def __init__(self, chain_config: ChainConfig) -> None:
23-
super().__init__(chain_config)
25+
def __init__(self, plugin_manager: PluginManager, chain_config: ChainConfig) -> None:
26+
super().__init__(plugin_manager, chain_config)
2427
self._bootstrap_nodes = chain_config.bootstrap_nodes
2528
self._preferred_nodes = chain_config.preferred_nodes
2629
self._network_id = chain_config.network_id
2730
self._node_key = chain_config.nodekey
2831
self._node_port = chain_config.port
2932
self._max_peers = chain_config.max_peers
30-
self.create_and_add_tx_pool()
33+
self.notify_resource_available()
3134

3235
def get_chain(self) -> BaseChain:
3336
if self._chain is None:

trinity/nodes/light.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from trinity.chains.light import (
1616
LightDispatchChain,
1717
)
18+
from trinity.extensibility import (
19+
PluginManager
20+
)
1821
from trinity.nodes.base import Node
1922
from trinity.config import (
2023
ChainConfig,
@@ -31,8 +34,8 @@ class LightNode(Node):
3134
network_id: int = None
3235
nodekey: PrivateKey = None
3336

34-
def __init__(self, chain_config: ChainConfig) -> None:
35-
super().__init__(chain_config)
37+
def __init__(self, plugin_manager: PluginManager, chain_config: ChainConfig) -> None:
38+
super().__init__(plugin_manager, chain_config)
3639

3740
self.network_id = chain_config.network_id
3841
self.nodekey = chain_config.nodekey
@@ -51,7 +54,7 @@ def __init__(self, chain_config: ChainConfig) -> None:
5154
self.add_service(self._discovery)
5255
self.add_service(self._peer_pool)
5356
self.add_service(self._peer_chain)
54-
self.create_and_add_tx_pool()
57+
self.notify_resource_available()
5558

5659
async def _run(self) -> None:
5760
# TODO add a datagram endpoint service that can be added with self.add_service

trinity/nodes/mainnet.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from evm.chains.mainnet import (
22
MainnetChain,
3-
BYZANTIUM_MAINNET_BLOCK
43
)
54

65
from trinity.chains.mainnet import (
@@ -12,9 +11,7 @@
1211

1312
class MainnetFullNode(FullNode):
1413
chain_class = MainnetChain
15-
initial_tx_validation_block_number = BYZANTIUM_MAINNET_BLOCK
1614

1715

1816
class MainnetLightNode(LightNode):
1917
chain_class = MainnetLightDispatchChain
20-
initial_tx_validation_block_number = BYZANTIUM_MAINNET_BLOCK

trinity/nodes/ropsten.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from evm.chains.ropsten import (
22
RopstenChain,
3-
BYZANTIUM_ROPSTEN_BLOCK
43
)
54

65
from trinity.chains.ropsten import (
@@ -12,9 +11,7 @@
1211

1312
class RopstenFullNode(FullNode):
1413
chain_class = RopstenChain
15-
initial_tx_validation_block_number = BYZANTIUM_ROPSTEN_BLOCK
1614

1715

1816
class RopstenLightNode(LightNode):
1917
chain_class = RopstenLightDispatchChain
20-
initial_tx_validation_block_number = BYZANTIUM_ROPSTEN_BLOCK
File renamed without changes.

trinity/plugins/builtin/__init__.py

Whitespace-only changes.

trinity/plugins/builtin/tx_pool/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)