Skip to content

Instantly share code, notes, and snippets.

@elmato
Created September 25, 2023 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmato/35e1623fd3d3f552db5c67f57393801f to your computer and use it in GitHub Desktop.
Save elmato/35e1623fd3d3f552db5c67f57393801f to your computer and use it in GitHub Desktop.
EOS EVM WebSocket Proxy

eos-evm-ws-proxy

Description:

The eos-evm-ws-proxy offers a WebSocket interface to the EOS EVM. Given that Silkworm lacks WebSocket support, eos-evm-ws-proxy fills this gap enabling real-time, bidirectional interaction between clients and the EOS EVM.

Internal Components:

  1. SubscriptionServer: Acts as the heart of the project, managing subscriptions and coordinating events between the BlockMonitor and WebSocketHandler components.

  2. BlockMonitor: Monitors new blocks on the EOS EVM blockchain, managing reversible blocks and emitting corresponding events for block additions, removals, and forks. It interfaces with both eos-evm-rpc and nodeos RPC endpoints to retrieve essential block data and ensure the consistency of blocks by handling forks appropriately.

  3. WebSocketHandler: Manages incoming WebSocket connections and forwards Ethereum JSON-RPC method calls to the underlying eos-evm-rpc endpoint. It emits relevant events for subscriptions like 'newHeads' and 'logs', and 'unsubscribe' as well as when a WebSocket connection is closed.

BlockMonitor

The BlockMonitor component is responsible for monitoring new blocks on the EOS EVM blockchain and managing reversible blocks. It needs to communicate with both eos-evm-rcp and nodeos RPC endpoints to retrieve block data and handle block additions, removals, and forks, emitting corresponding events.

  • Create a BlockMonitor class that extends EventEmitter to allow emitting events upon certain actions, such as block addition, removal, or fork.
  • Develop a polling mechanism to keep track of the latest blocks and reversible blocks and ensure it is error-resistant, handling possible issues gracefully.
  • Emit appropriate events on block addition, removal, and fork, allowing other components to react to these events.
  • Ensure the consistency of blocks by handling forks appropriately, and adjusting the monitored blocks accordingly.
  • Ensure that values such as web3_rpc_endpoint, nodeos_rpc_endpoint, are configurable.

WebSocketHandler

The WebSocketHandler component is responsible for managing WebSocket connections and forwarding Ethereum JSON-RPC method calls to the designated eos-evm-rpc endpoint.

  • Create a WebSocketHandler class that extends EventEmitter to handle WebSocket events and Ethereum JSON-RPC method calls.
  • Implement methods to manage incoming WebSocket connections, forwarding Ethereum JSON-RPC method calls to the underlying eos-evm-rpc endpoint other that eth_subscribe and eth_unsubscribe.
  • Emit relevant events on 'eth_subscribe' for 'newHeads' and 'logs' and 'eth_unsubscribe', as well as when a WebSocket connection is closed.
  • Implement robust error-handling mechanisms for incorrect or malformed JSON-RPC requests and responses.
  • Manage subscriptions effectively, utilizing UUIDs for subscription IDs and emitting relevant information on subscription events.
  • Ensure the configurability of the component by allowing the ws_listening_host, ws_listening_port, and web3_rpc_endpoint to be set externally.
  • Handle the 'disconnect' event properly to manage the cleanup of any resources related to the disconnected WebSocket connection.

SubscriptionServer

The SubscriptionServer component is in charge of managing subscriptions, tracking messages sent to WebSocket clients, and coordinating with the BlockMonitor and WebSocketHandler components.

  • Develop a SubscriptionServer class that handles the lifecycle and events of block monitor and WebSocket connections.
  • Implement methods to handle various subscription events like 'newHeads', 'logs', 'unsubscribe', and 'disconnect' emitted by WebSocketHandler.
  • Track and manage sent log messages subscriptions, that need to be resent as removed when a fork is detected.
  • Ensure the proper filtration and forwarding of log and block subscriptions to the corresponding WebSocket connections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment