From c48ff9b84d8e118e2b9b255e18ebd41d186ea9cc Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 19 Mar 2025 13:40:37 +0100 Subject: [PATCH] Move logFilters to JsonRpcSubscriptions --- ethers/providers/jsonrpc/subscriptions.nim | 11 ++++++----- .../providers/jsonrpc/testJsonRpcSubscriptions.nim | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index f628330..3d48931 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -19,6 +19,11 @@ type client: RpcClient callbacks: Table[JsonNode, SubscriptionCallback] methodHandlers: Table[string, MethodHandler] + # We need to keep around the filters that are used to create log filters on the RPC node + # as there might be a time when they need to be recreated as RPC node might prune/forget + # about them + # This is used of resubscribe all the subscriptions when using websocket with hardhat + logFilters: Table[JsonNode, EventFilter] MethodHandler* = proc (j: JsonNode) {.gcsafe, raises: [].} SubscriptionCallback = proc(id: JsonNode, arguments: ?!JsonNode) {.gcsafe, raises:[].} @@ -140,6 +145,7 @@ method subscribeLogs(subscriptions: WebSocketSubscriptions, convertErrorsToSubscriptionError: let id = await subscriptions.client.eth_subscribe("logs", filter) subscriptions.callbacks[id] = callback + subscriptions.logFilters[id] = filter return id method unsubscribe*(subscriptions: WebSocketSubscriptions, @@ -160,11 +166,6 @@ type PollingSubscriptions* = ref object of JsonRpcSubscriptions polling: Future[void] - # We need to keep around the filters that are used to create log filters on the RPC node - # as there might be a time when they need to be recreated as RPC node might prune/forget - # about them - logFilters: Table[JsonNode, EventFilter] - # Used when filters are recreated to translate from the id that user # originally got returned to new filter id subscriptionMapping: Table[JsonNode, JsonNode] diff --git a/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim b/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim index cba22a4..2edc95f 100644 --- a/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim +++ b/testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim @@ -114,6 +114,7 @@ suite "HTTP polling subscriptions - mock tests": var mockServer: MockRpcHttpServer privateAccess(PollingSubscriptions) + privateAccess(JsonRpcSubscriptions) proc startServer() {.async.} = mockServer = MockRpcHttpServer.new()