MCPcopy Create free account
hub / github.com/canopy-network/canopy / Plugin

Class Plugin

plugin/python/contract/plugin.py:76–374  ·  view source on GitHub ↗

Plugin defines the 'VM-less' extension of the Finite State Machine. Matches Go's Plugin struct.

Source from the content-addressed store, hash-verified

74
75
76class Plugin:
77 """
78 Plugin defines the 'VM-less' extension of the Finite State Machine.
79 Matches Go's Plugin struct.
80 """
81
82 def __init__(self, config: Config):
83 self.config = config
84 self.fsm_config: Optional[PluginFSMConfig] = None
85 self.plugin_config = PluginConfig()
86 self._reader: Optional[asyncio.StreamReader] = None
87 self._writer: Optional[asyncio.StreamWriter] = None
88 self._pending: Dict[int, asyncio.Future] = {}
89 self._request_contracts: Dict[int, Contract] = {}
90 self._listen_task: Optional[asyncio.Task] = None
91 self._message_tasks: Set[asyncio.Task] = set()
92 self._is_connected = False
93
94 # Setup plugin config
95 self.plugin_config.name = CONTRACT_CONFIG["name"]
96 self.plugin_config.id = CONTRACT_CONFIG["id"]
97 self.plugin_config.version = CONTRACT_CONFIG["version"]
98 for tx_type in CONTRACT_CONFIG["supported_transactions"]:
99 self.plugin_config.supported_transactions.append(tx_type)
100 for url in CONTRACT_CONFIG.get("transaction_type_urls", []):
101 self.plugin_config.transaction_type_urls.append(url)
102 for url in CONTRACT_CONFIG.get("event_type_urls", []):
103 self.plugin_config.event_type_urls.append(url)
104 for fd in CONTRACT_CONFIG.get("file_descriptor_protos", []):
105 self.plugin_config.file_descriptor_protos.append(fd)
106
107 async def start(self) -> None:
108 """Start the plugin - connect to socket and begin listening."""
109 sock_path = os.path.join(self.config.data_dir_path, SOCKET_PATH)
110
111 # Connect to the socket with retry (matching Go's polling loop)
112 while True:
113 try:
114 self._reader, self._writer = await asyncio.open_unix_connection(sock_path)
115 self._is_connected = True
116 logger.info(f"Connected to plugin socket: {sock_path}")
117 break
118 except Exception as err:
119 logger.warning(f"Failed to connect to plugin socket: {err}")
120 await asyncio.sleep(1.0)
121
122 # Begin listening service
123 self._listen_task = asyncio.create_task(self._listen_for_inbound())
124
125 # Execute handshake
126 await self._handshake()
127
128 async def close(self) -> None:
129 """Close the plugin gracefully."""
130 self._is_connected = False
131
132 if self._listen_task and not self._listen_task.done():
133 self._listen_task.cancel()

Callers 1

start_pluginFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected