Move logFilters to JsonRpcSubscriptions

This commit is contained in:
Arnaud 2025-03-19 13:40:37 +01:00
parent 7081e6922f
commit c48ff9b84d
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
2 changed files with 7 additions and 5 deletions

View File

@ -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]

View File

@ -114,6 +114,7 @@ suite "HTTP polling subscriptions - mock tests":
var mockServer: MockRpcHttpServer
privateAccess(PollingSubscriptions)
privateAccess(JsonRpcSubscriptions)
proc startServer() {.async.} =
mockServer = MockRpcHttpServer.new()